From fea68d56aa10f9e91f922323676ef7ff468340f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Tue, 5 Jan 2021 15:56:48 +0100 Subject: [PATCH] Vsync instead of fps + print display modes --- src/game/app.rs | 48 ++++++++++++++++++++++++++++++++---------------- src/main.rs | 4 ++-- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/game/app.rs b/src/game/app.rs index b37e8fd..362af2e 100644 --- a/src/game/app.rs +++ b/src/game/app.rs @@ -1,11 +1,11 @@ use rand::Rng; use sdl2::event::Event; -use sdl2::EventPump; +use sdl2::{EventPump, VideoSubsystem}; use sdl2::keyboard::Keycode; use sdl2::pixels::Color; use sdl2::render::BlendMode; use sdl2::render::Canvas; -use sdl2::video::Window; +use sdl2::video::{SwapInterval, Window}; use ::{SCREEN_HEIGHT, SCREEN_WIDTH}; use boll::*; @@ -19,7 +19,6 @@ pub type Nanoseconds = u64; #[derive(Default)] pub struct AppBuilder { resolution: Rect, - fps: u8, state: Option>, title: Option, } @@ -30,12 +29,6 @@ impl AppBuilder { self } - pub fn with_fps(mut self, fps: u8) -> Self { - unimplemented!() - // self.fps = fps; - // self - } - pub fn with_state(mut self, state: Box) -> Self { self.state = Some(state); self @@ -46,13 +39,18 @@ impl AppBuilder { self } - pub fn start(self) -> App { + pub fn build(self) -> Result { let context = sdl2::init().unwrap(); - sdl2::image::init(sdl2::image::InitFlag::PNG).unwrap(); - let window = context - .video().unwrap() + sdl2::image::init(sdl2::image::InitFlag::PNG)?; + let video = context.video()?; + + self.print_video_display_modes(&video); + + let window = video .window(&self.title.unwrap(), self.resolution.width.into(), self.resolution.height.into()) .position_centered() + // .fullscreen() + // .fullscreen_desktop() .opengl() .build().unwrap(); context.mouse().show_cursor(false); @@ -63,15 +61,33 @@ impl AppBuilder { canvas.clear(); canvas.present(); - let event_pump = context.event_pump().unwrap(); + video.gl_set_swap_interval(SwapInterval::VSync)?; + + let event_pump = context.event_pump()?; let sprites = SpriteManager::new(canvas.texture_creator()); - App { + Ok(App { canvas, event_pump, sprites, state: self.state.unwrap_or(Box::new(ActiveState::new())), - } + }) + } + + 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()); } } diff --git a/src/main.rs b/src/main.rs index 31f9d3b..217918c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,10 +30,10 @@ fn main() { println!("starting..."); let mut app = App::new() .with_resolution(SCREEN_WIDTH as u16, SCREEN_HEIGHT as u16) - .with_fps(60) .with_state(Box::new(ActiveState::new())) .with_title("SDL test") - .start(); + .build() + .unwrap(); app.load_sprites(&[ ("block", "res/block.bmp"), ("mario", "res/mario-trans.png"), -- 2.11.0