quality baseline
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
use crate::memory_banks::TileBankPoolAccess;
|
||||
use std::sync::Arc;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::GfxBridge;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::sprite::Sprite;
|
||||
use prometeu_hal::tile::Tile;
|
||||
use prometeu_hal::tile_bank::{TileBank, TileSize};
|
||||
use prometeu_hal::tile_layer::{HudTileLayer, ScrollableTileLayer, TileMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Blending modes inspired by classic 16-bit hardware.
|
||||
/// Defines how source pixels are combined with existing pixels in the framebuffer.
|
||||
///
|
||||
///
|
||||
/// ### Usage Example:
|
||||
/// // Draw a semi-transparent blue rectangle
|
||||
/// gfx.fill_rect_blend(10, 10, 50, 50, Color::BLUE, BlendMode::Half);
|
||||
@@ -31,10 +31,10 @@ pub enum BlendMode {
|
||||
/// PROMETEU Graphics Subsystem (GFX).
|
||||
///
|
||||
/// Models a specialized graphics chip with a fixed resolution, double buffering,
|
||||
/// and a multi-layered tile/sprite architecture.
|
||||
///
|
||||
/// The GFX system works by composing several "layers" into a single 16-bit
|
||||
/// RGB565 framebuffer. It supports hardware-accelerated primitives (lines, rects)
|
||||
/// and a multi-layered tile/sprite architecture.
|
||||
///
|
||||
/// The GFX system works by composing several "layers" into a single 16-bit
|
||||
/// RGB565 framebuffer. It supports hardware-accelerated primitives (lines, rects)
|
||||
/// and specialized console features like background scrolling and sprite sorting.
|
||||
///
|
||||
/// ### Layer Composition Order (back to front):
|
||||
@@ -79,44 +79,139 @@ pub struct Gfx {
|
||||
}
|
||||
|
||||
impl GfxBridge for Gfx {
|
||||
fn size(&self) -> (usize, usize) { self.size() }
|
||||
fn front_buffer(&self) -> &[u16] { self.front_buffer() }
|
||||
fn clear(&mut self, color: Color) { self.clear(color) }
|
||||
fn fill_rect_blend(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color, mode: prometeu_hal::BlendMode) { let m = match mode { prometeu_hal::BlendMode::None => BlendMode::None, prometeu_hal::BlendMode::Half => BlendMode::Half, prometeu_hal::BlendMode::HalfPlus => BlendMode::HalfPlus, prometeu_hal::BlendMode::HalfMinus => BlendMode::HalfMinus, prometeu_hal::BlendMode::Full => BlendMode::Full }; self.fill_rect_blend(x, y, w, h, color, m) }
|
||||
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) { self.fill_rect(x, y, w, h, color) }
|
||||
fn draw_pixel(&mut self, x: i32, y: i32, color: Color) { self.draw_pixel(x, y, color) }
|
||||
fn draw_line(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color) { self.draw_line(x0, y0, x1, y1, color) }
|
||||
fn draw_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) { self.draw_circle(xc, yc, r, color) }
|
||||
fn draw_circle_points(&mut self, xc: i32, yc: i32, x: i32, y: i32, color: Color) { self.draw_circle_points(xc, yc, x, y, color) }
|
||||
fn fill_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) { self.fill_circle(xc, yc, r, color) }
|
||||
fn draw_circle_lines(&mut self, xc: i32, yc: i32, x: i32, y: i32, color: Color) { self.draw_circle_lines(xc, yc, x, y, color) }
|
||||
fn draw_disc(&mut self, x: i32, y: i32, r: i32, border_color: Color, fill_color: Color) { self.draw_disc(x, y, r, border_color, fill_color) }
|
||||
fn draw_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) { self.draw_rect(x, y, w, h, color) }
|
||||
fn draw_square(&mut self, x: i32, y: i32, w: i32, h: i32, border_color: Color, fill_color: Color) { self.draw_square(x, y, w, h, border_color, fill_color) }
|
||||
fn draw_horizontal_line(&mut self, x0: i32, x1: i32, y: i32, color: Color) { self.draw_horizontal_line(x0, x1, y, color) }
|
||||
fn draw_vertical_line(&mut self, x: i32, y0: i32, y1: i32, color: Color) { self.draw_vertical_line(x, y0, y1, color) }
|
||||
fn present(&mut self) { self.present() }
|
||||
fn render_all(&mut self) { self.render_all() }
|
||||
fn render_layer(&mut self, layer_idx: usize) { self.render_layer(layer_idx) }
|
||||
fn render_hud(&mut self) { self.render_hud() }
|
||||
fn draw_text(&mut self, x: i32, y: i32, text: &str, color: Color) { self.draw_text(x, y, text, color) }
|
||||
fn draw_char(&mut self, x: i32, y: i32, c: char, color: Color) { self.draw_char(x, y, c, color) }
|
||||
fn size(&self) -> (usize, usize) {
|
||||
self.size()
|
||||
}
|
||||
fn front_buffer(&self) -> &[u16] {
|
||||
self.front_buffer()
|
||||
}
|
||||
fn clear(&mut self, color: Color) {
|
||||
self.clear(color)
|
||||
}
|
||||
fn fill_rect_blend(
|
||||
&mut self,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
color: Color,
|
||||
mode: prometeu_hal::BlendMode,
|
||||
) {
|
||||
let m = match mode {
|
||||
prometeu_hal::BlendMode::None => BlendMode::None,
|
||||
prometeu_hal::BlendMode::Half => BlendMode::Half,
|
||||
prometeu_hal::BlendMode::HalfPlus => BlendMode::HalfPlus,
|
||||
prometeu_hal::BlendMode::HalfMinus => BlendMode::HalfMinus,
|
||||
prometeu_hal::BlendMode::Full => BlendMode::Full,
|
||||
};
|
||||
self.fill_rect_blend(x, y, w, h, color, m)
|
||||
}
|
||||
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
|
||||
self.fill_rect(x, y, w, h, color)
|
||||
}
|
||||
fn draw_pixel(&mut self, x: i32, y: i32, color: Color) {
|
||||
self.draw_pixel(x, y, color)
|
||||
}
|
||||
fn draw_line(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color) {
|
||||
self.draw_line(x0, y0, x1, y1, color)
|
||||
}
|
||||
fn draw_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) {
|
||||
self.draw_circle(xc, yc, r, color)
|
||||
}
|
||||
fn draw_circle_points(&mut self, xc: i32, yc: i32, x: i32, y: i32, color: Color) {
|
||||
self.draw_circle_points(xc, yc, x, y, color)
|
||||
}
|
||||
fn fill_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) {
|
||||
self.fill_circle(xc, yc, r, color)
|
||||
}
|
||||
fn draw_circle_lines(&mut self, xc: i32, yc: i32, x: i32, y: i32, color: Color) {
|
||||
self.draw_circle_lines(xc, yc, x, y, color)
|
||||
}
|
||||
fn draw_disc(&mut self, x: i32, y: i32, r: i32, border_color: Color, fill_color: Color) {
|
||||
self.draw_disc(x, y, r, border_color, fill_color)
|
||||
}
|
||||
fn draw_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
|
||||
self.draw_rect(x, y, w, h, color)
|
||||
}
|
||||
fn draw_square(
|
||||
&mut self,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
border_color: Color,
|
||||
fill_color: Color,
|
||||
) {
|
||||
self.draw_square(x, y, w, h, border_color, fill_color)
|
||||
}
|
||||
fn draw_horizontal_line(&mut self, x0: i32, x1: i32, y: i32, color: Color) {
|
||||
self.draw_horizontal_line(x0, x1, y, color)
|
||||
}
|
||||
fn draw_vertical_line(&mut self, x: i32, y0: i32, y1: i32, color: Color) {
|
||||
self.draw_vertical_line(x, y0, y1, color)
|
||||
}
|
||||
fn present(&mut self) {
|
||||
self.present()
|
||||
}
|
||||
fn render_all(&mut self) {
|
||||
self.render_all()
|
||||
}
|
||||
fn render_layer(&mut self, layer_idx: usize) {
|
||||
self.render_layer(layer_idx)
|
||||
}
|
||||
fn render_hud(&mut self) {
|
||||
self.render_hud()
|
||||
}
|
||||
fn draw_text(&mut self, x: i32, y: i32, text: &str, color: Color) {
|
||||
self.draw_text(x, y, text, color)
|
||||
}
|
||||
fn draw_char(&mut self, x: i32, y: i32, c: char, color: Color) {
|
||||
self.draw_char(x, y, c, color)
|
||||
}
|
||||
|
||||
fn layer(&self, index: usize) -> &ScrollableTileLayer { &self.layers[index] }
|
||||
fn layer_mut(&mut self, index: usize) -> &mut ScrollableTileLayer { &mut self.layers[index] }
|
||||
fn hud(&self) -> &HudTileLayer { &self.hud }
|
||||
fn hud_mut(&mut self) -> &mut HudTileLayer { &mut self.hud }
|
||||
fn sprite(&self, index: usize) -> &Sprite { &self.sprites[index] }
|
||||
fn sprite_mut(&mut self, index: usize) -> &mut Sprite { &mut self.sprites[index] }
|
||||
fn layer(&self, index: usize) -> &ScrollableTileLayer {
|
||||
&self.layers[index]
|
||||
}
|
||||
fn layer_mut(&mut self, index: usize) -> &mut ScrollableTileLayer {
|
||||
&mut self.layers[index]
|
||||
}
|
||||
fn hud(&self) -> &HudTileLayer {
|
||||
&self.hud
|
||||
}
|
||||
fn hud_mut(&mut self) -> &mut HudTileLayer {
|
||||
&mut self.hud
|
||||
}
|
||||
fn sprite(&self, index: usize) -> &Sprite {
|
||||
&self.sprites[index]
|
||||
}
|
||||
fn sprite_mut(&mut self, index: usize) -> &mut Sprite {
|
||||
&mut self.sprites[index]
|
||||
}
|
||||
|
||||
fn scene_fade_level(&self) -> u8 { self.scene_fade_level }
|
||||
fn set_scene_fade_level(&mut self, level: u8) { self.scene_fade_level = level; }
|
||||
fn scene_fade_color(&self) -> Color { self.scene_fade_color }
|
||||
fn set_scene_fade_color(&mut self, color: Color) { self.scene_fade_color = color; }
|
||||
fn hud_fade_level(&self) -> u8 { self.hud_fade_level }
|
||||
fn set_hud_fade_level(&mut self, level: u8) { self.hud_fade_level = level; }
|
||||
fn hud_fade_color(&self) -> Color { self.hud_fade_color }
|
||||
fn set_hud_fade_color(&mut self, color: Color) { self.hud_fade_color = color; }
|
||||
fn scene_fade_level(&self) -> u8 {
|
||||
self.scene_fade_level
|
||||
}
|
||||
fn set_scene_fade_level(&mut self, level: u8) {
|
||||
self.scene_fade_level = level;
|
||||
}
|
||||
fn scene_fade_color(&self) -> Color {
|
||||
self.scene_fade_color
|
||||
}
|
||||
fn set_scene_fade_color(&mut self, color: Color) {
|
||||
self.scene_fade_color = color;
|
||||
}
|
||||
fn hud_fade_level(&self) -> u8 {
|
||||
self.hud_fade_level
|
||||
}
|
||||
fn set_hud_fade_level(&mut self, level: u8) {
|
||||
self.hud_fade_level = level;
|
||||
}
|
||||
fn hud_fade_color(&self) -> Color {
|
||||
self.hud_fade_color
|
||||
}
|
||||
fn set_hud_fade_color(&mut self, color: Color) {
|
||||
self.hud_fade_color = color;
|
||||
}
|
||||
}
|
||||
|
||||
impl Gfx {
|
||||
@@ -187,7 +282,9 @@ impl Gfx {
|
||||
color: Color,
|
||||
mode: BlendMode,
|
||||
) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
let fw = self.w as i32;
|
||||
let fh = self.h as i32;
|
||||
@@ -216,7 +313,9 @@ impl Gfx {
|
||||
|
||||
/// Draws a single pixel.
|
||||
pub fn draw_pixel(&mut self, x: i32, y: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
if x >= 0 && x < self.w as i32 && y >= 0 && y < self.h as i32 {
|
||||
self.back[y as usize * self.w + x as usize] = color.0;
|
||||
}
|
||||
@@ -224,7 +323,9 @@ impl Gfx {
|
||||
|
||||
/// Draws a line between two points using Bresenham's algorithm.
|
||||
pub fn draw_line(&mut self, x0: i32, y0: i32, x1: i32, y1: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
let dx = (x1 - x0).abs();
|
||||
let sx = if x0 < x1 { 1 } else { -1 };
|
||||
@@ -237,7 +338,9 @@ impl Gfx {
|
||||
|
||||
loop {
|
||||
self.draw_pixel(x, y, color);
|
||||
if x == x1 && y == y1 { break; }
|
||||
if x == x1 && y == y1 {
|
||||
break;
|
||||
}
|
||||
let e2 = 2 * err;
|
||||
if e2 >= dy {
|
||||
err += dy;
|
||||
@@ -252,9 +355,13 @@ impl Gfx {
|
||||
|
||||
/// Draws a circle outline using Midpoint Circle Algorithm.
|
||||
pub fn draw_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
if r < 0 { return; }
|
||||
if r < 0 {
|
||||
return;
|
||||
}
|
||||
let mut x = 0;
|
||||
let mut y = r;
|
||||
let mut d = 3 - 2 * r;
|
||||
@@ -284,9 +391,13 @@ impl Gfx {
|
||||
|
||||
/// Draws a filled circle.
|
||||
pub fn fill_circle(&mut self, xc: i32, yc: i32, r: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
if r < 0 { return; }
|
||||
if r < 0 {
|
||||
return;
|
||||
}
|
||||
let mut x = 0;
|
||||
let mut y = r;
|
||||
let mut d = 3 - 2 * r;
|
||||
@@ -318,9 +429,13 @@ impl Gfx {
|
||||
|
||||
/// Draws a rectangle outline.
|
||||
pub fn draw_rect(&mut self, x: i32, y: i32, w: i32, h: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
if w <= 0 || h <= 0 { return; }
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
self.draw_horizontal_line(x, x + w - 1, y, color);
|
||||
self.draw_horizontal_line(x, x + w - 1, y + h - 1, color);
|
||||
self.draw_vertical_line(x, y, y + h - 1, color);
|
||||
@@ -328,30 +443,50 @@ impl Gfx {
|
||||
}
|
||||
|
||||
/// Draws a square (filled rectangle with border).
|
||||
pub fn draw_square(&mut self, x: i32, y: i32, w: i32, h: i32, border_color: Color, fill_color: Color) {
|
||||
pub fn draw_square(
|
||||
&mut self,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
border_color: Color,
|
||||
fill_color: Color,
|
||||
) {
|
||||
self.fill_rect(x, y, w, h, fill_color);
|
||||
self.draw_rect(x, y, w, h, border_color);
|
||||
}
|
||||
|
||||
fn draw_horizontal_line(&mut self, x0: i32, x1: i32, y: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
if y < 0 || y >= self.h as i32 { return; }
|
||||
if y < 0 || y >= self.h as i32 {
|
||||
return;
|
||||
}
|
||||
let start = x0.max(0);
|
||||
let end = x1.min(self.w as i32 - 1);
|
||||
if start > end { return; }
|
||||
if start > end {
|
||||
return;
|
||||
}
|
||||
for x in start..=end {
|
||||
self.back[y as usize * self.w + x as usize] = color.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_vertical_line(&mut self, x: i32, y0: i32, y1: i32, color: Color) {
|
||||
if color == Color::COLOR_KEY { return; }
|
||||
if color == Color::COLOR_KEY {
|
||||
return;
|
||||
}
|
||||
|
||||
if x < 0 || x >= self.w as i32 { return; }
|
||||
if x < 0 || x >= self.w as i32 {
|
||||
return;
|
||||
}
|
||||
let start = y0.max(0);
|
||||
let end = y1.min(self.h as i32 - 1);
|
||||
if start > end { return; }
|
||||
if start > end {
|
||||
return;
|
||||
}
|
||||
for y in start..=end {
|
||||
self.back[y as usize * self.w + x as usize] = color.0;
|
||||
}
|
||||
@@ -365,7 +500,7 @@ impl Gfx {
|
||||
|
||||
/// The main rendering pipeline.
|
||||
///
|
||||
/// This method composes the final frame by rasterizing layers and sprites in the
|
||||
/// This method composes the final frame by rasterizing layers and sprites in the
|
||||
/// correct priority order into the back buffer.
|
||||
/// Follows the hardware model where layers and sprites are composed every frame.
|
||||
pub fn render_all(&mut self) {
|
||||
@@ -382,18 +517,40 @@ impl Gfx {
|
||||
}
|
||||
|
||||
// 1. Priority 0 sprites: drawn at the very back, behind everything else.
|
||||
Self::draw_bucket_on_buffer(&mut self.back, self.w, self.h, &self.priority_buckets[0], &self.sprites, &*self.tile_banks);
|
||||
Self::draw_bucket_on_buffer(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.priority_buckets[0],
|
||||
&self.sprites,
|
||||
&*self.tile_banks,
|
||||
);
|
||||
|
||||
// 2. Main layers and prioritized sprites.
|
||||
// Order: Layer 0 -> Sprites 1 -> Layer 1 -> Sprites 2 ...
|
||||
for i in 0..self.layers.len() {
|
||||
let bank_id = self.layers[i].bank_id as usize;
|
||||
if let Some(bank) = self.tile_banks.tile_bank_slot(bank_id) {
|
||||
Self::draw_tile_map(&mut self.back, self.w, self.h, &self.layers[i].map, &bank, self.layers[i].scroll_x, self.layers[i].scroll_y);
|
||||
Self::draw_tile_map(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.layers[i].map,
|
||||
&bank,
|
||||
self.layers[i].scroll_x,
|
||||
self.layers[i].scroll_y,
|
||||
);
|
||||
}
|
||||
|
||||
// Draw sprites that belong to this depth level
|
||||
Self::draw_bucket_on_buffer(&mut self.back, self.w, self.h, &self.priority_buckets[i + 1], &self.sprites, &*self.tile_banks);
|
||||
Self::draw_bucket_on_buffer(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.priority_buckets[i + 1],
|
||||
&self.sprites,
|
||||
&*self.tile_banks,
|
||||
);
|
||||
}
|
||||
|
||||
// 4. Scene Fade: Applies a color blend to the entire world (excluding HUD).
|
||||
@@ -408,7 +565,9 @@ impl Gfx {
|
||||
|
||||
/// Renders a specific game layer.
|
||||
pub fn render_layer(&mut self, layer_idx: usize) {
|
||||
if layer_idx >= self.layers.len() { return; }
|
||||
if layer_idx >= self.layers.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
let bank_id = self.layers[layer_idx].bank_id as usize;
|
||||
let scroll_x = self.layers[layer_idx].scroll_x;
|
||||
@@ -419,7 +578,15 @@ impl Gfx {
|
||||
_ => return,
|
||||
};
|
||||
|
||||
Self::draw_tile_map(&mut self.back, self.w, self.h, &self.layers[layer_idx].map, &bank, scroll_x, scroll_y);
|
||||
Self::draw_tile_map(
|
||||
&mut self.back,
|
||||
self.w,
|
||||
self.h,
|
||||
&self.layers[layer_idx].map,
|
||||
&bank,
|
||||
scroll_x,
|
||||
scroll_y,
|
||||
);
|
||||
}
|
||||
|
||||
/// Renders the HUD (fixed position, no scroll).
|
||||
@@ -427,7 +594,13 @@ impl Gfx {
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.tile_banks);
|
||||
}
|
||||
|
||||
fn render_hud_with_pool(back: &mut [u16], w: usize, h: usize, hud: &HudTileLayer, tile_banks: &dyn TileBankPoolAccess) {
|
||||
fn render_hud_with_pool(
|
||||
back: &mut [u16],
|
||||
w: usize,
|
||||
h: usize,
|
||||
hud: &HudTileLayer,
|
||||
tile_banks: &dyn TileBankPoolAccess,
|
||||
) {
|
||||
let bank_id = hud.bank_id as usize;
|
||||
let bank = match tile_banks.tile_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
@@ -445,7 +618,7 @@ impl Gfx {
|
||||
map: &TileMap,
|
||||
bank: &TileBank,
|
||||
scroll_x: i32,
|
||||
scroll_y: i32
|
||||
scroll_y: i32,
|
||||
) {
|
||||
let tile_size = bank.tile_size as usize;
|
||||
|
||||
@@ -469,34 +642,58 @@ impl Gfx {
|
||||
let map_y = (start_tile_y + ty as i32) as usize;
|
||||
|
||||
// Bounds check: don't draw if the camera is outside the map.
|
||||
if map_x >= map.width || map_y >= map.height { continue; }
|
||||
if map_x >= map.width || map_y >= map.height {
|
||||
continue;
|
||||
}
|
||||
|
||||
let tile = map.tiles[map_y * map.width + map_x];
|
||||
|
||||
|
||||
// Optimized skip for empty (ID 0) tiles.
|
||||
if tile.id == 0 { continue; }
|
||||
if tile.id == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 5. Project the tile pixels to screen space.
|
||||
let screen_tile_x = (tx as i32 * tile_size as i32) - fine_scroll_x;
|
||||
let screen_tile_y = (ty as i32 * tile_size as i32) - fine_scroll_y;
|
||||
|
||||
Self::draw_tile_pixels(back, screen_w, screen_h, screen_tile_x, screen_tile_y, tile, bank);
|
||||
Self::draw_tile_pixels(
|
||||
back,
|
||||
screen_w,
|
||||
screen_h,
|
||||
screen_tile_x,
|
||||
screen_tile_y,
|
||||
tile,
|
||||
bank,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal helper to copy a single tile's pixels to the framebuffer.
|
||||
/// Handles flipping and palette resolution.
|
||||
fn draw_tile_pixels(back: &mut [u16], screen_w: usize, screen_h: usize, x: i32, y: i32, tile: Tile, bank: &TileBank) {
|
||||
fn draw_tile_pixels(
|
||||
back: &mut [u16],
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
x: i32,
|
||||
y: i32,
|
||||
tile: Tile,
|
||||
bank: &TileBank,
|
||||
) {
|
||||
let size = bank.tile_size as usize;
|
||||
|
||||
for local_y in 0..size {
|
||||
let world_y = y + local_y as i32;
|
||||
if world_y < 0 || world_y >= screen_h as i32 { continue; }
|
||||
if world_y < 0 || world_y >= screen_h as i32 {
|
||||
continue;
|
||||
}
|
||||
|
||||
for local_x in 0..size {
|
||||
let world_x = x + local_x as i32;
|
||||
if world_x < 0 || world_x >= screen_w as i32 { continue; }
|
||||
if world_x < 0 || world_x >= screen_w as i32 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle flip flags by reversing the fetch coordinates.
|
||||
let fetch_x = if tile.flip_x { size - 1 - local_x } else { local_x };
|
||||
@@ -506,7 +703,9 @@ impl Gfx {
|
||||
let px_index = bank.get_pixel_index(tile.id, fetch_x, fetch_y);
|
||||
|
||||
// 2. Hardware rule: Color index 0 is always fully transparent.
|
||||
if px_index == 0 { continue; }
|
||||
if px_index == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. Resolve the virtual index to a real RGB565 color using the tile's assigned palette.
|
||||
let color = bank.resolve_color(tile.palette_id, px_index);
|
||||
@@ -538,7 +737,7 @@ impl Gfx {
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
sprite: &Sprite,
|
||||
bank: &TileBank
|
||||
bank: &TileBank,
|
||||
) {
|
||||
// ... (same bounds/clipping calculation we already had) ...
|
||||
let size = bank.tile_size as usize;
|
||||
@@ -559,7 +758,9 @@ impl Gfx {
|
||||
let px_index = bank.get_pixel_index(sprite.tile.id, fetch_x, fetch_y);
|
||||
|
||||
// 2. Transparency
|
||||
if px_index == 0 { continue; }
|
||||
if px_index == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 3. Resolve color via palette (from the tile inside the sprite)
|
||||
let color = bank.resolve_color(sprite.tile.palette_id, px_index);
|
||||
@@ -572,7 +773,9 @@ impl Gfx {
|
||||
/// Applies the fade effect to the entire back buffer.
|
||||
/// level: 0 (full color) to 31 (visible)
|
||||
fn apply_fade_to_buffer(back: &mut [u16], level: u8, fade_color: Color) {
|
||||
if level >= 31 { return; } // Fully visible, skip processing
|
||||
if level >= 31 {
|
||||
return;
|
||||
} // Fully visible, skip processing
|
||||
|
||||
let weight = level as u16;
|
||||
let inv_weight = 31 - weight;
|
||||
@@ -674,7 +877,7 @@ mod tests {
|
||||
let mut gfx = Gfx::new(10, 10, banks);
|
||||
gfx.draw_pixel(5, 5, Color::WHITE);
|
||||
assert_eq!(gfx.back[5 * 10 + 5], Color::WHITE.0);
|
||||
|
||||
|
||||
// Out of bounds should not panic
|
||||
gfx.draw_pixel(-1, -1, Color::WHITE);
|
||||
gfx.draw_pixel(10, 10, Color::WHITE);
|
||||
|
||||
Reference in New Issue
Block a user