Struct ggez::timer::TimeContext

source ·
pub struct TimeContext { /* private fields */ }
Expand description

A structure that contains our time-tracking state.

Implementations§

source§

impl TimeContext

source

pub fn new() -> TimeContext

Creates a new TimeContext and initializes the start to this instant.

source

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.

source

pub fn average_delta(&self) -> Duration

Gets the average time of a frame, averaged over the last 200 frames.

source

pub fn fps(&self) -> f64

Gets the FPS of the game, averaged over the last 200 frames.

source

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.

source

pub fn time_since_start(&self) -> Duration

Returns the time since the game was initialized, as reported by the system clock.

source

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.

source

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/)

source

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.

Trait Implementations§

source§

impl Debug for TimeContext

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for TimeContext

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T> Has<T> for T

source§

fn retrieve(&self) -> &T

Method to retrieve the context type.
source§

impl<T> HasMut<T> for T

source§

fn retrieve_mut(&mut self) -> &mut T

Method to retrieve the context type as mutable.
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,