diff --git a/gb-emu/src/main.rs b/gb-emu/src/main.rs index c48290f..759381a 100644 --- a/gb-emu/src/main.rs +++ b/gb-emu/src/main.rs @@ -167,7 +167,7 @@ fn run(args: Args) -> ! { let mut window_manager = WindowManager::new(sender, stream); - let window = window_manager.add( + let window = window_manager.add_main( scale_override, Some(Gilrs::new().unwrap()), shader_path, diff --git a/gb-emu/src/window.rs b/gb-emu/src/window.rs index 0b105ec..d042f07 100644 --- a/gb-emu/src/window.rs +++ b/gb-emu/src/window.rs @@ -45,6 +45,7 @@ pub struct WindowManager { } struct WindowManagerData { + main_window: Option, windows: HashMap>, window_data_manager: Arc, input: Arc>, @@ -63,6 +64,7 @@ impl WindowManager { Self { event_loop, data: WindowManagerData { + main_window: None, windows: HashMap::new(), window_data_manager, input: Arc::new(Mutex::new(WinitInputHelper::new())), @@ -72,6 +74,23 @@ impl WindowManager { } } + pub(crate) fn add_main( + &mut self, + factor: usize, + gamepad_handler: Option, + shader_path: Option, + resizable: bool, + ) -> WindowRenderer { + self.data.add( + factor, + gamepad_handler, + shader_path, + resizable, + &self.event_loop, + true, + ) + } + pub(crate) fn add( &mut self, factor: usize, @@ -85,6 +104,7 @@ impl WindowManager { shader_path, resizable, &self.event_loop, + false, ) } @@ -109,6 +129,7 @@ impl WindowManagerData { shader_path: Option, resizable: bool, event_loop: &EventLoop<()>, + is_main: bool, ) -> WindowRenderer { let (r, info) = WindowRenderer::new( factor, @@ -120,6 +141,9 @@ impl WindowManagerData { resizable, ); self.windows.insert(info.id, info.data); + if is_main { + self.main_window = Some(info.id); + } r } @@ -134,7 +158,11 @@ impl WindowManagerData { if let Ok(mut i) = self.input.lock() { if i.update(&event) && i.key_pressed(VirtualKeyCode::Space) { - self.windows.iter().for_each(|(_id, window)| { + if let Some(window) = self + .main_window + .as_ref() + .and_then(|id| self.windows.get(id)) + { if let Ok(buf) = window.last_buf.lock() { if let Ok(size) = window.rendered_size.read() { let image = ImageBuffer::, _>::from_raw( @@ -169,7 +197,7 @@ impl WindowManagerData { .expect("Could not save screenshot!"); } } - }); + } } } @@ -179,9 +207,17 @@ impl WindowManagerData { } Event::WindowEvent { event: WindowEvent::CloseRequested, - window_id: _, + window_id, } => { - self.sender.send(EmulatorMessage::Exit).unwrap(); + if self.main_window.is_some_and(|v| v == window_id) { + self.sender.send(EmulatorMessage::Exit).unwrap(); + } else if let Some(window) = self + .main_window + .as_ref() + .and_then(|id| self.windows.get(id)) + { + window.window.focus_window(); + } } Event::MainEventsCleared => { if run_return {