From 61b82c37bc1798c4878bea2ca0139f3b9ad8ecd8 Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Thu, 6 Jul 2017 02:19:42 -0400 Subject: [PATCH] Move editor code into Timer. Maybe rename Timer to Player? Not today. --- lib.rs | 2 +- main.rs | 24 ------------------------ timer.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 25 deletions(-) diff --git a/lib.rs b/lib.rs index 765068f..2afc947 100644 --- a/lib.rs +++ b/lib.rs @@ -5,7 +5,7 @@ extern crate glutin; #[cfg(feature = "image")] extern crate image; extern crate libxm; -#[cfg(feature = "rust_rocket")] +#[cfg(feature = "editor")] extern crate rust_rocket; extern crate sdl2; diff --git a/main.rs b/main.rs index 9d89c2c..ef61ea8 100644 --- a/main.rs +++ b/main.rs @@ -5,9 +5,6 @@ extern crate cgmath; extern crate glium; extern crate glutin; -#[cfg(feature = "rust-rocket")] -extern crate rust_rocket; - use cgmath::{Euler, Matrix4, Rad, SquareMatrix, Vector3, Vector4, Zero}; use cgmath::conv::array4x4; use glium::{DisplayBuild, Surface}; @@ -68,12 +65,6 @@ pub fn set_main_loop_callback(callback : F) where F : FnMut() -> support::Act fn main() { let mut soundplayer = sound::start(); - let mut rocket = rust_rocket::Rocket::new().unwrap(); - rocket.get_track_mut("test"); - rocket.get_track_mut("test2"); - rocket.get_track_mut("a:test2"); - let mut current_row = 0; - let display = glutin::WindowBuilder::new() .with_dimensions(1280, 720) .with_gl_profile(glutin::GlProfile::Core) @@ -236,21 +227,6 @@ fn main() { } } - if let Some(event) = rocket.poll_events() { - match event { - rust_rocket::Event::SetRow(row) => { - println!("SetRow (row: {:?})", row); - current_row = row; - } - rust_rocket::Event::Pause(_) => { - let track1 = rocket.get_track("test").unwrap(); - println!("Pause (value: {:?}) (row: {:?})", track1.get_value(current_row as f32), current_row); - } - _ => (), - } - println!("{:?}", event); - } - timer.update(); support::Action::Continue diff --git a/timer.rs b/timer.rs index defc9e8..8434a69 100644 --- a/timer.rs +++ b/timer.rs @@ -1,5 +1,16 @@ 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 = (); + +const BPS: f32 = 10.0; + #[derive(Debug)] pub struct Timer { pub t: f32, @@ -13,6 +24,8 @@ pub struct Timer { accum_idle_time: Duration, pub pause: bool, + + pub rocket: Option, } impl Timer { @@ -26,11 +39,13 @@ impl Timer { 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.poll_rocket(); if !self.pause { // Increment time self.t += 0.01; @@ -64,4 +79,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 => () + } + } } -- 2.25.1