pub struct FpsCounter {
    update_interval: f32,
    accumulated_time: f32,
    frame_count: u32,
    fps: f32,
}

impl FpsCounter {
    pub fn new(update_interval: f32) -> Self {
        Self {
            update_interval,
            accumulated_time: 0.0,
            frame_count: 0,
            fps: 0.0,
        }
    }

    pub fn tick(&mut self, delta_time: f32) {
        self.accumulated_time += delta_time;
        self.frame_count += 1;

        if self.accumulated_time >= self.update_interval {
            self.fps = self.frame_count as f32 / self.accumulated_time;
            self.frame_count = 0;
            self.accumulated_time = 0.0;
        }
    }

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

    pub fn fps_rounded(&self) -> u32 {
        self.fps.round() as u32
    }
}

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