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", || {
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);
}
});

View File

@ -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!(