chron

Crates.iochron
lib.rschron
version0.1.4
sourcesrc
created_at2023-01-16 20:44:27.001539
updated_at2024-01-21 13:08:52.073252
descriptionA game loop with a fixed timestep.
homepage
repositoryhttps://codeberg.org/tinturing/chron
max_upload_size
id760584
size49,435
(tinturing)

documentation

https://docs.rs/chron

README

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 } => {
            // ...
        }
    }
}
Commit count: 0

cargo fmt