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