added minimal machine structure£

This commit is contained in:
Nilton Constantino
2026-01-09 09:49:24 +00:00
parent b78ccbc62d
commit d7f4bbc5f7
8 changed files with 157 additions and 18 deletions
+55
View File
@@ -0,0 +1,55 @@
use crate::machine::Color;
pub struct Gfx {
w: usize,
h: usize,
front: Vec<u32>,
back: Vec<u32>,
}
impl Gfx {
pub fn new(w: usize, h: usize) -> Self {
let len = w * h;
Self {
w,
h,
front: vec![0; len],
back: vec![0; len],
}
}
pub fn size(&self) -> (usize, usize) {
(self.w, self.h)
}
/// O buffer que o host deve exibir.
pub fn front_buffer(&self) -> &[u32] {
&self.front
}
pub fn clear(&mut self, color: Color) {
self.back.fill(color.0);
}
pub fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
let fw = self.w as i32;
let fh = self.h as i32;
let x0 = x.clamp(0, fw);
let y0 = y.clamp(0, fh);
let x1 = (x + w).clamp(0, fw);
let y1 = (y + h).clamp(0, fh);
for yy in y0..y1 {
let row = (yy as usize) * self.w;
for xx in x0..x1 {
self.back[row + (xx as usize)] = color.0;
}
}
}
/// Double buffer swap.
pub fn present(&mut self) {
std::mem::swap(&mut self.front, &mut self.back);
}
}
+13
View File
@@ -0,0 +1,13 @@
#[derive(Default, Clone, Copy, Debug)]
pub struct Input {
pub up: bool,
pub down: bool,
pub left: bool,
pub right: bool,
pub a_down: bool,
pub b_down: bool,
pub start: bool,
pub select: bool,
}
+7
View File
@@ -0,0 +1,7 @@
mod gfx;
mod input;
mod touch;
pub use gfx::Gfx;
pub use input::Input;
pub use touch::Touch;
+9
View File
@@ -0,0 +1,9 @@
#[derive(Default, Clone, Copy, Debug)]
pub struct Touch {
pub present: bool,
pub down: bool,
pub pressed: bool,
pub released: bool,
pub x: i32,
pub y: i32,
}