From 5b2e2c16fe08bf2c9e1e8ca17c6dc15f965add7d Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 29 Oct 2023 13:41:34 +0100 Subject: [PATCH] Use 4-bpp colors from CGRAM --- src/snes/ppu/render.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/snes/ppu/render.rs b/src/snes/ppu/render.rs index 07023db..62217c7 100644 --- a/src/snes/ppu/render.rs +++ b/src/snes/ppu/render.rs @@ -5,6 +5,14 @@ impl PPU 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]; @@ -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); + } } } }