From bc5316d0a5b1b9a323d8b674e54e8aba04501d84 Mon Sep 17 00:00:00 2001
From: Skye Terran <skyeterran@gmail.com>
Date: Thu, 3 Mar 2022 19:15:30 -0800
Subject: [PATCH] changes

---
 examples/basic.rs | 16 +++++-----------
 src/time.rs       | 43 ++++++++++++++++++++++---------------------
 2 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/examples/basic.rs b/examples/basic.rs
index 9d4df2c..c84826b 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -1,5 +1,6 @@
 use slipwave::time::{State, Loop};
 use slipwave::log::{Logger};
+use std::time::{Duration, Instant};
 
 fn main() {
     println!("Slipwave Engine | 2021 | Skye Terran");
@@ -12,10 +13,10 @@ fn main() {
     let mut sim = Loop::new();
 
     // set if the sim is realtime or as fast as possible
-    sim.set_realtime(false);
+    sim.set_realtime(true);
 
     // set the loop update interval
-    sim.set_update_interval(40);
+    sim.set_update_interval(Duration::from_millis(40));
 
     // set the loop's timescale
     sim.get_state_mut().set_timescale(1.0);
@@ -34,17 +35,10 @@ fn main() {
         if sim.is_awake() {
            velocity -= 9.8 * sim.get_state().get_timestep();
            //println!("{}", velocity);
-           //sim.get_state().debug_time();
         }
+        sim.get_state().debug_time();
 
-        // display logic goes here
-        // problem: the timestep is not what we want here. we need to get the elapsed time 
-        //let timestep = sim.get_state().get_timestep();
-        //let x_interpolated: f32 = x as f32 + timestep;
-        //println!("x: {}", x_interpolated);
-
-        // End condition
-        if velocity < 50.0 {
+        if velocity <= 50.0 {
             sim.get_state().debug_time();
             break;
         }
diff --git a/src/time.rs b/src/time.rs
index 66da07f..b1872b9 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -7,7 +7,7 @@ pub struct State {
     simulate: bool,
     clock_start: Instant,
     last_tick: Instant,
-    delta_time: u32,
+    delta_time: Duration,
     lapse: f32,
     irl_time: Duration,
     sim_time: Duration
@@ -22,7 +22,7 @@ impl State {
             simulate: true,
             clock_start: Instant::now(),
             last_tick: Instant::now(),
-            delta_time: 0,
+            delta_time: Duration::new(0,0),
             lapse: 0.0,
             irl_time: Duration::new(0,0),
             sim_time: Duration::new(0,0)
@@ -33,13 +33,13 @@ impl State {
     }
 
     /// Returns the current "delta time", the real time (in ms) elapsed since the last update tick
-    pub fn get_delta_time(self) -> u32 {
+    pub fn get_delta_time(self) -> Duration {
         self.delta_time
     }
 
     /// Returns the current "timestep", which is the delta time represented in seconds as a float
     pub fn get_timestep(self) -> f32 {
-        self.delta_time as f32 / 1000.0
+        self.delta_time.as_nanos() as f32 / 1_000_000_000.0
     }
 
     /// Returns the current "lapse", the virtual time (in s) elapsed since the last update tick
@@ -92,8 +92,7 @@ impl State {
     /// - *Delta time (tick):* Real time (in ms) elapsed between the last tick and the previous tick
     /// - *lapse:* Virtual time (in s with ms accuracy) elapsed since the last tick
     pub fn debug_time(self) {
-        let elapsed_time = Instant::now().duration_since(self.last_tick);
-        println!("IRL time: {}ms | Sim time: {}ms | Delta time: {}ms | Lapse: {}", self.irl_time.as_millis(), self.sim_time.as_millis(), self.delta_time, self.lapse);
+        println!("IRL time: {}ms | Sim time: {}ms | Delta time: {}ms | Lapse: {}", duration_as_ms_float(self.irl_time), duration_as_ms_float(self.sim_time), duration_as_ms_float(self.delta_time), self.lapse);
     }
 }
 
@@ -101,7 +100,7 @@ impl State {
 pub struct Loop {
     state: State,
     realtime: bool,
-    update_interval: u32,
+    update_interval: Duration,
     awake: bool
 }
 
@@ -115,7 +114,7 @@ impl Loop {
         let mut new_loop = Loop {
             state: new_state,
             realtime: true,
-            update_interval: 40,
+            update_interval: Duration::from_millis(40),
             awake: false
         };
         
@@ -162,16 +161,17 @@ impl Loop {
         if self.state.simulate {
             // track elapsed real time each step
             let elapsed_time = Instant::now().duration_since(self.state.last_tick);
+            let mut current_lapse: f32 = 0.0;
 
             if !self.realtime || delta_time(self.state.last_tick) >= self.update_interval {    
                 // update clocks
                 if self.realtime {
-                    self.state.delta_time = delta_time(self.state.last_tick);
+                    self.state.delta_time =delta_time(self.state.last_tick);
                     self.state.sim_time += elapsed_time.mul_f32(self.state.timescale);
                     self.state.irl_time += elapsed_time;
                 } else {
                     self.state.delta_time = self.update_interval;
-                    self.state.sim_time += Duration::from_millis(self.update_interval as u64);
+                    self.state.sim_time += self.update_interval;
                     self.state.irl_time = Instant::now().duration_since(self.state.clock_start);
                 }
                 
@@ -183,12 +183,9 @@ impl Loop {
             } else {
                 // mark the loop as "asleep", meaning update logic should NOT occur
                 self.awake = false;
-            }
-            // compute the current lapse (a float describing the virtual time since last tick, in ticks)
-            let mut current_lapse = (elapsed_time.as_millis() as f32) / (self.update_interval as f32);
-            // prevent a lapse of 1.0 (which will throw off interpolation)
-            if current_lapse >= 1.0 {
-                current_lapse = 0.0;
+
+                // compute the current lapse (a float describing the virtual time since last tick, in ticks)
+                current_lapse = (elapsed_time.as_nanos() as f32) / (self.update_interval.as_nanos() as f32);
             }
             // update the sim lapse
             self.state.lapse = current_lapse;
@@ -201,17 +198,21 @@ impl Loop {
     }
 
     /// Returns the "update interval", the minimum time (in ms) which will elapse between update ticks
-    pub fn get_update_interval(self) -> u32 {
+    pub fn get_update_interval(self) -> Duration {
         self.update_interval
     }
 
     /// Changes the update interval
-    pub fn set_update_interval(&mut self, update_interval: u32) {
+    pub fn set_update_interval(&mut self, update_interval: Duration) {
         self.update_interval = update_interval;
     }
 }
 
-// gets the real time (in ms) that's elapsed since the earlier Instant
-fn delta_time(earlier: Instant) -> u32 {
-    Instant::now().duration_since(earlier).as_millis() as u32
+// gets the real time (in ns) that's elapsed since the earlier Instant
+fn delta_time(earlier: Instant) -> Duration {
+    Instant::now().duration_since(earlier)
+}
+
+fn duration_as_ms_float(duration: Duration) -> f32 {
+    duration.as_nanos() as f32 / 1_000_000.0
 }
\ No newline at end of file