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

#480 in Game dev

Download history 6/week @ 2024-06-08 12/week @ 2024-06-15 2/week @ 2024-06-22 27/week @ 2024-06-29 44/week @ 2024-07-06 88/week @ 2024-07-13 63/week @ 2024-07-20 119/week @ 2024-07-27 158/week @ 2024-08-03 220/week @ 2024-08-10 179/week @ 2024-08-17 140/week @ 2024-08-24 88/week @ 2024-08-31 81/week @ 2024-09-07 94/week @ 2024-09-14 60/week @ 2024-09-21

340 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