summaryrefslogtreecommitdiff
path: root/graphics/src/timer.rs
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-14 21:48:24 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-14 21:48:34 -0500
commit220d5c8aa8418d37ee9027c817d27b3e50380b81 (patch)
tree4a092dd67d9b2b824504e9cc851df5145c9606ea /graphics/src/timer.rs
parentgraphics: make more constants dependant on texture size (diff)
downloadDungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.tar.gz
DungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.tar.bz2
DungeonCrawl-220d5c8aa8418d37ee9027c817d27b3e50380b81.zip
graphics: new textures!
Diffstat (limited to 'graphics/src/timer.rs')
-rw-r--r--graphics/src/timer.rs31
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
+ }
}