rewrite it in rust #1

Merged
SonicSwordcane merged 20 commits from riir into main 2024-11-10 23:34:55 +00:00
2 changed files with 21 additions and 9 deletions
Showing only changes of commit a6200732ee - Show all commits

View File

@ -357,14 +357,15 @@ impl ApplicationHandler for App {
} }
}); });
ui.menu("Emulation", || { ui.menu("Emulation", || {
let has_game = self.client.has_game();
if self.client.is_running() { 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); 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); 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); self.client.send_command(EmulatorCommand::Reset);
} }
}); });

View File

@ -20,6 +20,7 @@ pub struct EmulatorBuilder {
rom: Option<PathBuf>, rom: Option<PathBuf>,
commands: mpsc::Receiver<EmulatorCommand>, commands: mpsc::Receiver<EmulatorCommand>,
running: Arc<AtomicBool>, running: Arc<AtomicBool>,
has_game: Arc<AtomicBool>,
} }
impl EmulatorBuilder { impl EmulatorBuilder {
@ -29,10 +30,12 @@ impl EmulatorBuilder {
rom: None, rom: None,
commands, commands,
running: Arc::new(AtomicBool::new(false)), running: Arc::new(AtomicBool::new(false)),
has_game: Arc::new(AtomicBool::new(false)),
}; };
let client = EmulatorClient { let client = EmulatorClient {
queue, queue,
running: builder.running.clone(), running: builder.running.clone(),
has_game: builder.has_game.clone(),
}; };
(builder, client) (builder, client)
} }
@ -45,7 +48,7 @@ impl EmulatorBuilder {
} }
pub fn build(self) -> Result<Emulator> { 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 { if let Some(path) = self.rom {
emulator.load_rom(&path)?; emulator.load_rom(&path)?;
} }
@ -59,18 +62,22 @@ pub struct Emulator {
commands: mpsc::Receiver<EmulatorCommand>, commands: mpsc::Receiver<EmulatorCommand>,
renderer: Option<GameRenderer>, renderer: Option<GameRenderer>,
running: Arc<AtomicBool>, running: Arc<AtomicBool>,
has_game: bool, has_game: Arc<AtomicBool>,
} }
impl Emulator { 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 { Ok(Self {
sim: CoreVB::new(), sim: CoreVB::new(),
audio: Audio::init()?, audio: Audio::init()?,
commands, commands,
renderer: None, renderer: None,
running, running,
has_game: false, has_game,
}) })
} }
@ -78,7 +85,7 @@ impl Emulator {
let bytes = fs::read(path)?; let bytes = fs::read(path)?;
self.sim.reset(); self.sim.reset();
self.sim.load_rom(bytes)?; self.sim.load_rom(bytes)?;
self.has_game = true; self.has_game.store(true, Ordering::Release);
self.running.store(true, Ordering::Release); self.running.store(true, Ordering::Release);
Ok(()) Ok(())
} }
@ -128,7 +135,7 @@ impl Emulator {
self.running.store(false, Ordering::Release); self.running.store(false, Ordering::Release);
} }
EmulatorCommand::Resume => { EmulatorCommand::Resume => {
if self.has_game { if self.has_game.load(Ordering::Acquire) {
self.running.store(true, Ordering::Relaxed); self.running.store(true, Ordering::Relaxed);
} }
} }
@ -156,12 +163,16 @@ pub enum EmulatorCommand {
pub struct EmulatorClient { pub struct EmulatorClient {
queue: mpsc::Sender<EmulatorCommand>, queue: mpsc::Sender<EmulatorCommand>,
running: Arc<AtomicBool>, running: Arc<AtomicBool>,
has_game: Arc<AtomicBool>,
} }
impl EmulatorClient { impl EmulatorClient {
pub fn is_running(&self) -> bool { pub fn is_running(&self) -> bool {
self.running.load(Ordering::Acquire) self.running.load(Ordering::Acquire)
} }
pub fn has_game(&self) -> bool {
self.has_game.load(Ordering::Acquire)
}
pub fn send_command(&self, command: EmulatorCommand) { pub fn send_command(&self, command: EmulatorCommand) {
if let Err(err) = self.queue.send(command) { if let Err(err) = self.queue.send(command) {
eprintln!( eprintln!(