comfy icon indicating copy to clipboard operation
comfy copied to clipboard

FPS for Wasm32 build is unbounded

Open cedeerwe opened this issue 1 year ago • 1 comments

Hi!

I have a game developed against comfy v0.3.1. When running cargo run, the FPS numbers show ~60. When I run it using trunk serve, my FPS numbers are ~1000. This is a problem, as it actually slows down the game by a bunch.

I have noticed this line of code in the main game loop:

#[cfg(not(target_arch = "wasm32"))]
loop_helper.loop_sleep();

For some reason the engine isn't sleeping for wasm32.

Finally, I wasn't able to reproduce this for comfy v0.4, as trunk build fails on some unrelated issue, with errors such as these:

error[E0599]: no method named `set_inner_size` found for mutable reference `&mut comfy_core::Window` in the current scope
   --> /Users/cd/.cargo/registry/src/index.crates.io-6f17d22bba15001f/comfy-0.4.0/src/game_loop.rs:95:16
    |
95  |         window.set_inner_size(PhysicalSize::new(

However, the code for comfy v0.4 seems very similar with regards to the sleep behaviour.

Am I missing something with regards to the Wasm32 FPS?

cedeerwe avatar May 20 '24 06:05 cedeerwe

My current hack is to have

#[cfg(target_arch = "wasm32")]
let start_time = web_sys::window().unwrap().performance().unwrap().now();

at the start of the update function and

 // Hack to have lower FPS in wasm -- wait until 16 ms have passed since the beginning of update
 #[cfg(target_arch = "wasm32")]
 loop {
     let current_time = web_sys::window().unwrap().performance().unwrap().now();
     if current_time - start_time >= 16. {
         break;
     }
}

at the end of it. It's not nice, but it seems to work.

cedeerwe avatar May 21 '24 18:05 cedeerwe