use crate::constants::frame_stats::DEFAULT_SAMPLE_WINDOW_SECS;
#[derive(Debug, Clone)]
pub(crate) struct FrameStats {
pub(crate) fps: f32,
pub(crate) frame_time_ms: f32,
pub(crate) draw_calls: u32,
pub(crate) triangle_count: u32,
pub(crate) last_sample_time: web_time::Instant,
pub(crate) frames_collected: u32,
pub(crate) sample_window_secs: f32,
}
impl FrameStats {
pub(crate) fn new() -> Self {
Self {
fps: 0.0,
frame_time_ms: 0.0,
draw_calls: 0,
triangle_count: 0,
last_sample_time: web_time::Instant::now(),
frames_collected: 0,
sample_window_secs: DEFAULT_SAMPLE_WINDOW_SECS,
}
}
pub(crate) fn with_sample_window(mut self, secs: f32) -> Self {
self.sample_window_secs = secs;
self
}
pub(crate) fn tick(&mut self, _dt: f32) {
let now = web_time::Instant::now();
self.frames_collected += 1;
let sample_elapsed = now.duration_since(self.last_sample_time).as_secs_f32();
if self.frames_collected > 0 && sample_elapsed >= self.sample_window_secs {
self.fps = self.frames_collected as f32 / sample_elapsed;
self.frame_time_ms = (sample_elapsed / self.frames_collected as f32) * 1000.0;
self.frames_collected = 0;
self.last_sample_time = now;
}
}
pub(crate) fn set_gpu_stats(&mut self, draw_calls: u32, triangle_count: u32) {
self.draw_calls = draw_calls;
self.triangle_count = triangle_count;
}
}
impl Default for FrameStats {
fn default() -> Self {
Self::new()
}
}