first window

This commit is contained in:
Nilton Constantino
2026-01-10 05:42:37 +00:00
parent d7f4bbc5f7
commit 83eee0f387
9 changed files with 2789 additions and 34 deletions
+106 -8
View File
@@ -1,10 +1,25 @@
use crate::machine::Color;
use crate::color::Color;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BlendMode {
/// dst = src
#[default]
None,
/// dst = (src + dst) / 2
Half,
/// dst = dst + (src / 2)
HalfPlus,
/// dst = dst - (src / 2)
HalfMinus,
/// dst = dst + src
Full,
}
pub struct Gfx {
w: usize,
h: usize,
front: Vec<u32>,
back: Vec<u32>,
front: Vec<u16>,
back: Vec<u16>,
}
impl Gfx {
@@ -22,8 +37,8 @@ impl Gfx {
(self.w, self.h)
}
/// O buffer que o host deve exibir.
pub fn front_buffer(&self) -> &[u32] {
/// O buffer que o host deve exibir (RGB565).
pub fn front_buffer(&self) -> &[u16] {
&self.front
}
@@ -31,7 +46,16 @@ impl Gfx {
self.back.fill(color.0);
}
pub fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
/// Retângulo com modo de blend.
pub fn fill_rect_blend(
&mut self,
x: i32,
y: i32,
w: i32,
h: i32,
color: Color,
mode: BlendMode,
) {
let fw = self.w as i32;
let fh = self.h as i32;
@@ -40,16 +64,90 @@ impl Gfx {
let x1 = (x + w).clamp(0, fw);
let y1 = (y + h).clamp(0, fh);
let src = color.0;
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;
let idx = row + (xx as usize);
let dst = self.back[idx];
self.back[idx] = blend_rgb565(dst, src, mode);
}
}
}
/// Double buffer swap.
/// Conveniência: retângulo normal (sem blend).
pub fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
self.fill_rect_blend(x, y, w, h, color, BlendMode::None);
}
/// Double buffer swap (O(1), sem cópia de pixels).
pub fn present(&mut self) {
std::mem::swap(&mut self.front, &mut self.back);
}
}
/// Faz blend em RGB565 por canal com saturação.
/// `dst` e `src` são pixels RGB565 (u16).
fn blend_rgb565(dst: u16, src: u16, mode: BlendMode) -> u16 {
match mode {
BlendMode::None => src,
BlendMode::Half => {
let (dr, dg, db) = unpack_rgb565(dst);
let (sr, sg, sb) = unpack_rgb565(src);
let r = ((dr as u16 + sr as u16) >> 1) as u8;
let g = ((dg as u16 + sg as u16) >> 1) as u8;
let b = ((db as u16 + sb as u16) >> 1) as u8;
pack_rgb565(r, g, b)
}
BlendMode::HalfPlus => {
let (dr, dg, db) = unpack_rgb565(dst);
let (sr, sg, sb) = unpack_rgb565(src);
let r = (dr as u16 + ((sr as u16) >> 1)).min(31) as u8;
let g = (dg as u16 + ((sg as u16) >> 1)).min(63) as u8;
let b = (db as u16 + ((sb as u16) >> 1)).min(31) as u8;
pack_rgb565(r, g, b)
}
BlendMode::HalfMinus => {
let (dr, dg, db) = unpack_rgb565(dst);
let (sr, sg, sb) = unpack_rgb565(src);
let r = (dr as i16 - ((sr as i16) >> 1)).max(0) as u8;
let g = (dg as i16 - ((sg as i16) >> 1)).max(0) as u8;
let b = (db as i16 - ((sb as i16) >> 1)).max(0) as u8;
pack_rgb565(r, g, b)
}
BlendMode::Full => {
let (dr, dg, db) = unpack_rgb565(dst);
let (sr, sg, sb) = unpack_rgb565(src);
let r = (dr as u16 + sr as u16).min(31) as u8;
let g = (dg as u16 + sg as u16).min(63) as u8;
let b = (db as u16 + sb as u16).min(31) as u8;
pack_rgb565(r, g, b)
}
}
}
/// Extrai canais já na faixa nativa do RGB565:
/// R: 0..31, G: 0..63, B: 0..31
#[inline(always)]
fn unpack_rgb565(px: u16) -> (u8, u8, u8) {
let r = ((px >> 11) & 0x1F) as u8;
let g = ((px >> 5) & 0x3F) as u8;
let b = (px & 0x1F) as u8;
(r, g, b)
}
#[inline(always)]
fn pack_rgb565(r: u8, g: u8, b: u8) -> u16 {
((r as u16 & 0x1F) << 11) | ((g as u16 & 0x3F) << 5) | (b as u16 & 0x1F)
}
+7
View File
@@ -11,3 +11,10 @@ pub struct Input {
pub start: bool,
pub select: bool,
}
impl Input {
pub fn begin_frame(&mut self) {
// No v0, nada a fazer.
// No futuro: a_pressed/a_released, etc.
}
}
+8
View File
@@ -7,3 +7,11 @@ pub struct Touch {
pub x: i32,
pub y: i32,
}
impl Touch {
/// Flags transitórias devem durar apenas 1 frame.
pub fn begin_frame(&mut self) {
self.pressed = false;
self.released = false;
}
}