Disable emulation options when game is not running
This commit is contained in:
+17
-6
@@ -20,6 +20,7 @@ pub struct EmulatorBuilder {
|
||||
rom: Option<PathBuf>,
|
||||
commands: mpsc::Receiver<EmulatorCommand>,
|
||||
running: Arc<AtomicBool>,
|
||||
has_game: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
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<Emulator> {
|
||||
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<EmulatorCommand>,
|
||||
renderer: Option<GameRenderer>,
|
||||
running: Arc<AtomicBool>,
|
||||
has_game: bool,
|
||||
has_game: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Emulator {
|
||||
fn new(commands: mpsc::Receiver<EmulatorCommand>, running: Arc<AtomicBool>) -> Result<Self> {
|
||||
fn new(
|
||||
commands: mpsc::Receiver<EmulatorCommand>,
|
||||
running: Arc<AtomicBool>,
|
||||
has_game: Arc<AtomicBool>,
|
||||
) -> Result<Self> {
|
||||
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<EmulatorCommand>,
|
||||
running: Arc<AtomicBool>,
|
||||
has_game: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
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!(
|
||||
|
||||
Reference in New Issue
Block a user