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