-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract libretro devices and logging to another modules
- Loading branch information
Showing
3 changed files
with
134 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use std::{ffi::c_void, mem::size_of}; | ||
|
||
use libretro_sys::*; | ||
|
||
use magenboy_common::audio::*; | ||
use magenboy_core::{apu::audio_device::*, keypad::{button::*, joypad::*, joypad_provider::*}, ppu::{gb_ppu::*, gfx_device::*}, GB_FREQUENCY}; | ||
|
||
use super::GAMEBOY_CORE; | ||
|
||
pub struct RetroGfxDevice; | ||
impl GfxDevice for RetroGfxDevice{ | ||
fn swap_buffer(&mut self, buffer:&[Pixel; SCREEN_HEIGHT * SCREEN_WIDTH]) { | ||
unsafe{(GAMEBOY_CORE.video_cb.unwrap())(buffer.as_ptr() as *const c_void, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32, SCREEN_WIDTH * size_of::<Pixel>())}; | ||
} | ||
} | ||
|
||
pub struct RetroJoypadProvider; | ||
impl JoypadProvider for RetroJoypadProvider{ | ||
fn provide(&mut self, joypad:&mut Joypad) { | ||
unsafe{ | ||
(GAMEBOY_CORE.input_poll_cb.unwrap())(); | ||
|
||
let input_cb: unsafe extern fn(port:u32, device:u32, index:u32, id:u32) -> i16 = GAMEBOY_CORE.input_cb.unwrap(); | ||
joypad.buttons[Button::A as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_A) != 0 || | ||
input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_X) != 0; | ||
joypad.buttons[Button::B as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_B) != 0 || | ||
input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_Y) != 0; | ||
joypad.buttons[Button::Start as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_START) != 0; | ||
joypad.buttons[Button::Select as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_SELECT) != 0; | ||
joypad.buttons[Button::Up as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_UP) != 0; | ||
joypad.buttons[Button::Down as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_DOWN) != 0; | ||
joypad.buttons[Button::Right as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_RIGHT) != 0; | ||
joypad.buttons[Button::Left as usize] = input_cb(0, DEVICE_JOYPAD, 0, DEVICE_ID_JOYPAD_LEFT) != 0; | ||
} | ||
} | ||
} | ||
|
||
pub struct RetroAudioDevice{ | ||
resampler: ManualAudioResampler | ||
} | ||
|
||
impl RetroAudioDevice{ | ||
pub const OUTPUT_FREQUENCY:u32 = 48000; | ||
} | ||
|
||
impl Default for RetroAudioDevice{ | ||
fn default()->Self{ | ||
Self { resampler: ManualAudioResampler::new(GB_FREQUENCY, Self::OUTPUT_FREQUENCY) } | ||
} | ||
} | ||
|
||
impl AudioDevice for RetroAudioDevice{ | ||
fn push_buffer(&mut self, buffer:&[StereoSample; BUFFER_SIZE]) { | ||
let mut resampled = self.resampler.resample(buffer); | ||
unsafe{GAMEBOY_CORE.audio_buffer.append(&mut resampled)}; | ||
} | ||
} | ||
|
||
impl RetroAudioDevice{ | ||
pub unsafe fn push_audio_buffer_to_libretro(){ | ||
let mut remaining_frames = GAMEBOY_CORE.audio_buffer.len(); | ||
let mut buffer_pos_ptr = GAMEBOY_CORE.audio_buffer.as_ptr() as *const Sample; | ||
while remaining_frames > 0 { | ||
let uploaded_frames = (GAMEBOY_CORE.audio_cb.unwrap())(buffer_pos_ptr, remaining_frames); | ||
remaining_frames -= uploaded_frames; | ||
buffer_pos_ptr = buffer_pos_ptr.add(uploaded_frames); | ||
} | ||
GAMEBOY_CORE.audio_buffer.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::{ffi::CString ,sync::{Mutex, OnceLock}}; | ||
|
||
use libretro_sys::*; | ||
use log::Log; | ||
|
||
pub struct RetroLogger{ | ||
retro_logger: Option<Mutex<LogCallback>> | ||
} | ||
|
||
impl RetroLogger{ | ||
pub fn init(max_log_level:log::LevelFilter, log_cb: Option<LogCallback>){ | ||
static LOGGER: OnceLock<RetroLogger> = OnceLock::new(); | ||
let logger = match log_cb{ | ||
Some(cb) => LOGGER.get_or_init(||Self{ retro_logger: Some(Mutex::new(cb))}), | ||
None => LOGGER.get_or_init(||Self { retro_logger: None }), | ||
}; | ||
log::set_logger(logger).unwrap(); | ||
log::set_max_level(max_log_level); | ||
} | ||
} | ||
|
||
impl Log for RetroLogger{ | ||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||
return self.retro_logger.is_some() && metadata.level() >= log::max_level(); | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
let logger = self.retro_logger.as_ref().unwrap().lock().unwrap(); | ||
let level = match record.level(){ | ||
log::Level::Error => LogLevel::Error, | ||
log::Level::Warn => LogLevel::Warn, | ||
log::Level::Info => LogLevel::Info, | ||
log::Level::Debug | | ||
log::Level::Trace => LogLevel::Debug, | ||
}; | ||
let message = CString::new(format!("{}\n", record.args())).unwrap(); | ||
unsafe{(logger.log)(level, message.as_ptr())}; | ||
} | ||
|
||
fn flush(&self) {} | ||
} |