From f028518664d4f6535f1659bf410e0f5536cf7c54 Mon Sep 17 00:00:00 2001 From: Sour Date: Fri, 22 Feb 2019 22:31:20 -0500 Subject: [PATCH] PPU: Implement brightness control --- Core/Ppu.cpp | 17 ++++++++++++++--- Core/Ppu.h | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Core/Ppu.cpp b/Core/Ppu.cpp index a800900..0a82586 100644 --- a/Core/Ppu.cpp +++ b/Core/Ppu.cpp @@ -314,6 +314,7 @@ void Ppu::RenderScanline() } ApplyColorMath(); + ApplyBrightness(); //Process sprites for next scanline memset(_spritePriority, 0xFF, sizeof(_spritePriority)); @@ -627,7 +628,6 @@ void Ppu::ApplyColorMath() otherPixel = _fixedColor; } - if(_colorMathSubstractMode) { uint16_t r = std::max((mainPixel & 0x001F) - (otherPixel & 0x001F), 0) >> halfShift; uint16_t g = std::max(((mainPixel >> 5) & 0x001F) - ((otherPixel >> 5) & 0x001F), 0) >> halfShift; @@ -645,6 +645,19 @@ void Ppu::ApplyColorMath() } } +void Ppu::ApplyBrightness() +{ + if(_screenBrightness != 15) { + for(int x = 0; x < 256; x++) { + uint16_t &pixel = _currentBuffer[(_scanline << 8) | x]; + uint16_t r = (pixel & 0x1F) * _screenBrightness / 15; + uint16_t g = ((pixel >> 5) & 0x1F) * _screenBrightness / 15; + uint16_t b = ((pixel >> 10) & 0x1F) * _screenBrightness / 15; + pixel = r | (g << 5) | (b << 10); + } + } +} + template bool Ppu::ProcessMaskWindow(uint8_t activeWindowCount, int x) { @@ -830,8 +843,6 @@ void Ppu::Write(uint32_t addr, uint8_t value) switch(addr) { case 0x2100: _forcedVblank = (value & 0x80) != 0; - - //TODO Apply brightness _screenBrightness = value & 0x0F; //TODO : Also, writing this register on the first line of V-Blank (225 or 240, depending on overscan) when force blank is currently active causes the OAM Address Reset to occur. diff --git a/Core/Ppu.h b/Core/Ppu.h index e6dab3b..65bad06 100644 --- a/Core/Ppu.h +++ b/Core/Ppu.h @@ -158,6 +158,7 @@ private: void RenderScanline(); void ApplyColorMath(); + void ApplyBrightness(); template bool ProcessMaskWindow(uint8_t activeWindowCount, int x);