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 #[cfg(feature = "editor")]
13 const BPS: f32 = 10.0;
21 last_report_time: Instant,
22 last_report_frame: u32,
24 accum_draw_time: Duration,
25 accum_idle_time: Duration,
29 pub rocket: Option<Rocket>,
33 pub fn new() -> Self {
34 let now = Instant::now();
38 last_report_time: now,
40 accum_draw_time: Duration::default(),
41 accum_idle_time: Duration::default(),
43 rocket: Timer::init_rocket(),
47 pub fn update(&mut self) {
48 let now = Instant::now();
55 self.maybe_report(now);
58 #[cfg(not(feature = "logging"))]
59 fn maybe_report(&mut self, _: Instant) {}
61 #[cfg(feature = "logging")]
62 fn maybe_report(&mut self, now: Instant) {
63 if now - self.last_report_time > Duration::from_secs(5) {
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);
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
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);
84 #[cfg(not(feature = "editor"))]
85 fn init_rocket() -> Option<Rocket> { None }
87 #[cfg(not(feature = "editor"))]
88 fn poll_rocket(&mut self) {}
90 #[cfg(feature = "editor")]
91 fn init_rocket() -> Option<Rocket> {
95 #[cfg(feature = "editor")]
96 fn poll_rocket(&mut self) {
98 Some(ref mut rocket) => {
99 let current_row = (self.t * BPS) as u32;
100 if let Some(event) = rocket.poll_events() {
102 rust_rocket::Event::SetRow(row) => {
103 println!("SetRow (row: {:?})", row);
104 self.t = row as f32 / BPS;
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);
112 println!("{:?}", event);