X-Git-Url: http://git.dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fgame%2Fapp.rs;h=f6adba046d0d64f4885d68b6cebf3f05d2282ce7;hb=541aee90f8168269805c4194d757a2520e040e2a;hp=362af2ef4f209876129b45af3544cae2c3cd5459;hpb=fea68d56aa10f9e91f922323676ef7ff468340f6;p=kaka%2Frust-sdl-test.git diff --git a/src/game/app.rs b/src/game/app.rs index 362af2e..f6adba0 100644 --- a/src/game/app.rs +++ b/src/game/app.rs @@ -1,21 +1,24 @@ +use boll::*; +use common::{Point2D, Rect}; +use point; // defined in common, but loaded from main... use rand::Rng; -use sdl2::event::Event; -use sdl2::{EventPump, VideoSubsystem}; +use sdl2::event::{Event, WindowEvent}; +use sdl2::gfx::primitives::DrawRenderer; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; -use sdl2::render::BlendMode; -use sdl2::render::Canvas; -use sdl2::video::{SwapInterval, Window}; - -use ::{SCREEN_HEIGHT, SCREEN_WIDTH}; -use boll::*; -use common::{Point2D, Rect}; +use sdl2::rect::Rect as SDLRect; +use sdl2::render::{BlendMode, Canvas}; +use sdl2::video::{FullscreenType, SwapInterval, Window}; +use sdl2::{EventPump, VideoSubsystem}; use sprites::SpriteManager; -use NS_PER_FRAME; -use point; // defined in common, but loaded from main... +use std::f32::consts::PI; +use time::PreciseTime; pub type Nanoseconds = u64; +const FPS: u32 = 60; +const NS_PER_FRAME: u32 = 1_000_000_000 / FPS; + #[derive(Default)] pub struct AppBuilder { resolution: Rect, @@ -25,34 +28,39 @@ pub struct AppBuilder { impl AppBuilder { pub fn with_resolution(mut self, width: u16, height: u16) -> Self { - self.resolution = Rect { width, height }; - self + self.resolution = Rect { width, height }; + self } pub fn with_state(mut self, state: Box) -> Self { - self.state = Some(state); - self + self.state = Some(state); + self } pub fn with_title(mut self, title: &str) -> Self { - self.title = Some(title.to_string()); - self + self.title = Some(title.to_string()); + self } pub fn build(self) -> Result { let context = sdl2::init().unwrap(); sdl2::image::init(sdl2::image::InitFlag::PNG)?; - let video = context.video()?; + let video = context.video()?; - self.print_video_display_modes(&video); + self.print_video_display_modes(&video); let window = video - .window(&self.title.unwrap(), self.resolution.width.into(), self.resolution.height.into()) + .window( + &self.title.unwrap(), + self.resolution.width.into(), + self.resolution.height.into(), + ) .position_centered() - // .fullscreen() - // .fullscreen_desktop() + // .fullscreen() + // .fullscreen_desktop() .opengl() - .build().unwrap(); + .build() + .unwrap(); context.mouse().show_cursor(false); let mut canvas = window.into_canvas().build().unwrap(); @@ -61,33 +69,57 @@ impl AppBuilder { canvas.clear(); canvas.present(); - video.gl_set_swap_interval(SwapInterval::VSync)?; + video.gl_set_swap_interval(SwapInterval::VSync)?; let event_pump = context.event_pump()?; let sprites = SpriteManager::new(canvas.texture_creator()); + let screen = canvas.output_size().unwrap(); Ok(App { canvas, event_pump, sprites, - state: self.state.unwrap_or(Box::new(ActiveState::new())), + state: self.state.unwrap_or_else(|| Box::new(ActiveState::new(screen))), }) } fn print_video_display_modes(&self, video: &VideoSubsystem) { - println!("video subsystem: {:?}", video); - println!("current_video_driver: {:?}", video.current_video_driver()); - for display in 0..video.num_video_displays().unwrap() { - println!("=== display {} - {} ===", display, video.display_name(display).unwrap()); - println!(" display_bounds: {:?}", video.display_bounds(display).unwrap()); - println!(" num_display_modes: {:?}", video.num_display_modes(display).unwrap()); - println!(" desktop_display_mode: {:?}", video.desktop_display_mode(display).unwrap()); - println!(" current_display_mode: {:?}", video.current_display_mode(display).unwrap()); - for mode in 0..video.num_display_modes(display).unwrap() { - println!(" {:2}: {:?}", mode, video.display_mode(display, mode).unwrap()); - } - } - println!("swap interval: {:?}", video.gl_get_swap_interval()); + println!("video subsystem: {:?}", video); + println!("current_video_driver: {:?}", video.current_video_driver()); + for display in 0..video.num_video_displays().unwrap() { + println!( + "=== display {} - {} ===", + display, + video.display_name(display).unwrap() + ); + println!( + " display_bounds: {:?}", + video.display_bounds(display).unwrap() + ); + println!( + " num_display_modes: {:?}", + video.num_display_modes(display).unwrap() + ); + println!( + " desktop_display_mode: {:?}", + video.desktop_display_mode(display).unwrap() + ); + let current = video.current_display_mode(display).unwrap(); + println!( + " current_display_mode: {:?}", + current + ); + for idx in 0..video.num_display_modes(display).unwrap() { + let mode = video.display_mode(display, idx).unwrap(); + println!( + " {}{:2}: {:?}", + if mode == current { "*" } else { " " }, + idx, + mode + ); + } + } + println!("swap interval: {:?}", video.gl_get_swap_interval()); } } @@ -99,8 +131,9 @@ pub struct App { } impl App { + #[allow(clippy::new_ret_no_self)] pub fn new() -> AppBuilder { - Default::default() + Default::default() } pub fn load_sprites(&mut self, sprites: &[(&str, &str)]) { @@ -108,31 +141,145 @@ impl App { self.sprites.load(name, file); } } + + pub fn start(&mut self) { + let mut frame_count: u64 = 0; + let mut fps_time = PreciseTime::now(); + let mut last_time = PreciseTime::now(); + + 'running: loop { + if let Err(_) = self.handle_events() { + break 'running; + } + + let duration = + last_time.to(PreciseTime::now()).num_nanoseconds().unwrap() as Nanoseconds; + last_time = PreciseTime::now(); + self.state.update(duration); + + self.render(); + + 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(); + } + } + + self.state.leave(); + } + + fn handle_events(&mut self) -> Result<(), ()> { + for event in self.event_pump.poll_iter() { + match event { + Event::Quit { .. } + | Event::KeyDown { + keycode: Some(Keycode::Escape), + .. + } => { + return Err(()) + } + Event::KeyDown { + keycode: Some(Keycode::F11), + .. + } => { + match self.canvas.window().fullscreen_state() { + FullscreenType::Off => self + .canvas + .window_mut() + .set_fullscreen(FullscreenType::Desktop), + _ => self.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") + } + _ => self.state.on_event(event), + } + } + Ok(()) + } + + fn render(&mut self) { + self.canvas.set_draw_color(Color::RGB(0, 0, 0)); + self.canvas.clear(); + self.state.render(&mut self.canvas, &mut self.sprites); + self.canvas.present(); + } } pub trait AppState { fn update(&mut self, dt: Nanoseconds); - fn render(&self, canvas: &mut Canvas); + fn render(&mut self, canvas: &mut Canvas, sprites: &mut SpriteManager); fn leave(&self); fn on_event(&mut self, event: Event); } type Bollar = Vec>; +#[derive(Default)] pub struct ActiveState { + screen: Rect, bolls: Bollar, boll_size: u32, + mario_angle: f64, } impl ActiveState { - pub fn new() -> ActiveState { + pub fn new(screen: (u32, u32)) -> ActiveState { ActiveState { bolls: Bollar::new(), boll_size: 1, + screen: Rect::from(screen), + ..Default::default() } } fn change_boll_count(&mut self, delta: i32) { + #[allow(clippy::comparison_chain)] if delta > 0 { for _i in 0..delta { self.add_boll(); @@ -147,7 +294,10 @@ impl ActiveState { fn add_boll(&mut self) { let mut rng = rand::thread_rng(); self.bolls.push(Box::new(SquareBoll { - pos: point!(rng.gen_range(0, SCREEN_WIDTH) as f64, rng.gen_range(0, SCREEN_HEIGHT) as f64), + pos: point!( + rng.gen_range(0, self.screen.width) as f64, + rng.gen_range(0, self.screen.height) as f64 + ), vel: point!(rng.gen_range(-2.0, 2.0), rng.gen_range(-2.0, 2.0)), })); } @@ -160,13 +310,112 @@ impl AppState for ActiveState { } match dt { - ns if ns < (NS_PER_FRAME - 90_0000) as u64 => { self.change_boll_count(100) } - ns if ns > (NS_PER_FRAME + 90_0000) as u64 => { self.change_boll_count(-100) } + ns if ns < (NS_PER_FRAME - 90_0000) as u64 => self.change_boll_count(100), + ns if ns > (NS_PER_FRAME + 90_0000) as u64 => self.change_boll_count(-100), _ => {} } } - fn render(&self, canvas: &mut Canvas) { + fn render(&mut self, canvas: &mut Canvas, sprites: &mut SpriteManager) { + /* draw square of blocks */ { + let blocks = 20; + let size = 32; + let offset = point!( + (self.screen.width as i32 - (blocks + 1) * size) / 2, + (self.screen.height as i32 - (blocks + 1) * size) / 2 + ); + let block = sprites.get("block"); + for i in 0..blocks { + canvas + .copy( + block, + None, + SDLRect::new((i) * size + offset.x, offset.y, size as u32, size as u32), + ) + .unwrap(); + canvas + .copy( + block, + None, + SDLRect::new( + (blocks - i) * size + offset.x, + (blocks) * size + offset.y, + size as u32, + size as u32, + ), + ) + .unwrap(); + canvas + .copy( + block, + None, + SDLRect::new( + offset.x, + (blocks - i) * size + offset.y, + size as u32, + size as u32, + ), + ) + .unwrap(); + canvas + .copy( + block, + None, + SDLRect::new( + (blocks) * size + offset.x, + (i) * size + offset.y, + size as u32, + size as u32, + ), + ) + .unwrap(); + } + } + + /* draw mario */ { + let size = 64; + let offset = point!( + (self.screen.width as i32 - size) / 2, + (self.screen.height as i32 - size) / 2 + ); + let radius = 110.0 + size as f32 * 0.5; + let angle = (self.mario_angle as f32 - 90.0) * PI / 180.0; + let offset2 = point!((angle.cos() * radius) as i32, (angle.sin() * radius) as i32); + canvas + .copy_ex( + sprites.get("mario"), + None, + SDLRect::new( + offset.x + offset2.x, + offset.y + offset2.y, + size as u32, + size as u32, + ), + self.mario_angle, + sdl2::rect::Point::new(size / 2, size / 2), + false, + false, + ) + .unwrap(); + self.mario_angle = (self.mario_angle + 1.0) % 360.0; + } + + /* draw circles and ellipses*/ { + let p = point!((self.screen.width / 2) as i16, (self.screen.height / 2) as i16); + canvas + .circle(p.x, p.y, 100, Color::RGB(255, 255, 255)) + .unwrap(); + canvas + .aa_circle(p.x, p.y, 110, Color::RGB(255, 255, 255)) + .unwrap(); + canvas + .ellipse(p.x, p.y, 50, 100, Color::RGB(255, 255, 255)) + .unwrap(); + canvas + .aa_ellipse(p.x, p.y, 110, 55, Color::RGB(255, 255, 255)) + .unwrap(); + } + for b in &self.bolls { b.draw(canvas, self.boll_size); } @@ -178,14 +427,18 @@ impl AppState for ActiveState { fn on_event(&mut self, event: Event) { match event { - Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { self.boll_size = std::cmp::min(self.boll_size + 1, 32) } - Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { self.boll_size = std::cmp::max(self.boll_size - 1, 1) } - Event::MouseMotion { x, y, .. } => { - self.bolls.push(Box::new(CircleBoll::new( - point!(x as f64, y as f64), - point!(0.0, 0.0), - ))) - } + Event::KeyDown { + keycode: Some(Keycode::KpPlus), + .. + } => self.boll_size = std::cmp::min(self.boll_size + 1, 32), + Event::KeyDown { + keycode: Some(Keycode::KpMinus), + .. + } => self.boll_size = std::cmp::max(self.boll_size - 1, 1), + Event::MouseMotion { x, y, .. } => self.bolls.push(Box::new(CircleBoll::new( + point!(x as f64, y as f64), + point!(0.0, 0.0), + ))), _ => {} } }