This commit is contained in:
Alex Janka 2023-10-06 16:53:57 +11:00
parent f8c14fc5d2
commit 16a3328c42
2 changed files with 37 additions and 22 deletions

View file

@ -7,10 +7,12 @@ use gb_emu_lib::{
AudioOutput, CgbRomType, DownsampleType, EmulatorCoreTrait, EmulatorMessage, AudioOutput, CgbRomType, DownsampleType, EmulatorCoreTrait, EmulatorMessage,
EmulatorOptions, JoypadButtons, NoCamera, RomFile, SerialTarget, EmulatorOptions, JoypadButtons, NoCamera, RomFile, SerialTarget,
}, },
renderer::RendererBackendManager,
EmulatorCore, HEIGHT, WIDTH, EmulatorCore, HEIGHT, WIDTH,
}; };
use nih_plug::prelude::*; use nih_plug::prelude::*;
use nih_plug::{midi::MidiResult::Basic, params::persist::PersistentField}; use nih_plug::{midi::MidiResult::Basic, params::persist::PersistentField};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
path::PathBuf, path::PathBuf,
@ -107,6 +109,7 @@ pub struct GameboyEmu {
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
key_handler: Arc<JoypadSender>, key_handler: Arc<JoypadSender>,
params: Arc<EmuParams>, params: Arc<EmuParams>,
renderer_manager: Arc<Mutex<Option<Arc<RendererBackendManager>>>>,
} }
type Frame = Vec<[u8; 4]>; type Frame = Vec<[u8; 4]>;
@ -247,7 +250,7 @@ impl Plugin for GameboyEmu {
ProcessStatus::KeepAlive ProcessStatus::KeepAlive
} }
fn editor(&mut self, _: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> { fn editor(&mut self, _e: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
let configs = access_config(); let configs = access_config();
let size = Size::new( let size = Size::new(
@ -258,6 +261,7 @@ impl Plugin for GameboyEmu {
Some(Box::new(Emulator::new( Some(Box::new(Emulator::new(
self.frame_receiver.clone(), self.frame_receiver.clone(),
self.key_handler.clone(), self.key_handler.clone(),
self.renderer_manager.clone(),
size, size,
))) )))
} }

View file

@ -2,7 +2,7 @@ use std::{
path::PathBuf, path::PathBuf,
sync::{ sync::{
mpsc::{self, Receiver, Sender}, mpsc::{self, Receiver, Sender},
Arc, Arc, Mutex,
}, },
}; };
@ -19,6 +19,7 @@ use crate::{access_config, Frame, FrameReceiver, JoypadInfo, JoypadSender, IS_CG
pub struct Emulator { pub struct Emulator {
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
joypad_sender: Arc<JoypadSender>, joypad_sender: Arc<JoypadSender>,
manager: Arc<Mutex<Option<Arc<RendererBackendManager>>>>,
size: Size, size: Size,
} }
@ -26,11 +27,13 @@ impl Emulator {
pub fn new( pub fn new(
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
joypad_sender: Arc<JoypadSender>, joypad_sender: Arc<JoypadSender>,
manager: Arc<Mutex<Option<Arc<RendererBackendManager>>>>,
size: Size, size: Size,
) -> Self { ) -> Self {
Self { Self {
frame_receiver, frame_receiver,
joypad_sender, joypad_sender,
manager,
size, size,
} }
} }
@ -74,6 +77,8 @@ impl Editor for Emulator {
(HEIGHT * scale_factor) as f64, (HEIGHT * scale_factor) as f64,
); );
let m = self.manager.clone();
Window::open_parented( Window::open_parented(
&parent, &parent,
WindowOpenOptions { WindowOpenOptions {
@ -87,15 +92,18 @@ impl Editor for Emulator {
w, w,
fr_cloned, fr_cloned,
js_cloned, js_cloned,
m,
size, size,
#[cfg(feature = "vulkan")] #[cfg(feature = "vulkan")]
shader_path, shader_path,
) )
.0
}, },
); );
Box::new(Self::new( Box::new(Self::new(
self.frame_receiver.clone(), self.frame_receiver.clone(),
self.joypad_sender.clone(), self.joypad_sender.clone(),
self.manager.clone(),
size, size,
)) ))
} }
@ -128,9 +136,10 @@ impl EmulatorWindow {
window: &mut Window, window: &mut Window,
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
joypad_sender: Arc<JoypadSender>, joypad_sender: Arc<JoypadSender>,
manager: Arc<Mutex<Option<Arc<RendererBackendManager>>>>,
size: Size, size: Size,
#[cfg(feature = "vulkan")] shader_path: Option<PathBuf>, #[cfg(feature = "vulkan")] shader_path: Option<PathBuf>,
) -> Self { ) -> (Self, Arc<RendererBackendManager>) {
let current_resolution = ResolutionData { let current_resolution = ResolutionData {
real_width: size.width as u32, real_width: size.width as u32,
real_height: size.height as u32, real_height: size.height as u32,
@ -139,29 +148,32 @@ impl EmulatorWindow {
}; };
#[cfg(feature = "vulkan")] #[cfg(feature = "vulkan")]
let (manager, window_options) = { let window_options = WindowOptions { shader_path };
use raw_window_handle::HasRawDisplayHandle;
(
Arc::new(RendererBackendManager::new(window.raw_display_handle())),
WindowOptions { shader_path },
)
};
#[cfg(feature = "pixels")] #[cfg(feature = "pixels")]
let (manager, window_options) = let window_options = WindowOptions {};
{ (Arc::new(RendererBackendManager::new()), WindowOptions {}) };
let renderer = let mut guard = manager.lock().unwrap();
RendererBackend::new(current_resolution, window, window_options, manager.clone());
Self { let m = guard.get_or_insert_with(|| {
renderer, use raw_window_handle::HasRawDisplayHandle;
manager, Arc::new(RendererBackendManager::new(window.raw_display_handle()))
frame_receiver, });
joypad_sender,
current_resolution, let renderer = RendererBackend::new(current_resolution, window, window_options, m.clone());
}
(
Self {
renderer,
manager: m.clone(),
frame_receiver,
joypad_sender,
current_resolution,
},
m.clone(),
)
} }
} }
impl WindowHandler for EmulatorWindow { impl WindowHandler for EmulatorWindow {
fn on_frame(&mut self, _window: &mut Window) { fn on_frame(&mut self, _window: &mut Window) {
if let Some(ref mut receiver) = *self.frame_receiver.lock().expect("failed to lock mutex") { if let Some(ref mut receiver) = *self.frame_receiver.lock().expect("failed to lock mutex") {
@ -209,7 +221,6 @@ impl WindowHandler for EmulatorWindow {
EventStatus::Ignored EventStatus::Ignored
} }
} }
Event::Window(WindowEvent::WillClose) => EventStatus::Ignored,
_ => EventStatus::Ignored, _ => EventStatus::Ignored,
} }
} }