6 releases

0.1.4 Jan 21, 2024
0.1.3 Nov 19, 2023
0.1.2 Sep 6, 2023
0.1.1 Jan 17, 2023
0.0.0 Jan 16, 2023

#396 in Game dev

27 downloads per month

AGPL-3.0-only

17KB
82 lines

chron

Crates.io Documentation License

A game loop with a fixed timestep.

Features:

  • An optional frame skip. It makes sure that at least one Tick::Render is emitted after a certain amount of Tick::Updates. This prevents the game from not rendering at all, if the the specified updates per second cannot be maintained.
  • An optional frame limit, to prevent the game from running at unnecessarily high frame rates. When the frame limit is not used, the frame rate is unlocked.

Note: The frame limiter uses std::thread::sleep. Its accuracy may or may not be good enough, depending on the platform. So far it seems to work fine (on Linux).

Example

use std::num::NonZeroU32;

let updates_per_second = NonZeroU32::new(50).unwrap();
let frames_per_second = NonZeroU32::new(60).unwrap();

let clock = chron::Clock::new(updates_per_second)
    .with_frame_limit(frames_per_second)
    .with_frame_skip(3);

for tick in clock {
    match tick {
        chron::Tick::Update => {
            // ...
        }
        chron::Tick::Render { interpolation } => {
            // ...
        }
    }
}

No runtime deps