This commit is contained in:
Nilton Constantino
2026-01-13 08:30:15 +00:00
parent 140c56d731
commit 04ce8c195e
2 changed files with 49 additions and 2 deletions
+38 -2
View File
@@ -24,7 +24,12 @@ pub struct Gfx {
pub layers: [ScrollableTileLayer; 4],
pub hud: HudTileLayer,
pub banks: [Option<TileBank>; 16],
pub sprites: [Sprite; 512], // Nossa "OAM"
pub sprites: [Sprite; 512],
pub scene_fade_level: u8, // 0..31
pub scene_fade_color: Color,
pub hud_fade_level: u8, // 0..31
pub hud_fade_color: Color,
}
impl Gfx {
@@ -56,6 +61,10 @@ impl Gfx {
hud: HudTileLayer::new(64, 32),
banks: [EMPTY_BANK; 16],
sprites: [EMPTY_SPRITE; 512],
scene_fade_level: 31,
scene_fade_color: Color::BLACK,
hud_fade_level: 31,
hud_fade_color: Color::BLACK,
}
}
@@ -129,8 +138,14 @@ impl Gfx {
self.render_sprites_by_priority((i + 1) as u8);
}
// 3. HUD (Sempre por cima)
// 4. Aplica Scene Fade (Afeta tudo desenhado até agora)
Self::apply_fade_to_buffer(&mut self.back, self.scene_fade_level, self.scene_fade_color);
// 5. HUD (Sempre por cima)
self.render_hud();
// 6. Aplica HUD Fade (Opcional, apenas para o HUD)
Self::apply_fade_to_buffer(&mut self.back, self.hud_fade_level, self.hud_fade_color);
}
pub fn render_layer(&mut self, layer_idx: usize) {
@@ -261,6 +276,27 @@ impl Gfx {
}
}
/// Aplica o efeito de fade em todo o back buffer.
/// level: 0 (cor total) até 31 (visível)
fn apply_fade_to_buffer(back: &mut [u16], level: u8, fade_color: Color) {
if level >= 31 { return; } // Totalmente visível, pula processamento
let weight = level as u16;
let inv_weight = 31 - weight;
let (fr, fg, fb) = Color::unpack_to_native(fade_color.0);
for px in back.iter_mut() {
let (sr, sg, sb) = Color::unpack_to_native(*px);
// Fórmula: (src * weight + fade * inv_weight) / 31
let r = ((sr as u16 * weight + fr as u16 * inv_weight) / 31) as u8;
let g = ((sg as u16 * weight + fg as u16 * inv_weight) / 31) as u8;
let b = ((sb as u16 * weight + fb as u16 * inv_weight) / 31) as u8;
*px = Color::pack_from_native(r, g, b);
}
}
/// Retorna o uso estimado de memória das estruturas gráficas em bytes.
/// Isso ajuda a monitorar o "custo" do hardware simulado.
pub fn memory_usage_bytes(&self) -> usize {