video and audio callback API change:
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint); fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint); fn video_refresh_hw(&mut self, width: c_uint, height: c_uint); fn audio_sample(&mut self, left: i16, right: i16); fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize; have been replaced with fn video_refresh(&mut self, frame: &VideoFrame); fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize; where VideoFrame is pub enum VideoFrame<'a> { XRGB1555 { data: &'a [u16], width: c_uint, height: c_uint, pitch_u16: usize }, RGB565 { data: &'a [u16], width: c_uint, height: c_uint, pitch_u16: usize }, XRGB8888 { data: &'a [u32], width: c_uint, height: c_uint, pitch_u32: usize }, Duplicate { width: c_uint, height: c_uint, pitch_u8: usize, }, HardwareRender { width: c_uint, height: c_uint, }, } use `pub fn VideoFrame::data_pitch_as_bytes(&self) -> Option<(&'a [u8], usize)>` for things that need to access the framebuffer data as a byte array rather than a pixel array.
This commit is contained in:
parent
9d3e74b7cb
commit
ebea5ffd22
|
@ -27,7 +27,3 @@ example_ffmpeg = ["ffmpeg-next", "structopt"]
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "sdl2_emulator"
|
name = "sdl2_emulator"
|
||||||
required-features = ["example_sdl2"]
|
required-features = ["example_sdl2"]
|
||||||
|
|
||||||
[[example]]
|
|
||||||
name = "ffmpeg_recorder"
|
|
||||||
required-features = ["example_ffmpeg"]
|
|
||||||
|
|
|
@ -1,584 +0,0 @@
|
||||||
extern crate ferretro_base;
|
|
||||||
extern crate ffmpeg_next as ffmpeg;
|
|
||||||
|
|
||||||
use std::collections::VecDeque;
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use std::pin::Pin;
|
|
||||||
|
|
||||||
use structopt::StructOpt;
|
|
||||||
|
|
||||||
use ferretro_base::retro;
|
|
||||||
use ferretro_base::retro::ffi::{PixelFormat, GameGeometry, SystemAvInfo, SystemInfo};
|
|
||||||
use ferretro_base::retro::wrapper::{LibretroWrapper, RetroCallbacks};
|
|
||||||
|
|
||||||
use ferretro_base::retro::wrapped_types::{Variable2};
|
|
||||||
|
|
||||||
use ffmpeg::{ChannelLayout, Packet, codec, filter, format, frame, media};
|
|
||||||
use ffmpeg::util::rational::Rational;
|
|
||||||
|
|
||||||
struct MyEmulator {
|
|
||||||
retro: retro::wrapper::LibretroWrapper,
|
|
||||||
sys_info: SystemInfo,
|
|
||||||
av_info: SystemAvInfo,
|
|
||||||
audio_buf: Vec<(i16, i16)>,
|
|
||||||
video_pixel_format: format::Pixel,
|
|
||||||
prev_video_frame: Option<frame::Video>,
|
|
||||||
video_frames: VecDeque<frame::Video>,
|
|
||||||
video_encoder: ffmpeg::encoder::Video,
|
|
||||||
audio_encoder: ffmpeg::encoder::Audio,
|
|
||||||
video_filter: filter::Graph,
|
|
||||||
audio_filter: filter::Graph,
|
|
||||||
sys_path: Option<PathBuf>,
|
|
||||||
frame_properties_locked: bool,
|
|
||||||
octx: ffmpeg::format::context::Output,
|
|
||||||
frame: u64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn video_filter(
|
|
||||||
video_encoder: &ffmpeg::encoder::video::Video,
|
|
||||||
av_info: &SystemAvInfo,
|
|
||||||
pix_fmt: PixelFormat,
|
|
||||||
) -> Result<filter::Graph, ffmpeg::Error> {
|
|
||||||
let mut vfilter = filter::Graph::new();
|
|
||||||
let pix_fmt = match pix_fmt {
|
|
||||||
PixelFormat::ARGB1555 => if cfg!(target_endian = "big") { "rgb555be" } else { "rgb555le" },
|
|
||||||
PixelFormat::ARGB8888 => "argb",
|
|
||||||
PixelFormat::RGB565 => if cfg!(target_endian = "big") { "rgb565be" } else { "rgb565le" },
|
|
||||||
};
|
|
||||||
let pixel_aspect = av_info.geometry.aspect_ratio / (av_info.geometry.base_width as f32 / av_info.geometry.base_height as f32);
|
|
||||||
let fps = if av_info.timing.fps == 0.0 { 60.0 } else { av_info.timing.fps };
|
|
||||||
let args = format!(
|
|
||||||
"width={}:height={}:pix_fmt={}:frame_rate={}:pixel_aspect={}:time_base=1/{}",
|
|
||||||
av_info.geometry.base_width,
|
|
||||||
av_info.geometry.base_height,
|
|
||||||
pix_fmt,
|
|
||||||
fps,
|
|
||||||
pixel_aspect,
|
|
||||||
fps,
|
|
||||||
);
|
|
||||||
eprintln!("🎥 filter args: {}", args);
|
|
||||||
vfilter.add(&filter::find("buffer").unwrap(), "in", &args)?;
|
|
||||||
//scale?
|
|
||||||
vfilter.add(&filter::find("buffersink").unwrap(), "out", "")?;
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut out = vfilter.get("out").unwrap();
|
|
||||||
out.set_pixel_format(video_encoder.format());
|
|
||||||
}
|
|
||||||
|
|
||||||
vfilter.output("in", 0)?
|
|
||||||
.input("out", 0)?
|
|
||||||
.parse("null")?; // passthrough filter for video
|
|
||||||
|
|
||||||
vfilter.validate()?;
|
|
||||||
// human-readable filter graph
|
|
||||||
eprintln!("{}", vfilter.dump());
|
|
||||||
|
|
||||||
Ok(vfilter)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_filter(
|
|
||||||
audio_encoder: &ffmpeg::codec::encoder::Audio,
|
|
||||||
sample_rate: f64,
|
|
||||||
) -> Result<filter::Graph, ffmpeg::Error> {
|
|
||||||
let mut afilter = filter::Graph::new();
|
|
||||||
let sample_rate = if sample_rate == 0.0 { 32040.0 } else { sample_rate };
|
|
||||||
let args = format!("sample_rate={}:sample_fmt=s16:channel_layout=stereo:time_base=1/60", sample_rate);
|
|
||||||
eprintln!("🔊 filter args: {}", args);
|
|
||||||
afilter.add(&filter::find("abuffer").unwrap(), "in", &args)?;
|
|
||||||
//aresample?
|
|
||||||
afilter.add(&filter::find("abuffersink").unwrap(), "out", "")?;
|
|
||||||
|
|
||||||
{
|
|
||||||
let mut out = afilter.get("out").unwrap();
|
|
||||||
out.set_sample_format(audio_encoder.format());
|
|
||||||
out.set_channel_layout(audio_encoder.channel_layout());
|
|
||||||
out.set_sample_rate(audio_encoder.rate());
|
|
||||||
}
|
|
||||||
|
|
||||||
afilter.output("in", 0)?
|
|
||||||
.input("out", 0)?
|
|
||||||
.parse("anull")?;
|
|
||||||
afilter.validate()?;
|
|
||||||
// human-readable filter graph
|
|
||||||
eprintln!("{}", afilter.dump());
|
|
||||||
|
|
||||||
if let Some(codec) = audio_encoder.codec() {
|
|
||||||
if !codec
|
|
||||||
.capabilities()
|
|
||||||
.contains(ffmpeg::codec::capabilities::Capabilities::VARIABLE_FRAME_SIZE)
|
|
||||||
{
|
|
||||||
eprintln!("setting constant frame size {}", audio_encoder.frame_size());
|
|
||||||
afilter
|
|
||||||
.get("out")
|
|
||||||
.unwrap()
|
|
||||||
.sink()
|
|
||||||
.set_frame_size(audio_encoder.frame_size());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(afilter)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MyEmulator {
|
|
||||||
pub fn new(
|
|
||||||
core_path: impl AsRef<Path>,
|
|
||||||
sys_path: &Option<impl AsRef<Path>>,
|
|
||||||
video_path: impl AsRef<Path>,
|
|
||||||
mut octx: ffmpeg::format::context::Output,
|
|
||||||
) -> Pin<Box<Self>> {
|
|
||||||
let lib = libloading::Library::new(core_path.as_ref()).unwrap();
|
|
||||||
let raw_retro = retro::loading::LibretroApi::from_library(lib).unwrap();
|
|
||||||
let retro = retro::wrapper::LibretroWrapper::from(raw_retro);
|
|
||||||
|
|
||||||
let sys_info = retro.get_system_info();
|
|
||||||
let mut av_info = retro.get_system_av_info();
|
|
||||||
|
|
||||||
let fps_int = av_info.timing.fps.round() as i32;
|
|
||||||
let fps_int = if fps_int == 0 { 60 } else { fps_int };
|
|
||||||
|
|
||||||
let detected_vcodec = octx.format().codec(&video_path, media::Type::Video);
|
|
||||||
//let detected_acodec = octx.format().codec(&video_path, media::Type::Audio);
|
|
||||||
let wavname = Path::new("out.wav");
|
|
||||||
let detected_acodec = octx.format().codec(&wavname, media::Type::Audio);
|
|
||||||
|
|
||||||
let vcodec = ffmpeg::encoder::find(detected_vcodec).unwrap().video().unwrap();
|
|
||||||
let acodec = ffmpeg::encoder::find(detected_acodec).unwrap().audio().unwrap();
|
|
||||||
|
|
||||||
let mut video_output = octx.add_stream(vcodec).unwrap();
|
|
||||||
video_output.set_time_base(Rational::new(1, 60));
|
|
||||||
let mut video_encoder = video_output.codec().encoder().video().unwrap();
|
|
||||||
|
|
||||||
video_encoder.set_bit_rate(2560000);
|
|
||||||
video_encoder.set_format(video_encoder.codec().unwrap().video().unwrap().formats().unwrap().nth(0).unwrap());
|
|
||||||
|
|
||||||
video_encoder.set_time_base(Rational::new(1, 60));
|
|
||||||
video_encoder.set_frame_rate(Some(Rational::new(fps_int, 1)));
|
|
||||||
|
|
||||||
//video_encoder.set_frame_rate(av_info.timing.fps.into());
|
|
||||||
|
|
||||||
if av_info.geometry.base_height == 0 && av_info.geometry.base_width == 0 {
|
|
||||||
av_info.geometry.base_width = 320;
|
|
||||||
av_info.geometry.base_height = 224;
|
|
||||||
av_info.geometry.aspect_ratio = 4.33;
|
|
||||||
}
|
|
||||||
if av_info.timing.sample_rate == 0.0 {
|
|
||||||
av_info.timing.sample_rate = 44100.0;
|
|
||||||
}
|
|
||||||
video_encoder.set_width(av_info.geometry.base_width);
|
|
||||||
video_encoder.set_height(av_info.geometry.base_height);
|
|
||||||
//video_encoder.set_aspect_ratio(av_info.geometry.aspect_ratio as f64);
|
|
||||||
|
|
||||||
let pix_fmt = PixelFormat::ARGB1555; // temporary until env call is made
|
|
||||||
let video_filter = video_filter(&video_encoder, &av_info, pix_fmt).unwrap();
|
|
||||||
|
|
||||||
let video_encoder = video_encoder.open_as(vcodec).unwrap();
|
|
||||||
//video_output.set_parameters(&video_encoder);
|
|
||||||
|
|
||||||
let mut audio_output = octx.add_stream(acodec).unwrap();
|
|
||||||
let mut audio_encoder = audio_output.codec().encoder().audio().unwrap();
|
|
||||||
|
|
||||||
//let mut video_encoder = octx.add_stream(vcodec).unwrap().codec().encoder().video().unwrap();
|
|
||||||
/*
|
|
||||||
let mut audio_output = octx.add_stream(acodec).unwrap();
|
|
||||||
let mut audio_encoder = audio_output.codec().encoder().audio().unwrap();
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
retroarch inits
|
|
||||||
static bool ffmpeg_init_config(struct ff_config_param *params,
|
|
||||||
if (!ffmpeg_init_muxer_pre(handle))
|
|
||||||
if (!ffmpeg_init_video(handle))
|
|
||||||
av_frame_alloc
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
audio_encoder.set_bit_rate(640000);
|
|
||||||
audio_encoder.set_max_bit_rate(990000);
|
|
||||||
|
|
||||||
//audio_encoder.set_rate(44100);
|
|
||||||
audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32);
|
|
||||||
audio_encoder.set_channels(2);
|
|
||||||
audio_encoder.set_channel_layout(ChannelLayout::STEREO);
|
|
||||||
audio_encoder.set_format(audio_encoder.codec().unwrap().audio().unwrap().formats().unwrap().nth(0).unwrap());
|
|
||||||
audio_encoder.set_time_base(Rational::new(1, 60));
|
|
||||||
audio_output.set_time_base(Rational::new(1, 60));
|
|
||||||
|
|
||||||
let audio_encoder = audio_encoder.open_as(acodec).unwrap();
|
|
||||||
//audio_output.set_parameters(&audio_encoder);
|
|
||||||
|
|
||||||
let audio_filter = audio_filter(&audio_encoder, av_info.timing.sample_rate).unwrap();
|
|
||||||
|
|
||||||
//audio_encoder.set_rate(av_info.timing.sample_rate.round() as i32);
|
|
||||||
|
|
||||||
octx.write_header().unwrap();
|
|
||||||
ffmpeg::format::context::output::dump(&octx, 0, None);
|
|
||||||
|
|
||||||
let emu = MyEmulator {
|
|
||||||
retro,
|
|
||||||
sys_info,
|
|
||||||
av_info: av_info.clone(),
|
|
||||||
audio_buf: Default::default(),
|
|
||||||
video_pixel_format: format::Pixel::RGB555,
|
|
||||||
prev_video_frame: None,
|
|
||||||
video_frames: Default::default(),
|
|
||||||
video_encoder,
|
|
||||||
audio_encoder,
|
|
||||||
video_filter,
|
|
||||||
audio_filter,
|
|
||||||
sys_path: sys_path.as_ref().map(|x| x.as_ref().to_path_buf()),
|
|
||||||
frame_properties_locked: false,
|
|
||||||
octx,
|
|
||||||
frame: 0
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut pin_emu = Box::pin(emu);
|
|
||||||
retro::wrapper::set_handler(pin_emu.as_mut());
|
|
||||||
pin_emu.retro.init();
|
|
||||||
pin_emu.set_system_av_info(&av_info);
|
|
||||||
pin_emu
|
|
||||||
}
|
|
||||||
|
|
||||||
fn receive_and_write_packets(&mut self, encoder: EncoderToWriteFrom)
|
|
||||||
{
|
|
||||||
let stream_index = match encoder {
|
|
||||||
EncoderToWriteFrom::Video => 0,
|
|
||||||
EncoderToWriteFrom::Audio => 1,
|
|
||||||
};
|
|
||||||
let mut encoded_packet = ffmpeg::Packet::empty();
|
|
||||||
loop
|
|
||||||
{
|
|
||||||
match match encoder {
|
|
||||||
EncoderToWriteFrom::Video => self.video_encoder.receive_packet(&mut encoded_packet),
|
|
||||||
EncoderToWriteFrom::Audio => self.audio_encoder.receive_packet(&mut encoded_packet),
|
|
||||||
} {
|
|
||||||
Ok(..) => {
|
|
||||||
//if encoded_packet.size() > 0 {
|
|
||||||
encoded_packet.set_stream(stream_index);
|
|
||||||
eprintln!("📦 Writing packet, pts {:?} dts {:?} size {}", encoded_packet.pts(), encoded_packet.dts(), encoded_packet.size());
|
|
||||||
if stream_index == 0 {
|
|
||||||
encoded_packet.rescale_ts(Rational(1, 60), self.octx.stream(stream_index).unwrap().time_base());
|
|
||||||
}
|
|
||||||
eprintln!("📦 rescaled , pts {:?} dts {:?} size {}", encoded_packet.pts(), encoded_packet.dts(), encoded_packet.size());
|
|
||||||
|
|
||||||
match encoded_packet.write_interleaved(&mut self.octx) {
|
|
||||||
Ok(..) => eprintln!("Write OK"),
|
|
||||||
Err(e) => eprintln!("Error writing: {}", e),
|
|
||||||
}
|
|
||||||
//encoded_packet.write_interleaved(&mut self.octx).unwrap(); // AAA
|
|
||||||
//}
|
|
||||||
//else {
|
|
||||||
//eprintln!("Did not try to write 0-length packet");
|
|
||||||
//}
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error writing packet: {:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(&mut self, frame: i64) {
|
|
||||||
self.frame += 1;
|
|
||||||
self.retro.run();
|
|
||||||
|
|
||||||
match self.video_frames.pop_front() {
|
|
||||||
Some(mut vframe) => {
|
|
||||||
vframe.set_pts(Some(frame));
|
|
||||||
eprintln!("🎞 queue frame pts {:?}", vframe.pts());
|
|
||||||
self.video_filter.get("in").unwrap().source().add(&vframe).unwrap();
|
|
||||||
let mut filtered_vframe = frame::Video::empty();
|
|
||||||
|
|
||||||
loop {
|
|
||||||
match self.video_filter.get("out").unwrap().sink().frame(&mut filtered_vframe) {
|
|
||||||
Ok(..) => {
|
|
||||||
eprintln!("🎥 Got filtered video frame {}x{} pts {:?}", filtered_vframe.width(), filtered_vframe.height(), filtered_vframe.pts());
|
|
||||||
if self.video_filter.get("in").unwrap().source().failed_requests() > 0 {
|
|
||||||
println!("🎥 failed to put filter input frame");
|
|
||||||
}
|
|
||||||
//filtered_vframe.set_pts(Some(frame));
|
|
||||||
self.video_encoder.send_frame(&filtered_vframe).unwrap();
|
|
||||||
|
|
||||||
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error getting filtered video frame: {:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut aframe = frame::Audio::new(
|
|
||||||
format::Sample::I16(format::sample::Type::Packed),
|
|
||||||
self.audio_buf.len(),
|
|
||||||
ChannelLayout::STEREO
|
|
||||||
);
|
|
||||||
if aframe.planes() > 0 {
|
|
||||||
aframe.set_channels(2);
|
|
||||||
aframe.set_rate(44100);
|
|
||||||
aframe.set_pts(Some(frame));
|
|
||||||
let aplane: &mut [(i16, i16)] = aframe.plane_mut(0);
|
|
||||||
eprintln!("Audio buffer length {} -> {}", self.audio_buf.len(), aplane.len());
|
|
||||||
aplane.copy_from_slice(self.audio_buf.as_ref());
|
|
||||||
//eprintln!("src: {:?}, dest: {:?}", self.audio_buf, aplane);
|
|
||||||
self.audio_buf.clear();
|
|
||||||
|
|
||||||
eprintln!("frame audio: {:?}", aframe);
|
|
||||||
|
|
||||||
eprintln!("🎞 queue frame pts {:?}", aframe.pts());
|
|
||||||
self.audio_filter.get("in").unwrap().source().add(&aframe).unwrap();
|
|
||||||
|
|
||||||
let mut filtered_aframe = frame::Audio::empty();
|
|
||||||
loop {
|
|
||||||
match self.audio_filter.get("out").unwrap().sink().frame(&mut filtered_aframe) {
|
|
||||||
Ok(..) => {
|
|
||||||
eprintln!("🔊 Got filtered audio frame {:?} pts {:?}", filtered_aframe, filtered_aframe.pts());
|
|
||||||
if self.audio_filter.get("in").unwrap().source().failed_requests() > 0 {
|
|
||||||
println!("🎥 failed to put filter input frame");
|
|
||||||
}
|
|
||||||
//let faplane: &[f32] = filtered_aframe.plane(0);
|
|
||||||
//filtered_aframe.set_pts(Some(frame));
|
|
||||||
|
|
||||||
self.audio_encoder.send_frame(&filtered_aframe).unwrap();
|
|
||||||
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
|
|
||||||
},
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error getting filtered audio frame: {:?}", e);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => println!("Video not ready during frame {}", self.frame)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn load_game(&mut self, rom: impl AsRef<Path>) {
|
|
||||||
let path = rom.as_ref();
|
|
||||||
let mut data = None;
|
|
||||||
let mut v = Vec::new();
|
|
||||||
if !self.sys_info.need_fullpath {
|
|
||||||
if let Ok(mut f) = std::fs::File::open(path) {
|
|
||||||
if f.read_to_end(&mut v).is_ok() {
|
|
||||||
data = Some(v.as_ref());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.retro
|
|
||||||
.load_game(Some(path), data, None)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn end(&mut self) {
|
|
||||||
self.video_encoder.send_eof();
|
|
||||||
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
|
||||||
self.audio_encoder.send_eof();
|
|
||||||
self.receive_and_write_packets(EncoderToWriteFrom::Audio);
|
|
||||||
self.octx.write_trailer().unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn unserialize(&mut self, state: impl AsRef<Path>) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let path = state.as_ref();
|
|
||||||
let mut v = Vec::new();
|
|
||||||
if let Ok(mut f) = std::fs::File::open(path) {
|
|
||||||
if f.read_to_end(&mut v).is_ok(){
|
|
||||||
return self.retro.unserialize(v.as_ref());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err("Couldn't read file to unserialize".into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl retro::wrapper::LibretroWrapperAccess for MyEmulator {
|
|
||||||
fn libretro_core(&mut self) -> &mut LibretroWrapper {
|
|
||||||
&mut self.retro
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl retro::wrapper::RetroCallbacks for MyEmulator {
|
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
|
||||||
let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
|
|
||||||
|
|
||||||
let stride = vframe.stride(0);
|
|
||||||
let pitch = pitch as usize;
|
|
||||||
|
|
||||||
let vplane = vframe.data_mut(0);
|
|
||||||
if data.len() == vplane.len() && pitch == stride {
|
|
||||||
vplane.copy_from_slice(&data);
|
|
||||||
} else {
|
|
||||||
for y in 0..(height as usize) {
|
|
||||||
let ffbegin = y * stride;
|
|
||||||
let lrbegin = y * pitch;
|
|
||||||
let min = usize::min(stride, pitch);
|
|
||||||
vplane[ffbegin..(ffbegin + min)].copy_from_slice(
|
|
||||||
&data[lrbegin..(lrbegin + min)]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//vframe.set_pts(Some(self.frame as i64));
|
|
||||||
|
|
||||||
self.prev_video_frame.replace(vframe.clone());
|
|
||||||
self.video_frames.push_back(vframe);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn video_refresh_dupe(&mut self, width: u32, height: u32, _pitch: u32) {
|
|
||||||
if let Some(frame) = &self.prev_video_frame {
|
|
||||||
self.video_frames.push_back(frame.clone());
|
|
||||||
} else {
|
|
||||||
let vframe = frame::Video::new(self.video_pixel_format, width, height);
|
|
||||||
self.video_frames.push_back(vframe);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
|
||||||
self.audio_buf.push((left, right));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
||||||
let left_iter = stereo_pcm.iter().step_by(2).cloned();
|
|
||||||
let right_iter = stereo_pcm.iter().skip(1).step_by(2).cloned();
|
|
||||||
self.audio_buf.extend(Iterator::zip(left_iter, right_iter));
|
|
||||||
stereo_pcm.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_system_directory(&mut self) -> Option<PathBuf> {
|
|
||||||
self.sys_path.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
|
|
||||||
if self.frame_properties_locked {
|
|
||||||
return Some(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.video_pixel_format = match format {
|
|
||||||
PixelFormat::ARGB1555 => format::Pixel::RGB555,
|
|
||||||
PixelFormat::ARGB8888 => format::Pixel::RGB32,
|
|
||||||
PixelFormat::RGB565 => format::Pixel::RGB565,
|
|
||||||
};
|
|
||||||
self.video_filter = video_filter(&self.video_encoder, &self.av_info, format).unwrap();
|
|
||||||
Some(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
|
|
||||||
if self.frame_properties_locked {
|
|
||||||
return Some(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
//self.video_encoder.set_frame_rate(system_av_info.timing.fps.into());
|
|
||||||
//self.video_encoder.set_time_base(Rational::new(1, 60));
|
|
||||||
//self.video_encoder.set_frame_rate(Some(Rational::new(1, 60)));
|
|
||||||
if system_av_info.timing.sample_rate.round() as i32 > 0 {
|
|
||||||
self.audio_encoder.set_rate(system_av_info.timing.sample_rate.round() as i32);
|
|
||||||
}
|
|
||||||
self.av_info.timing = system_av_info.timing.clone();
|
|
||||||
self.set_geometry(&system_av_info.geometry);
|
|
||||||
Some(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_geometry(&mut self, geometry: &GameGeometry) -> Option<bool> {
|
|
||||||
if self.frame_properties_locked {
|
|
||||||
return Some(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.video_encoder.set_width(geometry.base_width);
|
|
||||||
self.video_encoder.set_height(geometry.base_height);
|
|
||||||
//self.video_encoder.set_aspect_ratio(geometry.aspect_ratio as f64);
|
|
||||||
self.av_info.geometry = geometry.clone();
|
|
||||||
let pixel_format = match self.video_pixel_format {
|
|
||||||
format::Pixel::RGB555 => PixelFormat::ARGB1555,
|
|
||||||
format::Pixel::RGB32 => PixelFormat::ARGB8888,
|
|
||||||
format::Pixel::RGB565 => PixelFormat::RGB565,
|
|
||||||
_ => unimplemented!(),
|
|
||||||
};
|
|
||||||
self.video_filter = video_filter(&self.video_encoder, &self.av_info, pixel_format).unwrap();
|
|
||||||
Some(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn get_variable(&mut self, key: &str) -> Option<String> {
|
|
||||||
match key {
|
|
||||||
"beetle_saturn_analog_stick_deadzone" => Some("15%".to_string()),
|
|
||||||
//"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
|
|
||||||
"parallel-n64-astick-deadzone" => Some("15%".to_string()),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_variables(&mut self, variables: &Vec<Variable2>) -> Option<bool> {
|
|
||||||
for v in variables {
|
|
||||||
eprintln!("{:?}", v);
|
|
||||||
}
|
|
||||||
Some(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log_print(&mut self, level: retro::ffi::LogLevel, msg: &str) {
|
|
||||||
eprint!("🕹️ [{:?}] {}", level, msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(StructOpt)]
|
|
||||||
struct Opt {
|
|
||||||
/// Core module to use.
|
|
||||||
#[structopt(short, long, parse(from_os_str))]
|
|
||||||
core: PathBuf,
|
|
||||||
/// ROM to load using the core.
|
|
||||||
#[structopt(short, long, parse(from_os_str))]
|
|
||||||
rom: PathBuf,
|
|
||||||
/// Recorded video to write.
|
|
||||||
#[structopt(short, long, parse(from_os_str))]
|
|
||||||
video: PathBuf,
|
|
||||||
/// Save state to load at startup.
|
|
||||||
#[structopt(long, parse(from_os_str))]
|
|
||||||
state: Option<PathBuf>,
|
|
||||||
/// System directory, often containing BIOS files
|
|
||||||
#[structopt(short, long, parse(from_os_str))]
|
|
||||||
system: Option<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let opt: Opt = Opt::from_args();
|
|
||||||
ffmpeg::log::set_level(ffmpeg::log::Level::Trace);
|
|
||||||
ffmpeg::init().unwrap();
|
|
||||||
|
|
||||||
let mut octx = format::output(&opt.video)?;
|
|
||||||
unsafe {
|
|
||||||
(*octx.as_mut_ptr()).debug = 1;
|
|
||||||
eprintln!("oformat: {:?}", &((*octx.as_mut_ptr()).oformat));
|
|
||||||
eprintln!("flags: {:?}", (*(*octx.as_mut_ptr()).oformat).flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
//octx.write_header().unwrap();
|
|
||||||
|
|
||||||
let mut emu = MyEmulator::new(opt.core, &opt.system, &opt.video, octx);
|
|
||||||
emu.load_game(opt.rom);
|
|
||||||
|
|
||||||
emu.frame_properties_locked = true;
|
|
||||||
|
|
||||||
if let Some(state) = opt.state {
|
|
||||||
emu.unserialize(state)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
//for frame in 0..60*10 {
|
|
||||||
for frame in 0..800 {
|
|
||||||
eprintln!("🖼️ frame: {}", frame);
|
|
||||||
emu.run(frame);
|
|
||||||
|
|
||||||
}
|
|
||||||
let mut packet = Packet::empty();
|
|
||||||
eprintln!("flushed: {:?}", emu.video_encoder.flush(&mut packet)?);
|
|
||||||
|
|
||||||
emu.end();
|
|
||||||
//octx.write_trailer().unwrap();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
enum EncoderToWriteFrom {
|
|
||||||
Video,
|
|
||||||
Audio,
|
|
||||||
}
|
|
|
@ -3,10 +3,7 @@ extern crate ferretro_base;
|
||||||
extern crate sdl2;
|
extern crate sdl2;
|
||||||
|
|
||||||
use ferretro_base::retro;
|
use ferretro_base::retro;
|
||||||
use ferretro_base::retro::ffi::{GameGeometry, SystemInfo, SystemAvInfo};
|
use ferretro_base::prelude::*;
|
||||||
use ferretro_base::retro::constants::{InputIndex, JoypadButton, AnalogAxis, DeviceType};
|
|
||||||
use ferretro_base::retro::wrapped_types::{ControllerDescription2, InputDescriptor2, InputDeviceId, SubsystemInfo2, Variable2};
|
|
||||||
use ferretro_base::retro::wrapper::LibretroWrapper;
|
|
||||||
|
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
@ -79,7 +76,6 @@ impl MyEmulator {
|
||||||
.video()
|
.video()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.window(title.as_str(), av_info.geometry.base_width, av_info.geometry.base_height)
|
.window(title.as_str(), av_info.geometry.base_width, av_info.geometry.base_height)
|
||||||
.opengl()
|
|
||||||
.build()
|
.build()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
@ -260,31 +256,33 @@ impl retro::wrapper::LibretroWrapperAccess for MyEmulator {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl retro::wrapper::RetroCallbacks for MyEmulator {
|
impl retro::wrapper::RetroCallbacks for MyEmulator {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
let rect = Rect::new(0, 0, width, height);
|
match frame {
|
||||||
|
VideoFrame::XRGB1555 { width, height, .. }
|
||||||
|
| VideoFrame::RGB565 { width, height, .. }
|
||||||
|
| VideoFrame::XRGB8888 { width, height, .. } => {
|
||||||
|
let rect = Rect::new(0, 0, *width, *height);
|
||||||
|
let (pixel_data, pitch) = frame.data_pitch_as_bytes().unwrap();
|
||||||
|
|
||||||
if let Ok(mut tex) =
|
if let Ok(mut tex) =
|
||||||
self.canvas
|
self.canvas
|
||||||
.texture_creator()
|
.texture_creator()
|
||||||
.create_texture_static(self.pixel_format, width, height)
|
.create_texture_static(self.pixel_format, *width, *height)
|
||||||
{
|
{
|
||||||
if tex.update(rect, data, pitch as usize).is_ok() {
|
if tex.update(rect, pixel_data, pitch).is_ok() {
|
||||||
self.canvas.clear();
|
self.canvas.clear();
|
||||||
self.canvas.copy(&tex, None, None).unwrap();
|
self.canvas.copy(&tex, None, None).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
}
|
||||||
self.audio_buffer.push(left);
|
|
||||||
self.audio_buffer.push(right);
|
|
||||||
self.send_audio_samples()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
self.audio_buffer.extend(stereo_pcm);
|
self.audio_buffer.extend(stereo_pcm);
|
||||||
self.send_audio_samples();
|
self.send_audio_samples();
|
||||||
stereo_pcm.len()
|
stereo_pcm.len() / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn input_poll(&mut self) {
|
fn input_poll(&mut self) {
|
||||||
|
|
|
@ -7,20 +7,11 @@ use crate::prelude::*;
|
||||||
use crate::retro::ffi::*;
|
use crate::retro::ffi::*;
|
||||||
|
|
||||||
impl<T: RetroCallbacks> RetroCallbacks for Rc<RefCell<T>> {
|
impl<T: RetroCallbacks> RetroCallbacks for Rc<RefCell<T>> {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
RefCell::borrow_mut(self).video_refresh(data, width, height, pitch)
|
RefCell::borrow_mut(self).video_refresh(frame)
|
||||||
}
|
}
|
||||||
fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint) {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
RefCell::borrow_mut(self).video_refresh_dupe(width, height, pitch)
|
RefCell::borrow_mut(self).audio_samples(stereo_pcm)
|
||||||
}
|
|
||||||
fn video_refresh_hw(&mut self, width: c_uint, height: c_uint) {
|
|
||||||
RefCell::borrow_mut(self).video_refresh_hw(width, height)
|
|
||||||
}
|
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
|
||||||
RefCell::borrow_mut(self).audio_sample(left, right)
|
|
||||||
}
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
||||||
RefCell::borrow_mut(self).audio_sample_batch(stereo_pcm)
|
|
||||||
}
|
}
|
||||||
fn input_poll(&mut self) {
|
fn input_poll(&mut self) {
|
||||||
RefCell::borrow_mut(self).input_poll()
|
RefCell::borrow_mut(self).input_poll()
|
||||||
|
|
|
@ -5,6 +5,35 @@ use std::slice::from_raw_parts;
|
||||||
|
|
||||||
use super::constants::*;
|
use super::constants::*;
|
||||||
use super::ffi::*;
|
use super::ffi::*;
|
||||||
|
use std::mem::size_of;
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum VideoFrame<'a> {
|
||||||
|
XRGB1555 { data: &'a [u16], width: c_uint, height: c_uint, pitch_u16: usize },
|
||||||
|
RGB565 { data: &'a [u16], width: c_uint, height: c_uint, pitch_u16: usize },
|
||||||
|
XRGB8888 { data: &'a [u32], width: c_uint, height: c_uint, pitch_u32: usize },
|
||||||
|
Duplicate { width: c_uint, height: c_uint, pitch_u8: usize, },
|
||||||
|
HardwareRender { width: c_uint, height: c_uint, },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> VideoFrame<'a> {
|
||||||
|
pub fn data_pitch_as_bytes(&self) -> Option<(&'a [u8], usize)> {
|
||||||
|
match self {
|
||||||
|
VideoFrame::RGB565 { data, pitch_u16, .. }
|
||||||
|
| VideoFrame::XRGB1555 { data, pitch_u16, .. } => {
|
||||||
|
let ptr = data.as_ptr() as *const u8;
|
||||||
|
let len = data.len() * size_of::<u16>();
|
||||||
|
Some((unsafe { from_raw_parts(ptr, len) }, pitch_u16 * size_of::<u16>()))
|
||||||
|
}
|
||||||
|
VideoFrame::XRGB8888 { data, pitch_u32, .. } => {
|
||||||
|
let ptr = data.as_ptr() as *const u8;
|
||||||
|
let len = data.len() * size_of::<u32>();
|
||||||
|
Some((unsafe { from_raw_parts(ptr, len) }, pitch_u32 * size_of::<u32>()))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum InputDeviceId {
|
pub enum InputDeviceId {
|
||||||
|
|
|
@ -17,7 +17,15 @@ use super::ffi::*;
|
||||||
use super::loading::*;
|
use super::loading::*;
|
||||||
use super::wrapped_types::*;
|
use super::wrapped_types::*;
|
||||||
|
|
||||||
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks { handler: None };
|
// #[cfg(doc)] <- broken as of (at least) 1.56
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use libretro_sys::{self, CoreAPI};
|
||||||
|
use std::mem::size_of;
|
||||||
|
|
||||||
|
static mut CB_SINGLETON: StaticCallbacks = StaticCallbacks {
|
||||||
|
handler: None,
|
||||||
|
pix_fmt: PixelFormat::ARGB1555,
|
||||||
|
};
|
||||||
|
|
||||||
// stable Rust doesn't have varargs, so we can't represent a callback with the signature of
|
// stable Rust doesn't have varargs, so we can't represent a callback with the signature of
|
||||||
// void (*retro_log_printf_t)(enum retro_log_level level, const char* fmt, ...)
|
// void (*retro_log_printf_t)(enum retro_log_level level, const char* fmt, ...)
|
||||||
|
@ -28,13 +36,19 @@ extern "C" {
|
||||||
fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn);
|
fn c_ext_set_log_print_cb(cb: WrappedLogPrintFn);
|
||||||
}
|
}
|
||||||
|
|
||||||
// method docs largely copied/lightly-adapted-to-rust straight from libretro.h.
|
/// This trait represents all the callbacks in the libretro API (including `retro_environment`
|
||||||
|
/// extensions), wrapped as methods on `&mut self`, all optional with default no-op implementations.
|
||||||
|
///
|
||||||
|
/// NOTE: Most of the method docs provided here are adapted to Rust from the ones written in
|
||||||
|
/// libretro.h, and many of them are descriptions of the API contract written with an intended
|
||||||
|
/// audience of backend/core authors.
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
pub trait RetroCallbacks: Unpin + 'static {
|
pub trait RetroCallbacks: Unpin + 'static {
|
||||||
// -- main callbacks --
|
// -- main callbacks --
|
||||||
/// Render a frame. Pixel format is 15-bit 0RGB1555 native endian
|
/// Render a frame.
|
||||||
/// unless changed (see [Self::set_pixel_format]).
|
///
|
||||||
|
/// Pixel format is 15-bit 0RGB1555 native endian unless changed (see [Self::set_pixel_format]).
|
||||||
///
|
///
|
||||||
/// Width and height specify dimensions of buffer.
|
/// Width and height specify dimensions of buffer.
|
||||||
/// Pitch specifices length in bytes between two lines in buffer.
|
/// Pitch specifices length in bytes between two lines in buffer.
|
||||||
|
@ -43,21 +57,15 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// that is packed in memory, i.e. pitch == width * byte_per_pixel.
|
/// that is packed in memory, i.e. pitch == width * byte_per_pixel.
|
||||||
/// Certain graphic APIs, such as OpenGL ES, do not like textures
|
/// Certain graphic APIs, such as OpenGL ES, do not like textures
|
||||||
/// that are not packed in memory.
|
/// that are not packed in memory.
|
||||||
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {}
|
fn video_refresh(&mut self, frame: &VideoFrame) {}
|
||||||
/// Called instead of video_refresh when a core reports a duplicate frame (NULL).
|
/// Renders audio frames.
|
||||||
fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint) {}
|
|
||||||
/// Called instead of video_refresh when a core uses hardware rendering (HW_FRAMEBUFFER_VALID).
|
|
||||||
fn video_refresh_hw(&mut self, width: c_uint, height: c_uint) {}
|
|
||||||
/// Renders a single audio frame. Should only be used if implementation
|
|
||||||
/// generates a single sample at a time.
|
|
||||||
/// Format is signed 16-bit native endian.
|
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {}
|
|
||||||
/// Renders multiple audio frames in one go.
|
|
||||||
///
|
///
|
||||||
/// One frame is defined as a sample of left and right channels, interleaved.
|
/// One frame is defined as a sample of left and right channels, interleaved.
|
||||||
/// I.e. int16_t buf\[4\] = { l, r, l, r }; would be 2 frames.
|
/// I.e. `int16_t buf[4] = { l, r, l, r };` would be 2 frames.
|
||||||
/// Only one of the audio callbacks must ever be used.
|
/// Format is signed 16-bit native endian PCM.
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize { stereo_pcm.len() }
|
///
|
||||||
|
/// The frontend should return the number of frames used (stereo slice length divided by two!)
|
||||||
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize { stereo_pcm.len() }
|
||||||
/// Polls input.
|
/// Polls input.
|
||||||
fn input_poll(&mut self) {}
|
fn input_poll(&mut self) {}
|
||||||
/// Queries for input for player 'port'.
|
/// Queries for input for player 'port'.
|
||||||
|
@ -75,8 +83,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Sets a message to be displayed in implementation-specific manner
|
/// Sets a message to be displayed in implementation-specific manner
|
||||||
/// for a certain amount of 'frames'.
|
/// for a certain amount of 'frames'.
|
||||||
/// Should not be used for trivial messages, which should simply be
|
/// Should not be used for trivial messages, which should simply be
|
||||||
/// logged via [Self::get_log_interface] (or as a
|
/// logged via `retro_get_log_interface` (or as a fallback, stderr).
|
||||||
/// fallback, stderr).
|
|
||||||
fn set_message(&mut self, message: &Message) -> Option<bool> { None }
|
fn set_message(&mut self, message: &Message) -> Option<bool> { None }
|
||||||
/// Requests the frontend to shutdown.
|
/// Requests the frontend to shutdown.
|
||||||
/// Should only be used if game has a specific
|
/// Should only be used if game has a specific
|
||||||
|
@ -95,7 +102,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// This function can be called on a per-game basis,
|
/// This function can be called on a per-game basis,
|
||||||
/// as certain games an implementation can play might be
|
/// as certain games an implementation can play might be
|
||||||
/// particularly demanding.
|
/// particularly demanding.
|
||||||
/// If called, it should be called in [libretro_sys::CoreAPI::retro_load_game].
|
/// If called, it should be called in [CoreAPI::retro_load_game].
|
||||||
fn set_performance_level(&mut self, level: c_uint) -> Option<bool> { None }
|
fn set_performance_level(&mut self, level: c_uint) -> Option<bool> { None }
|
||||||
/// Returns the "system" directory of the frontend.
|
/// Returns the "system" directory of the frontend.
|
||||||
/// This directory can be used to store system specific
|
/// This directory can be used to store system specific
|
||||||
|
@ -115,21 +122,21 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// If the call returns false, the frontend does not support this pixel
|
/// If the call returns false, the frontend does not support this pixel
|
||||||
/// format.
|
/// format.
|
||||||
///
|
///
|
||||||
/// The core should call this function inside [libretro_sys::CoreAPI::retro_load_game] or
|
/// The core should call this function inside [CoreAPI::retro_load_game] or
|
||||||
/// [Self::set_system_av_info].
|
/// [Self::set_system_av_info].
|
||||||
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> { None }
|
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> { None }
|
||||||
/// Sets an array of [crate::prelude::InputDescriptor2].
|
/// Sets an array of [InputDescriptor2](crate::prelude::InputDescriptor2).
|
||||||
/// It is up to the frontend to present this in a usable way.
|
/// It is up to the frontend to present this in a usable way.
|
||||||
/// This function can be called at any time, but it is recommended
|
/// This function can be called at any time, but it is recommended
|
||||||
/// for the core to call it as early as possible.
|
/// for the core to call it as early as possible.
|
||||||
fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> Option<bool> { None }
|
fn set_input_descriptors(&mut self, input_descriptors: &Vec<InputDescriptor2>) -> Option<bool> { None }
|
||||||
/// Sets an interface to let a libretro core render with
|
/// Sets an interface to let a libretro core render with
|
||||||
/// hardware acceleration.
|
/// hardware acceleration.
|
||||||
/// The core should call this in [libretro_sys::CoreAPI::retro_load_game].
|
/// The core should call this in [CoreAPI::retro_load_game].
|
||||||
/// If successful, libretro cores will be able to render to a
|
/// If successful, libretro cores will be able to render to a
|
||||||
/// frontend-provided framebuffer.
|
/// frontend-provided framebuffer.
|
||||||
/// The size of this framebuffer will be at least as large as
|
/// The size of this framebuffer will be at least as large as
|
||||||
/// max_width/max_height provided in [libretro_sys::CoreAPI::retro_get_system_av_info].
|
/// max_width/max_height provided in [CoreAPI::retro_get_system_av_info].
|
||||||
/// If HW rendering is used, pass only [libretro_sys::HW_FRAME_BUFFER_VALID] or
|
/// If HW rendering is used, pass only [libretro_sys::HW_FRAME_BUFFER_VALID] or
|
||||||
/// NULL to [libretro_sys::VideoRefreshFn].
|
/// NULL to [libretro_sys::VideoRefreshFn].
|
||||||
fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> Option<bool> { None }
|
fn set_hw_render(&mut self, hw_render_callback: &HwRenderCallback) -> Option<bool> { None }
|
||||||
|
@ -144,19 +151,19 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// This allows the frontend to present these variables to
|
/// This allows the frontend to present these variables to
|
||||||
/// a user dynamically.
|
/// a user dynamically.
|
||||||
/// The core should call this for the first time as early as
|
/// The core should call this for the first time as early as
|
||||||
/// possible (ideally in [libretro_sys::CoreAPI::retro_set_environment]).
|
/// possible (ideally in [CoreAPI::retro_set_environment]).
|
||||||
/// Afterward it may be called again for the core to communicate
|
/// Afterward it may be called again for the core to communicate
|
||||||
/// updated options to the frontend, but the number of core
|
/// updated options to the frontend, but the number of core
|
||||||
/// options must not change from the number in the initial call.
|
/// options must not change from the number in the initial call.
|
||||||
///
|
///
|
||||||
/// [crate::prelude::Variable2::key] should be namespaced to not collide
|
/// [Variable2::key](crate::prelude::Variable2::key) should be namespaced to not collide
|
||||||
/// with other implementations' keys. E.g. A core called
|
/// with other implementations' keys. E.g. A core called
|
||||||
/// 'foo' should use keys named as 'foo_option'.
|
/// 'foo' should use keys named as 'foo_option'.
|
||||||
///
|
///
|
||||||
/// [crate::prelude::Variable2::description] should contain a human readable
|
/// [Variable2::description](crate::prelude::Variable2::description) should contain a human readable
|
||||||
/// description of the key.
|
/// description of the key.
|
||||||
///
|
///
|
||||||
/// [crate::prelude::Variable2::options] should contain the list of expected values.
|
/// [Variable2::options](crate::prelude::Variable2::options) should contain the list of expected values.
|
||||||
/// The number of possible options should be very limited,
|
/// The number of possible options should be very limited,
|
||||||
/// i.e. it should be feasible to cycle through options
|
/// i.e. it should be feasible to cycle through options
|
||||||
/// without a keyboard. The first entry should be treated as a default.
|
/// without a keyboard. The first entry should be treated as a default.
|
||||||
|
@ -169,9 +176,9 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Variables should be queried with [Self::get_variable].
|
/// Variables should be queried with [Self::get_variable].
|
||||||
fn get_variable_update(&mut self) -> Option<bool> { None }
|
fn get_variable_update(&mut self) -> Option<bool> { None }
|
||||||
/// If true, the libretro implementation supports calls to
|
/// If true, the libretro implementation supports calls to
|
||||||
/// [libretro_sys::CoreAPI::retro_load_game] with NULL as argument.
|
/// [CoreAPI::retro_load_game] with NULL as argument.
|
||||||
/// Used by cores which can run without particular game data.
|
/// Used by cores which can run without particular game data.
|
||||||
/// This should be called within [libretro_sys::CoreAPI::retro_set_environment] only.
|
/// This should be called within [CoreAPI::retro_set_environment] only.
|
||||||
fn set_support_no_game(&mut self, supports_no_game: bool) -> Option<bool> { None }
|
fn set_support_no_game(&mut self, supports_no_game: bool) -> Option<bool> { None }
|
||||||
/// Retrieves the absolute path from where this libretro
|
/// Retrieves the absolute path from where this libretro
|
||||||
/// implementation was loaded.
|
/// implementation was loaded.
|
||||||
|
@ -186,7 +193,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Devices which are not handled or recognized always return
|
/// Devices which are not handled or recognized always return
|
||||||
/// 0 in [Self::input_state].
|
/// 0 in [Self::input_state].
|
||||||
/// Example bitmask: caps = (1 << [libretro_sys::DEVICE_JOYPAD]) | (1 << [libretro_sys::DEVICE_ANALOG]).
|
/// Example bitmask: caps = (1 << [libretro_sys::DEVICE_JOYPAD]) | (1 << [libretro_sys::DEVICE_ANALOG]).
|
||||||
/// Should only be called in [libretro_sys::CoreAPI::retro_run].
|
/// Should only be called in [CoreAPI::retro_run].
|
||||||
fn get_input_device_capabilities(&mut self) -> Option<u64> { None }
|
fn get_input_device_capabilities(&mut self) -> Option<u64> { None }
|
||||||
/// Returns the "core assets" directory of the frontend.
|
/// Returns the "core assets" directory of the frontend.
|
||||||
/// This directory can be used to store specific assets that the
|
/// This directory can be used to store specific assets that the
|
||||||
|
@ -199,7 +206,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Returns the "save" directory of the frontend, unless there is no
|
/// Returns the "save" directory of the frontend, unless there is no
|
||||||
/// save directory available. The save directory should be used to
|
/// save directory available. The save directory should be used to
|
||||||
/// store SRAM, memory cards, high scores, etc, if the libretro core
|
/// store SRAM, memory cards, high scores, etc, if the libretro core
|
||||||
/// cannot use the regular memory interface ([libretro_sys::CoreAPI::retro_get_memory_data]).
|
/// cannot use the regular memory interface ([CoreAPI::retro_get_memory_data]).
|
||||||
///
|
///
|
||||||
/// If the frontend cannot designate a save directory, it will return
|
/// If the frontend cannot designate a save directory, it will return
|
||||||
/// `None` to indicate that the core should attempt to operate without a
|
/// `None` to indicate that the core should attempt to operate without a
|
||||||
|
@ -210,7 +217,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// [Self::get_system_directory].
|
/// [Self::get_system_directory].
|
||||||
fn get_save_directory(&mut self) -> Option<PathBuf> { None }
|
fn get_save_directory(&mut self) -> Option<PathBuf> { None }
|
||||||
/// Sets a new av_info structure. This can only be called from
|
/// Sets a new av_info structure. This can only be called from
|
||||||
/// within [libretro_sys::CoreAPI::retro_run].
|
/// within [CoreAPI::retro_run].
|
||||||
/// This should *only* be used if the core is completely altering the
|
/// This should *only* be used if the core is completely altering the
|
||||||
/// internal resolutions, aspect ratios, timings, sampling rate, etc.
|
/// internal resolutions, aspect ratios, timings, sampling rate, etc.
|
||||||
/// Calling this can require a full reinitialization of video/audio
|
/// Calling this can require a full reinitialization of video/audio
|
||||||
|
@ -220,7 +227,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// the users explicit consent.
|
/// the users explicit consent.
|
||||||
/// An eventual driver reinitialize will happen so that video and
|
/// An eventual driver reinitialize will happen so that video and
|
||||||
/// audio callbacks
|
/// audio callbacks
|
||||||
/// happening after this call within the same [libretro_sys::CoreAPI::retro_run] call will
|
/// happening after this call within the same [CoreAPI::retro_run] call will
|
||||||
/// target the newly initialized driver.
|
/// target the newly initialized driver.
|
||||||
///
|
///
|
||||||
/// This callback makes it possible to support configurable resolutions
|
/// This callback makes it possible to support configurable resolutions
|
||||||
|
@ -232,7 +239,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// expected to be a temporary change, for the reasons of possible
|
/// expected to be a temporary change, for the reasons of possible
|
||||||
/// driver reinitialization.
|
/// driver reinitialization.
|
||||||
/// This call is not a free pass for not trying to provide
|
/// This call is not a free pass for not trying to provide
|
||||||
/// correct values in [libretro_sys::CoreAPI::retro_get_system_av_info]. If you need to change
|
/// correct values in [CoreAPI::retro_get_system_av_info]. If you need to change
|
||||||
/// things like aspect ratio or nominal width/height,
|
/// things like aspect ratio or nominal width/height,
|
||||||
/// use [Self::set_geometry], which is a softer variant
|
/// use [Self::set_geometry], which is a softer variant
|
||||||
/// of [Self::set_system_av_info].
|
/// of [Self::set_system_av_info].
|
||||||
|
@ -248,16 +255,16 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// It can also be used to pick among subsystems in an explicit way
|
/// It can also be used to pick among subsystems in an explicit way
|
||||||
/// if the libretro implementation is a multi-system emulator itself.
|
/// if the libretro implementation is a multi-system emulator itself.
|
||||||
///
|
///
|
||||||
/// Loading a game via a subsystem is done with [libretro_sys::CoreAPI::retro_load_game_special],
|
/// Loading a game via a subsystem is done with [CoreAPI::retro_load_game_special],
|
||||||
/// and this environment call allows a libretro core to expose which
|
/// and this environment call allows a libretro core to expose which
|
||||||
/// subsystems are supported for use with [libretro_sys::CoreAPI::retro_load_game_special].
|
/// subsystems are supported for use with [CoreAPI::retro_load_game_special].
|
||||||
///
|
///
|
||||||
/// If a core wants to expose this interface, [Self::set_subsystem_info]
|
/// If a core wants to expose this interface, [Self::set_subsystem_info]
|
||||||
/// **MUST** be called from within [libretro_sys::CoreAPI::retro_set_environment].
|
/// **MUST** be called from within [CoreAPI::retro_set_environment].
|
||||||
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> { None }
|
fn set_subsystem_info(&mut self, subsystem_info: &Vec<SubsystemInfo2>) -> Option<bool> { None }
|
||||||
/// This environment call lets a libretro core tell the frontend
|
/// This environment call lets a libretro core tell the frontend
|
||||||
/// which controller subclasses are recognized in calls to
|
/// which controller subclasses are recognized in calls to
|
||||||
/// [libretro_sys::CoreAPI::retro_set_controller_port_device].
|
/// [CoreAPI::retro_set_controller_port_device].
|
||||||
///
|
///
|
||||||
/// Some emulators such as Super Nintendo support multiple lightgun
|
/// Some emulators such as Super Nintendo support multiple lightgun
|
||||||
/// types which must be specifically selected from. It is therefore
|
/// types which must be specifically selected from. It is therefore
|
||||||
|
@ -269,9 +276,9 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// they must be defined as a specialized subclass of the generic device
|
/// they must be defined as a specialized subclass of the generic device
|
||||||
/// types already defined in the libretro API.
|
/// types already defined in the libretro API.
|
||||||
///
|
///
|
||||||
/// The core must pass an array of [crate::prelude::ControllerDescription2]. Each element of the
|
/// The core must pass an array of [ControllerDescription2](crate::prelude::ControllerDescription2). Each element of the
|
||||||
/// array corresponds to the ascending port index
|
/// array corresponds to the ascending port index
|
||||||
/// that is passed to [libretro_sys::CoreAPI::retro_set_controller_port_device] when that function
|
/// that is passed to [CoreAPI::retro_set_controller_port_device] when that function
|
||||||
/// is called to indicate to the core that the frontend has changed the
|
/// is called to indicate to the core that the frontend has changed the
|
||||||
/// active device subclass.
|
/// active device subclass.
|
||||||
///
|
///
|
||||||
|
@ -284,7 +291,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// codes of all device subclasses that are available for the corresponding
|
/// codes of all device subclasses that are available for the corresponding
|
||||||
/// User or Player, beginning with the generic Libretro device that the
|
/// User or Player, beginning with the generic Libretro device that the
|
||||||
/// subclasses are derived from. The second inner element of each entry is the
|
/// subclasses are derived from. The second inner element of each entry is the
|
||||||
/// total number of subclasses that are listed in the [crate::prelude::ControllerDescription2].
|
/// total number of subclasses that are listed in the [ControllerDescription2](crate::prelude::ControllerDescription2).
|
||||||
///
|
///
|
||||||
/// NOTE: Even if special device types are set in the libretro core,
|
/// NOTE: Even if special device types are set in the libretro core,
|
||||||
/// libretro should only poll input based on the base input device types.
|
/// libretro should only poll input based on the base input device types.
|
||||||
|
@ -296,15 +303,15 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Should only be used by emulators; it doesn't make much sense for
|
/// Should only be used by emulators; it doesn't make much sense for
|
||||||
/// anything else.
|
/// anything else.
|
||||||
/// It is recommended to expose all relevant pointers through
|
/// It is recommended to expose all relevant pointers through
|
||||||
/// [libretro_sys::CoreAPI::retro_get_memory_data] and
|
/// [CoreAPI::retro_get_memory_data] and
|
||||||
/// [libretro_sys::CoreAPI::retro_get_memory_size] as well.
|
/// [CoreAPI::retro_get_memory_size] as well.
|
||||||
///
|
///
|
||||||
/// Can be called from [libretro_sys::CoreAPI::retro_init] and [libretro_sys::CoreAPI::retro_load_game].
|
/// Can be called from [CoreAPI::retro_init] and [CoreAPI::retro_load_game].
|
||||||
fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option<bool> { None }
|
fn set_memory_maps(&mut self, memory_map: &MemoryMap) -> Option<bool> { None }
|
||||||
/// This environment call is similar to [Self::set_system_av_info] for changing
|
/// This environment call is similar to [Self::set_system_av_info] for changing
|
||||||
/// video parameters, but provides a guarantee that drivers will not be
|
/// video parameters, but provides a guarantee that drivers will not be
|
||||||
/// reinitialized.
|
/// reinitialized.
|
||||||
/// This can only be called from within [libretro_sys::CoreAPI::retro_run].
|
/// This can only be called from within [CoreAPI::retro_run].
|
||||||
///
|
///
|
||||||
/// The purpose of this call is to allow a core to alter nominal
|
/// The purpose of this call is to allow a core to alter nominal
|
||||||
/// width/heights as well as aspect ratios on-the-fly, which can be
|
/// width/heights as well as aspect ratios on-the-fly, which can be
|
||||||
|
@ -339,7 +346,7 @@ pub trait RetroCallbacks: Unpin + 'static {
|
||||||
/// Strength has a range of \[0, 0xffff\].
|
/// Strength has a range of \[0, 0xffff\].
|
||||||
///
|
///
|
||||||
/// Returns true if rumble state request was honored.
|
/// Returns true if rumble state request was honored.
|
||||||
/// Calling this before first [libretro_sys::CoreAPI::retro_run] is likely to return false.
|
/// Calling this before first [CoreAPI::retro_run] is likely to return false.
|
||||||
fn set_rumble_state(&mut self, port: c_uint, effect: RumbleEffect, strength: u16) -> bool { false }
|
fn set_rumble_state(&mut self, port: c_uint, effect: RumbleEffect, strength: u16) -> bool { false }
|
||||||
/// Returns current time in microseconds.
|
/// Returns current time in microseconds.
|
||||||
/// Tries to use the most accurate timer available.
|
/// Tries to use the most accurate timer available.
|
||||||
|
@ -379,9 +386,15 @@ pub trait LibretroWrapperAccess {
|
||||||
pub trait RootRetroCallbacks : RetroCallbacks + LibretroWrapperAccess {}
|
pub trait RootRetroCallbacks : RetroCallbacks + LibretroWrapperAccess {}
|
||||||
impl<T: RetroCallbacks + LibretroWrapperAccess> RootRetroCallbacks for T {}
|
impl<T: RetroCallbacks + LibretroWrapperAccess> RootRetroCallbacks for T {}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct StaticCallbacks {
|
struct StaticCallbacks {
|
||||||
handler: Option<Pin<&'static mut dyn RootRetroCallbacks>>,
|
handler: Option<Pin<&'static mut dyn RootRetroCallbacks>>,
|
||||||
|
pix_fmt: PixelFormat,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for StaticCallbacks {
|
||||||
|
fn default() -> Self {
|
||||||
|
StaticCallbacks { handler: None, pix_fmt: PixelFormat::ARGB1555 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for StaticCallbacks {}
|
unsafe impl Sync for StaticCallbacks {}
|
||||||
|
@ -448,7 +461,9 @@ impl StaticCallbacks {
|
||||||
Self::path_into_void(data, handler.get_system_directory()?)?
|
Self::path_into_void(data, handler.get_system_directory()?)?
|
||||||
}
|
}
|
||||||
EnvCmd::SetPixelFormat => {
|
EnvCmd::SetPixelFormat => {
|
||||||
handler.set_pixel_format(PixelFormat::from_uint(*Self::from_void(data)?)?)?
|
let format = PixelFormat::from_uint(*Self::from_void(data)?)?;
|
||||||
|
unsafe { CB_SINGLETON.pix_fmt = format };
|
||||||
|
handler.set_pixel_format(format)?
|
||||||
}
|
}
|
||||||
EnvCmd::SetInputDescriptors => {
|
EnvCmd::SetInputDescriptors => {
|
||||||
let mut input_desc = data as *const InputDescriptor;
|
let mut input_desc = data as *const InputDescriptor;
|
||||||
|
@ -632,21 +647,36 @@ impl StaticCallbacks {
|
||||||
) {
|
) {
|
||||||
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
||||||
const NULL: *const c_void = std::ptr::null();
|
const NULL: *const c_void = std::ptr::null();
|
||||||
match data {
|
let frame = match data {
|
||||||
NULL => cb.video_refresh_dupe(width, height, pitch as c_uint),
|
NULL => VideoFrame::Duplicate { width, height, pitch_u8: pitch },
|
||||||
HW_FRAME_BUFFER_VALID => cb.video_refresh_hw(width, height),
|
HW_FRAME_BUFFER_VALID => VideoFrame::HardwareRender { width, height },
|
||||||
data => {
|
ptr => match unsafe { CB_SINGLETON.pix_fmt } {
|
||||||
let data = data as *const u8;
|
PixelFormat::ARGB1555 => {
|
||||||
|
let pitch = pitch / size_of::<u16>();
|
||||||
let len = pitch * (height as usize);
|
let len = pitch * (height as usize);
|
||||||
let slice = unsafe { from_raw_parts(data, len) };
|
let data = unsafe { from_raw_parts(ptr as *const u16, len) };
|
||||||
cb.video_refresh(slice, width, height, pitch as c_uint);
|
VideoFrame::XRGB1555 { data, width, height, pitch_u16: pitch }
|
||||||
|
}
|
||||||
|
PixelFormat::RGB565 => {
|
||||||
|
let pitch = pitch / size_of::<u16>();
|
||||||
|
let len = pitch * (height as usize);
|
||||||
|
let data = unsafe { from_raw_parts(ptr as *const u16, len) };
|
||||||
|
VideoFrame::RGB565 { data, width, height, pitch_u16: pitch }
|
||||||
|
}
|
||||||
|
PixelFormat::ARGB8888 => {
|
||||||
|
let pitch = pitch / size_of::<u32>();
|
||||||
|
let len = pitch * (height as usize);
|
||||||
|
let data = unsafe { from_raw_parts(ptr as *const u32, len) };
|
||||||
|
VideoFrame::XRGB8888 { data, width, height, pitch_u32: pitch }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
cb.video_refresh(&frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
extern "C" fn audio_sample_cb(left: i16, right: i16) {
|
extern "C" fn audio_sample_cb(left: i16, right: i16) {
|
||||||
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
if let Some(cb) = unsafe { CB_SINGLETON.handler.as_mut() } {
|
||||||
cb.audio_sample(left, right);
|
cb.audio_samples(&[left, right]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
extern "C" fn audio_sample_batch_cb(data: *const i16, frames: usize) -> usize {
|
extern "C" fn audio_sample_batch_cb(data: *const i16, frames: usize) -> usize {
|
||||||
|
@ -655,9 +685,13 @@ impl StaticCallbacks {
|
||||||
Some(cb) => match data.is_null() {
|
Some(cb) => match data.is_null() {
|
||||||
true => 0,
|
true => 0,
|
||||||
false => {
|
false => {
|
||||||
let len = frames * 2; // stereo
|
// paraLLEl-n64 sometimes gives us garbage here during initialization
|
||||||
let result = cb.audio_sample_batch(from_raw_parts(data, len));
|
let (len, over) = frames.overflowing_mul(2); // stereo
|
||||||
result / 2
|
if over {
|
||||||
|
frames
|
||||||
|
} else {
|
||||||
|
cb.audio_samples(from_raw_parts(data, len))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => 0,
|
None => 0,
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
name = "ferretro_components"
|
name = "ferretro_components"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["lifning <lifning+git@pm.me>", "viv <vvnl+git@protonmail.com>"]
|
authors = ["lifning <lifning+git@pm.me>", "viv <vvnl+git@protonmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cc = "^1"
|
cc = "^1"
|
||||||
|
|
|
@ -34,44 +34,46 @@ struct Opt {
|
||||||
video: Option<PathBuf>,
|
video: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let opt: Opt = Opt::from_args();
|
let opt: Opt = Opt::from_args();
|
||||||
|
|
||||||
let mut emu = RetroComponentBase::new(&opt.core);
|
let mut emu = RetroComponentBase::new(&opt.core);
|
||||||
|
|
||||||
let mut sdl_context = sdl2::init().unwrap();
|
let mut sdl_context = sdl2::init()?;
|
||||||
|
|
||||||
emu.register_component(StderrLogComponent { prefix: "{log} ".to_string() });
|
emu.register_component(StderrLogComponent { prefix: "{log} ".to_string() })?;
|
||||||
|
|
||||||
let sdl2_ogl = SimpleSdl2OpenglComponent::new(&mut sdl_context, emu.libretro_core()).unwrap();
|
// must register before opengl so it can have priority in queries about what N64 plugin to use
|
||||||
emu.register_component(sdl2_ogl);
|
// (only supports software-rendered 2D frames currently)
|
||||||
|
if let Some(video) = opt.video {
|
||||||
|
ffmpeg::log::set_level(ffmpeg::log::Level::Info);
|
||||||
|
ffmpeg::init()?;
|
||||||
|
let ffmpeg_comp = FfmpegComponent::new(emu.libretro_core(), video);
|
||||||
|
emu.register_component(ffmpeg_comp)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sdl2_ogl = SimpleSdl2OpenglComponent::new(&mut sdl_context, emu.libretro_core())?;
|
||||||
|
emu.register_component(sdl2_ogl)?;
|
||||||
|
|
||||||
let sdl2_audio = SimpleSdl2AudioComponent::new(&mut sdl_context, emu.libretro_core());
|
let sdl2_audio = SimpleSdl2AudioComponent::new(&mut sdl_context, emu.libretro_core());
|
||||||
emu.register_component(sdl2_audio);
|
emu.register_component(sdl2_audio)?;
|
||||||
|
|
||||||
emu.register_component(SimpleSdl2GamepadComponent::new(&mut sdl_context));
|
emu.register_component(SimpleSdl2GamepadComponent::new(&mut sdl_context))?;
|
||||||
|
|
||||||
let sleep_fps = SleepFramerateLimitComponent::new(emu.libretro_core());
|
let sleep_fps = SleepFramerateLimitComponent::new(emu.libretro_core());
|
||||||
emu.register_component(sleep_fps);
|
emu.register_component(sleep_fps)?;
|
||||||
|
|
||||||
emu.register_component(PathBufComponent {
|
emu.register_component(PathBufComponent {
|
||||||
sys_path: opt.system.clone(),
|
sys_path: opt.system.clone(),
|
||||||
libretro_path: Some(opt.core.to_path_buf()),
|
libretro_path: Some(opt.core.to_path_buf()),
|
||||||
core_assets_path: None,
|
core_assets_path: None,
|
||||||
save_path: Some(std::env::temp_dir()),
|
save_path: Some(std::env::temp_dir()),
|
||||||
});
|
})?;
|
||||||
|
|
||||||
if let Some(video) = opt.video {
|
emu.init()?;
|
||||||
ffmpeg::log::set_level(ffmpeg::log::Level::Info);
|
emu.load_game(&opt.rom)?;
|
||||||
ffmpeg::init().unwrap();
|
|
||||||
let ffmpeg_comp = FfmpegComponent::new(emu.libretro_core(), video);
|
|
||||||
emu.register_component(ffmpeg_comp);
|
|
||||||
}
|
|
||||||
|
|
||||||
emu.init().unwrap();
|
|
||||||
emu.load_game(&opt.rom).unwrap();
|
|
||||||
if let Some(state) = opt.state {
|
if let Some(state) = opt.state {
|
||||||
emu.unserialize_path(state).unwrap();
|
emu.unserialize_path(state)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut frame = 0;
|
let mut frame = 0;
|
||||||
|
@ -80,4 +82,5 @@ pub fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("Ran for {} frames.", frame);
|
eprintln!("Ran for {} frames.", frame);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -227,33 +227,15 @@ impl LibretroWrapperAccess for RetroComponentBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RetroCallbacks for RetroComponentBase {
|
impl RetroCallbacks for RetroComponentBase {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
for comp in &mut self.components {
|
for comp in &mut self.components {
|
||||||
comp.video_refresh(data, width, height, pitch);
|
comp.video_refresh(frame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint) {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
for comp in &mut self.components {
|
|
||||||
comp.video_refresh_dupe(width, height, pitch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn video_refresh_hw(&mut self, width: c_uint, height: c_uint) {
|
|
||||||
for comp in &mut self.components {
|
|
||||||
comp.video_refresh_hw(width, height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
|
||||||
for comp in &mut self.components {
|
|
||||||
comp.audio_sample(left, right);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
||||||
self.components.iter_mut()
|
self.components.iter_mut()
|
||||||
.map(|comp| comp.audio_sample_batch(stereo_pcm))
|
.map(|comp| comp.audio_samples(stereo_pcm))
|
||||||
.max()
|
.max()
|
||||||
.unwrap_or_default()
|
.unwrap_or_default()
|
||||||
}
|
}
|
||||||
|
|
|
@ -350,7 +350,7 @@ static bool ffmpeg_init_config(struct ff_config_param *params,
|
||||||
|
|
||||||
pub fn end(&mut self) {
|
pub fn end(&mut self) {
|
||||||
let mut packet = Packet::empty();
|
let mut packet = Packet::empty();
|
||||||
eprintln!("flushed: {:?}", self.video_encoder.flush(&mut packet).unwrap());
|
eprintln!("flushed: {:?}", self.video_encoder.receive_packet(&mut packet).unwrap());
|
||||||
|
|
||||||
self.video_encoder.send_eof().unwrap();
|
self.video_encoder.send_eof().unwrap();
|
||||||
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
self.receive_and_write_packets(EncoderToWriteFrom::Video);
|
||||||
|
@ -367,17 +367,21 @@ impl Drop for FfmpegComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RetroCallbacks for FfmpegComponent {
|
impl RetroCallbacks for FfmpegComponent {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
let mut vframe = frame::Video::new(self.video_pixel_format, width, height);
|
match frame {
|
||||||
|
VideoFrame::XRGB1555 { width, height, .. }
|
||||||
|
| VideoFrame::RGB565 { width, height, .. }
|
||||||
|
| VideoFrame::XRGB8888 { width, height, .. } => {
|
||||||
|
let (data, pitch) = frame.data_pitch_as_bytes().unwrap();
|
||||||
|
|
||||||
|
let mut vframe = frame::Video::new(self.video_pixel_format, *width, *height);
|
||||||
|
|
||||||
let stride = vframe.stride(0);
|
let stride = vframe.stride(0);
|
||||||
let pitch = pitch as usize;
|
|
||||||
|
|
||||||
let vplane = vframe.data_mut(0);
|
let vplane = vframe.data_mut(0);
|
||||||
if data.len() == vplane.len() && pitch == stride {
|
if data.len() == vplane.len() && pitch == stride {
|
||||||
vplane.copy_from_slice(&data);
|
vplane.copy_from_slice(&data);
|
||||||
} else {
|
} else {
|
||||||
for y in 0..(height as usize) {
|
for y in 0..(*height as usize) {
|
||||||
let ffbegin = y * stride;
|
let ffbegin = y * stride;
|
||||||
let lrbegin = y * pitch;
|
let lrbegin = y * pitch;
|
||||||
let min = usize::min(stride, pitch);
|
let min = usize::min(stride, pitch);
|
||||||
|
@ -392,25 +396,23 @@ impl RetroCallbacks for FfmpegComponent {
|
||||||
self.prev_video_frame.replace(vframe.clone());
|
self.prev_video_frame.replace(vframe.clone());
|
||||||
self.video_frames.push_back(vframe);
|
self.video_frames.push_back(vframe);
|
||||||
}
|
}
|
||||||
|
VideoFrame::Duplicate { width, height, .. } => {
|
||||||
fn video_refresh_dupe(&mut self, width: u32, height: u32, _pitch: u32) {
|
if let Some(prev_frame) = &self.prev_video_frame {
|
||||||
if let Some(frame) = &self.prev_video_frame {
|
self.video_frames.push_back(prev_frame.clone());
|
||||||
self.video_frames.push_back(frame.clone());
|
|
||||||
} else {
|
} else {
|
||||||
let vframe = frame::Video::new(self.video_pixel_format, width, height);
|
let vframe = frame::Video::new(self.video_pixel_format, *width, *height);
|
||||||
self.video_frames.push_back(vframe);
|
self.video_frames.push_back(vframe);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
VideoFrame::HardwareRender { .. } => {}
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
}
|
||||||
self.audio_buf.push((left, right));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
let left_iter = stereo_pcm.iter().step_by(2).cloned();
|
let left_iter = stereo_pcm.iter().step_by(2).cloned();
|
||||||
let right_iter = stereo_pcm.iter().skip(1).step_by(2).cloned();
|
let right_iter = stereo_pcm.iter().skip(1).step_by(2).cloned();
|
||||||
self.audio_buf.extend(Iterator::zip(left_iter, right_iter));
|
self.audio_buf.extend(Iterator::zip(left_iter, right_iter));
|
||||||
stereo_pcm.len()
|
stereo_pcm.len() / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
|
fn set_pixel_format(&mut self, format: PixelFormat) -> Option<bool> {
|
||||||
|
|
|
@ -24,6 +24,7 @@ impl AudioCallback for MySdlAudio {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Trivially sends the core's audio data to the SDL audio subsystem for playback.
|
||||||
pub struct SimpleSdl2AudioComponent {
|
pub struct SimpleSdl2AudioComponent {
|
||||||
sample_rate: f64,
|
sample_rate: f64,
|
||||||
audio_buffer: Vec<i16>,
|
audio_buffer: Vec<i16>,
|
||||||
|
@ -33,16 +34,10 @@ pub struct SimpleSdl2AudioComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RetroCallbacks for SimpleSdl2AudioComponent {
|
impl RetroCallbacks for SimpleSdl2AudioComponent {
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
self.audio_buffer.push(left);
|
|
||||||
self.audio_buffer.push(right);
|
|
||||||
self.send_audio_samples()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
||||||
self.audio_buffer.extend(stereo_pcm);
|
self.audio_buffer.extend(stereo_pcm);
|
||||||
self.send_audio_samples();
|
self.send_audio_samples();
|
||||||
stereo_pcm.len()
|
stereo_pcm.len() / 2
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_system_av_info(&mut self, av_info: &SystemAvInfo) -> Option<bool> {
|
fn set_system_av_info(&mut self, av_info: &SystemAvInfo) -> Option<bool> {
|
||||||
|
|
|
@ -6,6 +6,10 @@ use sdl2::Sdl;
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
use sdl2::render::WindowCanvas;
|
use sdl2::render::WindowCanvas;
|
||||||
|
|
||||||
|
/// Creates a root window with SDL2, then displays each 2D video frame provided by the core
|
||||||
|
/// by converting it to a [sdl2::render::Texture] and copying it to the window's canvas.
|
||||||
|
///
|
||||||
|
/// This component has no public interface and manages the SDL2 window on its own.
|
||||||
pub struct SimpleSdl2CanvasComponent {
|
pub struct SimpleSdl2CanvasComponent {
|
||||||
canvas: WindowCanvas,
|
canvas: WindowCanvas,
|
||||||
pixel_format: sdl2::pixels::PixelFormatEnum,
|
pixel_format: sdl2::pixels::PixelFormatEnum,
|
||||||
|
@ -38,20 +42,28 @@ impl SimpleSdl2CanvasComponent {
|
||||||
|
|
||||||
impl RetroComponent for SimpleSdl2CanvasComponent {}
|
impl RetroComponent for SimpleSdl2CanvasComponent {}
|
||||||
impl RetroCallbacks for SimpleSdl2CanvasComponent {
|
impl RetroCallbacks for SimpleSdl2CanvasComponent {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
let rect = Rect::new(0, 0, width, height);
|
match frame {
|
||||||
|
VideoFrame::XRGB1555 { width, height, .. }
|
||||||
|
| VideoFrame::RGB565 { width, height, .. }
|
||||||
|
| VideoFrame::XRGB8888 { width, height, .. } => {
|
||||||
|
let rect = Rect::new(0, 0, *width, *height);
|
||||||
|
|
||||||
if let Ok(mut tex) = self.canvas
|
if let Ok(mut tex) = self.canvas
|
||||||
.texture_creator()
|
.texture_creator()
|
||||||
.create_texture_static(self.pixel_format, width, height)
|
.create_texture_static(self.pixel_format, *width, *height)
|
||||||
{
|
{
|
||||||
if tex.update(rect, data, pitch as usize).is_ok() {
|
let (pixel_data, pitch) = frame.data_pitch_as_bytes().unwrap();
|
||||||
|
if tex.update(rect, pixel_data, pitch).is_ok() {
|
||||||
self.canvas.clear();
|
self.canvas.clear();
|
||||||
self.canvas.copy(&tex, None, None).unwrap();
|
self.canvas.copy(&tex, None, None).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.canvas.present();
|
self.canvas.present();
|
||||||
}
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
|
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
|
||||||
self.pixel_format = match pix_fmt {
|
self.pixel_format = match pix_fmt {
|
||||||
|
|
|
@ -9,12 +9,24 @@ use sdl2::controller::{Axis, Button, GameController};
|
||||||
use sdl2::event::Event;
|
use sdl2::event::Event;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
|
|
||||||
|
/// Trivially maps the "RetroPad" layout to the SDL_GameController API.
|
||||||
|
///
|
||||||
|
/// NOTE: This component is intended for exceedingly simple use-cases, and will *own* and
|
||||||
|
/// process an [sdl2::EventPump] if one hasn't been claimed from [sdl2::Sdl::event_pump]
|
||||||
|
/// at the time of the component's creation with [SimpleSdl2GamepadComponent::new].
|
||||||
|
///
|
||||||
|
/// This means *if you need to manage your own events*, you must
|
||||||
|
/// *instantiate your own [sdl2::EventPump] before constructing this component*.
|
||||||
|
/// (If you do this, you are of course responsible for pumping the event queue yourself.)
|
||||||
|
///
|
||||||
|
/// It opens all connected controllers recognized by the [sdl2::Sdl] context and presents them
|
||||||
|
/// to the core in the order provided by the OS. Port and button remapping are not supported.
|
||||||
pub struct SimpleSdl2GamepadComponent {
|
pub struct SimpleSdl2GamepadComponent {
|
||||||
preferred_pad: Option<u32>,
|
preferred_pad: Option<u32>,
|
||||||
|
|
||||||
gamepad_subsys: sdl2::GameControllerSubsystem,
|
gamepad_subsys: sdl2::GameControllerSubsystem,
|
||||||
gamepads: Vec<GameController>,
|
gamepads: Vec<GameController>,
|
||||||
event_pump: sdl2::EventPump,
|
event_pump: Option<sdl2::EventPump>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RetroCallbacks for SimpleSdl2GamepadComponent {
|
impl RetroCallbacks for SimpleSdl2GamepadComponent {
|
||||||
|
@ -71,7 +83,8 @@ impl RetroCallbacks for SimpleSdl2GamepadComponent {
|
||||||
|
|
||||||
impl RetroComponent for SimpleSdl2GamepadComponent {
|
impl RetroComponent for SimpleSdl2GamepadComponent {
|
||||||
fn pre_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
|
fn pre_run(&mut self, _retro: &mut LibretroWrapper) -> ControlFlow {
|
||||||
for event in self.event_pump.poll_iter() {
|
if let Some(pump) = self.event_pump.as_mut() {
|
||||||
|
for event in pump.poll_iter() {
|
||||||
match event {
|
match event {
|
||||||
Event::Quit { .. }
|
Event::Quit { .. }
|
||||||
| Event::KeyDown {
|
| Event::KeyDown {
|
||||||
|
@ -81,6 +94,7 @@ impl RetroComponent for SimpleSdl2GamepadComponent {
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
ControlFlow::Continue
|
ControlFlow::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +116,7 @@ impl SimpleSdl2GamepadComponent {
|
||||||
gamepads.extend(gamepad_subsys.open(i).into_iter());
|
gamepads.extend(gamepad_subsys.open(i).into_iter());
|
||||||
}
|
}
|
||||||
|
|
||||||
let event_pump = sdl_context.event_pump().unwrap();
|
let event_pump = sdl_context.event_pump().ok();
|
||||||
|
|
||||||
SimpleSdl2GamepadComponent {
|
SimpleSdl2GamepadComponent {
|
||||||
preferred_pad: None,
|
preferred_pad: None,
|
||||||
|
|
|
@ -7,6 +7,14 @@ use sdl2::Sdl;
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
use sdl2::render::WindowCanvas;
|
use sdl2::render::WindowCanvas;
|
||||||
|
|
||||||
|
/// Uses SDL2 to create a root window with an OpenGL context and attaches libretro's
|
||||||
|
/// `hw_get_proc_address` calls to that of the [sdl2::VideoSubsystem].
|
||||||
|
///
|
||||||
|
/// If the core provides 2D framebuffer data to the component, it will simply display it with a
|
||||||
|
/// (GL-accelerated) [sdl2::render::Texture], just as
|
||||||
|
/// [SimpleSdl2CanvasComponent](super::canvas::SimpleSdl2CanvasComponent) does.
|
||||||
|
///
|
||||||
|
/// This component has no public interface and manages the SDL2 window on its own.
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
pub struct SimpleSdl2OpenglComponent {
|
pub struct SimpleSdl2OpenglComponent {
|
||||||
canvas: WindowCanvas,
|
canvas: WindowCanvas,
|
||||||
|
@ -84,25 +92,31 @@ impl SimpleSdl2OpenglComponent {
|
||||||
|
|
||||||
impl RetroComponent for SimpleSdl2OpenglComponent {}
|
impl RetroComponent for SimpleSdl2OpenglComponent {}
|
||||||
impl RetroCallbacks for SimpleSdl2OpenglComponent {
|
impl RetroCallbacks for SimpleSdl2OpenglComponent {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
let rect = Rect::new(0, 0, width, height);
|
match frame {
|
||||||
|
VideoFrame::XRGB1555 { width, height, .. }
|
||||||
|
| VideoFrame::RGB565 { width, height, .. }
|
||||||
|
| VideoFrame::XRGB8888 { width, height, .. } => {
|
||||||
|
let rect = Rect::new(0, 0, *width, *height);
|
||||||
if let Ok(mut tex) = self.canvas
|
if let Ok(mut tex) = self.canvas
|
||||||
.texture_creator()
|
.texture_creator()
|
||||||
.create_texture_static(self.pixel_format, width, height)
|
.create_texture_static(self.pixel_format, *width, *height)
|
||||||
{
|
{
|
||||||
if tex.update(rect, data, pitch as usize).is_ok() {
|
let (pixel_data, pitch) = frame.data_pitch_as_bytes().unwrap();
|
||||||
|
if tex.update(rect, pixel_data, pitch).is_ok() {
|
||||||
self.canvas.clear();
|
self.canvas.clear();
|
||||||
self.canvas.copy(&tex, None, None).unwrap();
|
self.canvas.copy(&tex, None, None).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.canvas.present();
|
self.canvas.present();
|
||||||
}
|
}
|
||||||
|
VideoFrame::HardwareRender { .. } => {
|
||||||
fn video_refresh_hw(&mut self, _width: c_uint, _height: c_uint) {
|
|
||||||
self.canvas.present();
|
self.canvas.present();
|
||||||
unsafe { (self.glClear)(gl::COLOR_BUFFER_BIT); }
|
unsafe { (self.glClear)(gl::COLOR_BUFFER_BIT); }
|
||||||
}
|
}
|
||||||
|
VideoFrame::Duplicate { .. } => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
|
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
|
||||||
self.pixel_format = match pix_fmt {
|
self.pixel_format = match pix_fmt {
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::prelude::*;
|
||||||
|
|
||||||
use sdl2::surface::Surface;
|
use sdl2::surface::Surface;
|
||||||
|
|
||||||
|
/// Provides access to an [sdl2::surface::Surface] representing the core's most recent video frame.
|
||||||
pub struct Sdl2SurfaceComponent {
|
pub struct Sdl2SurfaceComponent {
|
||||||
surface: Surface<'static>,
|
surface: Surface<'static>,
|
||||||
pixel_format: sdl2::pixels::PixelFormatEnum,
|
pixel_format: sdl2::pixels::PixelFormatEnum,
|
||||||
|
@ -31,12 +32,27 @@ impl Sdl2SurfaceComponent {
|
||||||
|
|
||||||
impl RetroComponent for Sdl2SurfaceComponent {}
|
impl RetroComponent for Sdl2SurfaceComponent {}
|
||||||
impl RetroCallbacks for Sdl2SurfaceComponent {
|
impl RetroCallbacks for Sdl2SurfaceComponent {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: u32, height: u32, pitch: u32) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
|
match frame {
|
||||||
|
VideoFrame::XRGB1555 { width, height, .. }
|
||||||
|
| VideoFrame::RGB565 { width, height, .. }
|
||||||
|
| VideoFrame::XRGB8888 { width, height, .. } => {
|
||||||
|
// dirty, but must be &mut for SDL API.
|
||||||
|
// safe as long as we don't offer a &mut Surface in the API.
|
||||||
|
let (bytes, pitch) = frame.data_pitch_as_bytes().unwrap();
|
||||||
let data = unsafe {
|
let data = unsafe {
|
||||||
core::slice::from_raw_parts_mut(data.as_ptr() as *mut u8, data.len())
|
core::slice::from_raw_parts_mut(bytes.as_ptr() as *mut u8, bytes.len())
|
||||||
};
|
};
|
||||||
if let Ok(surf) = Surface::from_data(data, width, height, pitch, self.pixel_format) {
|
if let Ok(surf) = Surface::from_data(data, *width, *height, pitch as u32, self.pixel_format) {
|
||||||
self.surface = surf;
|
if self.surface.size() != (*width, *height) {
|
||||||
|
if let Ok(new) = Surface::new(*width, *height, self.pixel_format) {
|
||||||
|
self.surface = new;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let _ = surf.blit(None, &mut self.surface, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,20 +78,11 @@ pub struct StderrCallTraceComponent {
|
||||||
|
|
||||||
impl RetroComponent for StderrCallTraceComponent {}
|
impl RetroComponent for StderrCallTraceComponent {}
|
||||||
impl RetroCallbacks for StderrCallTraceComponent {
|
impl RetroCallbacks for StderrCallTraceComponent {
|
||||||
fn video_refresh(&mut self, data: &[u8], width: c_uint, height: c_uint, pitch: c_uint) {
|
fn video_refresh(&mut self, frame: &VideoFrame) {
|
||||||
eprintln!("{}video_refresh([u8; {}], {}, {}, {})", self.prefix, data.len(), width, height, pitch);
|
eprintln!("{}video_refresh({:?})", self.prefix, frame);
|
||||||
}
|
}
|
||||||
fn video_refresh_dupe(&mut self, width: c_uint, height: c_uint, pitch: c_uint) {
|
fn audio_samples(&mut self, stereo_pcm: &[i16]) -> usize {
|
||||||
eprintln!("{}video_refresh_dupe({}, {}, {})", self.prefix, width, height, pitch);
|
eprintln!("{}audio_samples([i16; {}])", self.prefix, stereo_pcm.len());
|
||||||
}
|
|
||||||
fn video_refresh_hw(&mut self, width: c_uint, height: c_uint) {
|
|
||||||
eprintln!("{}video_refresh_hw({}, {})", self.prefix, width, height);
|
|
||||||
}
|
|
||||||
fn audio_sample(&mut self, left: i16, right: i16) {
|
|
||||||
eprintln!("{}audio_sample({}, {})", self.prefix, left, right);
|
|
||||||
}
|
|
||||||
fn audio_sample_batch(&mut self, stereo_pcm: &[i16]) -> usize {
|
|
||||||
eprintln!("{}audio_sample_batch([i16; {}])", self.prefix, stereo_pcm.len());
|
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
fn input_poll(&mut self) {
|
fn input_poll(&mut self) {
|
||||||
|
@ -227,13 +218,7 @@ impl RetroComponent for SleepFramerateLimitComponent {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl RetroCallbacks for SleepFramerateLimitComponent {
|
impl RetroCallbacks for SleepFramerateLimitComponent {
|
||||||
fn video_refresh(&mut self, _data: &[u8], _width: c_uint, _height: c_uint, _pitch: c_uint) {
|
fn video_refresh(&mut self, _frame: &VideoFrame) {
|
||||||
self.do_sleep();
|
|
||||||
}
|
|
||||||
fn video_refresh_dupe(&mut self, _width: c_uint, _height: c_uint, _pitch: c_uint) {
|
|
||||||
self.do_sleep();
|
|
||||||
}
|
|
||||||
fn video_refresh_hw(&mut self, _width: c_uint, _height: c_uint) {
|
|
||||||
self.do_sleep();
|
self.do_sleep();
|
||||||
}
|
}
|
||||||
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
|
fn set_system_av_info(&mut self, system_av_info: &SystemAvInfo) -> Option<bool> {
|
||||||
|
|
Loading…
Reference in New Issue