From ade03121eeefcead59ba6e56d067d96124a695a1 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Tue, 21 Jan 2020 01:11:50 +0200 Subject: [PATCH] feat(sdl2): Support window resizing Former-commit-id: f5e70c8a15b53eb7aded9f630e5c1fb1dce55fba --- src/plat/sdl2/main.rs | 35 +++++++++++------------------------ src/plat/sdl2/video.rs | 3 +-- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/src/plat/sdl2/main.rs b/src/plat/sdl2/main.rs index 2ffb19d..43ee680 100644 --- a/src/plat/sdl2/main.rs +++ b/src/plat/sdl2/main.rs @@ -26,7 +26,7 @@ mod video; use audio::create_audio_player; use input::create_input; -use video::{create_video_interface, SCALE, SCREEN_HEIGHT, SCREEN_WIDTH}; +use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH}; extern crate rustboyadvance_ng; use rustboyadvance_ng::prelude::*; @@ -61,21 +61,19 @@ fn main() -> Result<(), Box> { let debug = matches.occurrences_of("debug") != 0; let sdl_context = sdl2::init().expect("failed to initialize sdl2"); - let mut event_pump = sdl_context.event_pump().unwrap(); + let mut event_pump = sdl_context.event_pump()?; - let video_subsystem = sdl_context.video().unwrap(); - let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG).unwrap(); + let video_subsystem = sdl_context.video()?; + let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?; let window = video_subsystem - .window( - "RustBoyAdvance", - SCREEN_WIDTH * SCALE, - SCREEN_HEIGHT * SCALE, - ) + .window("RustBoyAdvance", SCREEN_WIDTH * 3, SCREEN_HEIGHT * 3) .opengl() .position_centered() - .build() - .unwrap(); - let mut canvas = window.into_canvas().accelerated().build().unwrap(); + .resizable() + .build()?; + let mut canvas = window.into_canvas().accelerated().build()?; + + canvas.set_logical_size(SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32)?; // Display the icon as a placeholder canvas.set_draw_color(Color::RGB(0x40, 0x22, 0x20)); // default background color for the icon @@ -84,18 +82,7 @@ fn main() -> Result<(), Box> { let icon_texture = texture_creator .load_texture("assets/icon.png") .expect("failed to load icon"); - canvas - .copy( - &icon_texture, - None, - Some(Rect::new( - (SCREEN_WIDTH as i32) * ((SCALE as i32) - 1) / 2, - (SCREEN_HEIGHT as i32) * ((SCALE as i32) - 1) / 2, - SCREEN_WIDTH, - SCREEN_HEIGHT, - )), - ) - .unwrap(); + canvas.copy(&icon_texture, None, None).unwrap(); canvas.present(); // TODO also set window icon diff --git a/src/plat/sdl2/video.rs b/src/plat/sdl2/video.rs index 99164e9..51ecfca 100644 --- a/src/plat/sdl2/video.rs +++ b/src/plat/sdl2/video.rs @@ -8,7 +8,6 @@ use rustboyadvance_ng::VideoInterface; pub const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32; pub const SCREEN_HEIGHT: u32 = DISPLAY_HEIGHT as u32; -pub const SCALE: u32 = 3; // TODO control via CLI & support window resize pub struct Sdl2Video<'a> { _tc: TextureCreator, // only kept alive because of the texture @@ -35,7 +34,7 @@ impl<'a> VideoInterface for Sdl2Video<'a> { .copy( &self.texture, None, - Some(Rect::new(0, 0, SCREEN_WIDTH * SCALE, SCREEN_HEIGHT * SCALE)), + Some(Rect::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)), ) .unwrap(); self.canvas.present();