From d5a5b454a4670a72826882c6ce4c87d1f597767c Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Sat, 7 Dec 2024 17:25:37 -0500 Subject: Native --- src/state.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 8c70c51..c5478a9 100644 --- a/src/state.rs +++ b/src/state.rs @@ -93,9 +93,15 @@ pub struct PointLight { pub attenuation: glam::Vec2, } +#[cfg(target_arch = "wasm32")] +type Timestamp = f64; + +#[cfg(not(target_arch = "wasm32"))] +type Timestamp = std::time::Instant; + pub struct State { pub acc: f64, - pub last: f64, + pub last: Timestamp, pub tick: u64, pub rebinding: Option, @@ -119,10 +125,16 @@ pub struct State { pub log: Vec<(u64, String)>, } -pub fn now(ctx: &context::Context) -> f64 { +#[cfg(target_arch = "wasm32")] +pub fn now(ctx: &context::Context) -> Timestamp { ctx.performance.now() / 1000.0 } +#[cfg(not(target_arch = "wasm32"))] +pub fn now(_ctx: &context::Context) -> Timestamp { + std::time::Instant::now() +} + pub fn default_keybindings() -> BiHashMap { BiHashMap::from_iter(vec![ (winit::keyboard::KeyCode::KeyW, Key::Up), @@ -156,9 +168,12 @@ impl State { let cwaker = Box::leak(Box::new(waker.into())); let waker_ctx = std::task::Context::from_waker(cwaker); + let acc = 0.0; + let last = now(ctx); + Self { - acc: 0.0, - last: now(ctx), + acc, + last, // we initialize the tick to 1000, which allows us to use "0" as the default time for // various animation starts on entities without having them all play at game start tick: 1000, @@ -421,7 +436,12 @@ impl State { pub fn run_update(&mut self, ctx: &context::Context, game: &mut G) where G: Game { let now = now(ctx); + + #[cfg(target_arch = "wasm32")] let diff = now - self.last; + #[cfg(not(target_arch = "wasm32"))] + let diff = now.duration_since(self.last).as_secs_f64(); + self.acc += diff; self.last = now; -- cgit v1.2.3