Throttle FPS
ThibaultLemaire opened this issue · comments
Hi, nice library!
So I'm currently porting an example project written with tcod-rs over to your lib, and I was wondering because I didn't see it in any of the examples: how do you throttle or control the max fps your code is running at ?
Because the perf example shows ~60 fps and uses as much CPU as my code which simply checks a boolean every time render
is called. So I'm guessing we're simply running on the browser loop here which is limited to 60 fps.
But I don't want a roguelike (especially as simple as mine) gobbling up 20% of my CPU just doing nothing, so I'd like to down-throttle it to something like 20 fps, that'd be more appropriate.
Hey, thanks for using doryen-rs.
What CPU are you using ? It's using only 2% CPU for me when nothing is rendered. I can probably add some throttling code to call the render function only once in a while. doryen-rs is in the process of moving its backend to bracket-lib and bracket-lib has such a fps throttling parameter, though it's not supported on the web yet
CPU: Intel i5-4200M (4) @ 3.100GHz
according to neofetch
.
So on Firefox, with nothing else open, and at default zoom level (I noticed zooming in increases CPU as well), I get 13% for main process + 7% for tab on perf, and 19% for main process + 8% for tab on fonts.
Though it's just annoying and not that much of an issue, I think to recall you can control the game loop with emscripten (as shown here), which is why I thought we could do the same here.
Ok you're probably using a laptop with no hardware acceleration. That would explain why zooming increase the CPU usage. It should only impact GPU usage. There's probably a way to avoid rendering the frame when nothing has changed in the console data to lower CPU usage on software renderers. But that's quite a specific case. Can you post the console log when you run an example. It should display some information about the OpenGL version
Yep it's a laptop. Here's what the log says
opengl WebGL 2.0
shading language WebGL GLSL ES 3.00
vendor Mozilla
Ok I've added a max_fps field in the AppOptions on the master. This should cover your need. Feel free to reopen this if there is an issue.
Alright nice! I'm down to 6% CPU with 20FPS now! Thanks! 🙂
Funny enough, I'm now implementing throttle again, but on the update
side because my player is moving too fast.
Luckily though, if I'm not mistaken the update
method is called at a fixed rate of 60 times per second right? So I can simply use a counter.
Actually, nope, bad idea: the game just misses input and feels sluggish.
You can either use f32 coordinates (probably the easiest way but has big impact on your data format) or use an energy counter :
fn update(...) {
player.energy += amount;
if player.energy > WALK_ENERGY {
player.move(...);
player.energy = 0;
}
}
Yeah I saw the f32 coordinates in the demo example, but I ended up using a cooldown system similar to your energy counter, thanks.