Moved main loop to App
[kaka/rust-sdl-test.git] / src / main.rs
index 217918c..877d367 100644 (file)
@@ -2,124 +2,26 @@ extern crate rand;
 extern crate sdl2;
 extern crate time;
 
-use std::f32::consts::PI;
-
-use sdl2::event::Event;
-use sdl2::event::WindowEvent;
-use sdl2::gfx::primitives::DrawRenderer;
-use sdl2::keyboard::Keycode;
-use sdl2::pixels::Color;
-use sdl2::rect::Rect;
-use sdl2::video::FullscreenType;
-use time::PreciseTime;
-
 use game::app::*;
-use common::Point2D;
 
 mod game;
-#[macro_use] mod common;
+#[macro_use]
+mod common;
 mod boll;
 mod sprites;
 
 const SCREEN_WIDTH: u32 = 1280;
 const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32;
-const FPS: u32 = 60;
-const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
 
 fn main() {
     println!("starting...");
     let mut app = App::new()
-       .with_resolution(SCREEN_WIDTH as u16, SCREEN_HEIGHT as u16)
-       .with_state(Box::new(ActiveState::new()))
-       .with_title("SDL test")
-       .build()
-       .unwrap();
-    app.load_sprites(&[
-        ("block", "res/block.bmp"),
-        ("mario", "res/mario-trans.png"),
-    ]);
-
-    let mut frame_count: u64 = 0;
-    let mut fps_time = PreciseTime::now();
-    let mut last_time = PreciseTime::now();
-
-    let mut mario_angle = 0.0;
-
-    'running: loop {
-        app.canvas.set_draw_color(Color::RGB(0, 0, 0));
-        app.canvas.clear();
-        {
-            let blocks = 20;
-            let size = 32;
-            let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2);
-            let block = app.sprites.get("block");
-            for i in 0..blocks {
-                app.canvas.copy(block, None, Rect::new((i) * size + offset.x, offset.y, size as u32, size as u32)).unwrap();
-                app.canvas.copy(block, None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap();
-                app.canvas.copy(block, None, Rect::new(offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap();
-                app.canvas.copy(block, None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap();
-            }
-        }
-        {
-            let size = 64;
-            let offset = point!((SCREEN_WIDTH as i32 - size) / 2, (SCREEN_HEIGHT as i32 - size) / 2);
-            let radius = 110.0 + size as f32 * 0.5;
-            let angle = (mario_angle as f32 - 90.0) * PI / 180.0;
-            let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
-            app.canvas.copy_ex(
-                app.sprites.get("mario"),
-                None, Rect::new(offset.x + offset2.x, offset.y + offset2.y, size as u32, size as u32),
-                mario_angle,
-                sdl2::rect::Point::new(size / 2, size / 2),
-                false, false).unwrap();
-            mario_angle += 1.0;
-            if mario_angle >= 360.0 { mario_angle -= 360.0 }
-        }
-        {
-            let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16);
-            app.canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap();
-            app.canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap();
-            app.canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap();
-            app.canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap();
-        }
-
-//        window.gl_swap_window();
-        for event in app.event_pump.poll_iter() {
-            match event {
-                Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
-                    break 'running;
-                }
-                Event::KeyDown { keycode: Some(Keycode::F11), .. } => {
-                    match app.canvas.window().fullscreen_state() {
-                        FullscreenType::Off => app.canvas.window_mut().set_fullscreen(FullscreenType::Desktop),
-                        _                   => app.canvas.window_mut().set_fullscreen(FullscreenType::Off)
-                    }.unwrap();
-                }
-                Event::Window { win_event: WindowEvent::Resized(x, y), .. } => { println!("window resized({}, {})", x, y) }
-                Event::Window { win_event: WindowEvent::Maximized, .. } => { println!("window maximized") }
-                Event::Window { win_event: WindowEvent::Restored, .. } => { println!("window restored") }
-                Event::Window { win_event: WindowEvent::Enter, .. } => { println!("window enter") }
-                Event::Window { win_event: WindowEvent::Leave, .. } => { println!("window leave") }
-                Event::Window { win_event: WindowEvent::FocusGained, .. } => { println!("window focus gained") }
-                Event::Window { win_event: WindowEvent::FocusLost, .. } => { println!("window focus lost") }
-                _ => { app.state.on_event(event) }
-            }
-        }
-
-        let duration = last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds;
-        last_time = PreciseTime::now();
-        app.state.update(duration);
-        app.state.render(&mut app.canvas);
-        app.canvas.present();
-
-        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();
-        }
-    }
-
-    app.state.leave();
+        .with_resolution(SCREEN_WIDTH as u16, SCREEN_HEIGHT as u16)
+        .with_state(Box::new(ActiveState::new()))
+        .with_title("SDL test")
+        .build()
+        .unwrap();
+    app.load_sprites(&[("block", "res/block.bmp"), ("mario", "res/mario-trans.png")]);
+
+    app.start();
 }