1 use std::time::{Duration, Instant};
3 #[cfg(feature = "editor")]
6 #[cfg(feature = "editor")]
7 type Rocket = rust_rocket::Rocket;
9 #[cfg(not(feature = "editor"))]
12 const BPS: f32 = 10.0;
20 last_report_time: Instant,
21 last_report_frame: u32,
23 accum_draw_time: Duration,
24 accum_idle_time: Duration,
28 pub rocket: Option<Rocket>,
32 pub fn new() -> Self {
33 let now = Instant::now();
37 last_report_time: now,
39 accum_draw_time: Duration::default(),
40 accum_idle_time: Duration::default(),
42 rocket: Timer::init_rocket(),
46 pub fn update(&mut self) {
47 let now = Instant::now();
54 self.maybe_report(now);
57 #[cfg(not(feature = "logging"))]
58 fn maybe_report(&mut self, _: Instant) {}
60 #[cfg(feature = "logging")]
61 fn maybe_report(&mut self, now: Instant) {
62 if now - self.last_report_time > Duration::from_secs(5) {
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);
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
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);
83 #[cfg(not(feature = "editor"))]
84 fn init_rocket() -> Option<Rocket> { None }
86 #[cfg(not(feature = "editor"))]
87 fn poll_rocket(&mut self) {}
89 #[cfg(feature = "editor")]
90 fn init_rocket() -> Option<Rocket> {
94 #[cfg(feature = "editor")]
95 fn poll_rocket(&mut self) {
97 Some(ref mut rocket) => {
98 let current_row = (self.t * BPS) as u32;
99 if let Some(event) = rocket.poll_events() {
101 rust_rocket::Event::SetRow(row) => {
102 println!("SetRow (row: {:?})", row);
103 self.t = row as f32 / BPS;
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);
111 println!("{:?}", event);