From a6200732ee65d67f89e33d280d4aafee6291a0eb Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Wed, 6 Nov 2024 23:36:33 -0500 Subject: [PATCH] Disable emulation options when game is not running --- src/app.rs | 7 ++++--- src/emulator.rs | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index 5aadce6..96d0a1d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -357,14 +357,15 @@ impl ApplicationHandler for App { } }); ui.menu("Emulation", || { + let has_game = self.client.has_game(); if self.client.is_running() { - if ui.menu_item("Pause") { + if ui.menu_item_config("Pause").enabled(has_game).build() { self.client.send_command(EmulatorCommand::Pause); } - } else if ui.menu_item("Resume") { + } else if ui.menu_item_config("Resume").enabled(has_game).build() { self.client.send_command(EmulatorCommand::Resume); } - if ui.menu_item("Reset") { + if ui.menu_item_config("Reset").enabled(has_game).build() { self.client.send_command(EmulatorCommand::Reset); } }); diff --git a/src/emulator.rs b/src/emulator.rs index f93d310..8682209 100644 --- a/src/emulator.rs +++ b/src/emulator.rs @@ -20,6 +20,7 @@ pub struct EmulatorBuilder { rom: Option, commands: mpsc::Receiver, running: Arc, + has_game: Arc, } impl EmulatorBuilder { @@ -29,10 +30,12 @@ impl EmulatorBuilder { rom: None, commands, running: Arc::new(AtomicBool::new(false)), + has_game: Arc::new(AtomicBool::new(false)), }; let client = EmulatorClient { queue, running: builder.running.clone(), + has_game: builder.has_game.clone(), }; (builder, client) } @@ -45,7 +48,7 @@ impl EmulatorBuilder { } pub fn build(self) -> Result { - let mut emulator = Emulator::new(self.commands, self.running)?; + let mut emulator = Emulator::new(self.commands, self.running, self.has_game)?; if let Some(path) = self.rom { emulator.load_rom(&path)?; } @@ -59,18 +62,22 @@ pub struct Emulator { commands: mpsc::Receiver, renderer: Option, running: Arc, - has_game: bool, + has_game: Arc, } impl Emulator { - fn new(commands: mpsc::Receiver, running: Arc) -> Result { + fn new( + commands: mpsc::Receiver, + running: Arc, + has_game: Arc, + ) -> Result { Ok(Self { sim: CoreVB::new(), audio: Audio::init()?, commands, renderer: None, running, - has_game: false, + has_game, }) } @@ -78,7 +85,7 @@ impl Emulator { let bytes = fs::read(path)?; self.sim.reset(); self.sim.load_rom(bytes)?; - self.has_game = true; + self.has_game.store(true, Ordering::Release); self.running.store(true, Ordering::Release); Ok(()) } @@ -128,7 +135,7 @@ impl Emulator { self.running.store(false, Ordering::Release); } EmulatorCommand::Resume => { - if self.has_game { + if self.has_game.load(Ordering::Acquire) { self.running.store(true, Ordering::Relaxed); } } @@ -156,12 +163,16 @@ pub enum EmulatorCommand { pub struct EmulatorClient { queue: mpsc::Sender, running: Arc, + has_game: Arc, } impl EmulatorClient { pub fn is_running(&self) -> bool { self.running.load(Ordering::Acquire) } + pub fn has_game(&self) -> bool { + self.has_game.load(Ordering::Acquire) + } pub fn send_command(&self, command: EmulatorCommand) { if let Err(err) = self.queue.send(command) { eprintln!(