diff --git a/gb-emu/src/debug.rs b/gb-emu/src/debug.rs index 236d152..9e2c0ea 100644 --- a/gb-emu/src/debug.rs +++ b/gb-emu/src/debug.rs @@ -1,9 +1,8 @@ -use gb_emu_lib::connect::{EmulatorCoreTrait, EmulatorMessage}; +use gb_emu_lib::connect::EmulatorCoreTrait; use std::{ collections::HashMap, io::{self, Write}, str::FromStr, - sync::mpsc::Receiver, }; pub enum CommandErr { @@ -49,7 +48,6 @@ impl FromStr for Commands { pub struct Debugger { core: Box, - debug_receiver: Receiver, stepping: bool, last_command: String, watches: HashMap, @@ -57,13 +55,9 @@ pub struct Debugger { } impl Debugger { - pub fn new( - core: Box, - debug_receiver: Receiver, - ) -> Self { + pub fn new(core: Box) -> Self { Self { core, - debug_receiver, stepping: true, last_command: String::from(""), watches: HashMap::new(), @@ -72,9 +66,7 @@ impl Debugger { } pub fn step(&mut self) { - if let Ok(EmulatorMessage::Stop) = self.debug_receiver.try_recv() { - self.core.run(); - } + self.core.process_messages(); if self.should_pause() { println!("cycles: {}", self.core.cycle_count()); println!(); diff --git a/gb-emu/src/main.rs b/gb-emu/src/main.rs index dbf26ee..2e36c6c 100644 --- a/gb-emu/src/main.rs +++ b/gb-emu/src/main.rs @@ -107,18 +107,19 @@ impl EmulatorHandler { }; let (sender, receiver) = channel::(); - let (debug_sender, debug_receiver) = channel::(); - ctrlc::set_handler(move || { - sender.send(EmulatorMessage::Stop).unwrap(); - debug_sender.send(EmulatorMessage::Stop).unwrap() - }) - .unwrap(); + { + let sender = sender.clone(); + ctrlc::set_handler(move || { + sender.send(EmulatorMessage::Stop).unwrap(); + }) + .unwrap(); + } let (output, _stream) = audio::create_output(args.mute); let rom = RomFile::Path(args.rom); - let mut window_manager = WindowManager::new(); + let mut window_manager = WindowManager::new(sender); let window = window_manager.add(factor, Some(Gilrs::new().unwrap())); let tile_window: Option = if args.tile_window { @@ -163,7 +164,7 @@ impl EmulatorHandler { let core = Box::new(EmulatorCore::init(receiver, options, Webcam::new())); let emu = if args.debug { - EmulatorTypes::Debug(Debugger::new(core, debug_receiver)) + EmulatorTypes::Debug(Debugger::new(core)) } else { EmulatorTypes::Normal(core) }; diff --git a/gb-emu/src/window.rs b/gb-emu/src/window.rs index 738055f..20318a0 100644 --- a/gb-emu/src/window.rs +++ b/gb-emu/src/window.rs @@ -1,10 +1,10 @@ use std::{ collections::HashMap, - sync::{Arc, Mutex}, + sync::{mpsc::Sender, Arc, Mutex}, }; use gb_emu_lib::{ - connect::{JoypadState, Renderer}, + connect::{EmulatorMessage, JoypadState, Renderer}, util::scale_buffer_in_place, }; use gilrs::{ @@ -35,14 +35,16 @@ pub struct WindowManager { event_loop: EventLoop<()>, windows: HashMap>, input: Arc>, + sender: Sender, } impl WindowManager { - pub(crate) fn new() -> Self { + pub(crate) fn new(sender: Sender) -> Self { Self { event_loop: EventLoop::new(), windows: HashMap::new(), input: Arc::new(Mutex::new(WinitInputHelper::new())), + sender, } } @@ -69,7 +71,7 @@ impl WindowManager { event: WindowEvent::CloseRequested, window_id: _, } => { - // quit = true; + self.sender.send(EmulatorMessage::Stop).unwrap(); } Event::MainEventsCleared => { control_flow.set_exit(); diff --git a/lib/src/connect/mod.rs b/lib/src/connect/mod.rs index 2bc3a9f..acd265e 100644 --- a/lib/src/connect/mod.rs +++ b/lib/src/connect/mod.rs @@ -253,4 +253,5 @@ pub trait EmulatorCoreTrait { fn run(&mut self); fn run_stepped(&mut self, step_size: usize); fn run_until_buffer_full(&mut self); + fn process_messages(&mut self); } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 0316c8f..361bdb5 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -246,4 +246,8 @@ where self.run(); } } + + fn process_messages(&mut self) { + self.process_messages(); + } }