wormhole-engine 0.1.0

A portable, no-editor game engine with Rust core and Crystal scripting
Documentation
use std::time::{Instant, Duration};

pub struct Timer {
    last_frame: Instant,
    fps_counter: u32,
    fps_timer: Instant,
    current_fps: u32,
}

impl Timer {
    pub fn new() -> Self {
        let now = Instant::now();
        Self {
            last_frame: now,
            fps_counter: 0,
            fps_timer: now,
            current_fps: 0,
        }
    }

    pub fn delta_time(&mut self) -> f32 {
        let now = Instant::now();
        let delta = now.duration_since(self.last_frame).as_secs_f32();
        self.last_frame = now;
        delta
    }

    pub fn update_fps(&mut self) -> Option<u32> {
        self.fps_counter += 1;
        let elapsed = self.fps_timer.elapsed();
        
        if elapsed >= Duration::from_secs(1) {
            self.current_fps = self.fps_counter;
            self.fps_counter = 0;
            self.fps_timer = Instant::now();
            Some(self.current_fps)
        } else {
            None
        }
    }

    pub fn fps(&self) -> u32 {
        self.current_fps
    }
}