ferretro/ferretro_components/src/provided/sdl2/surface.rs

71 lines
2.5 KiB
Rust

use crate::prelude::*;
use sdl2::surface::Surface;
/// Provides access to an [sdl2::surface::Surface] representing the core's most recent video frame.
pub struct Sdl2SurfaceComponent {
surface: Surface<'static>,
pixel_format: sdl2::pixels::PixelFormatEnum,
}
impl Sdl2SurfaceComponent {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
let pixel_format = sdl2::pixels::PixelFormatEnum::ARGB1555;
// automatically replaced in video_refresh whenever size doesn't match
let surface = Surface::new(1, 1, pixel_format)?;
Ok(Sdl2SurfaceComponent {
surface,
pixel_format,
})
}
pub fn surface(&self) -> &Surface {
&self.surface
}
}
impl RetroComponent for Sdl2SurfaceComponent {}
impl RetroCallbacks for Sdl2SurfaceComponent {
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 {
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 as u32, self.pixel_format) {
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);
}
}
_ => {}
}
}
fn set_pixel_format(&mut self, pix_fmt: PixelFormat) -> Option<bool> {
self.pixel_format = match pix_fmt {
PixelFormat::ARGB1555 => sdl2::pixels::PixelFormatEnum::RGB555,
PixelFormat::ARGB8888 => sdl2::pixels::PixelFormatEnum::ARGB8888,
PixelFormat::RGB565 => sdl2::pixels::PixelFormatEnum::RGB565,
};
Some(true)
}
fn get_variable(&mut self, key: &str) -> Option<String> {
match key {
"parallel-n64-gfxplugin" => Some("angrylion".to_string()),
_ => None,
}
}
}