[gpu] Add graphics and adapter

This commit is contained in:
2024-02-18 15:11:37 +05:30
parent 91702f3997
commit 7492ba8dd3
5 changed files with 90 additions and 2 deletions

30
src/emu/graphics.rs Normal file
View File

@@ -0,0 +1,30 @@
use crate::misc::emulator_error::DeviceType::GRAPHICS;
use crate::misc::emulator_error::EmulatorError;
use crate::misc::result::EmulatorResult;
pub const DEVICE_FRAMEBUFFER_SIZE: usize = 256 * 256;
pub struct GraphicsProcessor {
framebuffer: Box<[u8; DEVICE_FRAMEBUFFER_SIZE]>,
}
/// Abstracted graphics processor. Refer GraphicsAdapter
impl GraphicsProcessor {
pub fn try_new() -> EmulatorResult<GraphicsProcessor> {
let framebuffer = vec![0; DEVICE_FRAMEBUFFER_SIZE].into_boxed_slice()
.try_into()
.map_err(|_| {
EmulatorError::AllocationFailure(GRAPHICS, "Failed to allocate graphics")
})?;
Ok(GraphicsProcessor {
framebuffer
})
}
pub fn set_framebuffer(&mut self,memory_slice: &[u8]){
self.framebuffer.copy_from_slice(memory_slice);
}
pub fn get_framebuffer(&self) -> &[u8;DEVICE_FRAMEBUFFER_SIZE] {
&self.framebuffer
}
}

View File

@@ -3,3 +3,4 @@ pub mod mmu;
pub mod ram;
pub mod iomem;
pub mod program_counter;
pub mod graphics;

View File

@@ -0,0 +1,17 @@
use crate::emu::graphics::{DEVICE_FRAMEBUFFER_SIZE, GraphicsProcessor};
use crate::misc::result::EmulatorResult;
pub trait GraphicsAdapter{
fn draw(&mut self,frame_buf:&[u8;DEVICE_FRAMEBUFFER_SIZE])->EmulatorResult<()>;
}
pub struct SDLGraphicsAdapter{
graphics_processor: GraphicsProcessor
}
impl GraphicsAdapter for SDLGraphicsAdapter{
fn draw(&mut self, frame_buf: &[u8; DEVICE_FRAMEBUFFER_SIZE]) -> EmulatorResult<()> {
todo!()
}
}

View File

@@ -1 +1,2 @@
pub mod color;
pub mod graphics_adapter;

View File

@@ -1,3 +1,7 @@
use std::time::Duration;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use simple_logger::SimpleLogger;
use crate::emu::iomem::MemoryMappedIO;
use crate::emu::ram::RamMemory;
@@ -21,5 +25,40 @@ fn main() -> EmulatorResult<()> {
log::info!("Memory at {} is {}",i,mmu.try_get_byte(i)?);
}
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("rust-sdl2 demo", 512, 512)
.position_centered()
.build()
.unwrap();
let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0;
'running: loop {
i = (i + 1) % 255;
canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
canvas.clear();
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} |
Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
event => {
log::info!("Received window event {:?}",event);
}
}
}
// The rest of the game loop goes here...
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
}
Ok(())
}