From 0ae9ef14c387c05b02cf6b333e399b23f441f79e Mon Sep 17 00:00:00 2001 From: Tibor Nagy Date: Tue, 12 May 2020 06:32:59 +0200 Subject: [PATCH] Fix busy waiting in wait_for_rom() by using a blocking event iterator Also move the redraw code into a closure to be able to call it from two places. By the result of this waiting for ROMs no longer consumes 100% of a CPU core. Former-commit-id: bc86ade197221eb8d357af91a3b59f4eb165adc5 Former-commit-id: 521f361872964d77b88097d7baa6a4b6b713224e --- platform/rustboyadvance-sdl2/src/main.rs | 33 ++++++++++++++++-------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/platform/rustboyadvance-sdl2/src/main.rs b/platform/rustboyadvance-sdl2/src/main.rs index 6e8ada0..0171fe0 100644 --- a/platform/rustboyadvance-sdl2/src/main.rs +++ b/platform/rustboyadvance-sdl2/src/main.rs @@ -1,5 +1,5 @@ use sdl2; -use sdl2::event::Event; +use sdl2::event::{Event, WindowEvent}; use sdl2::image::{InitFlag, LoadTexture}; use sdl2::keyboard::Scancode; use sdl2::messagebox::*; @@ -63,16 +63,8 @@ fn wait_for_rom(canvas: &mut WindowCanvas, event_pump: &mut EventPump) -> Result .load_texture("assets/icon_cropped_small.png") .expect("failed to load icon"); let background = Color::RGB(0xDD, 0xDD, 0xDD); - loop { - for event in event_pump.poll_iter() { - match event { - Event::DropFile { filename, .. } => { - return Ok(filename); - } - Event::Quit { .. } => process::exit(0), - _ => {} - } - } + + let mut redraw = || -> Result<(), String> { canvas.set_draw_color(background); canvas.clear(); canvas.copy( @@ -85,6 +77,25 @@ fn wait_for_rom(canvas: &mut WindowCanvas, event_pump: &mut EventPump) -> Result )), )?; canvas.present(); + Ok(()) + }; + + redraw()?; + + loop { + for event in event_pump.wait_iter() { + match event { + Event::DropFile { filename, .. } => { + return Ok(filename); + } + Event::Quit { .. } => process::exit(0), + Event::Window { win_event, .. } => match win_event { + WindowEvent::SizeChanged(..) => redraw()?, + _ => {} + }, + _ => {} + } + } } }