No need for mutable references
[kaka/rust-sdl-test.git] / src / core / app.rs
index 62d7507..cac2a37 100644 (file)
@@ -1,5 +1,5 @@
 use boll::*;
-use common::{Nanoseconds, Point2D, Rect};
+use common::{Point2D, Rect};
 use core::controller::ControllerManager;
 use point; // defined in common, but loaded from main...
 use rand::Rng;
@@ -13,7 +13,7 @@ use sdl2::video::{FullscreenType, SwapInterval, Window};
 use sdl2::{EventPump, VideoSubsystem};
 use sprites::SpriteManager;
 use std::f32::consts::PI;
-use time::PreciseTime;
+use time::{Duration, Instant, prelude::*};
 
 const FPS: u32 = 60;
 const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
@@ -72,15 +72,13 @@ impl AppBuilder {
         let event_pump = context.event_pump()?;
         let sprites = SpriteManager::new(canvas.texture_creator());
        let screen = canvas.output_size().unwrap();
-       let ctrl = context.game_controller()?;
-       ctrl.set_event_state(true);
 
         Ok(App {
             canvas,
             event_pump,
             sprites,
             state: self.state.unwrap_or_else(|| Box::new(ActiveState::new(screen))),
-           ctrl_man: ControllerManager::new(ctrl, context.haptic()?),
+           ctrl_man: ControllerManager::new(context.joystick()?, context.haptic()?),
         })
     }
 
@@ -146,9 +144,7 @@ impl App {
     }
 
     pub fn start(&mut self) {
-        // let mut frame_count: u64 = 0;
-        // let mut fps_time = PreciseTime::now();
-        let mut last_time = PreciseTime::now();
+        let mut last_time = Instant::now();
 
        self.state.enter(&mut self.ctrl_man);
 
@@ -157,21 +153,13 @@ impl App {
                break 'running;
            }
 
-            let duration =
-                last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds;
-            last_time = PreciseTime::now();
+            let duration = Instant::now() - last_time;
+           last_time = Instant::now();
+
+           self.ctrl_man.update(duration);
             self.state.update(duration);
 
            self.render();
-
-            // frame_count += 1;
-            // if frame_count == FPS as u64 {
-            //     let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64
-            //         / 1_000_000_000.0;
-            //     // println!("fps: {}", frame_count as f64 / duration);
-            //     frame_count = 0;
-            //     fps_time = PreciseTime::now();
-            // }
         }
 
         self.state.leave();
@@ -258,10 +246,10 @@ impl App {
 }
 
 pub trait AppState {
-    fn enter(&mut self, ctrl_man: &mut ControllerManager);
+    fn enter(&mut self, ctrl_man: &ControllerManager);
     fn leave(&mut self);
-    fn update(&mut self, dt: Nanoseconds);
-    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager);
+    fn update(&mut self, dt: Duration);
+    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &SpriteManager);
     fn handle_event(&mut self, event: Event);
 }
 
@@ -311,21 +299,21 @@ impl ActiveState {
 }
 
 impl AppState for ActiveState {
-    fn enter(&mut self, ctrl_man: &mut ControllerManager) {}
+    fn enter(&mut self, _ctrl_man: &ControllerManager) {}
 
-    fn update(&mut self, dt: Nanoseconds) {
+    fn update(&mut self, dt: Duration) {
         for b in &mut self.bolls {
             b.update();
         }
 
         match dt {
-            ns if ns < (NS_PER_FRAME - 90_0000) as u64 => self.change_boll_count(100),
-            ns if ns > (NS_PER_FRAME + 90_0000) as u64 => self.change_boll_count(-100),
+            ns if ns < (NS_PER_FRAME - 90_0000).nanoseconds() => self.change_boll_count(100),
+            ns if ns > (NS_PER_FRAME + 90_0000).nanoseconds() => self.change_boll_count(-100),
             _ => {}
         }
     }
 
-    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &mut SpriteManager) {
+    fn render(&mut self, canvas: &mut Canvas<Window>, sprites: &SpriteManager) {
         /* draw square of blocks */ {
             let blocks = 20;
             let size = 32;