added pallete system

This commit is contained in:
Nilton Constantino
2026-01-14 08:46:23 +00:00
parent 484e0895c1
commit 8bbaf59f62
3 changed files with 118 additions and 127 deletions
+26 -9
View File
@@ -11,7 +11,12 @@ pub struct TileBank {
pub tile_size: TileSize,
pub width: usize, // em pixels
pub height: usize, // em pixels
pub pixels: Vec<Color>,
// pub pixels: Vec<Color>,
/// Agora guardamos índices de 0 a 15 (4 bits por pixel simulados em 8 bits)
pub pixel_indices: Vec<u8>,
/// 256 paletas, cada uma com 16 cores (RGB565 como u16)
pub palettes: [[u16; 16]; 256],
}
impl TileBank {
@@ -20,26 +25,38 @@ impl TileBank {
tile_size,
width,
height,
pixels: vec![Color::BLACK; width * height],
pixel_indices: vec![0; width * height], // Índice 0 = Transparente
palettes: [[0; 16]; 256],
}
}
/// Retorna a cor de um pixel específico dentro de um tile.
/// tile_id: o índice do tile no banco
/// local_x, local_y: a posição do pixel dentro do tile (0 até tile_size-1)
pub fn get_pixel(&self, tile_id: u16, local_x: usize, local_y: usize) -> Color {
let tile_size = self.tile_size as usize;
let tiles_per_row = self.width / tile_size;
let tile_x = (tile_id as usize % tiles_per_row) * tile_size;
let tile_y = (tile_id as usize / tiles_per_row) * tile_size;
pub fn get_pixel_index(&self, tile_id: u16, local_x: usize, local_y: usize) -> u8 {
let size = self.tile_size as usize;
let tiles_per_row = self.width / size;
let tile_x = (tile_id as usize % tiles_per_row) * size;
let tile_y = (tile_id as usize / tiles_per_row) * size;
let pixel_x = tile_x + local_x;
let pixel_y = tile_y + local_y;
if pixel_x < self.width && pixel_y < self.height {
self.pixels[pixel_y * self.width + pixel_x]
self.pixel_indices[pixel_y * self.width + pixel_x]
} else {
Color::BLACK
0 // Fora do banco = Transparente
}
}
/// Resolve um índice de pixel para uma Color real usando uma paleta.
pub fn resolve_color(&self, palette_id: u8, pixel_index: u8) -> Color {
// Regra: Índice 0 é sempre transparente (usamos MAGENTA/COLOR_KEY como vácuo)
if pixel_index == 0 {
return Color::COLOR_KEY;
}
let raw_color = self.palettes[palette_id as usize][pixel_index as usize];
Color::from_raw(raw_color)
}
}