Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to sleep in the main game loop? #580

Closed
PonasKovas opened this issue Feb 24, 2020 · 6 comments
Closed

How to sleep in the main game loop? #580

PonasKovas opened this issue Feb 24, 2020 · 6 comments
Labels
question Ask away!

Comments

@PonasKovas
Copy link
Contributor

I'm used to having game loops somewhat like this:

loop {
    do_logic();

    std::thread::sleep(std::time::Duration::from_nanos(1_000_000_000/60)); // 60 fps
}

But I doing this on quicksilver appears to break the app: the game gets stuck on the first frame and I get a bunch of RuntimeError: unreachable executed errors in the console.

How am I supposed to get 60 fps (and not more)?

@lenscas
Copy link
Contributor

lenscas commented Feb 24, 2020

assuming quicksilver 3: the update and draw function get executed as often as specified in the settings https://docs.rs/quicksilver/0.3.20/quicksilver/lifecycle/struct.Settings.html

For quicksilver 4: I believe the same is true, but then for the events. (I may be wrong on that as I haven't used that version yet).

Either way, you shouldn't have a need to sleep yourself

@ryanisaacg ryanisaacg added the question Ask away! label Feb 24, 2020
@ryanisaacg
Copy link
Owner

@PonasKovas I'm assuming from your error that you're running the web backend, where the std::thread module is not available (which is the source of your error).

As @lenscas mentioned, the update and draw functions in the old lifecycle system provide this functionality, but they don't currently have equivalents in the 0.4 alpha versions. If you check #552, there is a bullet point for them: "Helpers for planning when to update / draw"

@PonasKovas
Copy link
Contributor Author

Are there any plans on how to implement that? I may want to contribute

@ryanisaacg
Copy link
Owner

I don't have firm plans yet; one draft design would look something like:

pub struct Timer { ... }
impl Timer {
    pub fn new(period_in_ms: f32) -> Timer { ... }
    pub fn tick(&mut self) -> bool { ... }
    pub fn ms_remaining(&self) -> f32 { ... {
}

with a usage pattern that looks roughly like:

loop {
    // handle events
    if update.tick() {
        // handle update
    }
    if draw.tick() {
        // handle draw
    }

A good reference is Fix Your Timestep when implementing timing functionality.

@johnpmayer
Copy link
Contributor

I've implemented sleep (works in all build targets - desktop/stdweb/web-sys) in https://crates.io/crates/quicksilver-utils-async

@ryanisaacg
Copy link
Owner

@PonasKovas check out the new timer example, this should let you lock your update and draw rates to whatever speeds you like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask away!
Projects
None yet
Development

No branches or pull requests

4 participants