dreamwell-runtime 1.0.0

Dreamwell Runtime — cross-platform GPU-accelerated game client
Documentation
// Frame timing — fixed timestep, delta time, FPS tracking.

use std::time::Instant;

/// Frame timer with delta time and FPS tracking.
pub struct FrameTimer {
    last_frame: Instant,
    delta: f32,
    fps: f32,
    frame_count: u64,
}

impl Default for FrameTimer {
    fn default() -> Self {
        Self::new()
    }
}

impl FrameTimer {
    pub fn new() -> Self {
        Self {
            last_frame: Instant::now(),
            delta: 0.0,
            fps: 0.0,
            frame_count: 0,
        }
    }

    /// Call once per frame to update timing.
    pub fn tick(&mut self) {
        let now = Instant::now();
        self.delta = now.duration_since(self.last_frame).as_secs_f32();
        self.last_frame = now;
        self.frame_count += 1;

        // Clamp delta to avoid division by zero or Inf propagation.
        // Upper bound of 50ms prevents spiral-of-death on hitches.
        let dt = self.delta.clamp(0.0001, 0.05);

        // Exponential moving average FPS
        let instant_fps = 1.0 / dt;
        let new_fps = self.fps * 0.95 + instant_fps * 0.05;
        if new_fps.is_finite() {
            self.fps = new_fps.clamp(0.0, 1000.0);
        }
    }

    /// Seconds since last frame.
    pub fn delta_time(&self) -> f32 {
        self.delta
    }

    /// Smoothed frames per second.
    pub fn fps(&self) -> f32 {
        self.fps
    }

    /// Total frames rendered.
    pub fn frame_count(&self) -> u64 {
        self.frame_count
    }
}