JFLib

game/src/tick.rs#

Source: lib/game/src/tick.rs

tick -- the fixed simulation rate: an accumulator over time that comes from outside, a cap on the
steps a frame may catch up, the alpha of the frame between two ticks; systems as functions run in a
fixed order; Game ties world, systems and clock together

  FixedTick         rate (60 Hz unless set), advance_to(now_ns) / advance_by(elapsed_ns) -> steps
                    due, alpha() in [0, 1), at most max_steps (8 unless set) per call: the rest of a
                    long frame is dropped (dropped_ns), reset() forgets the clock (after a pause)
  Step              what a system gets: the tick's number and dt (1 / rate seconds)
  Systems<R>        functions fn(&mut World, &mut R, &Step) in the order they were added
  Game<R>           a World, the resources R (everything that is not a component: the physics
                    world, the input of the tick, the map), the Systems and the FixedTick;
                    frame(now_ns) runs the steps due, step() one step now

Time is counted exactly: the accumulator holds nanoseconds times the rate, and a step takes
10^9 of it, so 60 Hz loses nothing to the 16.666... ms of a tick (no float sums, no drift after
hours).  The clock comes from outside (plat::now_us in a window, follows the script's virtual clock
in a headless run; a test passes its own numbers), so a test runs the same ticks every time.
/// the simulation rate unless set (Hz)
DEFAULT_RATE: u32 = 60
/// the steps one call may catch up unless set (8 ticks at 60 Hz: 133 ms of a frame that hung)
DEFAULT_MAX_STEPS: u32 = 8
  • struct FixedTick the fixed rate's accumulator (see the module header)

impl FixedTick

  • fn new(rate: u32) -> FixedTick rate ticks a second (1 to 10 000), at most DEFAULT_MAX_STEPS per call

  • fn with_max_steps(mut self, n: u32) -> FixedTick the same with another cap on the steps one call may return (at least 1)

  • fn rate(&self) -> u32 ticks a second

  • fn max_steps(&self) -> u32 the cap on the steps of one call

  • fn dt(&self) -> f64 the length of a tick in seconds

  • fn advance_to(&mut self, now_ns: u64) -> u32 the clock stands at now_ns (any monotonic clock in nanoseconds) -> the steps due. The first call only starts the clock (0); a clock that goes back counts as standing still

  • fn advance_by(&mut self, elapsed_ns: u64) -> u32 elapsed_ns more have passed -> the steps due (at most max_steps; the time beyond them is dropped, what is left of a step stays for the next call and the alpha)

  • fn alpha(&self) -> f64 how far the clock is into the next tick, in [0, 1): the alpha a frame interpolates with (Pose::interpolate)

  • fn dropped_ns(&self) -> u64 the time the cap has dropped so far (frames that hung longer than max_steps ticks)

  • fn reset(&mut self) forget the clock and the part of a tick: the next advance_to starts again (after a pause, so that the time paused is not caught up)

  • struct Step what a system gets for its tick

    • tick: u64 -- the tick's number (World::tick: 1 for the first)
    • dt: f64 -- its length in seconds (1 / rate)
  • type System<R> = fn(&mut World, &mut R, &Step) a system: a function over the world and the game's resources, once per tick

  • struct Systems<R> systems run in the order they were added (no scheduler: the order is the program's)

impl<R> Systems<R>

  • const fn new() -> Systems<R> none

  • fn add(&mut self, name: &'static str, f: System<R>) -> &mut Systems<R> a system after the others (the name is for lists and messages)

  • fn add_before(&mut self, before: &str, name: &'static str, f: System<R>) -> &mut Systems<R> a system right before the one named before (at the end without it)

  • fn remove(&mut self, name: &str) -> bool the system named so goes (false: none)

  • fn run(&self, world: &mut World, res: &mut R, step: &Step) every system once, in order

  • fn names(&self) -> Vec<&'static str> their names in order

  • fn len(&self) -> usize how many

  • fn is_empty(&self) -> bool none

  • struct Game<R> a world, the game's resources, its systems and its clock (see the module header)

    • world: World -- the entities and their components
    • res: R -- everything else the systems use
    • systems: Systems<R> -- what runs every tick, in order
    • clock: FixedTick -- the fixed rate

impl<R> Game<R>

  • fn new(res: R) -> Game<R> an empty world with these resources, no systems, 60 Hz
  • fn frame(&mut self, now_ns: u64) -> u32 the clock stands at now_ns: the steps due run (FixedTick::advance_to) -> how many ran. Then alpha() says where the frame lies between the last two states
  • fn step(&mut self) one tick now, whatever the clock says: World::begin_tick, then every system
  • fn alpha(&self) -> f64 the frame's place between the last two ticks (FixedTick::alpha)

Source: docs/API.md in the JFLib repository.