close windows

This commit is contained in:
Alex Janka 2023-07-17 12:26:33 +10:00
parent ed6ad62e22
commit 2685bc06e6
5 changed files with 23 additions and 23 deletions

View file

@ -1,9 +1,8 @@
use gb_emu_lib::connect::{EmulatorCoreTrait, EmulatorMessage}; use gb_emu_lib::connect::EmulatorCoreTrait;
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::{self, Write}, io::{self, Write},
str::FromStr, str::FromStr,
sync::mpsc::Receiver,
}; };
pub enum CommandErr { pub enum CommandErr {
@ -49,7 +48,6 @@ impl FromStr for Commands {
pub struct Debugger { pub struct Debugger {
core: Box<dyn EmulatorCoreTrait>, core: Box<dyn EmulatorCoreTrait>,
debug_receiver: Receiver<EmulatorMessage>,
stepping: bool, stepping: bool,
last_command: String, last_command: String,
watches: HashMap<u16, u8>, watches: HashMap<u16, u8>,
@ -57,13 +55,9 @@ pub struct Debugger {
} }
impl Debugger { impl Debugger {
pub fn new( pub fn new(core: Box<dyn EmulatorCoreTrait>) -> Self {
core: Box<dyn EmulatorCoreTrait>,
debug_receiver: Receiver<EmulatorMessage>,
) -> Self {
Self { Self {
core, core,
debug_receiver,
stepping: true, stepping: true,
last_command: String::from(""), last_command: String::from(""),
watches: HashMap::new(), watches: HashMap::new(),
@ -72,9 +66,7 @@ impl Debugger {
} }
pub fn step(&mut self) { pub fn step(&mut self) {
if let Ok(EmulatorMessage::Stop) = self.debug_receiver.try_recv() { self.core.process_messages();
self.core.run();
}
if self.should_pause() { if self.should_pause() {
println!("cycles: {}", self.core.cycle_count()); println!("cycles: {}", self.core.cycle_count());
println!(); println!();

View file

@ -107,18 +107,19 @@ impl EmulatorHandler {
}; };
let (sender, receiver) = channel::<EmulatorMessage>(); let (sender, receiver) = channel::<EmulatorMessage>();
let (debug_sender, debug_receiver) = channel::<EmulatorMessage>();
ctrlc::set_handler(move || { {
sender.send(EmulatorMessage::Stop).unwrap(); let sender = sender.clone();
debug_sender.send(EmulatorMessage::Stop).unwrap() ctrlc::set_handler(move || {
}) sender.send(EmulatorMessage::Stop).unwrap();
.unwrap(); })
.unwrap();
}
let (output, _stream) = audio::create_output(args.mute); let (output, _stream) = audio::create_output(args.mute);
let rom = RomFile::Path(args.rom); 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 window = window_manager.add(factor, Some(Gilrs::new().unwrap()));
let tile_window: Option<WindowRenderer> = if args.tile_window { let tile_window: Option<WindowRenderer> = if args.tile_window {
@ -163,7 +164,7 @@ impl EmulatorHandler {
let core = Box::new(EmulatorCore::init(receiver, options, Webcam::new())); let core = Box::new(EmulatorCore::init(receiver, options, Webcam::new()));
let emu = if args.debug { let emu = if args.debug {
EmulatorTypes::Debug(Debugger::new(core, debug_receiver)) EmulatorTypes::Debug(Debugger::new(core))
} else { } else {
EmulatorTypes::Normal(core) EmulatorTypes::Normal(core)
}; };

View file

@ -1,10 +1,10 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::{Arc, Mutex}, sync::{mpsc::Sender, Arc, Mutex},
}; };
use gb_emu_lib::{ use gb_emu_lib::{
connect::{JoypadState, Renderer}, connect::{EmulatorMessage, JoypadState, Renderer},
util::scale_buffer_in_place, util::scale_buffer_in_place,
}; };
use gilrs::{ use gilrs::{
@ -35,14 +35,16 @@ pub struct WindowManager {
event_loop: EventLoop<()>, event_loop: EventLoop<()>,
windows: HashMap<WindowId, Arc<WindowData>>, windows: HashMap<WindowId, Arc<WindowData>>,
input: Arc<Mutex<WinitInputHelper>>, input: Arc<Mutex<WinitInputHelper>>,
sender: Sender<EmulatorMessage>,
} }
impl WindowManager { impl WindowManager {
pub(crate) fn new() -> Self { pub(crate) fn new(sender: Sender<EmulatorMessage>) -> Self {
Self { Self {
event_loop: EventLoop::new(), event_loop: EventLoop::new(),
windows: HashMap::new(), windows: HashMap::new(),
input: Arc::new(Mutex::new(WinitInputHelper::new())), input: Arc::new(Mutex::new(WinitInputHelper::new())),
sender,
} }
} }
@ -69,7 +71,7 @@ impl WindowManager {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
window_id: _, window_id: _,
} => { } => {
// quit = true; self.sender.send(EmulatorMessage::Stop).unwrap();
} }
Event::MainEventsCleared => { Event::MainEventsCleared => {
control_flow.set_exit(); control_flow.set_exit();

View file

@ -253,4 +253,5 @@ pub trait EmulatorCoreTrait {
fn run(&mut self); fn run(&mut self);
fn run_stepped(&mut self, step_size: usize); fn run_stepped(&mut self, step_size: usize);
fn run_until_buffer_full(&mut self); fn run_until_buffer_full(&mut self);
fn process_messages(&mut self);
} }

View file

@ -246,4 +246,8 @@ where
self.run(); self.run();
} }
} }
fn process_messages(&mut self) {
self.process_messages();
}
} }