#constant #frame-rate #timer #iterator #clock #effort #fps

ticktock

Best effort constant framerate clock and fast timers

18 releases

0.8.0 Mar 3, 2020
0.7.0 Jun 20, 2018
0.6.0 Mar 11, 2018
0.4.0 Jul 14, 2017
0.2.2 Mar 14, 2016

#3 in #effort


Used in 3 crates (via process_guard)

MIT license

23KB
434 lines

Frame-clock and timers

ticktock makes it very easy to access frame-timing iterators to achieve constant framerates:

// run with a constant framerate of 30 fps
for (tick, now) in Clock::framerate(30.0).iter() {
  // ...
}

See the documentation for details.


lib.rs:

Timing module for frame-based applications

Contains methods for slowing down to a fixed framerate, as well as measuring actual frames per second.

An example game loop:

use std::time;
use ticktock::{Clock, Timer};

let now = time::Instant::now();

// initialize game
// ...

// show some fps measurements every 5 seconds
let mut fps_counter = Timer::apply(|delta_t, prev_tick| (delta_t, *prev_tick), 0)
    .every(time::Duration::from_secs(5))
    .start(now);

// run with a constant framerate of 30 fps
for (tick, now) in Clock::framerate(30.0).iter() {
    // this loop will run approx. every 33.3333 ms

    // update, render, etc
    // ...

    // update or display fps count
    if let Some((delta_t, prev_tick)) = fps_counter.update(now) {
        fps_counter.set_value(tick);

        let fps = (tick - prev_tick) as f64 / delta_t.as_secs_f64();
        println!("FPS: {}", fps);
    }
    break; // ignore, for doctests
}

No runtime deps