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