dont fully reinitialise if u dont need to

This commit is contained in:
Alex Janka 2023-03-08 15:41:17 +11:00
parent 11dec11079
commit 0f919c612c
4 changed files with 34 additions and 13 deletions

View file

@ -112,24 +112,32 @@ impl Plugin for GameboyEmu {
verbose: false, verbose: false,
cycle_count: false, cycle_count: false,
}; };
let (sender, receiver) = channel::<EmulatorMessage>();
let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false); if let Some(ref mut vars) = self.vars {
let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false);
let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new(); vars.emulator_core.replace_output(output);
vars.rx = rx;
} else {
let (sender, receiver) = channel::<EmulatorMessage>();
*self.frame_receiver.lock().unwrap() = Some(frame_receiver); let (output, rx) = AudioOutput::new_unfilled(buffer_config.sample_rate, false);
*self.key_handler.lock().unwrap() = Some(key_handler);
let mut emulator_core = let (renderer, frame_receiver, key_handler) = EmulatorRenderer::new();
EmulatorCore::init(receiver, options, Box::new(renderer), output, None);
emulator_core.run_until_buffer_full();
self.vars = Some(EmuVars { *self.frame_receiver.lock().unwrap() = Some(frame_receiver);
rx, *self.key_handler.lock().unwrap() = Some(key_handler);
sender,
emulator_core, let mut emulator_core =
}); EmulatorCore::init(receiver, options, Box::new(renderer), output, None);
emulator_core.run_until_buffer_full();
self.vars = Some(EmuVars {
rx,
sender,
emulator_core,
});
}
true true
} }

View file

@ -124,6 +124,10 @@ impl<ColourFormat: From<Colour> + Clone> EmulatorCore<ColourFormat> {
} }
} }
pub fn replace_output(&mut self, new: AudioOutput) {
self.cpu.memory.replace_output(new);
}
pub fn run(&mut self) { pub fn run(&mut self) {
self.process_messages(); self.process_messages();
self.cycle_num += 1; self.cycle_num += 1;

View file

@ -226,6 +226,10 @@ impl<ColourFormat: From<Colour> + Clone> Memory<ColourFormat> {
pub fn is_audio_buffer_full(&self) -> bool { pub fn is_audio_buffer_full(&self) -> bool {
self.apu.is_buffer_full() self.apu.is_buffer_full()
} }
pub fn replace_output(&mut self, new: AudioOutput) {
self.apu.replace_output(new);
}
} }
impl<ColourFormat: From<Colour> + Clone> Cpu<ColourFormat> { impl<ColourFormat: From<Colour> + Clone> Cpu<ColourFormat> {

View file

@ -64,6 +64,11 @@ impl Apu {
} }
} }
pub fn replace_output(&mut self, new: AudioOutput) {
self.converter = Downsampler::new(new.sample_rate);
self.output = new;
}
pub fn div_apu_tick(&mut self) { pub fn div_apu_tick(&mut self) {
self.div_apu = self.div_apu.wrapping_add(1); self.div_apu = self.div_apu.wrapping_add(1);
if self.div_apu % 8 == 0 { if self.div_apu % 8 == 0 {