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();
39 last_report_time: now,
41 accum_draw_time: Duration::default(),
42 accum_idle_time: Duration::default(),
44 rocket: Timer::init_rocket(),
48 pub fn update(&mut self) {
49 self.now = Instant::now();
59 #[cfg(not(feature = "logging"))]
60 fn maybe_report(&mut self) {}
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);
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
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);
85 #[cfg(not(feature = "editor"))]
86 fn init_rocket() -> Option<Rocket> { None }
88 #[cfg(not(feature = "editor"))]
89 fn poll_rocket(&mut self) {}
91 #[cfg(feature = "editor")]
92 fn init_rocket() -> Option<Rocket> {
96 #[cfg(feature = "editor")]
97 fn poll_rocket(&mut self) {
99 Some(ref mut rocket) => {
100 let current_row = (self.t * BPS) as u32;
101 if let Some(event) = rocket.poll_events() {
103 rust_rocket::Event::SetRow(row) => {
104 println!("SetRow (row: {:?})", row);
105 self.t = row as f32 / BPS;
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);
113 println!("{:?}", event);