[???] Lots of work
Cleanup un-needed elements, simplify memory management
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
use crate::misc::emulator_error::EmulatorError;
|
||||
use crate::misc::result::EmulatorResult;
|
||||
|
||||
/// A Color as represented in BytePusher.
|
||||
/// It is a byte that contains a packed 6 digit red, green and blue
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct Color(u8);
|
||||
|
||||
@@ -26,16 +28,24 @@ impl Color {
|
||||
}
|
||||
Ok(Color(in_mem_color))
|
||||
}
|
||||
/// wrap to black if needed
|
||||
pub fn new(in_mem_color: u8)->Color{
|
||||
if in_mem_color > Self::COLOR_MAX {
|
||||
Color(0)
|
||||
}else{
|
||||
Color(in_mem_color)
|
||||
}
|
||||
}
|
||||
pub fn get_mem_byte(&self) -> u8 {
|
||||
self.0
|
||||
}
|
||||
/// This fetches the rgb triplet
|
||||
pub fn get_rgb(self) -> [u8; 3] {
|
||||
pub fn get_rgb(self) -> (u8,u8,u8) {
|
||||
let r = self.0 / Self::RED_MULT;
|
||||
let gb_byte_remainder = self.0 % Self::RED_MULT;
|
||||
let g = gb_byte_remainder / Self::GREEN_MULT;
|
||||
let b = gb_byte_remainder % Self::GREEN_MULT;
|
||||
[r * Self::COLOR_FACTOR_8_BIT, g * Self::COLOR_FACTOR_8_BIT, b * Self::COLOR_FACTOR_8_BIT]
|
||||
(r * Self::COLOR_FACTOR_8_BIT, g * Self::COLOR_FACTOR_8_BIT, b * Self::COLOR_FACTOR_8_BIT)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +57,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_from_mem_zero() {
|
||||
let color = Color::try_new(0).unwrap();
|
||||
assert_eq!([0u8; 3], color.get_rgb())
|
||||
assert_eq!((0,0,0), color.get_rgb())
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -59,7 +69,7 @@ mod tests {
|
||||
#[test]
|
||||
pub fn test_from_mem_max() {
|
||||
let color = Color::try_new(Color::COLOR_MAX).unwrap();
|
||||
assert_eq!([255u8; 3], color.get_rgb())
|
||||
assert_eq!((255,255,255), color.get_rgb())
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@@ -1,18 +1,53 @@
|
||||
use std::cell::Ref;
|
||||
use std::fmt::Debug;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use sdl2::render::{Canvas, WindowCanvas};
|
||||
use crate::emu::graphics::{DEVICE_FRAMEBUFFER_SIZE, GraphicsProcessor};
|
||||
use crate::graphics::color::Color;
|
||||
use crate::misc::emulator_error::EmulatorError;
|
||||
use crate::misc::result::EmulatorResult;
|
||||
|
||||
pub trait GraphicsAdapter: Debug {
|
||||
fn draw(&self, frame_buf: Ref<Box<[u8; 65536]>>) -> EmulatorResult<()>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SDLGraphicsAdapter<'a> {
|
||||
pub graphics_processor: &'a GraphicsProcessor<'a>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SDLGraphicsAdapter {
|
||||
}
|
||||
|
||||
impl GraphicsAdapter for SDLGraphicsAdapter {
|
||||
fn draw(&self, frame_buffer: Ref<Box<[u8; 65536]>>) -> EmulatorResult<()> {
|
||||
todo!()
|
||||
impl <'a> Debug for SDLGraphicsAdapter<'a> {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str("SDL2 adapter")
|
||||
}
|
||||
}
|
||||
|
||||
impl <'a> SDLGraphicsAdapter<'a> {
|
||||
pub fn new(graphics_processor: &'a GraphicsProcessor)->SDLGraphicsAdapter<'a>{
|
||||
SDLGraphicsAdapter{
|
||||
graphics_processor
|
||||
}
|
||||
}
|
||||
pub fn draw(&self, canvas:&mut WindowCanvas) -> EmulatorResult<()> {
|
||||
let mut canvas_ref = &canvas;
|
||||
let fb = self.graphics_processor.get_framebuffer();
|
||||
for i in 0..fb.len() {
|
||||
let i = i as u32;
|
||||
let y_coord = i&0xff00;
|
||||
let x_coord = i&0x00ff;
|
||||
|
||||
}
|
||||
let xyc = fb.iter().enumerate().map(|(i,e)| {
|
||||
let i = i as u32;
|
||||
let y_coord = i&0xff00;
|
||||
let x_coord = i&0x00ff;
|
||||
let color = Color::new(*e);
|
||||
(x_coord,y_coord,color)
|
||||
});
|
||||
for (x,y,c) in xyc{
|
||||
canvas.set_draw_color(c.get_rgb());
|
||||
let coordinates = (x as i32,y as i32);
|
||||
let draw_result = canvas.draw_point(coordinates).map_err(|str|EmulatorError::OtherError(str));
|
||||
|
||||
draw_result?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user