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
This commit is contained in:
Tibor Nagy 2020-05-12 06:32:59 +02:00 committed by MishMish
parent 8879d40262
commit 0ae9ef14c3

View file

@ -1,5 +1,5 @@
use sdl2; use sdl2;
use sdl2::event::Event; use sdl2::event::{Event, WindowEvent};
use sdl2::image::{InitFlag, LoadTexture}; use sdl2::image::{InitFlag, LoadTexture};
use sdl2::keyboard::Scancode; use sdl2::keyboard::Scancode;
use sdl2::messagebox::*; 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") .load_texture("assets/icon_cropped_small.png")
.expect("failed to load icon"); .expect("failed to load icon");
let background = Color::RGB(0xDD, 0xDD, 0xDD); let background = Color::RGB(0xDD, 0xDD, 0xDD);
loop {
for event in event_pump.poll_iter() { let mut redraw = || -> Result<(), String> {
match event {
Event::DropFile { filename, .. } => {
return Ok(filename);
}
Event::Quit { .. } => process::exit(0),
_ => {}
}
}
canvas.set_draw_color(background); canvas.set_draw_color(background);
canvas.clear(); canvas.clear();
canvas.copy( canvas.copy(
@ -85,6 +77,25 @@ fn wait_for_rom(canvas: &mut WindowCanvas, event_pump: &mut EventPump) -> Result
)), )),
)?; )?;
canvas.present(); 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()?,
_ => {}
},
_ => {}
}
}
} }
} }