feat(sdl2): Support window resizing

Former-commit-id: f5e70c8a15b53eb7aded9f630e5c1fb1dce55fba
This commit is contained in:
Michel Heily 2020-01-21 01:11:50 +02:00
parent 6dbc3d5cf0
commit ade03121ee
2 changed files with 12 additions and 26 deletions

View file

@ -26,7 +26,7 @@ mod video;
use audio::create_audio_player; use audio::create_audio_player;
use input::create_input; 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; extern crate rustboyadvance_ng;
use rustboyadvance_ng::prelude::*; use rustboyadvance_ng::prelude::*;
@ -61,21 +61,19 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let debug = matches.occurrences_of("debug") != 0; let debug = matches.occurrences_of("debug") != 0;
let sdl_context = sdl2::init().expect("failed to initialize sdl2"); 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 video_subsystem = sdl_context.video()?;
let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG).unwrap(); let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?;
let window = video_subsystem let window = video_subsystem
.window( .window("RustBoyAdvance", SCREEN_WIDTH * 3, SCREEN_HEIGHT * 3)
"RustBoyAdvance",
SCREEN_WIDTH * SCALE,
SCREEN_HEIGHT * SCALE,
)
.opengl() .opengl()
.position_centered() .position_centered()
.build() .resizable()
.unwrap(); .build()?;
let mut canvas = window.into_canvas().accelerated().build().unwrap(); 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 // Display the icon as a placeholder
canvas.set_draw_color(Color::RGB(0x40, 0x22, 0x20)); // default background color for the icon canvas.set_draw_color(Color::RGB(0x40, 0x22, 0x20)); // default background color for the icon
@ -84,18 +82,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let icon_texture = texture_creator let icon_texture = texture_creator
.load_texture("assets/icon.png") .load_texture("assets/icon.png")
.expect("failed to load icon"); .expect("failed to load icon");
canvas canvas.copy(&icon_texture, None, None).unwrap();
.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.present(); canvas.present();
// TODO also set window icon // TODO also set window icon

View file

@ -8,7 +8,6 @@ use rustboyadvance_ng::VideoInterface;
pub const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32; pub const SCREEN_WIDTH: u32 = DISPLAY_WIDTH as u32;
pub const SCREEN_HEIGHT: u32 = DISPLAY_HEIGHT 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> { pub struct Sdl2Video<'a> {
_tc: TextureCreator<WindowContext>, // only kept alive because of the texture _tc: TextureCreator<WindowContext>, // only kept alive because of the texture
@ -35,7 +34,7 @@ impl<'a> VideoInterface for Sdl2Video<'a> {
.copy( .copy(
&self.texture, &self.texture,
None, None,
Some(Rect::new(0, 0, SCREEN_WIDTH * SCALE, SCREEN_HEIGHT * SCALE)), Some(Rect::new(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)),
) )
.unwrap(); .unwrap();
self.canvas.present(); self.canvas.present();