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