4c399c62a56f1ded337ac3de533a9bebdab6bafb
[kaka/rust-sdl-test.git] / src / main.rs
1 extern crate rand;
2 extern crate sdl2;
3 extern crate time;
4
5 use std::f32::consts::PI;
6
7 use sdl2::event::Event;
8 use sdl2::event::WindowEvent;
9 use sdl2::gfx::primitives::DrawRenderer;
10 use sdl2::keyboard::Keycode;
11 use sdl2::pixels::Color;
12 use sdl2::rect::Rect;
13 use sdl2::video::FullscreenType;
14 use time::PreciseTime;
15
16 use app::*;
17 use common::Point2D;
18
19 mod app;
20 #[macro_use] mod common;
21 mod boll;
22 mod sprites;
23
24 const SCREEN_WIDTH: u32 = 1280;
25 const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32;
26 const FPS: u32 = 60;
27 const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
28
29 fn main() {
30     println!("starting...");
31     let mut app = App::new();
32     app.load_sprites(&[
33         ("block", "res/block.bmp"),
34         ("mario", "res/mario-trans.png"),
35     ]);
36
37     let mut frame_count: u64 = 0;
38     let mut fps_time = PreciseTime::now();
39     let mut last_time = PreciseTime::now();
40
41     let mut mario_angle = 0.0;
42
43     'running: loop {
44         app.canvas.set_draw_color(Color::RGB(0, 0, 0));
45         app.canvas.clear();
46         {
47             let blocks = 20;
48             let size = 32;
49             let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2);
50             let block = app.sprites.get("block");
51             for i in 0..blocks {
52                 app.canvas.copy(block, None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap();
53                 app.canvas.copy(block, None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap();
54                 app.canvas.copy(block, None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap();
55                 app.canvas.copy(block, None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap();
56             }
57         }
58         {
59             let size = 64;
60             let offset = point!((SCREEN_WIDTH as i32 - size) / 2, (SCREEN_HEIGHT as i32 - size) / 2);
61             let radius = 110.0 + size as f32 * 0.5;
62             let angle = (mario_angle as f32 - 90.0) * PI / 180.0;
63             let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
64             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();
65             mario_angle += 1.0;
66             if mario_angle >= 360.0 { mario_angle -= 360.0 }
67         }
68         {
69             let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16);
70             app.canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap();
71             app.canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap();
72             app.canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap();
73             app.canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap();
74         }
75
76 //        window.gl_swap_window();
77         for event in app.event_pump.poll_iter() {
78             match event {
79                 Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
80                     break 'running;
81                 }
82                 Event::KeyDown { keycode: Some(Keycode::F11), .. } => {
83                     match app.canvas.window().fullscreen_state() {
84                         FullscreenType::Off => app.canvas.window_mut().set_fullscreen(FullscreenType::Desktop),
85                         _                   => app.canvas.window_mut().set_fullscreen(FullscreenType::Off)
86                     }.unwrap();
87                 }
88                 Event::Window { win_event: WindowEvent::Resized(x, y), .. } => { println!("window resized({}, {})", x, y) }
89                 Event::Window { win_event: WindowEvent::Maximized, .. } => { println!("window maximized") }
90                 Event::Window { win_event: WindowEvent::Restored, .. } => { println!("window restored") }
91                 Event::Window { win_event: WindowEvent::Enter, .. } => { println!("window enter") }
92                 Event::Window { win_event: WindowEvent::Leave, .. } => { println!("window leave") }
93                 Event::Window { win_event: WindowEvent::FocusGained, .. } => { println!("window focus gained") }
94                 Event::Window { win_event: WindowEvent::FocusLost, .. } => { println!("window focus lost") }
95                 _ => { app.state.on_event(event) }
96             }
97         }
98
99         let duration = last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds;
100         last_time = PreciseTime::now();
101         app.state.update(duration);
102         app.state.render(&mut app.canvas);
103         app.canvas.present();
104
105         frame_count += 1;
106         if frame_count == FPS as u64 {
107             let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64 / 1_000_000_000.0;
108             println!("fps: {}", frame_count as f64 / duration);
109             frame_count = 0;
110             fps_time = PreciseTime::now();
111         }
112     }
113
114     app.state.leave();
115 }