From f4c849688253a4e38ae0ba4e61f32d60e45315f7 Mon Sep 17 00:00:00 2001 From: "Adrian.Conlon" Date: Fri, 15 Sep 2017 18:23:02 +0100 Subject: [PATCH] Generalise tile rendering code for background and sprites. Signed-off-by: Adrian.Conlon --- LR35902/inc/Display.h | 10 ++++++ LR35902/src/Display.cpp | 75 +++++++++++++++++++++-------------------- 2 files changed, 48 insertions(+), 37 deletions(-) diff --git a/LR35902/inc/Display.h b/LR35902/inc/Display.h index 10cc24f..b9467d4 100644 --- a/LR35902/inc/Display.h +++ b/LR35902/inc/Display.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "GameBoyBus.h" @@ -8,6 +9,9 @@ namespace EightBit { namespace GameBoy { + + class CharacterDefinition; + class Display { public: enum { @@ -42,6 +46,12 @@ namespace EightBit { void renderObjects(); void renderObjects(int objBlockHeight); + + void renderTile( + int drawX, int drawY, int offsetX, int offsetY, + bool flipX, bool flipY, bool allowTransparencies, + const std::array& palette, + const CharacterDefinition& definition); }; } } \ No newline at end of file diff --git a/LR35902/src/Display.cpp b/LR35902/src/Display.cpp index 9d46cc6..344fd33 100644 --- a/LR35902/src/Display.cpp +++ b/LR35902/src/Display.cpp @@ -61,6 +61,7 @@ void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) { auto oamAddress = 0xfe00; for (int i = 0; i < 40; ++i) { + const auto current = ObjectAttribute(m_bus, oamAddress + 4 * i, objBlockHeight); const auto sprite = current.pattern(); const auto definition = CharacterDefinition(m_bus, objDefinitionAddress + 16 * sprite); @@ -70,25 +71,11 @@ void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) { const auto flipX = current.flipX(); const auto flipY = current.flipY(); - for (int cy = 0; cy < 8; ++cy) { - - uint8_t y = spriteY + (flipY ? 7 - cy : cy); - if (y >= RasterHeight) - break; - - for (int cx = 0; cx < 8; ++cx) { - - uint8_t x = spriteX + (flipX ? 7 - cx : cx); - if (x >= RasterWidth) - break; - - auto outputPixel = y * RasterWidth + x; - - auto colour = definition.get()[cy * 8 + cx]; - if (colour > 0) // transparency - m_pixels[outputPixel] = m_colours->getColour(palette[colour]); - } - } + renderTile( + spriteX, spriteY, -8, -16, + flipX, flipY, true, + palette, + definition); } } @@ -137,23 +124,37 @@ void EightBit::GameBoy::Display::renderBackground( auto definition = definitionPair->second; - for (int cy = 0; cy < 8; ++cy) { - for (int cx = 0; cx < 8; ++cx) { - - uint8_t x = column * 8 + cx + offsetX - scrollX; - if (x >= RasterWidth) - break; - - uint8_t y = row * 8 + cy + offsetY - scrollY; - if (y >= RasterHeight) - break; - - auto outputPixel = y * RasterWidth + x; - - auto colour = palette[definition.get()[cy * 8 + cx]]; - m_pixels[outputPixel] = m_colours->getColour(colour); - } - } + renderTile( + column * 8, row * 8, offsetX - scrollX, offsetY - scrollY, + false, false, false, + palette, + definition); } } -} \ No newline at end of file +} + +void EightBit::GameBoy::Display::renderTile( + int drawX, int drawY, int offsetX, int offsetY, + bool flipX, bool flipY, bool allowTransparencies, + const std::array& palette, + const CharacterDefinition& definition) { + + for (int cy = 0; cy < 8; ++cy) { + + for (int cx = 0; cx < 8; ++cx) { + + uint8_t y = drawY + cy + offsetY; + if (y >= RasterHeight) + break; + + uint8_t x = drawX + cx + offsetX; + if (x >= RasterWidth) + break; + + auto outputPixel = y * RasterWidth + x; + + auto colour = palette[definition.get()[cy * 8 + cx]]; + m_pixels[outputPixel] = m_colours->getColour(colour); + } + } +}