From c0c592c438f1c1d6aae9708002f5d21851b45294 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 16 Oct 2022 13:06:11 +0200 Subject: [PATCH] Emulator: Make palette colors more accurate --- src/ppu.cpp | 27 ++++++++++++++++----------- src/ppu.h | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/ppu.cpp b/src/ppu.cpp index ad0d767..5fdfb4d 100644 --- a/src/ppu.cpp +++ b/src/ppu.cpp @@ -98,7 +98,12 @@ void PPU::render() if (!(lcd_control & LCDC::BGandWindowEnable)) { // When Bit 0 is cleared, both background and window become blank (white) - m_screen.fill(255); + auto pixel = getPixelColor(0, Palette::BGP); + for (size_t i = 0; i < m_screen.size(); i += 3) { + m_screen[i + 0] = pixel[0]; + m_screen[i + 1] = pixel[1]; + m_screen[i + 2] = pixel[2]; + } } else { // Tile map @@ -148,10 +153,10 @@ void PPU::drawTile(uint32_t x, uint32_t y, uint32_t tile_address) } uint8_t pixel_index = (pixels_lsb >> (7 - tile_x) | ((pixels_msb >> (7 - tile_x)) << 1)) & 0x3; - uint8_t pixel_color = getPixelColor(pixel_index, Palette::BGP); - m_screen[index + 0] = pixel_color; - m_screen[index + 1] = pixel_color; - m_screen[index + 2] = pixel_color; + auto pixel_color = getPixelColor(pixel_index, Palette::BGP); + m_screen[index + 0] = pixel_color[0]; + m_screen[index + 1] = pixel_color[1]; + m_screen[index + 2] = pixel_color[2]; } // Move to next line @@ -159,7 +164,7 @@ void PPU::drawTile(uint32_t x, uint32_t y, uint32_t tile_address) } } -uint8_t PPU::getPixelColor(uint8_t color_index, Palette palette) +std::array PPU::getPixelColor(uint8_t color_index, Palette palette) { VERIFY(color_index < 4, "trying to fetch invalid color index '{}'", color_index); @@ -169,13 +174,13 @@ uint8_t PPU::getPixelColor(uint8_t color_index, Palette palette) uint8_t palette_value = palette_data >> (color_index * 2) & 0x3; switch (palette_value) { case 0: - return 255; + return { 200, 199, 168 }; case 1: - return 168; + return { 160, 160, 136 }; case 2: - return 84; + return { 104, 104, 80 }; case 3: - return 0; + return { 39, 40, 24 }; default: VERIFY_NOT_REACHED(); }; @@ -186,7 +191,7 @@ uint8_t PPU::getPixelColor(uint8_t color_index, Palette palette) VERIFY_NOT_REACHED(); } - return 0; + return {}; } void PPU::resetFrame() diff --git a/src/ppu.h b/src/ppu.h index b0043c8..645e7d8 100644 --- a/src/ppu.h +++ b/src/ppu.h @@ -55,7 +55,7 @@ public: void render(); void drawTile(uint32_t screen_x, uint32_t screen_y, uint32_t tile_address); - uint8_t getPixelColor(uint8_t color_index, Palette palette); + std::array getPixelColor(uint8_t color_index, Palette palette); void resetFrame();