diff options
| author | Mica White <botahamec@outlook.com> | 2026-07-06 13:59:36 -0400 |
|---|---|---|
| committer | Mica White <botahamec@outlook.com> | 2026-07-06 13:59:36 -0400 |
| commit | e8f85e4a723b772c099a3c03a6cb721c48561e4b (patch) | |
| tree | a879493f40ba52399c2af05eacdb95b8939b0712 /src/time.rs | |
| parent | bdbd5faeb7be5e5f81c1f4a952c8110df9e9b990 (diff) | |
Fully support WASM (in some contexts)
Diffstat (limited to 'src/time.rs')
| -rw-r--r-- | src/time.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/time.rs b/src/time.rs new file mode 100644 index 0000000..5978b57 --- /dev/null +++ b/src/time.rs @@ -0,0 +1,45 @@ +use std::time::{Duration, Instant, SystemTime}; + +pub struct TimeContext { + start_time: Instant, + last_frame: Instant, + current_frame: Instant, + system_time: SystemTime, +} + +impl TimeContext { + pub fn new() -> Self { + let now = Instant::now(); + Self { + start_time: now, + last_frame: now, + current_frame: now, + system_time: SystemTime::now(), + } + } + + pub fn next_frame(&mut self) { + puffin::profile_scope!("update time"); + self.last_frame = self.current_frame; + self.current_frame = Instant::now(); + self.system_time = SystemTime::now() + } + + pub fn delta_time(&self) -> Duration { + self.current_frame - self.last_frame + } + + pub fn monotonic_now(&self) -> Duration { + self.current_frame - self.start_time + } + + pub fn system_now(&self) -> SystemTime { + self.system_time + } +} + +impl Default for TimeContext { + fn default() -> Self { + Self::new() + } +} |
