Skip to main content

Time

Struct Time 

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

Time resource, updated automatically every frame.

Access via ctx.time inside your [GameState] implementation.

Implementations§

Source§

impl Time

Source

pub fn elapsed(&self) -> f64

Seconds since the engine started.

Source

pub fn delta(&self) -> f64

Duration of the last frame in seconds (affected by time_scale).

Examples found in repository?
examples/particles.rs (line 50)
49    fn update(&mut self, ctx: &mut Context) {
50        let dt = ctx.time.delta() as f32;
51
52        // Move fire emitter to mouse X
53        self.emitters[0].position.x = ctx.input.mouse.position.x;
54        self.emitters[0].position.y = ctx.screen_height() - 50.0;
55
56        // Burst on click
57        if ctx.input.mouse.is_pressed(MouseButton::Left) {
58            self.emitters[1].position = ctx.input.mouse.position;
59            self.emitters[1].burst_now(50);
60        }
61
62        // Update all emitters
63        for emitter in &mut self.emitters {
64            emitter.update(dt);
65        }
66
67        if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
68            ctx.quit();
69        }
70    }
More examples
Hide additional examples
examples/basics.rs (line 45)
44    fn update(&mut self, ctx: &mut Context) {
45        let dt = ctx.time.delta() as f32;
46
47        // --- Ball physics ---
48        self.ball.velocity.y += 600.0 * dt; // Gravity
49        self.ball.position += self.ball.velocity * dt;
50
51        // Bounce off walls
52        let w = ctx.screen_width();
53        let h = ctx.screen_height();
54
55        if self.ball.position.x - self.ball.radius < 0.0 {
56            self.ball.position.x = self.ball.radius;
57            self.ball.velocity.x = self.ball.velocity.x.abs();
58        }
59        if self.ball.position.x + self.ball.radius > w {
60            self.ball.position.x = w - self.ball.radius;
61            self.ball.velocity.x = -self.ball.velocity.x.abs();
62        }
63        if self.ball.position.y - self.ball.radius < 0.0 {
64            self.ball.position.y = self.ball.radius;
65            self.ball.velocity.y = self.ball.velocity.y.abs();
66        }
67        if self.ball.position.y + self.ball.radius > h {
68            self.ball.position.y = h - self.ball.radius;
69            self.ball.velocity.y = -self.ball.velocity.y.abs() * 0.95; // Damping
70        }
71
72        // Update trail
73        self.ball.trail.push((self.ball.position, 1.0));
74        if self.ball.trail.len() > 60 {
75            self.ball.trail.remove(0);
76        }
77        for (_, alpha) in &mut self.ball.trail {
78            *alpha -= dt * 2.0;
79        }
80        self.ball.trail.retain(|(_, a)| *a > 0.0);
81
82        // --- Input ---
83        if ctx.input.is_action_pressed("shake") {
84            ctx.graphics.camera.shake(10.0, 0.3);
85        }
86
87        if ctx.input.mouse.is_pressed(MouseButton::Left) {
88            self.click_count += 1;
89            // Change ball color
90            self.hue = (self.hue + 30.0) % 360.0;
91            self.ball.color = Color::from_hsla(self.hue, 0.8, 0.6, 1.0);
92            // Boost ball toward click
93            let dir = ctx.input.mouse.position - self.ball.position;
94            self.ball.velocity += dir.normalize_or_zero() * 200.0;
95        }
96
97        // Zoom with scroll
98        let zoom = ctx.graphics.camera.zoom + ctx.input.mouse.scroll.y * 0.1;
99        ctx.graphics.camera.set_zoom(zoom);
100
101        // Escape to quit
102        if ctx.input.keyboard.is_pressed(KeyCode::Escape) {
103            ctx.quit();
104        }
105
106        // Update camera follow target
107        ctx.graphics.camera.follow(self.ball.position, 0.05);
108    }
Source

pub fn smoothed_delta(&self) -> f64

Smoothed delta time (exponential moving average).

Source

pub fn fps(&self) -> f64

Frames per second (averaged over last 60 frames).

Examples found in repository?
examples/basics.rs (line 133)
110    fn render(&mut self, ctx: &mut Context) {
111        ctx.graphics.clear(ctx.window.clear_color);
112
113        // Draw trail
114        for &(pos, alpha) in &self.ball.trail {
115            ctx.graphics.draw_circle(
116                pos.x, pos.y,
117                self.ball.radius * 0.5 * alpha,
118                self.ball.color.with_alpha(alpha * 0.5),
119            );
120        }
121
122        // Draw ball
123        ctx.graphics.draw_circle(
124            self.ball.position.x,
125            self.ball.position.y,
126            self.ball.radius,
127            self.ball.color,
128        );
129
130        // Draw HUD
131        ctx.graphics.reset_camera();
132        ctx.graphics.draw_text(
133            &format!("FPS: {:.0}", ctx.time.fps()),
134            10.0, 10.0, 18.0, Color::WHITE,
135        );
136        ctx.graphics.draw_text(
137            &format!("Clicks: {}", self.click_count),
138            10.0, 35.0, 18.0, Color::WHITE,
139        );
140        ctx.graphics.draw_text(
141            "Space = shake | Click = change color | Scroll = zoom | Esc = quit",
142            10.0, 60.0, 14.0, Color::LIGHT_GRAY,
143        );
144    }
Source

pub fn frame_count(&self) -> u64

Current frame number (starts at 0, increments each frame).

Source

pub fn time_scale(&self) -> f64

Current time scale factor (1.0 = normal, 0.0 = paused, 2.0 = double speed).

Source

pub fn set_time_scale(&mut self, scale: f64)

Set the time scale factor.

Source

pub fn set_fixed_timestep(&mut self, fps: u32)

Enable fixed timestep updates at the given rate (in Hz).

Source

pub fn disable_fixed_timestep(&mut self)

Disable fixed timestep updates.

Source

pub fn is_fixed_timestep_enabled(&self) -> bool

Whether fixed timestep is enabled.

Source

pub fn fixed_delta(&self) -> f64

The fixed timestep duration in seconds.

Trait Implementations§

Source§

impl Debug for Time

Source§

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

Formats the value using the given formatter. Read more

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<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, 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.