Add sdl2 handlers and bunch of tests

First semi-working state, games can be loaded and they open
a sdl2 window \o/

TODOS:
+ Stacks offset is wrong somewhere
+ Input not working (maybe because of above reason?)
+ Add mising tests
This commit is contained in:
Marco Thomas
2021-03-12 16:13:25 +01:00
parent f61b3ed74d
commit 41d9e5dad0
6 changed files with 432 additions and 35 deletions

52
src/display.rs Normal file
View File

@@ -0,0 +1,52 @@
use sdl2;
use sdl2::{render::Canvas, video::Window, pixels, rect::Rect};
pub struct Display {
canvas: Canvas<Window>,
}
impl Display {
pub fn new(sdl_ctx: &sdl2::Sdl) -> Self {
let video = sdl_ctx.video().unwrap();
let window = video
.window(
"FooBar",
(crate::SCREEN_WIDTH * crate::SCREEN_SCALE) as u32,
(crate::SCREEN_HEIGHT * crate::SCREEN_SCALE) as u32)
.position_centered()
.opengl()
.build()
.unwrap();
let mut canvas = window
.into_canvas()
.build()
.unwrap();
canvas.set_draw_color(pixels::Color::RGB(0, 0, 0));
canvas.clear();
canvas.present();
Display {
canvas,
}
}
pub fn draw(&mut self, pixel: &[[u8; crate::SCREEN_WIDTH]; crate::SCREEN_HEIGHT]) {
for (y, &row) in pixel.iter().enumerate() {
for (x, &column) in row.iter().enumerate() {
self.canvas.set_draw_color(
if column == 1 {
pixels::Color::RGB(255, 255, 255)
}
else {
pixels::Color::RGB(0, 0, 0)
});
let _ = self.canvas
.fill_rect(
Rect::new((x * crate::SCREEN_SCALE) as i32, (y * crate::SCREEN_SCALE) as i32, crate::SCREEN_SCALE as u32, crate::SCREEN_SCALE as u32));
}
}
self.canvas.present();
}
}