More scan line rendering changes.

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon 2017-10-02 10:12:25 +01:00
parent 10913bf1b8
commit 8b1da2097b
5 changed files with 66 additions and 43 deletions

View File

@ -1,47 +1,24 @@
#pragma once
#include <cstdint>
#include <vector>
#include <Bus.h>
#include <array>
namespace EightBit {
class Bus;
namespace GameBoy {
class CharacterDefinition {
public:
CharacterDefinition() {}
CharacterDefinition();
CharacterDefinition(EightBit::Bus* bus, uint16_t address, int height);
CharacterDefinition(Bus& bus, uint16_t address, int height) {
const int width = 8;
m_definition.resize(width * height);
for (auto row = 0; row < height; ++row) {
auto planeAddress = address + row * 2;
auto planeLow = bus.peek(planeAddress);
auto planeHigh = bus.peek(planeAddress + 1);
for (int bit = 0; bit < width; ++bit) {
auto mask = 1 << bit;
auto bitLow = planeLow & mask ? 1 : 0;
auto bitHigh = planeHigh & mask ? 0b10 : 0;
auto colour = bitHigh | bitLow;
m_definition[row * width + ((width - 1) - bit)] = colour;
}
}
}
const std::vector<int>& get() const { return m_definition; }
std::array<int, 8> get(int row) const;
private:
std::vector<int> m_definition;
EightBit::Bus* m_bus;
uint16_t m_address;
int m_height;
};
}
}

View File

@ -0,0 +1,40 @@
#include "stdafx.h"
#include "CharacterDefinition.h"
#include <Bus.h>
EightBit::GameBoy::CharacterDefinition::CharacterDefinition()
: m_bus(nullptr),
m_address(~0),
m_height(0) {
}
EightBit::GameBoy::CharacterDefinition::CharacterDefinition(EightBit::Bus* bus, uint16_t address, int height)
: m_bus(bus),
m_address(address),
m_height(height) {
}
std::array<int, 8> EightBit::GameBoy::CharacterDefinition::get(int row) const {
std::array<int, 8> returned;
auto planeAddress = m_address + row * 2;
auto planeLow = m_bus->peek(planeAddress);
auto planeHigh = m_bus->peek(planeAddress + 1);
for (int bit = 0; bit < 8; ++bit) {
auto mask = 1 << bit;
auto bitLow = planeLow & mask ? 1 : 0;
auto bitHigh = planeHigh & mask ? 0b10 : 0;
auto colour = bitHigh | bitLow;
returned[7 - bit] = colour;
}
return returned;
}

View File

@ -81,7 +81,7 @@ void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) {
if (!hidden) {
const auto definition = CharacterDefinition(m_bus, objDefinitionAddress + 16 * sprite, objBlockHeight);
const auto definition = CharacterDefinition(&m_bus, objDefinitionAddress + 16 * sprite, objBlockHeight);
const auto& palette = palettes[current.palette()];
const auto flipX = current.flipX();
const auto flipY = current.flipY();
@ -135,7 +135,7 @@ void EightBit::GameBoy::Display::renderBackground(
auto definitionPair = definitions.find(character);
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);
}
@ -162,21 +162,23 @@ void EightBit::GameBoy::Display::renderTile(
for (int cy = 0; cy < height; ++cy) {
for (int cx = 0; cx < width; ++cx) {
uint8_t y = drawY + (flipY ? (height - 1) - cy : cy) + offsetY;
if (y >= RasterHeight)
continue;
uint8_t y = drawY + (flipY ? (height - 1) - cy : cy) + offsetY;
if (y >= RasterHeight)
break;
auto rowDefinition = definition.get(cy);
for (int cx = 0; cx < width; ++cx) {
uint8_t x = drawX + (flipX ? (width - 1) - cx : cx) + offsetX;
if (x >= RasterWidth)
break;
auto outputPixel = y * RasterWidth + x;
auto colour = definition.get()[cy * width + cx];
if (!allowTransparencies || (allowTransparencies && (colour > 0)))
auto colour = rowDefinition[cx];
if (!allowTransparencies || (allowTransparencies && (colour > 0))) {
auto outputPixel = y * RasterWidth + x;
m_pixels[outputPixel] = m_colours->getColour(palette[colour]);
}
}
}
}

View File

@ -150,6 +150,7 @@
<ClInclude Include="stdafx.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CharacterDefinition.cpp" />
<ClCompile Include="GameBoyBus.cpp" />
<ClCompile Include="Disassembler.cpp" />
<ClCompile Include="Display.cpp" />

View File

@ -58,5 +58,8 @@
<ClCompile Include="GameBoyBus.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CharacterDefinition.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>