Move editor code into Timer.
authorBernie Innocenti <bernie@codewiz.org>
Thu, 6 Jul 2017 06:19:42 +0000 (02:19 -0400)
committerBernie Innocenti <bernie@codewiz.org>
Thu, 6 Jul 2017 06:19:42 +0000 (02:19 -0400)
Maybe rename Timer to Player? Not today.

lib.rs
main.rs
timer.rs

diff --git a/lib.rs b/lib.rs
index 765068fb15529fd1ed61ebb010e8857963460943..2afc947314b23b7e19db9faf87b937dbcc820594 100644 (file)
--- 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 9d89c2c4acc6108c7fd64b469d4eba4c7b4d72d5..ef61ea816e0bb89f8468ae6a0078f3178c49db5d 100644 (file)
--- 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<F>(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
index defc9e88324c1ee7cc65427d612e052c3adc4e4d..8434a69976afeaecb2449d9636eb8700eb199ddb 100644 (file)
--- 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<Rocket>,
 }
 
 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<Rocket> { None }
+
+    #[cfg(not(feature = "editor"))]
+    fn poll_rocket(&mut self) {}
+
+    #[cfg(feature = "editor")]
+    fn init_rocket() -> Option<Rocket> {
+        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 => ()
+        }
+    }
 }