pub struct Timer { /* private fields */ }
Expand description
A timer that you can use to fix the time between actions, for example updates or draw calls.
See the article Fix Your Timestep for more on how to use timers to ensure framerate-independence.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn time_per_second(times: f32) -> Timer
pub fn time_per_second(times: f32) -> Timer
Create a timer that ticks n many times per second
Sourcepub fn with_duration(period: Duration) -> Timer
pub fn with_duration(period: Duration) -> Timer
Create a timer with a given period (time between ticks)
Sourcepub fn tick(&mut self) -> bool
pub fn tick(&mut self) -> bool
Look if the time has elapsed and if so, starts the countdown for the next tick.
You can use a while loop instead of an if to catch up in the event that you were late. Each tick will only ‘consume’ one period worth of time.
Sourcepub fn exhaust(&mut self) -> Option<NonZeroUsize>
pub fn exhaust(&mut self) -> Option<NonZeroUsize>
Similar to Self::tick() but tells you how many ticks have passed, rather than just if a tick has passed.
This is useful in situations where catching up isn’t needed or possible, like rendering to the screen. If you’ve missed rendering three frames, there’s no point in drawing them now: just render the current state and move on.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the timer to count from this moment.
This is the same as creating a new Timer with the same period