From 38544ed3f152950d5c926d852650a851b981215f Mon Sep 17 00:00:00 2001 From: bQUARKz Date: Sun, 24 May 2026 22:36:50 +0100 Subject: [PATCH] some adjustments on alpha by pixel --- crates/console/prometeu-drivers/src/gfx.rs | 134 ++++++++++++++++++--- 1 file changed, 115 insertions(+), 19 deletions(-) diff --git a/crates/console/prometeu-drivers/src/gfx.rs b/crates/console/prometeu-drivers/src/gfx.rs index 1c708e3e..3ebf43f3 100644 --- a/crates/console/prometeu-drivers/src/gfx.rs +++ b/crates/console/prometeu-drivers/src/gfx.rs @@ -774,11 +774,11 @@ impl Gfx { let fetch_x = if tile.entry.flip_x() { size - 1 - local_x } else { local_x }; let fetch_y = if tile.entry.flip_y() { size - 1 - local_y } else { local_y }; let px_index = tile.bank.get_pixel_index(tile.entry.glyph_id, fetch_x, fetch_y); - if px_index == 0 { + let color = tile.bank.resolve_color(tile.entry.palette_id, px_index); + if color.alpha() == 0 { continue; } - let color = tile.bank.resolve_color(tile.entry.palette_id, px_index); target.back[world_y as usize * target.screen_w + world_x as usize] = color.raw(); } } @@ -823,17 +823,12 @@ impl Gfx { let fetch_x = if sprite.flip_x { size - 1 - local_x } else { local_x }; let fetch_y = if sprite.flip_y { size - 1 - local_y } else { local_y }; - // 1. Get index let px_index = bank.get_pixel_index(sprite.glyph.glyph_id, fetch_x, fetch_y); - - // 2. Transparency - if px_index == 0 { + let color = bank.resolve_color(sprite.glyph.palette_id, px_index); + if color.alpha() == 0 { continue; } - // 3. Resolve color via palette (from the tile inside the sprite) - let color = bank.resolve_color(sprite.glyph.palette_id, px_index); - back[world_y as usize * screen_w + world_x as usize] = color.raw(); } } @@ -999,16 +994,23 @@ mod tests { use prometeu_hal::tile::Tile; use prometeu_hal::tilemap::TileMap; - fn make_glyph_bank(tile_size: TileSize, palette_colors: &[(u8, Color)]) -> GlyphBank { + fn make_glyph_bank(tile_size: TileSize, palette_colors: &[(u8, u8, Color)]) -> GlyphBank { let size = tile_size as usize; let mut bank = GlyphBank::new(tile_size, size, size); - for (palette_id, color) in palette_colors { - bank.palettes[*palette_id as usize][1] = *color; + for (palette_id, color_index, color) in palette_colors { + bank.palettes[*palette_id as usize][*color_index as usize] = *color; } - for y in 0..size { - for x in 0..size { - bank.pixel_indices[y * bank.width + x] = 1; - } + bank + } + + fn make_filled_glyph_bank( + tile_size: TileSize, + pixel_index: u8, + palette_colors: &[(u8, u8, Color)], + ) -> GlyphBank { + let mut bank = make_glyph_bank(tile_size, palette_colors); + for index in 0..bank.pixel_indices.len() { + bank.pixel_indices[index] = pixel_index; } bank } @@ -1094,6 +1096,90 @@ mod tests { assert_eq!(gfx.back[9 * 10 + 9], Color::WHITE.0); } + #[test] + fn test_sprite_draws_opaque_color_index_zero() { + let bank = make_filled_glyph_bank(TileSize::Size8, 0, &[(0, 0, Color::GREEN)]); + let mut back = vec![Color::BLACK.raw(); 8 * 8]; + let sprite = Sprite { + glyph: Glyph { glyph_id: 0, palette_id: 0 }, + x: 0, + y: 0, + layer: 0, + flip_x: false, + flip_y: false, + bank_id: 0, + active: true, + priority: 0, + }; + + Gfx::draw_sprite_pixel_by_pixel(&mut back, 8, 8, &sprite, &bank); + + assert_eq!(back[0], Color::GREEN.raw()); + } + + #[test] + fn test_sprite_skips_alpha_zero_color_index_zero() { + let bank = make_filled_glyph_bank(TileSize::Size8, 0, &[(0, 0, Color::TRANSPARENT)]); + let mut back = vec![Color::RED.raw(); 8 * 8]; + let sprite = Sprite { + glyph: Glyph { glyph_id: 0, palette_id: 0 }, + x: 0, + y: 0, + layer: 0, + flip_x: false, + flip_y: false, + bank_id: 0, + active: true, + priority: 0, + }; + + Gfx::draw_sprite_pixel_by_pixel(&mut back, 8, 8, &sprite, &bank); + + assert_eq!(back[0], Color::RED.raw()); + } + + #[test] + fn test_cached_tile_draws_opaque_color_index_zero() { + let bank = make_filled_glyph_bank(TileSize::Size8, 0, &[(0, 0, Color::GREEN)]); + let mut back = vec![Color::BLACK.raw(); 8 * 8]; + let mut target = RenderTarget { back: &mut back, screen_w: 8, screen_h: 8 }; + let entry = CachedTileEntry { + active: true, + glyph_id: 0, + palette_id: 0, + flags: 0, + glyph_asset_id: 0, + }; + + Gfx::draw_cached_tile_pixels( + &mut target, + CachedTileDraw { x: 0, y: 0, entry, bank: &bank, tile_size: TileSize::Size8 }, + ); + + assert_eq!(target.back[0], Color::GREEN.raw()); + } + + #[test] + fn test_cached_tile_skips_alpha_zero_color_index_zero() { + let bank = make_filled_glyph_bank(TileSize::Size8, 0, &[(0, 0, Color::TRANSPARENT)]); + let mut back = vec![Color::RED.raw(); 8 * 8]; + let mut target = RenderTarget { back: &mut back, screen_w: 8, screen_h: 8 }; + let entry = CachedTileEntry { + active: true, + glyph_id: 0, + palette_id: 0, + flags: 0, + glyph_asset_id: 0, + }; + + Gfx::draw_cached_tile_pixels( + &mut target, + CachedTileDraw { x: 0, y: 0, entry, bank: &bank, tile_size: TileSize::Size8 }, + ); + + assert_eq!(target.back[0], Color::RED.raw()); + } + #[test] fn test_draw_rect() { let banks = Arc::new(MemoryBanks::new()); @@ -1127,7 +1213,11 @@ mod tests { let banks = Arc::new(MemoryBanks::new()); banks.install_glyph_bank( 0, - Arc::new(make_glyph_bank(TileSize::Size8, &[(0, Color::RED), (1, Color::GREEN)])), + Arc::new(make_filled_glyph_bank( + TileSize::Size8, + 1, + &[(0, 1, Color::RED), (1, 1, Color::GREEN)], + )), ); let mut scene = make_scene([0, 0, 0, 0]); @@ -1152,9 +1242,15 @@ mod tests { let banks = Arc::new(MemoryBanks::new()); banks.install_glyph_bank( 0, - Arc::new(make_glyph_bank( + Arc::new(make_filled_glyph_bank( TileSize::Size8, - &[(0, Color::RED), (1, Color::GREEN), (2, Color::BLUE), (4, Color::WHITE)], + 1, + &[ + (0, 1, Color::RED), + (1, 1, Color::GREEN), + (2, 1, Color::BLUE), + (4, 1, Color::WHITE), + ], )), );