Skip to content

Commit

Permalink
Use 4-bpp colors from CGRAM
Browse files Browse the repository at this point in the history
  • Loading branch information
twvd committed Oct 29, 2023
1 parent d874c28 commit 5b2e2c1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/snes/ppu/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ impl<TRenderer> PPU<TRenderer>
where
TRenderer: Renderer,
{
fn cgram_to_color(&self, addr: u8) -> Color {
let entry = self.cgram[addr as usize];
(
(((entry >> 0) & 0x1F) as u8) << 3, // Red, 5-bit
(((entry >> 5) & 0x1F) as u8) << 3, // Green, 5-bit
(((entry >> 10) & 0x1F) as u8) << 3, // Blue, 5-bit
)
}
pub fn render_scanline(&mut self, scanline: usize) {
let mut line_idx: [u8; SCREEN_WIDTH] = [0; SCREEN_WIDTH];
let mut line_paletted: [Color; SCREEN_WIDTH] = [(0, 0, 0); SCREEN_WIDTH];
Expand All @@ -15,14 +23,21 @@ where
let ty = scanline % 8;

for tx in 0..8 {
let palette = (entry.palettenr() * 4);
let c = chr.get_coloridx(tx, ty);
let color = self.cgram_to_color(palette + c);
line_idx[x + tx] = c;
line_paletted[x + tx] = (70 * c, 70 * c, 70 * c);
line_paletted[x + tx] = color;
}
}

for (x, p) in line_paletted.into_iter().enumerate() {
self.renderer.set_pixel(x, scanline, p);
if line_idx[x] == 0 {
// Use main backdrop color
self.renderer.set_pixel(x, scanline, self.cgram_to_color(0));
} else {
self.renderer.set_pixel(x, scanline, p);
}
}
}
}

0 comments on commit 5b2e2c1

Please sign in to comment.