X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=timer.rs;h=be0261b23520f1e5d9a64c271037f28a2947ff28;hb=20c701434df0fea9118120e87aae12f914cc47d5;hp=defc9e88324c1ee7cc65427d612e052c3adc4e4d;hpb=7babb28c6782e473dd4302c930880cb147f0ceb8;p=mandelwow.git diff --git a/timer.rs b/timer.rs index defc9e8..be0261b 100644 --- a/timer.rs +++ b/timer.rs @@ -1,9 +1,21 @@ use std::time::{Duration, Instant}; +#[cfg(feature = "editor")] +use rust_rocket; + +#[cfg(feature = "editor")] +type Rocket = rust_rocket::Rocket; + +#[cfg(not(feature = "editor"))] +type Rocket = (); + +#[cfg(feature = "editor")] +const BPS: f32 = 10.0; + #[derive(Debug)] pub struct Timer { pub t: f32, - + pub now: Instant, frame: u32, last_report_time: Instant, @@ -13,6 +25,8 @@ pub struct Timer { accum_idle_time: Duration, pub pause: bool, + + pub rocket: Option, } impl Timer { @@ -20,33 +34,36 @@ impl Timer { let now = Instant::now(); Timer { t: 0.0, + now, frame: 0, last_report_time: now, last_report_frame: 0, accum_draw_time: Duration::default(), accum_idle_time: Duration::default(), pause: false, + rocket: Timer::init_rocket(), } } pub fn update(&mut self) { - let now = Instant::now(); + self.now = Instant::now(); + self.poll_rocket(); if !self.pause { // Increment time self.t += 0.01; self.frame += 1; } - self.maybe_report(now); + self.maybe_report(); } #[cfg(not(feature = "logging"))] - fn maybe_report(&mut self, _: Instant) {} + fn maybe_report(&mut self) {} #[cfg(feature = "logging")] - fn maybe_report(&mut self, now: Instant) { - if now - self.last_report_time > Duration::from_secs(5) { - self.report(now); - self.last_report_time = now; + fn maybe_report(&mut self) { + if self.now - self.last_report_time > Duration::from_secs(5) { + self.report(self.now); + self.last_report_time = self.now; self.last_report_frame = self.frame; self.accum_draw_time = Duration::new(0, 0); self.accum_idle_time = Duration::new(0, 0); @@ -64,4 +81,39 @@ impl Timer { let avg_idle_time = millis(self.accum_idle_time / frames_done); println!("fps={:.1} draw={:.1}ms idle={:.1}ms", fps, avg_draw_time, avg_idle_time); } + + #[cfg(not(feature = "editor"))] + fn init_rocket() -> Option { None } + + #[cfg(not(feature = "editor"))] + fn poll_rocket(&mut self) {} + + #[cfg(feature = "editor")] + fn init_rocket() -> Option { + Rocket::new().ok() + } + + #[cfg(feature = "editor")] + fn poll_rocket(&mut self) { + match self.rocket { + Some(ref mut rocket) => { + let current_row = (self.t * BPS) as u32; + if let Some(event) = rocket.poll_events() { + match event { + rust_rocket::Event::SetRow(row) => { + println!("SetRow (row: {:?})", row); + self.t = row as f32 / BPS; + } + rust_rocket::Event::Pause(_) => { + let track1 = rocket.get_track_mut("test"); + println!("Pause (value: {:?}) (row: {:?})", track1.get_value(current_row as f32), current_row); + } + _ => (), + } + println!("{:?}", event); + } + } + None => () + } + } }