Fix benchmarks for latest glium/glutin API
[mandelwow.git] / timer.rs
index afbfd1ab6183346636f6b6a0358fe09b82798d14..93458388961fc272201867377c5357033cf2b947 100644 (file)
--- a/timer.rs
+++ b/timer.rs
@@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
 use rust_rocket;
 
 #[cfg(feature = "editor")]
-type Rocket = rust_rocket::Rocket;
+type Rocket = rust_rocket::client::RocketClient;
 
 #[cfg(not(feature = "editor"))]
 type Rocket = ();
@@ -14,9 +14,10 @@ const BPS: f32 = 10.0;
 
 #[derive(Debug)]
 pub struct Timer {
-    pub t: f32,
-
-    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,
@@ -34,6 +35,8 @@ impl Timer {
         let now = Instant::now();
         Timer {
             t: 0.0,
+            now,
+            prev_time: now,
             frame: 0,
             last_report_time: now,
             last_report_frame: 0,
@@ -44,25 +47,28 @@ impl Timer {
         }
     }
 
+    // To be called once per frame, just before rendering
     pub fn update(&mut self) {
-        let now = Instant::now();
-        self.poll_rocket();
+        self.prev_time = self.now;
+        self.now = Instant::now();
         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.maybe_report(now);
+        self.poll_rocket();
+        self.maybe_report();
     }
 
     #[cfg(not(feature = "logging"))]
-    fn maybe_report(&mut self, _: Instant) {}
+    fn maybe_report(&mut self) {}
 
     #[cfg(feature = "logging")]
-    fn maybe_report(&mut self, now: Instant) {
-        if now - self.last_report_time > Duration::from_secs(5) {
-            self.report(now);
-            self.last_report_time = now;
+    fn maybe_report(&mut self) {
+        if self.now - self.last_report_time > Duration::from_secs(5) {
+            self.report(self.now);
+            self.last_report_time = self.now;
             self.last_report_frame = self.frame;
             self.accum_draw_time = Duration::new(0, 0);
             self.accum_idle_time = Duration::new(0, 0);
@@ -94,17 +100,19 @@ impl Timer {
 
     #[cfg(feature = "editor")]
     fn poll_rocket(&mut self) {
+        use rust_rocket::client::Event;
+
         match self.rocket {
             Some(ref mut rocket) => {
                 let current_row = (self.t * BPS) as u32;
-                if let Some(event) = rocket.poll_events() {
+                if let Some(event) = rocket.poll_events().unwrap() {
                     match event {
-                        rust_rocket::Event::SetRow(row) => {
+                        Event::SetRow(row) => {
                             println!("SetRow (row: {:?})", row);
                             self.t = row as f32 / BPS;
                         }
-                        rust_rocket::Event::Pause(_) => {
-                            let track1 = rocket.get_track_mut("test");
+                        Event::Pause(_) => {
+                            let track1 = rocket.get_track_mut("test").unwrap();
                             println!("Pause (value: {:?}) (row: {:?})", track1.get_value(current_row as f32), current_row);
                         }
                         _ => (),