2024-02-19 12:30:27 +05:30
|
|
|
use std::fmt::{Debug, Formatter};
|
2024-02-19 12:59:29 +05:30
|
|
|
use sdl2::rect::Rect;
|
2024-02-19 12:39:25 +05:30
|
|
|
use sdl2::render::WindowCanvas;
|
|
|
|
use crate::emu::graphics::GraphicsProcessor;
|
2024-02-19 12:30:27 +05:30
|
|
|
use crate::graphics::color::Color;
|
|
|
|
use crate::misc::emulator_error::EmulatorError;
|
2024-02-18 15:11:37 +05:30
|
|
|
use crate::misc::result::EmulatorResult;
|
|
|
|
|
2024-02-19 12:30:27 +05:30
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SDLGraphicsAdapter<'a> {
|
2024-02-19 14:25:57 +05:30
|
|
|
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> {
|
2024-02-19 12:30:27 +05:30
|
|
|
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> {
|
|
|
|
SDLGraphicsAdapter {
|
2024-02-19 12:30:27 +05:30
|
|
|
graphics_processor
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 12:59:29 +05:30
|
|
|
pub fn draw(&self, canvas: &mut WindowCanvas, draw_factor: u32) -> EmulatorResult<()> {
|
2024-02-19 12:30:27 +05:30
|
|
|
let fb = self.graphics_processor.get_framebuffer();
|
|
|
|
|
2024-02-19 12:59:29 +05:30
|
|
|
let xyc = fb.iter().enumerate().map(|(i, e)| {
|
2024-02-19 12:30:27 +05:30
|
|
|
let i = i as u32;
|
2024-02-19 12:59:29 +05:30
|
|
|
let y_coord = (i & 0xff00) >> 8;
|
|
|
|
let x_coord = i & 0x00ff;
|
2024-02-19 12:30:27 +05:30
|
|
|
let color = Color::new(*e);
|
2024-02-19 12:59:29 +05:30
|
|
|
(x_coord, y_coord, color)
|
2024-02-19 12:30:27 +05:30
|
|
|
});
|
2024-02-19 12:59:29 +05:30
|
|
|
for (x, y, c) in xyc {
|
2024-02-19 12:30:27 +05:30
|
|
|
canvas.set_draw_color(c.get_rgb());
|
2024-02-19 12:59:29 +05:30
|
|
|
let coordinates = (x as i32, y as i32);
|
|
|
|
let draw_result = Self::draw_scaled_point(canvas, coordinates, draw_factor);
|
2024-02-19 12:30:27 +05:30
|
|
|
draw_result?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2024-02-18 15:11:37 +05:30
|
|
|
}
|
2024-02-19 12:59:29 +05:30
|
|
|
|
|
|
|
fn draw_scaled_point(canvas: &mut WindowCanvas, coordinates: (i32, i32), draw_factor: u32) -> Result<(), EmulatorError> {
|
|
|
|
canvas
|
2024-02-19 14:25:57 +05:30
|
|
|
.fill_rect(Rect::new(coordinates.0 * draw_factor as i32, coordinates.1 * draw_factor as i32, draw_factor, draw_factor))
|
2024-02-19 12:59:29 +05:30
|
|
|
.map_err(|str| EmulatorError::OtherError(str))
|
|
|
|
}
|
2024-02-18 15:11:37 +05:30
|
|
|
}
|
|
|
|
|