added comments extensively
This commit is contained in:
@@ -1,24 +1,38 @@
|
||||
use crate::model::Color;
|
||||
|
||||
/// Standard sizes for square tiles.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum TileSize {
|
||||
/// 8x8 pixels.
|
||||
Size8 = 8,
|
||||
/// 16x16 pixels.
|
||||
Size16 = 16,
|
||||
/// 32x32 pixels.
|
||||
Size32 = 32,
|
||||
}
|
||||
|
||||
/// A container for graphical assets.
|
||||
///
|
||||
/// A TileBank stores both the raw pixel data (as palette indices) and the
|
||||
/// color palettes themselves. This encapsulates all the information needed
|
||||
/// to render a set of tiles.
|
||||
pub struct TileBank {
|
||||
/// Dimension of each individual tile in the bank.
|
||||
pub tile_size: TileSize,
|
||||
pub width: usize, // in pixels
|
||||
pub height: usize, // in pixels
|
||||
/// Width of the full bank sheet in pixels.
|
||||
pub width: usize,
|
||||
/// Height of the full bank sheet in pixels.
|
||||
pub height: usize,
|
||||
|
||||
/// Now we store indices from 0 to 15 (4 bits per pixel simulated in 8 bits)
|
||||
/// Pixel data stored as 4-bit indices (packed into 8-bit values).
|
||||
/// Index 0 is always reserved for transparency.
|
||||
pub pixel_indices: Vec<u8>,
|
||||
/// 256 palettes, each with 16 colors (RGB565 as u16)
|
||||
/// Table of 256 palettes, each containing 16 RGB565 colors.
|
||||
pub palettes: [[Color; 16]; 256],
|
||||
}
|
||||
|
||||
impl TileBank {
|
||||
/// Creates an empty tile bank with the specified dimensions.
|
||||
pub fn new(tile_size: TileSize, width: usize, height: usize) -> Self {
|
||||
Self {
|
||||
tile_size,
|
||||
@@ -29,7 +43,7 @@ impl TileBank {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the color of a specific pixel inside a tile.
|
||||
/// Resolves a global tile ID and local pixel coordinates to a palette index.
|
||||
/// tile_id: the tile index in the bank
|
||||
/// local_x, local_y: the pixel position inside the tile (0 to tile_size-1)
|
||||
pub fn get_pixel_index(&self, tile_id: u16, local_x: usize, local_y: usize) -> u8 {
|
||||
@@ -44,13 +58,14 @@ impl TileBank {
|
||||
if pixel_x < self.width && pixel_y < self.height {
|
||||
self.pixel_indices[pixel_y * self.width + pixel_x]
|
||||
} else {
|
||||
0 // Outside the bank = Transparent
|
||||
0 // Default to transparent if out of bounds
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolves a pixel index to a real Color using a palette.
|
||||
/// Maps a 4-bit index to a real RGB565 Color using the specified palette.
|
||||
pub fn resolve_color(&self, palette_id: u8, pixel_index: u8) -> Color {
|
||||
// Rule: Index 0 is always transparent (we use MAGENTA/COLOR_KEY as vacuum)
|
||||
// Hardware Rule: Index 0 is always transparent.
|
||||
// We use Magenta as the 'transparent' signal color during composition.
|
||||
if pixel_index == 0 {
|
||||
return Color::COLOR_KEY;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user