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:
| Field | Meaning | Use case |
|---|---|---|
delta | Seconds since the last frame | Physics, movement (frame-rate independent) |
fps | Smoothed frames-per-second | Display in HUD, performance monitoring |
elapsed | Total seconds since start | Timers, 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 speed | delta |
| “Time since game started” | elapsed |
| Timestamp for logging / profiling | now_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: u32Implementations§
Source§impl Time
impl Time
Sourcepub fn new() -> Self
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.
Sourcepub fn delta(&self) -> f64
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();Sourcepub fn now(&self) -> f64
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.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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