Files
byte-pusher-emu/src/graphics/graphics_adapter.rs

57 lines
1.8 KiB
Rust
Raw Normal View History

2024-02-19 22:38:09 +05:30
use std::cell::Ref;
use std::fmt::{Debug, Formatter};
2024-02-19 22:38:09 +05:30
use sdl2::pixels::PixelFormatEnum;
use sdl2::render::{TextureAccess, WindowCanvas};
use crate::emu::graphics::{DEVICE_FRAMEBUFFER_SIZE, GraphicsProcessor};
use crate::graphics::color::Color;
2024-02-18 15:11:37 +05:30
use crate::misc::result::EmulatorResult;
#[derive(Clone)]
pub struct SDLGraphicsAdapter<'a> {
2024-02-19 22:38:09 +05:30
color_fb: Box<[u8; DEVICE_FRAMEBUFFER_SIZE * 3]>,
graphics_processor: &'a GraphicsProcessor<'a>,
2024-02-18 15:11:37 +05:30
}
2024-02-19 12:59:29 +05:30
impl<'a> Debug for SDLGraphicsAdapter<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("SDL2 adapter")
}
2024-02-18 15:11:37 +05:30
}
2024-02-19 12:59:29 +05:30
impl<'a> SDLGraphicsAdapter<'a> {
pub fn new(graphics_processor: &'a GraphicsProcessor) -> SDLGraphicsAdapter<'a> {
2024-02-21 19:26:33 +05:30
let color_fb_vec = vec![0u8; DEVICE_FRAMEBUFFER_SIZE * 3].into_boxed_slice().try_into().expect("Failed conversion");
2024-02-19 12:59:29 +05:30
SDLGraphicsAdapter {
2024-02-19 22:38:09 +05:30
color_fb: color_fb_vec,
graphics_processor,
}
}
2024-02-19 22:38:09 +05:30
pub fn draw(&mut self, canvas: &mut WindowCanvas) -> EmulatorResult<()> {
let fb = self.graphics_processor.get_framebuffer();
2024-02-19 22:38:09 +05:30
self.fill_my_texture(fb);
let texture_creator = canvas.texture_creator();
let mut texture = texture_creator.create_texture(PixelFormatEnum::RGB24, TextureAccess::Streaming, 256, 256).expect("Failed to make texture");
texture.with_lock(None, |f, _i| {
f.copy_from_slice(self.color_fb.as_ref())
2024-02-21 19:26:33 +05:30
})?;
canvas.copy(&texture, None, None)?;
Ok(())
2024-02-18 15:11:37 +05:30
}
2024-02-19 22:38:09 +05:30
fn fill_my_texture(&mut self, dev_fb_ref: Ref<Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>>) {
for (i, e) in dev_fb_ref.iter().enumerate() {
let color = Color::new(*e).get_rgb();
self.color_fb[3 * i] = color.0;
self.color_fb[3 * i + 1] = color.1;
self.color_fb[3 * i + 2] = color.2;
}
2024-02-19 12:59:29 +05:30
}
2024-02-18 15:11:37 +05:30
}
2024-02-19 22:38:09 +05:30