From 8edf466271908067cb6f3ce1cba58f2458449394 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Fri, 17 Mar 2023 13:58:47 +1100 Subject: [PATCH] refactor options --- gb-emu/src/main.rs | 34 +++++++++--------- gb-vst/src/lib.rs | 13 ++++--- lib/src/connect/mod.rs | 80 +++++++++++++++++++++++++++++++++++++++++ lib/src/lib.rs | 82 +++++------------------------------------- 4 files changed, 114 insertions(+), 95 deletions(-) diff --git a/gb-emu/src/main.rs b/gb-emu/src/main.rs index 0bea01a..ad67865 100644 --- a/gb-emu/src/main.rs +++ b/gb-emu/src/main.rs @@ -10,7 +10,8 @@ use cpal::{ use futures::executor; use gb_emu_lib::{ connect::{ - AudioOutput, DownsampleType, EmulatorMessage, JoypadState, Renderer, RomFile, SerialTarget, + AudioOutput, DownsampleType, EmulatorMessage, EmulatorOptions, JoypadState, Renderer, + RomFile, SerialTarget, }, util::scale_buffer, EmulatorCore, @@ -77,17 +78,6 @@ fn main() { 3 }; - let options = gb_emu_lib::Options::new(RomFile::Path(args.rom)) - .with_save_path(args.save) - .with_serial_target(if args.connect_serial { - SerialTarget::Stdout - } else { - SerialTarget::None - }) - .with_bootrom(args.bootrom.map(RomFile::Path)) - .with_no_save(args.no_save) - .with_verbose(args.verbose); - let tile_window: Option = if args.tile_window { Some(WindowRenderer::new(factor, None)) } else { @@ -100,13 +90,23 @@ fn main() { let (output, _stream) = create_audio_output(); - let mut core = EmulatorCore::init( - receiver, - options, + let options = EmulatorOptions::new( WindowRenderer::new(factor, Some(Gilrs::new().unwrap())), + RomFile::Path(args.rom), output, - tile_window, - ); + ) + .with_save_path(args.save) + .with_serial_target(if args.connect_serial { + SerialTarget::Stdout + } else { + SerialTarget::None + }) + .with_bootrom(args.bootrom.map(RomFile::Path)) + .with_no_save(args.no_save) + .with_verbose(args.verbose) + .with_tile_window(tile_window); + + let mut core = EmulatorCore::init(receiver, options); match args.step_by { Some(step_size) => loop { diff --git a/gb-vst/src/lib.rs b/gb-vst/src/lib.rs index 541a146..73acde5 100644 --- a/gb-vst/src/lib.rs +++ b/gb-vst/src/lib.rs @@ -1,7 +1,10 @@ use async_ringbuf::AsyncHeapConsumer; use futures::executor; use gb_emu_lib::{ - connect::{AudioOutput, DownsampleType, EmulatorMessage, JoypadButtons, RomFile, SerialTarget}, + connect::{ + AudioOutput, DownsampleType, EmulatorMessage, EmulatorOptions, JoypadButtons, RomFile, + SerialTarget, + }, EmulatorCore, }; use nih_plug::midi::MidiResult::Basic; @@ -253,21 +256,21 @@ impl Plugin for GameboyEmu { { EmulatorCore::from_save_state(state, rom, receiver, window, output, serial_target) } else { - let options = gb_emu_lib::Options::new(rom) + let options = gb_emu_lib::Options::new(window, rom, output) .with_bootrom(bootrom) .with_serial_target(serial_target) .force_no_save(); - EmulatorCore::init(receiver, options, window, output, None) + EmulatorCore::init(receiver, options) }; #[cfg(not(feature = "savestate"))] let mut emulator_core = { - let options = gb_emu_lib::Options::new(rom) + let options = EmulatorOptions::new(window, rom, output) .with_bootrom(bootrom) .with_serial_target(serial_target) .force_no_save(); - EmulatorCore::init(receiver, options, window, output, None) + EmulatorCore::init(receiver, options) }; emulator_core.run_until_buffer_full(); diff --git a/lib/src/connect/mod.rs b/lib/src/connect/mod.rs index c6c5433..ec237ac 100644 --- a/lib/src/connect/mod.rs +++ b/lib/src/connect/mod.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use crate::processor::memory::mmio::gpu::Colour; pub use crate::processor::memory::mmio::joypad::{JoypadButtons, JoypadState}; pub use crate::processor::memory::mmio::serial::SerialTarget; @@ -62,3 +64,81 @@ impl AudioOutput { ) } } + +#[non_exhaustive] +pub struct EmulatorOptions + Clone, R: Renderer> { + pub(crate) window: R, + pub(crate) tile_window: Option, + pub(crate) rom: RomFile, + pub(crate) output: AudioOutput, + pub(crate) save_path: Option, + pub(crate) no_save: bool, + pub(crate) bootrom: Option, + pub(crate) serial_target: SerialTarget, + pub(crate) verbose: bool, + pub(crate) spooky: PhantomData, +} + +impl + Clone, R: Renderer> + EmulatorOptions +{ + pub fn new(window: R, rom: RomFile, output: AudioOutput) -> Self { + Self { + window, + tile_window: None, + rom, + output, + save_path: None, + no_save: false, + bootrom: None, + serial_target: SerialTarget::None, + verbose: false, + spooky: PhantomData, + } + } + + pub fn with_save_path(mut self, path: Option) -> Self { + self.save_path = path; + self + } + + pub fn force_no_save(mut self) -> Self { + self.no_save = true; + self + } + + pub fn with_no_save(mut self, no_save: bool) -> Self { + self.no_save = no_save; + self + } + + pub fn with_verbose(mut self, verbose: bool) -> Self { + self.verbose = verbose; + self + } + + pub fn with_bootrom(mut self, bootrom: Option) -> Self { + self.bootrom = bootrom; + self + } + + pub fn with_stdout(mut self) -> Self { + self.serial_target = SerialTarget::Stdout; + self + } + + pub fn with_serial_target(mut self, target: SerialTarget) -> Self { + self.serial_target = target; + self + } + + pub fn with_tile_window(mut self, window: Option) -> Self { + self.tile_window = window; + self + } + + pub fn verbose(mut self) -> Self { + self.verbose = true; + self + } +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 4028ca6..22367b8 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -1,7 +1,7 @@ #![feature(exclusive_range_pattern, let_chains, bigint_helper_methods)] use crate::{processor::memory::Memory, util::pause}; -use connect::{AudioOutput, EmulatorMessage, Renderer, RomFile, SerialTarget}; +use connect::{AudioOutput, EmulatorMessage, EmulatorOptions, Renderer, RomFile, SerialTarget}; use once_cell::sync::OnceCell; use processor::{ memory::{mmio::gpu::Colour, Rom}, @@ -22,69 +22,6 @@ mod constants; mod processor; pub mod util; -#[non_exhaustive] -pub struct Options { - pub rom: RomFile, - pub save_path: Option, - pub no_save: bool, - pub bootrom: Option, - pub serial_target: SerialTarget, - pub verbose: bool, -} - -impl Options { - pub fn new(rom: RomFile) -> Self { - Self { - rom, - save_path: None, - no_save: false, - bootrom: None, - serial_target: SerialTarget::None, - verbose: false, - } - } - - pub fn with_save_path(mut self, path: Option) -> Self { - self.save_path = path; - self - } - - pub fn force_no_save(mut self) -> Self { - self.no_save = true; - self - } - - pub fn with_no_save(mut self, no_save: bool) -> Self { - self.no_save = no_save; - self - } - - pub fn with_verbose(mut self, verbose: bool) -> Self { - self.verbose = verbose; - self - } - - pub fn with_bootrom(mut self, bootrom: Option) -> Self { - self.bootrom = bootrom; - self - } - - pub fn with_stdout(mut self) -> Self { - self.serial_target = SerialTarget::Stdout; - self - } - - pub fn with_serial_target(mut self, target: SerialTarget) -> Self { - self.serial_target = target; - self - } - - pub fn verbose(mut self) -> Self { - self.verbose = true; - self - } -} - static mut PAUSE_ENABLED: bool = false; static mut PAUSE_QUEUED: bool = false; @@ -101,10 +38,7 @@ pub struct EmulatorCore + Clone, R: Renderer + Clone, R: Renderer> EmulatorCore { pub fn init( receiver: Receiver, - options: Options, - mut window: R, - output: AudioOutput, - tile_window: Option, + mut options: EmulatorOptions, ) -> Self { if options.verbose { VERBOSE.set(true).unwrap(); @@ -133,8 +67,10 @@ impl + Clone, R: Renderer> EmulatorCore RomFile::Raw(data) => Rom::load(data, None), }; - window.prepare(WIDTH, HEIGHT); - window.set_title(format!("{} on {}", rom.get_title(), rom.mbc_type())); + options.window.prepare(WIDTH, HEIGHT); + options + .window + .set_title(format!("{} on {}", rom.get_title(), rom.mbc_type())); let bootrom_enabled = options.bootrom.is_some(); let bootrom: Option> = options.bootrom.map(|v| match v { @@ -154,10 +90,10 @@ impl + Clone, R: Renderer> EmulatorCore Memory::init( bootrom, rom, - window, - output, + options.window, + options.output, options.serial_target, - tile_window, + options.tile_window, ), bootrom_enabled, ),