implements PLN-0011
This commit is contained in:
@@ -2,10 +2,8 @@ use crate::memory_banks::GlyphBankPoolAccess;
|
||||
use prometeu_hal::GfxBridge;
|
||||
use prometeu_hal::color::Color;
|
||||
use prometeu_hal::glyph::Glyph;
|
||||
use prometeu_hal::glyph_bank::{GlyphBank, TileSize};
|
||||
use prometeu_hal::glyph_bank::GlyphBank;
|
||||
use prometeu_hal::sprite::Sprite;
|
||||
use prometeu_hal::tile::Tile;
|
||||
use prometeu_hal::tile_layer::{HudTileLayer, ScrollableTileLayer, TileMap};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Blending modes inspired by classic 16-bit hardware.
|
||||
@@ -57,10 +55,6 @@ pub struct Gfx {
|
||||
/// Back buffer: the "Work RAM" where new frames are composed.
|
||||
back: Vec<u16>,
|
||||
|
||||
/// 4 scrollable background layers. Each can have its own scroll (X, Y) and GlyphBank.
|
||||
pub layers: [ScrollableTileLayer; 4],
|
||||
/// 1 fixed layer for User Interface (HUD). It doesn't scroll.
|
||||
pub hud: HudTileLayer,
|
||||
/// Shared access to graphical memory banks (tiles and palettes).
|
||||
pub glyph_banks: Arc<dyn GlyphBankPoolAccess>,
|
||||
/// Hardware sprite list (512 slots). Equivalent to OAM (Object Attribute Memory).
|
||||
@@ -214,12 +208,6 @@ impl GfxBridge for Gfx {
|
||||
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)
|
||||
}
|
||||
@@ -227,18 +215,6 @@ impl GfxBridge for Gfx {
|
||||
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]
|
||||
}
|
||||
@@ -289,20 +265,11 @@ impl Gfx {
|
||||
};
|
||||
|
||||
let len = w * h;
|
||||
let layers = [
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
ScrollableTileLayer::new(64, 64, TileSize::Size16),
|
||||
];
|
||||
|
||||
Self {
|
||||
w,
|
||||
h,
|
||||
front: vec![0; len],
|
||||
back: vec![0; len],
|
||||
layers,
|
||||
hud: HudTileLayer::new(64, 32),
|
||||
glyph_banks,
|
||||
sprites: [EMPTY_SPRITE; 512],
|
||||
scene_fade_level: 31,
|
||||
@@ -588,192 +555,171 @@ impl Gfx {
|
||||
|
||||
// 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.glyph_banks.glyph_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,
|
||||
);
|
||||
}
|
||||
|
||||
// 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.glyph_banks,
|
||||
);
|
||||
}
|
||||
// for i in 0..self.layers.len() {
|
||||
// let bank_id = self.layers[i].bank_id as usize;
|
||||
// if let Some(bank) = self.glyph_banks.glyph_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,
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// // 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.glyph_banks,
|
||||
// );
|
||||
// }
|
||||
|
||||
// 4. Scene Fade: Applies a color blend to the entire world (excluding HUD).
|
||||
Self::apply_fade_to_buffer(&mut self.back, self.scene_fade_level, self.scene_fade_color);
|
||||
|
||||
// 5. HUD: The fixed interface layer, always drawn on top of the world.
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
// Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
|
||||
// 6. HUD Fade: Independent fade effect for the UI.
|
||||
Self::apply_fade_to_buffer(&mut self.back, self.hud_fade_level, self.hud_fade_color);
|
||||
}
|
||||
|
||||
/// Renders a specific game layer.
|
||||
pub fn render_layer(&mut self, layer_idx: usize) {
|
||||
if layer_idx >= self.layers.len() {
|
||||
return;
|
||||
}
|
||||
// /// Renders a specific game layer.
|
||||
// pub fn render_layer(&mut self, layer_idx: usize) {
|
||||
// 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;
|
||||
// let scroll_y = self.layers[layer_idx].scroll_y;
|
||||
//
|
||||
// let bank = match self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
// Some(b) => b,
|
||||
// _ => return,
|
||||
// };
|
||||
//
|
||||
// Self::draw_tile_map(
|
||||
// &mut self.back,
|
||||
// self.w,
|
||||
// self.h,
|
||||
// &self.layers[layer_idx].map,
|
||||
// &bank,
|
||||
// scroll_x,
|
||||
// scroll_y,
|
||||
// );
|
||||
// }
|
||||
|
||||
let bank_id = self.layers[layer_idx].bank_id as usize;
|
||||
let scroll_x = self.layers[layer_idx].scroll_x;
|
||||
let scroll_y = self.layers[layer_idx].scroll_y;
|
||||
// /// Rasterizes a TileMap into the provided pixel buffer using scrolling.
|
||||
// fn draw_tile_map(
|
||||
// back: &mut [u16],
|
||||
// screen_w: usize,
|
||||
// screen_h: usize,
|
||||
// map: &TileMap,
|
||||
// bank: &GlyphBank,
|
||||
// scroll_x: i32,
|
||||
// scroll_y: i32,
|
||||
// ) {
|
||||
// let tile_size = bank.tile_size as usize;
|
||||
//
|
||||
// // 1. Determine the range of visible tiles based on the scroll position.
|
||||
// // We add a margin of 1 tile to ensure smooth pixel-perfect scrolling at the borders.
|
||||
// let visible_tiles_x = (screen_w / tile_size) + 1;
|
||||
// let visible_tiles_y = (screen_h / tile_size) + 1;
|
||||
//
|
||||
// // 2. Calculate offsets within the tilemap.
|
||||
// let start_tile_x = scroll_x / tile_size as i32;
|
||||
// let start_tile_y = scroll_y / tile_size as i32;
|
||||
//
|
||||
// // 3. Fine scroll: how many pixels the tiles are shifted within the first visible cell.
|
||||
// let fine_scroll_x = scroll_x % tile_size as i32;
|
||||
// let fine_scroll_y = scroll_y % tile_size as i32;
|
||||
//
|
||||
// // 4. Iterate only through the tiles that are actually visible on screen.
|
||||
// for ty in 0..visible_tiles_y {
|
||||
// for tx in 0..visible_tiles_x {
|
||||
// let map_x = (start_tile_x + tx as i32) as usize;
|
||||
// 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;
|
||||
// }
|
||||
//
|
||||
// let tile = map.tiles[map_y * map.width + map_x];
|
||||
//
|
||||
// // Optimized skip for empty (ID 0) tiles.
|
||||
// if !tile.active {
|
||||
// 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,
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
let bank = match self.glyph_banks.glyph_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
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).
|
||||
pub fn render_hud(&mut self) {
|
||||
Self::render_hud_with_pool(&mut self.back, self.w, self.h, &self.hud, &*self.glyph_banks);
|
||||
}
|
||||
|
||||
fn render_hud_with_pool(
|
||||
back: &mut [u16],
|
||||
w: usize,
|
||||
h: usize,
|
||||
hud: &HudTileLayer,
|
||||
glyph_banks: &dyn GlyphBankPoolAccess,
|
||||
) {
|
||||
let bank_id = hud.bank_id as usize;
|
||||
let bank = match glyph_banks.glyph_bank_slot(bank_id) {
|
||||
Some(b) => b,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
Self::draw_tile_map(back, w, h, &hud.map, &bank, 0, 0);
|
||||
}
|
||||
|
||||
/// Rasterizes a TileMap into the provided pixel buffer using scrolling.
|
||||
fn draw_tile_map(
|
||||
back: &mut [u16],
|
||||
screen_w: usize,
|
||||
screen_h: usize,
|
||||
map: &TileMap,
|
||||
bank: &GlyphBank,
|
||||
scroll_x: i32,
|
||||
scroll_y: i32,
|
||||
) {
|
||||
let tile_size = bank.tile_size as usize;
|
||||
|
||||
// 1. Determine the range of visible tiles based on the scroll position.
|
||||
// We add a margin of 1 tile to ensure smooth pixel-perfect scrolling at the borders.
|
||||
let visible_tiles_x = (screen_w / tile_size) + 1;
|
||||
let visible_tiles_y = (screen_h / tile_size) + 1;
|
||||
|
||||
// 2. Calculate offsets within the tilemap.
|
||||
let start_tile_x = scroll_x / tile_size as i32;
|
||||
let start_tile_y = scroll_y / tile_size as i32;
|
||||
|
||||
// 3. Fine scroll: how many pixels the tiles are shifted within the first visible cell.
|
||||
let fine_scroll_x = scroll_x % tile_size as i32;
|
||||
let fine_scroll_y = scroll_y % tile_size as i32;
|
||||
|
||||
// 4. Iterate only through the tiles that are actually visible on screen.
|
||||
for ty in 0..visible_tiles_y {
|
||||
for tx in 0..visible_tiles_x {
|
||||
let map_x = (start_tile_x + tx as i32) as usize;
|
||||
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;
|
||||
}
|
||||
|
||||
let tile = map.tiles[map_y * map.width + map_x];
|
||||
|
||||
// Optimized skip for empty (ID 0) tiles.
|
||||
if !tile.active {
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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: &GlyphBank,
|
||||
) {
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// Handle flip flags by reversing the fetch coordinates.
|
||||
let fetch_x = if tile.flip_x { size - 1 - local_x } else { local_x };
|
||||
let fetch_y = if tile.flip_y { size - 1 - local_y } else { local_y };
|
||||
|
||||
// 1. Get the pixel color index (0-15) from the bank.
|
||||
let px_index = bank.get_pixel_index(tile.glyph.glyph_id, fetch_x, fetch_y);
|
||||
|
||||
// 2. Hardware rule: Color index 0 is always fully transparent.
|
||||
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.glyph.palette_id, px_index);
|
||||
|
||||
back[world_y as usize * screen_w + world_x as usize] = color.raw();
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// 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: &GlyphBank,
|
||||
// ) {
|
||||
// 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;
|
||||
// }
|
||||
//
|
||||
// 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;
|
||||
// }
|
||||
//
|
||||
// // Handle flip flags by reversing the fetch coordinates.
|
||||
// let fetch_x = if tile.flip_x { size - 1 - local_x } else { local_x };
|
||||
// let fetch_y = if tile.flip_y { size - 1 - local_y } else { local_y };
|
||||
//
|
||||
// // 1. Get the pixel color index (0-15) from the bank.
|
||||
// let px_index = bank.get_pixel_index(tile.glyph.glyph_id, fetch_x, fetch_y);
|
||||
//
|
||||
// // 2. Hardware rule: Color index 0 is always fully transparent.
|
||||
// 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.glyph.palette_id, px_index);
|
||||
//
|
||||
// back[world_y as usize * screen_w + world_x as usize] = color.raw();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
fn draw_bucket_on_buffer(
|
||||
back: &mut [u16],
|
||||
|
||||
Reference in New Issue
Block a user