From e8f85e4a723b772c099a3c03a6cb721c48561e4b Mon Sep 17 00:00:00 2001 From: Mica White Date: Mon, 6 Jul 2026 13:59:36 -0400 Subject: Fully support WASM (in some contexts) --- src/time.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/time.rs (limited to 'src/time.rs') 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() + } +} -- cgit v1.2.3