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 FixedTickthe fixed rate's accumulator (see the module header)
impl FixedTick
fn new(rate: u32) -> FixedTickrateticks a second (1 to 10 000), at most DEFAULT_MAX_STEPS per callfn with_max_steps(mut self, n: u32) -> FixedTickthe same with another cap on the steps one call may return (at least 1)fn rate(&self) -> u32ticks a secondfn max_steps(&self) -> u32the cap on the steps of one callfn dt(&self) -> f64the length of a tick in secondsfn advance_to(&mut self, now_ns: u64) -> u32the 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 stillfn advance_by(&mut self, elapsed_ns: u64) -> u32elapsed_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) -> f64how far the clock is into the next tick, in [0, 1): the alpha a frame interpolates with (Pose::interpolate)fn dropped_ns(&self) -> u64the 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 Stepwhat a system gets for its ticktick: 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 tickstruct 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>nonefn 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 namedbefore(at the end without it)fn remove(&mut self, name: &str) -> boolthe system named so goes (false: none)fn run(&self, world: &mut World, res: &mut R, step: &Step)every system once, in orderfn names(&self) -> Vec<&'static str>their names in orderfn len(&self) -> usizehow manyfn is_empty(&self) -> boolnonestruct Game<R>a world, the game's resources, its systems and its clock (see the module header)world: World-- the entities and their componentsres: R-- everything else the systems usesystems: Systems<R>-- what runs every tick, in orderclock: FixedTick-- the fixed rate
impl<R> Game<R>
fn new(res: R) -> Game<R>an empty world with these resources, no systems, 60 Hzfn frame(&mut self, now_ns: u64) -> u32the 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 statesfn step(&mut self)one tick now, whatever the clock says: World::begin_tick, then every systemfn alpha(&self) -> f64the frame's place between the last two ticks (FixedTick::alpha)