5 use std::f32::consts::PI;
8 use sdl2::event::Event;
9 use sdl2::event::WindowEvent;
11 use sdl2::gfx::primitives::DrawRenderer;
12 use sdl2::keyboard::Keycode;
13 use sdl2::pixels::Color;
15 use sdl2::render::BlendMode;
16 use sdl2::render::Canvas;
17 use sdl2::video::FullscreenType;
18 use sdl2::video::Window;
19 use time::PreciseTime;
21 use boll::{Boll, CircleBoll, SquareBoll};
23 use sprites::SpriteManager;
25 #[macro_use] mod common;
29 const SCREEN_WIDTH: u32 = 1280;
30 const SCREEN_HEIGHT: u32 = (SCREEN_WIDTH as f64 * (1440.0 / 2560.0)) as u32;
32 const NS_PER_FRAME: u32 = 1_000_000_000 / FPS;
34 type Bollar = Vec<Box<dyn Boll>>;
37 canvas: Canvas<Window>,
38 event_pump: EventPump,
39 sprites: SpriteManager,
44 let context = sdl2::init().unwrap();
45 sdl2::image::init(sdl2::image::InitFlag::PNG).unwrap();
46 let window = context.video().unwrap().window("SDL test", SCREEN_WIDTH, SCREEN_HEIGHT)
51 context.mouse().show_cursor(false);
52 let mut canvas = window.into_canvas().build().unwrap();
53 canvas.set_blend_mode(BlendMode::Add);
54 canvas.set_draw_color(Color::RGB(0, 0, 0));
57 let event_pump = context.event_pump().unwrap();
58 let sprites = SpriteManager::new(canvas.texture_creator());
66 fn load_sprites(&mut self, sprites: &[(&'static str, &str)]) {
67 for (name, file) in sprites {
68 self.sprites.load(name, file);
74 println!("starting...");
75 let mut app = App::new();
77 ("block", "res/block.bmp"),
78 ("mario", "res/mario-trans.png"),
81 let mut frame_count: u64 = 0;
82 let mut fps_time = PreciseTime::now();
84 let mut bolls: Bollar = Bollar::new();
85 let mut boll_size = 1;
87 let mut mario_angle = 0.0;
90 let loop_start = PreciseTime::now();
91 app.canvas.set_draw_color(Color::RGB(0, 0, 0));
96 let offset = point!((SCREEN_WIDTH as i32 - (blocks + 1) * size) / 2, (SCREEN_HEIGHT as i32 - (blocks + 1) * size) / 2);
97 let block = app.sprites.get("block");
99 app.canvas.copy(block, None, Rect::new((i) * size + offset.x, (0) * size + offset.y, size as u32, size as u32)).unwrap();
100 app.canvas.copy(block, None, Rect::new((blocks - i) * size + offset.x, (blocks) * size + offset.y, size as u32, size as u32)).unwrap();
101 app.canvas.copy(block, None, Rect::new((0) * size + offset.x, (blocks - i) * size + offset.y, size as u32, size as u32)).unwrap();
102 app.canvas.copy(block, None, Rect::new((blocks) * size + offset.x, (i) * size + offset.y, size as u32, size as u32)).unwrap();
107 let offset = point!((SCREEN_WIDTH as i32 - size) / 2, (SCREEN_HEIGHT as i32 - size) / 2);
108 let radius = 110.0 + size as f32 * 0.5;
109 let angle = (mario_angle as f32 - 90.0) * PI / 180.0;
110 let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32);
111 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();
113 if mario_angle >= 360.0 { mario_angle -= 360.0 }
116 let p = point!((SCREEN_WIDTH / 2) as i16, (SCREEN_HEIGHT / 2) as i16);
117 app.canvas.circle(p.x, p.y, 100, Color::RGB(255, 255, 255)).unwrap();
118 app.canvas.aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)).unwrap();
119 app.canvas.ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)).unwrap();
120 app.canvas.aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)).unwrap();
123 for b in &mut bolls {
125 b.draw(&mut app.canvas, boll_size);
128 // window.gl_swap_window();
129 for event in app.event_pump.poll_iter() {
131 Event::Quit { .. } | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
134 Event::KeyDown { keycode: Some(Keycode::F11), .. } => {
135 match app.canvas.window().fullscreen_state() {
136 FullscreenType::Off => app.canvas.window_mut().set_fullscreen(FullscreenType::Desktop),
137 _ => app.canvas.window_mut().set_fullscreen(FullscreenType::Off)
140 Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { boll_size = std::cmp::min(boll_size + 1, 32) }
141 Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { boll_size = std::cmp::max(boll_size - 1, 1) }
142 Event::MouseMotion { x, y, .. } => {
143 bolls.push(Box::new(CircleBoll::new(
144 point!(x as f64, y as f64),
148 Event::Window { win_event: WindowEvent::Resized(x, y), .. } => { println!("window resized({}, {})", x, y) }
149 Event::Window { win_event: WindowEvent::Maximized, .. } => { println!("window maximized") }
150 Event::Window { win_event: WindowEvent::Restored, .. } => { println!("window restored") }
151 Event::Window { win_event: WindowEvent::Enter, .. } => { println!("window enter") }
152 Event::Window { win_event: WindowEvent::Leave, .. } => { println!("window leave") }
153 Event::Window { win_event: WindowEvent::FocusGained, .. } => { println!("window focus gained") }
154 Event::Window { win_event: WindowEvent::FocusLost, .. } => { println!("window focus lost") }
158 app.canvas.present();
160 match loop_start.to(PreciseTime::now()).num_nanoseconds() {
161 Some(ns) if ns < (NS_PER_FRAME - 50_0000) as i64 => { change_boll_count(&mut bolls, 100) }
162 Some(ns) if ns > (NS_PER_FRAME + 50_0000) as i64 => { change_boll_count(&mut bolls, -100) }
167 if frame_count == FPS as u64 {
168 let duration = fps_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as f64 / 1_000_000_000.0;
169 println!("fps: {}", frame_count as f64 / duration);
171 fps_time = PreciseTime::now();
175 println!("number of bolls: {}", bolls.len());
178 fn change_boll_count(mut bolls: &mut Bollar, delta: i32) {
181 add_boll(&mut bolls);
183 } else if delta < 0 {
190 fn add_boll(bolls: &mut Bollar) {
191 let mut rng = rand::thread_rng();
192 bolls.push(Box::new(SquareBoll {
193 pos: point!(rng.gen_range(0, SCREEN_WIDTH) as f64, rng.gen_range(0, SCREEN_HEIGHT) as f64),
194 vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)),