Small tidy ups + optimisations.

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2017-10-02 15:28:41 +01:00
parent 8b1da2097b
commit c06c0d2cf9
2 changed files with 19 additions and 20 deletions

View File

@ -44,7 +44,6 @@ namespace EightBit {
void renderBackground( void renderBackground(
int bgArea, int bgCharacters, int bgArea, int bgCharacters,
int offsetX, int offsetY, int offsetX, int offsetY,
int scrollX, int scrollY,
const std::array<int, 4>& palette); const std::array<int, 4>& palette);
void renderObjects(); void renderObjects();
@ -52,7 +51,7 @@ namespace EightBit {
void renderTile( void renderTile(
int height, int height,
int drawX, int drawY, int offsetX, int offsetY, int drawX, int drawY,
bool flipX, bool flipY, bool allowTransparencies, bool flipX, bool flipY, bool allowTransparencies,
const std::array<int, 4>& palette, const std::array<int, 4>& palette,
const CharacterDefinition& definition); const CharacterDefinition& definition);

View File

@ -50,7 +50,7 @@ void EightBit::GameBoy::Display::renderObjects() {
void EightBit::GameBoy::Display::loadObjectAttributes() { void EightBit::GameBoy::Display::loadObjectAttributes() {
auto oamAddress = 0xfe00; const auto oamAddress = 0xfe00;
const auto control = m_bus.peekRegister(Bus::LCDC); const auto control = m_bus.peekRegister(Bus::LCDC);
const auto objBlockHeight = (control & Bus::ObjectBlockCompositionSelection) ? 16 : 8; const auto objBlockHeight = (control & Bus::ObjectBlockCompositionSelection) ? 16 : 8;
@ -66,8 +66,8 @@ void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) {
palettes[0] = createPalette(Bus::OBP0); palettes[0] = createPalette(Bus::OBP0);
palettes[1] = createPalette(Bus::OBP1); palettes[1] = createPalette(Bus::OBP1);
auto objDefinitionAddress = 0x8000; const auto objDefinitionAddress = 0x8000;
auto oamAddress = 0xfe00; const auto oamAddress = 0xfe00;
for (int i = 0; i < 40; ++i) { for (int i = 0; i < 40; ++i) {
@ -88,7 +88,7 @@ void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) {
renderTile( renderTile(
objBlockHeight, objBlockHeight,
spriteX, spriteY, -8, -16, spriteX - 8, spriteY - 16,
flipX, flipY, true, flipX, flipY, true,
palette, palette,
definition); definition);
@ -115,13 +115,12 @@ void EightBit::GameBoy::Display::renderBackground() {
const auto scrollX = m_bus.peekRegister(Bus::SCX); const auto scrollX = m_bus.peekRegister(Bus::SCX);
const auto scrollY = m_bus.peekRegister(Bus::SCY); const auto scrollY = m_bus.peekRegister(Bus::SCY);
renderBackground(bgArea, bgCharacters, offsetX, offsetY, scrollX, scrollY, palette); renderBackground(bgArea, bgCharacters, offsetX - scrollX, offsetY - scrollY, palette);
} }
void EightBit::GameBoy::Display::renderBackground( void EightBit::GameBoy::Display::renderBackground(
int bgArea, int bgCharacters, int bgArea, int bgCharacters,
int offsetX, int offsetY, int offsetX, int offsetY,
int scrollX, int scrollY,
const std::array<int, 4>& palette) { const std::array<int, 4>& palette) {
std::map<int, CharacterDefinition> definitions; std::map<int, CharacterDefinition> definitions;
@ -129,21 +128,19 @@ void EightBit::GameBoy::Display::renderBackground(
for (int row = 0; row < BufferCharacterHeight; ++row) { for (int row = 0; row < BufferCharacterHeight; ++row) {
for (int column = 0; column < BufferCharacterWidth; ++column) { for (int column = 0; column < BufferCharacterWidth; ++column) {
auto address = bgArea + row * BufferCharacterWidth + column; const auto address = bgArea + row * BufferCharacterWidth + column;
auto character = m_bus.peek(address); const auto character = m_bus.peek(address);
auto definitionPair = definitions.find(character); auto definitionPair = definitions.find(character);
if (definitionPair == definitions.end()) { if (definitionPair == definitions.end()) {
definitions[character] = CharacterDefinition(&m_bus, bgCharacters + 16 * character, 8); definitions[character] = CharacterDefinition(&m_bus, bgCharacters + 16 * character, 8);
definitionPair = definitions.find(character); definitionPair = definitions.find(character);
} }
auto definition = definitionPair->second; const auto definition = definitionPair->second;
renderTile( renderTile(
8, 8,
column * 8, row * 8, offsetX - scrollX, offsetY - scrollY, column * 8 + offsetX, row * 8 + offsetY,
false, false, false, false, false, false,
palette, palette,
definition); definition);
@ -153,30 +150,33 @@ void EightBit::GameBoy::Display::renderBackground(
void EightBit::GameBoy::Display::renderTile( void EightBit::GameBoy::Display::renderTile(
int height, int height,
int drawX, int drawY, int offsetX, int offsetY, int drawX, int drawY,
bool flipX, bool flipY, bool allowTransparencies, bool flipX, bool flipY, bool allowTransparencies,
const std::array<int, 4>& palette, const std::array<int, 4>& palette,
const CharacterDefinition& definition) { const CharacterDefinition& definition) {
const auto width = 8; const auto width = 8;
const auto flipMaskX = width - 1;
const auto flipMaskY = height - 1;
for (int cy = 0; cy < height; ++cy) { for (int cy = 0; cy < height; ++cy) {
uint8_t y = drawY + (flipY ? (height - 1) - cy : cy) + offsetY; const uint8_t y = drawY + (flipY ? ~cy & flipMaskY : cy);
if (y >= RasterHeight) if (y >= RasterHeight)
continue; continue;
auto rowDefinition = definition.get(cy); const auto rowDefinition = definition.get(cy);
for (int cx = 0; cx < width; ++cx) { for (int cx = 0; cx < width; ++cx) {
uint8_t x = drawX + (flipX ? (width - 1) - cx : cx) + offsetX; const uint8_t x = drawX + (flipX ? ~cx & flipMaskX : cx);
if (x >= RasterWidth) if (x >= RasterWidth)
break; break;
auto colour = rowDefinition[cx]; const auto colour = rowDefinition[cx];
if (!allowTransparencies || (allowTransparencies && (colour > 0))) { if (!allowTransparencies || (allowTransparencies && (colour > 0))) {
auto outputPixel = y * RasterWidth + x; const auto outputPixel = y * RasterWidth + x;
m_pixels[outputPixel] = m_colours->getColour(palette[colour]); m_pixels[outputPixel] = m_colours->getColour(palette[colour]);
} }
} }