Skip to main content

Time

Struct Time 

Source
pub struct Time {
    pub fps: f64,
    pub delta: f64,
    pub tick_count: u64,
    pub elapsed: f64,
    pub start_time: Instant,
    pub prev_time: Instant,
    pub prev_sec: Instant,
    pub local_tick: u32,
    /* private fields */
}
Expand description

Frame timing data — delta time, smoothed FPS, and elapsed wall-clock time.

Time is updated once per frame by the engine (via Time::update) before user code runs. It provides three fundamental measurements:

FieldMeaningUse case
deltaSeconds since the last framePhysics, movement (frame-rate independent)
fpsSmoothed frames-per-secondDisplay in HUD, performance monitoring
elapsedTotal seconds since startTimers, countdowns, replays

§FPS smoothing

The FPS value is a running average of the last 32 delta samples. This avoids jarring frame-to-frame fluctuations while still responding to sustained changes in frame rate. The smoothing window size is fixed.

§Which timing method should I use?

You want…Use
Frame-independent movement speeddelta
“Time since game started”elapsed
Timestamp for logging / profilingnow_ms
“5 seconds from now”let deadline = time.elapsed() + 5.0;

§Example

use optic_loop::Time;

let mut time = Time::new();

// Simulate a 16 ms frame
std::thread::sleep(std::time::Duration::from_millis(16));
time.update();

println!("Delta: {:.4}s  FPS: {:.1}", time.delta(), time.fps());
// → "Delta: 0.0160s  FPS: 62.5"

Fields§

§fps: f64§delta: f64§tick_count: u64§elapsed: f64§start_time: Instant§prev_time: Instant§prev_sec: Instant§local_tick: u32

Implementations§

Source§

impl Time

Source

pub fn new() -> Self

Creates a new timer with all counters at zero.

The internal start_time is set to the current wall-clock instant.

Source

pub fn update(&mut self)

Advances the timer by one frame.

Called automatically by the engine each frame. Increments tick_count, computes delta and elapsed from wall-clock time, and updates the smoothed FPS.

You should not normally call this manually — Game and GameLoop call it before invoking user code.

Source

pub fn fps(&self) -> f64

Smoothed frames-per-second (averaged over the last 32 frames).

Source

pub fn delta(&self) -> f64

Delta time in seconds since the last frame.

Multiply speeds by this value to get frame-rate-independent motion:

let speed = 10.0; // units per second
entity.position.x += speed * time.delta();
Source

pub fn elapsed(&self) -> f64

Total wall-clock seconds since Time::new was called.

Source

pub fn now(&self) -> f64

Current elapsed time in seconds (re-queries Instant::now).

Unlike elapsed, this always returns the very latest wall-clock time, even if update has not been called yet for this frame.

Source

pub fn now_ms(&self) -> u64

Current elapsed time in milliseconds.

Source

pub fn now_as_ms(&self) -> u64

Alias for now_ms.

Source

pub fn now_as_ns(&self) -> u64

Current elapsed time in nanoseconds.

Source

pub fn sleep(&self, secs: f64)

Blocks the current thread for the given fractional seconds.

Useful for frame-rate limiting in non-interactive contexts.

Source

pub fn sleep_ms(&self, millis: u64)

Blocks the current thread for the given milliseconds.

Source

pub fn sleep_ns(&self, nanos: u64)

Blocks the current thread for the given nanoseconds.

Auto Trait Implementations§

§

impl Freeze for Time

§

impl RefUnwindSafe for Time

§

impl Send for Time

§

impl Sync for Time

§

impl Unpin for Time

§

impl UnsafeUnpin 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<T> Upcast<T> for T

Source§

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

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more