Struct Time

Source
pub struct Time { /* private fields */ }
Expand description

Frame timing values.

Implementations§

Source§

impl Time

Source

pub fn delta_time(&self) -> Duration

Gets the time difference between frames.

Source

pub fn delta_real_time(&self) -> Duration

Gets the time difference between frames ignoring the time speed multiplier.

Source

pub fn fixed_time(&self) -> Duration

Gets the fixed time step. Must be used instead of delta_time during fixed updates.

Source

pub fn frame_number(&self) -> u64

Gets the current frame number. This increments by 1 every frame. There is no frame 0.

Source

pub fn absolute_time(&self) -> Duration

Gets the time since the start of the game, taking into account the speed multiplier.

Source

pub fn absolute_real_time(&self) -> Duration

Gets the time since the start of the game, ignoring the speed multiplier.

Source

pub fn time_scale(&self) -> f32

Gets the current time speed multiplier.

Source

pub fn advance_frame(&mut self, time_diff: Duration)

Sets delta_time to the given Duration. Updates the struct to reflect the changes of this frame. This should be called before using step_fixed_update.

Examples found in repository?
examples/simple.rs (line 9)
3fn main() {
4    let mut time = Time::default();
5    time.set_fixed_time(Duration::from_secs_f64(1.0 / 20.0));
6
7    let step = 1.0 / 60.0;
8    for _ in 0..60 {
9        time.advance_frame(Duration::from_secs_f64(step));
10        {} // Run dynamic frame (ie. game logic, rendering)
11        while time.step_fixed_update() {
12            // runs 20 times in a frame.
13            {} // Run fixed frame (ie. physics)
14        }
15    }
16}
Source

pub fn set_fixed_time(&mut self, time: Duration)

Sets both fixed_time and fixed_seconds based on the duration given.

Examples found in repository?
examples/simple.rs (line 5)
3fn main() {
4    let mut time = Time::default();
5    time.set_fixed_time(Duration::from_secs_f64(1.0 / 20.0));
6
7    let step = 1.0 / 60.0;
8    for _ in 0..60 {
9        time.advance_frame(Duration::from_secs_f64(step));
10        {} // Run dynamic frame (ie. game logic, rendering)
11        while time.step_fixed_update() {
12            // runs 20 times in a frame.
13            {} // Run fixed frame (ie. physics)
14        }
15    }
16}
Source

pub fn set_time_scale(&mut self, multiplier: f32)

Sets the time multiplier that affects how time values are computed, effectively slowing or speeding up your game.

§Panics

This will panic if multiplier is NaN, Infinity, or less than 0.

Source

pub fn step_fixed_update(&mut self) -> bool

Checks to see if we should perform another fixed update iteration, and if so, returns true and reduces the accumulator.

Examples found in repository?
examples/simple.rs (line 11)
3fn main() {
4    let mut time = Time::default();
5    time.set_fixed_time(Duration::from_secs_f64(1.0 / 20.0));
6
7    let step = 1.0 / 60.0;
8    for _ in 0..60 {
9        time.advance_frame(Duration::from_secs_f64(step));
10        {} // Run dynamic frame (ie. game logic, rendering)
11        while time.step_fixed_update() {
12            // runs 20 times in a frame.
13            {} // Run fixed frame (ie. physics)
14        }
15    }
16}

Trait Implementations§

Source§

impl Clone for Time

Source§

fn clone(&self) -> Time

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Time

Source§

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

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

impl Default for Time

Source§

fn default() -> Time

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

impl PartialEq for Time

Source§

fn eq(&self, other: &Time) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Time

Source§

impl StructuralPartialEq for Time

Auto Trait Implementations§

§

impl Freeze for Time

§

impl RefUnwindSafe for Time

§

impl Send for Time

§

impl Sync for Time

§

impl Unpin for Time

§

impl UnwindSafe for Time

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.