Struct ggez::timer::TimeContext
source · [−]pub struct TimeContext { /* private fields */ }
Expand description
A structure that contains our time-tracking state.
Implementations
sourceimpl TimeContext
impl TimeContext
sourcepub fn new() -> TimeContext
pub fn new() -> TimeContext
Creates a new TimeContext
and initializes the start to this instant.
sourcepub fn delta(&self) -> Duration
pub fn delta(&self) -> Duration
Get the time between the start of the last frame and the current one; in other words, the length of the last frame.
sourcepub fn average_delta(&self) -> Duration
pub fn average_delta(&self) -> Duration
Gets the average time of a frame, averaged over the last 200 frames.
sourcepub fn ticks(&self) -> usize
pub fn ticks(&self) -> usize
Gets the number of times the game has gone through its event loop.
Specifically, the number of times that TimeContext::tick()
has been called by it.
sourcepub fn time_since_start(&self) -> Duration
pub fn time_since_start(&self) -> Duration
Returns the time since the game was initialized, as reported by the system clock.
sourcepub fn check_update_time(&mut self, target_fps: u32) -> bool
pub fn check_update_time(&mut self, target_fps: u32) -> bool
Check whether or not the desired amount of time has elapsed since the last frame.
The intention is to use this in your update
call to control
how often game logic is updated per frame (see the astroblasto example).
Calling this decreases a timer inside the context if the function returns true. If called in a loop it may therefore return true once, twice or not at all, depending on how much time elapsed since the last frame.
For more info on the idea behind this see http://gafferongames.com/game-physics/fix-your-timestep/.
Due to the global nature of this timer it’s desirable to only use this function at one point of your code. If you want to limit the frame rate in both game logic and drawing consider writing your own event loop, or using a dirty bit for when to redraw graphics, which is set whenever the game logic runs.
sourcepub fn remaining_update_time(&self) -> Duration
pub fn remaining_update_time(&self) -> Duration
Returns the fractional amount of a frame not consumed
by check_update_time()
.
For example, if the desired
update frame time is 40 ms (25 fps), and 45 ms have
passed since the last frame, check_update_time()
will return true
and remaining_update_time()
will
return 5 ms – the amount of time “overflowing” from one
frame to the next.
The intention is for it to be called in your
draw()
callback
to interpolate physics states for smooth rendering.
(see http://gafferongames.com/game-physics/fix-your-timestep/)
sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Update the state of the TimeContext
to record that
another frame has taken place. Necessary for the FPS
tracking and check_update_time()
functions to work.
It’s usually not necessary to call this function yourself,
event::run()
will do it for you.
You only need to call this function if you’re writing your
own custom event loop.