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

70 lines
2.1 KiB
Rust

use crate::prelude::*;
use crate::provided::sdl2::canvas::frame_to_surface;
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() -> crate::base::Result<Self> {
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
}
pub fn surface_owned(&self) -> Surface {
let mut clone = Surface::new(
self.surface.width(),
self.surface.height(),
self.surface.pixel_format_enum(),
).unwrap();
self.surface.blit(None, &mut clone, None).unwrap();
clone
}
}
impl RetroComponent for Sdl2SurfaceComponent {}
impl RetroCallbacks for Sdl2SurfaceComponent {
fn video_refresh(&mut self, frame: &VideoFrame) {
if let Ok(surf) = frame_to_surface(frame) {
if self.surface.size() != surf.size() {
let (width, height) = surf.size();
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,
}
}
}