macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Is there really no way to limit fps?

Open aaratha opened this issue 1 year ago • 5 comments

I am compiling my game to WASM and hosting it on itch.io, and I've noticed that the speed at which it runs is completely different depending on the refresh rate of the device I'm using. It runs fine on my computer, but runs way too fast on my Android with 90hz. Is there any sort of automatic way to lock the physics process to 60hz?

aaratha avatar Jun 24 '24 22:06 aaratha

I don't think there is any automatic way, but it should not be too hard to use get_time() and wait until the next physics tick for the appropriate time using sleep(). I use a tick debt system, so when one tick takes a little longer than it should, the other one will not sleep for as long to compensate this.

AdamSlayer avatar Jun 27 '24 20:06 AdamSlayer

Yeah, this seems to be the only way... I don't think sleep() works in WASM, though.

aaratha avatar Jun 28 '24 00:06 aaratha

A better way to do this (if fps is too high) is to keep rendering without updating physics, so for example you render 2 frames for 1 physics update. And if fps is too low, you do the opposite - update physics multiple times every frame.

AdamSlayer avatar Jun 28 '24 00:06 AdamSlayer

Oh, so I could have a couple fps ranges (58-62, 68-72, 88-92), for instance, and modify the processes based on checks against those? That sounds like it could work.

aaratha avatar Jun 28 '24 13:06 aaratha

You can't use std::thread::sleep!

Puts the current thread to sleep for at least the specified amount of time.

The thread may sleep longer than the duration specified due to scheduling specifics or platform-dependent functionality. It will never sleep less.

This function is blocking, and should not be used in async functions.

You can only set the swap interval.

not-fl3 commented on Jul 28 Just for someone who will come here from google :)

Macroquad do not limit fps, it is not doing anything with fps at all. Some platforms (browsers in particular) gives us no control over fps, some - give siomecontrol over vsync, but it can be overriten by the user in the driver settings (windows), with env variable(linux) and, I guess, performance/battery saving settings (android/ios).

swap_interval : Some(0) in Conf might be a solution, but it does not guarantee a disbled vsync, it really is super platform specific and there are varios way user can enable/disable vsync. https://docs.rs/miniquad/latest/miniquad/conf/struct.Platform.html#structfield.swap_interval

The swap interval is platform specific and requires additional OpenGL extensions to support it. According to the OpenGL documentation, swap_interval values mean: -1: adaptive vsync, 0: no vsync, 1: vsync.

Many questions about this have been asked before, and my answer is the same for all of them. Here is a list of the same questions: #755 #380 #360 #170 #52 #839

Ztry8 avatar Sep 21 '24 06:09 Ztry8