Skip to content
Snippets Groups Projects
Verified Commit 9dd88a56 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

fix: realtime palette computation

parent 40f2e2cc
No related branches found
No related tags found
No related merge requests found
Pipeline #1537 passed
...@@ -515,33 +515,15 @@ impl Ppu { ...@@ -515,33 +515,15 @@ impl Ppu {
0x0043 => self.scx = value, 0x0043 => self.scx = value,
0x0045 => self.lyc = value, 0x0045 => self.lyc = value,
0x0047 => { 0x0047 => {
for index in 0..PALETTE_SIZE { Self::compute_palette(&mut self.palette, &self.palette_colors, value);
let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index {
0..=3 => self.palette[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index),
}
}
self.palettes[0] = value; self.palettes[0] = value;
} }
0x0048 => { 0x0048 => {
for index in 0..PALETTE_SIZE { Self::compute_palette(&mut self.palette_obj_0, &self.palette_colors, value);
let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index {
0..=3 => self.palette_obj_0[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index),
}
}
self.palettes[1] = value; self.palettes[1] = value;
} }
0x0049 => { 0x0049 => {
for index in 0..PALETTE_SIZE { Self::compute_palette(&mut self.palette_obj_1, &self.palette_colors, value);
let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index {
0..=3 => self.palette_obj_1[index] = self.palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index),
}
}
self.palettes[2] = value; self.palettes[2] = value;
} }
0x004a => self.wy = value, 0x004a => self.wy = value,
...@@ -567,6 +549,7 @@ impl Ppu { ...@@ -567,6 +549,7 @@ impl Ppu {
pub fn set_palette_colors(&mut self, value: &Palette) { pub fn set_palette_colors(&mut self, value: &Palette) {
self.palette_colors = *value; self.palette_colors = *value;
self.compute_palettes()
} }
pub fn palette(&self) -> Palette { pub fn palette(&self) -> Palette {
...@@ -934,6 +917,37 @@ impl Ppu { ...@@ -934,6 +917,37 @@ impl Ppu {
|| self.stat_vblank && self.mode == PpuMode::VBlank || self.stat_vblank && self.mode == PpuMode::VBlank
|| self.stat_hblank && self.mode == PpuMode::HBlank || self.stat_hblank && self.mode == PpuMode::HBlank
} }
/// Computes the values for all of the palettes, this method
/// is useful to "flush" color computation whenever the base
/// palette colors are changed.
fn compute_palettes(&mut self) {
Self::compute_palette(&mut self.palette, &self.palette_colors, self.palettes[0]);
Self::compute_palette(
&mut self.palette_obj_0,
&self.palette_colors,
self.palettes[1],
);
Self::compute_palette(
&mut self.palette_obj_1,
&self.palette_colors,
self.palettes[2],
);
}
/// Static method used for the base logic of computation of RGB
/// based palettes from the internal Game Boy color indexes.
/// This method should be called whenever the palette indexes
/// are changed.
fn compute_palette(palette: &mut Palette, palette_colors: &Palette, value: u8) {
for index in 0..PALETTE_SIZE {
let color_index: usize = (value as usize >> (index * 2)) & 3;
match color_index {
0..=3 => palette[index] = palette_colors[color_index],
color_index => panic!("Invalid palette color index {:04x}", color_index),
}
}
}
} }
impl Default for Ppu { impl Default for Ppu {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment