Remove deprecatd emscripten option ERROR_ON_MISSING_LIBRARIES
[mandelwow.git] / timer.rs
1 use std::time::{Duration, Instant};
2
3 #[cfg(feature = "editor")]
4 use rust_rocket;
5
6 #[cfg(feature = "editor")]
7 type Rocket = rust_rocket::Rocket;
8
9 #[cfg(not(feature = "editor"))]
10 type Rocket = ();
11
12 const BPS: f32 = 10.0;
13
14 #[derive(Debug)]
15 pub struct Timer {
16     pub t: f32,
17
18     frame: u32,
19
20     last_report_time: Instant,
21     last_report_frame: u32,
22
23     accum_draw_time: Duration,
24     accum_idle_time: Duration,
25
26     pub pause: bool,
27
28     pub rocket: Option<Rocket>,
29 }
30
31 impl Timer {
32     pub fn new() -> Self {
33         let now = Instant::now();
34         Timer {
35             t: 0.0,
36             frame: 0,
37             last_report_time: now,
38             last_report_frame: 0,
39             accum_draw_time: Duration::default(),
40             accum_idle_time: Duration::default(),
41             pause: false,
42             rocket: Timer::init_rocket(),
43         }
44     }
45
46     pub fn update(&mut self) {
47         let now = Instant::now();
48         self.poll_rocket();
49         if !self.pause {
50             // Increment time
51             self.t += 0.01;
52             self.frame += 1;
53         }
54         self.maybe_report(now);
55     }
56
57     #[cfg(not(feature = "logging"))]
58     fn maybe_report(&mut self, _: Instant) {}
59
60     #[cfg(feature = "logging")]
61     fn maybe_report(&mut self, now: Instant) {
62         if now - self.last_report_time > Duration::from_secs(5) {
63             self.report(now);
64             self.last_report_time = now;
65             self.last_report_frame = self.frame;
66             self.accum_draw_time = Duration::new(0, 0);
67             self.accum_idle_time = Duration::new(0, 0);
68         }
69     }
70
71     #[cfg(feature = "logging")]
72     fn report(&self, now: Instant) {
73         fn millis(d : Duration) -> f32 {
74             d.as_secs() as f32 * 1e3 + d.subsec_nanos() as f32 / 1e6
75         }
76         let frames_done = self.frame - self.last_report_frame;
77         let fps = frames_done as f32 / (now - self.last_report_time).as_secs() as f32;
78         let avg_draw_time = millis(self.accum_draw_time / frames_done);
79         let avg_idle_time = millis(self.accum_idle_time / frames_done);
80         println!("fps={:.1} draw={:.1}ms idle={:.1}ms", fps, avg_draw_time, avg_idle_time);
81     }
82
83     #[cfg(not(feature = "editor"))]
84     fn init_rocket() -> Option<Rocket> { None }
85
86     #[cfg(not(feature = "editor"))]
87     fn poll_rocket(&mut self) {}
88
89     #[cfg(feature = "editor")]
90     fn init_rocket() -> Option<Rocket> {
91         Rocket::new().ok()
92     }
93
94     #[cfg(feature = "editor")]
95     fn poll_rocket(&mut self) {
96         match self.rocket {
97             Some(ref mut rocket) => {
98                 let current_row = (self.t * BPS) as u32;
99                 if let Some(event) = rocket.poll_events() {
100                     match event {
101                         rust_rocket::Event::SetRow(row) => {
102                             println!("SetRow (row: {:?})", row);
103                             self.t = row as f32 / BPS;
104                         }
105                         rust_rocket::Event::Pause(_) => {
106                             let track1 = rocket.get_track_mut("test");
107                             println!("Pause (value: {:?}) (row: {:?})", track1.get_value(current_row as f32), current_row);
108                         }
109                         _ => (),
110                     }
111                     println!("{:?}", event);
112                 }
113             }
114             None => ()
115         }
116     }
117 }