diff options
Diffstat (limited to 'graphics/src/timer.rs')
| -rw-r--r-- | graphics/src/timer.rs | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/graphics/src/timer.rs b/graphics/src/timer.rs index e2209c7..9d4fcfc 100644 --- a/graphics/src/timer.rs +++ b/graphics/src/timer.rs @@ -5,6 +5,8 @@ const MAX_SAMPLES: usize = 100; /// Keeps track of frame intervals and FPS pub struct Timer { + start: Instant, + since_start: Duration, previous: Instant, tick_index: usize, tick_sum: Duration, @@ -13,13 +15,17 @@ pub struct Timer { } impl Timer { pub fn new() -> Self { - let previous = Instant::now(); + let start = Instant::now(); + let since_start = Duration::ZERO; + let previous = start; let tick_index = 0usize; let tick_sum = Duration::ZERO; let tick_list = Box::new([tick_sum; MAX_SAMPLES]); let frame = 0u32; Self { + start, + since_start, previous, tick_index, tick_sum, @@ -39,8 +45,11 @@ impl Timer { self.tick_sum += time; self.tick_list[self.tick_index] = time; - // update frame frame + // update frame count self.frame += 1; + + // update since + self.since_start = self.previous.duration_since(self.start); } pub const fn frame(&self) -> u32 { @@ -49,13 +58,13 @@ impl Timer { #[expect(clippy::cast_possible_truncation)] pub const fn fps(&self) -> u32 { - match self.frame_time().as_nanos() { - 0 => u32::MAX, - // fps can never be bigger than u32::MAX since we're - // computing in nanoseconds - nanos => 1_000_000_000u128.div_ceil(nanos) as u32, - } - } + match self.frame_time().as_nanos() { + 0 => u32::MAX, + // fps can never be bigger than u32::MAX since we're + // computing in nanoseconds + nanos => 1_000_000_000u128.div_ceil(nanos) as u32, + } + } #[expect(clippy::cast_possible_truncation)] pub const fn frame_time(&self) -> Duration { @@ -68,4 +77,8 @@ impl Timer { pub const fn delta_time(&self) -> Duration { self.tick_list[self.tick_index] } + + pub const fn since_start(&self) -> Duration { + self.since_start + } } |