1 use std::time::{Duration, Instant};
3 #[cfg(feature = "editor")]
6 #[cfg(feature = "editor")]
7 type Rocket = rust_rocket::client::RocketClient;
9 #[cfg(not(feature = "editor"))]
12 #[cfg(feature = "editor")]
13 const BPS: f32 = 10.0;
17 pub t: f32, /// Simulation time (starts from 0 and does not advance while on pause).
18 pub now: Instant, /// Wall time, use instead of Instant::now() for frame-consistent time.
19 prev_time: Instant, /// Time of previous frame.
20 frame: u32, /// Frame count, starts from 0 and does not increment while on pause.
22 last_report_time: Instant,
23 last_report_frame: u32,
25 accum_draw_time: Duration,
26 accum_idle_time: Duration,
30 pub rocket: Option<Rocket>,
34 pub fn new() -> Self {
35 let now = Instant::now();
41 last_report_time: now,
43 accum_draw_time: Duration::default(),
44 accum_idle_time: Duration::default(),
46 rocket: Timer::init_rocket(),
50 // To be called once per frame, just before rendering
51 pub fn update(&mut self) {
52 self.prev_time = self.now;
53 self.now = Instant::now();
55 // Increment simulation time
56 let frame_time = self.now - self.prev_time;
57 self.t += frame_time.as_secs_f32();
64 #[cfg(not(feature = "logging"))]
65 fn maybe_report(&mut self) {}
67 #[cfg(feature = "logging")]
68 fn maybe_report(&mut self) {
69 if self.now - self.last_report_time > Duration::from_secs(5) {
70 self.report(self.now);
71 self.last_report_time = self.now;
72 self.last_report_frame = self.frame;
73 self.accum_draw_time = Duration::new(0, 0);
74 self.accum_idle_time = Duration::new(0, 0);
78 #[cfg(feature = "logging")]
79 fn report(&self, now: Instant) {
80 fn millis(d : Duration) -> f32 {
81 d.as_secs() as f32 * 1e3 + d.subsec_nanos() as f32 / 1e6
83 let frames_done = self.frame - self.last_report_frame;
84 let fps = frames_done as f32 / (now - self.last_report_time).as_secs() as f32;
85 let avg_draw_time = millis(self.accum_draw_time / frames_done);
86 let avg_idle_time = millis(self.accum_idle_time / frames_done);
87 println!("fps={:.1} draw={:.1}ms idle={:.1}ms", fps, avg_draw_time, avg_idle_time);
90 #[cfg(not(feature = "editor"))]
91 fn init_rocket() -> Option<Rocket> { None }
93 #[cfg(not(feature = "editor"))]
94 fn poll_rocket(&mut self) {}
96 #[cfg(feature = "editor")]
97 fn init_rocket() -> Option<Rocket> {
101 #[cfg(feature = "editor")]
102 fn poll_rocket(&mut self) {
103 use rust_rocket::client::Event;
106 Some(ref mut rocket) => {
107 let current_row = (self.t * BPS) as u32;
108 if let Some(event) = rocket.poll_events().unwrap() {
110 Event::SetRow(row) => {
111 println!("SetRow (row: {:?})", row);
112 self.t = row as f32 / BPS;
115 let track1 = rocket.get_track_mut("test").unwrap();
116 println!("Pause (value: {:?}) (row: {:?})", track1.get_value(current_row as f32), current_row);
120 println!("{:?}", event);