Simulation time is now unaffected by events
authorBernie Innocenti <bernie@codewiz.org>
Mon, 1 Mar 2021 08:27:16 +0000 (15:27 +0700)
committerBernie Innocenti <bernie@codewiz.org>
Mon, 1 Mar 2021 08:28:42 +0000 (15:28 +0700)
main.rs
timer.rs

diff --git a/main.rs b/main.rs
index d00c783ebdaa3c4e6056c1d5f221249a5e0464b7..0cf1c18fce7db7f1a671c833373152252e9af75a 100644 (file)
--- a/main.rs
+++ b/main.rs
@@ -277,7 +277,6 @@ fn main() {
     let mut fullscreen = false;
 
     event_loop.run(move |event, _, control_flow| {
-        timer.update();
         let t = timer.t;
         let new_hit = sound::hit_event(&mut soundplayer);
         if new_hit > world.last_hit {
@@ -290,6 +289,7 @@ fn main() {
         *control_flow = ControlFlow::WaitUntil(timer.now + Duration::from_nanos(16_666_667));
         match event {
             Event::MainEventsCleared => {
+                timer.update();
                 world.draw_frame(&display, &camera, t);
             }
             Event::NewEvents(cause) => {
@@ -316,7 +316,7 @@ fn main() {
                                     VirtualKeyCode::PageUp => timer.t += 0.1,
                                     VirtualKeyCode::PageDown => timer.t -= 0.2,
                                     VirtualKeyCode::F10 => screenshot::take_screenshot(&display),
-                                    VirtualKeyCode::F | VirtualKeyCode::F11 | VirtualKeyCode::Return => {
+                                    VirtualKeyCode::F11 | VirtualKeyCode::Return => {
                                         fullscreen ^= true;
                                         let fs = if fullscreen {
                                             // let monitor_handle = display.gl_window().window()
index be0261b23520f1e5d9a64c271037f28a2947ff28..9f4abe0fa85ce830642c4a3f2e942f420a792375 100644 (file)
--- a/timer.rs
+++ b/timer.rs
@@ -14,9 +14,10 @@ const BPS: f32 = 10.0;
 
 #[derive(Debug)]
 pub struct Timer {
-    pub t: f32,
-    pub now: Instant,
-    frame: u32,
+    pub t: f32,          /// Simulation time (starts from 0 and does not advance while on pause).
+    pub now: Instant,    /// Wall time, use instead of Instant::now() for frame-consistent time.
+    prev_time: Instant,  /// Time of previous frame.
+    frame: u32,          /// Frame count, starts from 0 and does not increment while on pause.
 
     last_report_time: Instant,
     last_report_frame: u32,
@@ -35,6 +36,7 @@ impl Timer {
         Timer {
             t: 0.0,
             now,
+            prev_time: now,
             frame: 0,
             last_report_time: now,
             last_report_frame: 0,
@@ -45,14 +47,17 @@ impl Timer {
         }
     }
 
+    // To be called once per frame, just before rendering
     pub fn update(&mut self) {
+        self.prev_time = self.now;
         self.now = Instant::now();
-        self.poll_rocket();
         if !self.pause {
-            // Increment time
-            self.t += 0.01;
+            // Increment simulation time
+            let frame_time = self.now - self.prev_time;
+            self.t += frame_time.as_secs_f32();
             self.frame += 1;
         }
+        self.poll_rocket();
         self.maybe_report();
     }