2017-08-29 21:23:32 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "Display.h"
|
|
|
|
#include "Processor.h"
|
|
|
|
#include "CharacterDefinition.h"
|
2017-09-15 16:25:55 +00:00
|
|
|
#include "ObjectAttribute.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-10-04 17:00:53 +00:00
|
|
|
EightBit::GameBoy::Display::Display(const AbstractColourPalette* colours, Bus& bus, Ram& oam, Ram& vram)
|
2017-08-29 21:23:32 +00:00
|
|
|
: m_bus(bus),
|
2017-10-04 17:00:53 +00:00
|
|
|
m_oam(oam),
|
|
|
|
m_vram(vram),
|
2017-10-03 22:54:39 +00:00
|
|
|
m_colours(colours),
|
2017-10-03 23:30:59 +00:00
|
|
|
m_control(0),
|
|
|
|
m_scanLine(0) {
|
2017-08-29 21:23:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 00:15:28 +00:00
|
|
|
const std::vector<uint32_t>& EightBit::GameBoy::Display::pixels() const {
|
2017-08-29 21:23:32 +00:00
|
|
|
return m_pixels;
|
|
|
|
}
|
|
|
|
|
2017-09-07 00:15:28 +00:00
|
|
|
void EightBit::GameBoy::Display::initialise() {
|
2017-08-29 21:23:32 +00:00
|
|
|
m_pixels.resize(RasterWidth * RasterHeight);
|
|
|
|
}
|
|
|
|
|
2017-09-07 00:15:28 +00:00
|
|
|
void EightBit::GameBoy::Display::render() {
|
2017-10-04 00:28:33 +00:00
|
|
|
m_scanLine = m_bus.peekRegister(Bus::LY);
|
|
|
|
if (m_scanLine < RasterHeight) {
|
|
|
|
m_control = m_bus.peekRegister(Bus::LCDC);
|
|
|
|
if (m_control & Bus::LcdEnable) {
|
|
|
|
if (m_control & Bus::DisplayBackground)
|
|
|
|
renderBackground();
|
|
|
|
if (m_control & Bus::ObjectEnable)
|
|
|
|
renderObjects();
|
|
|
|
}
|
2017-09-15 16:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
std::array<int, 4> EightBit::GameBoy::Display::createPalette(const int address) {
|
|
|
|
const auto raw = m_bus.peekRegister(address);
|
|
|
|
std::array<int, 4> palette;
|
|
|
|
palette[0] = raw & 0b11;
|
|
|
|
palette[1] = (raw & 0b1100) >> 2;
|
|
|
|
palette[2] = (raw & 0b110000) >> 4;
|
|
|
|
palette[3] = (raw & 0b11000000) >> 6;
|
|
|
|
return palette;
|
|
|
|
}
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
void EightBit::GameBoy::Display::renderObjects() {
|
2017-10-03 23:30:59 +00:00
|
|
|
const auto objBlockHeight = (m_control & Bus::ObjectBlockCompositionSelection) ? 16 : 8;
|
2017-09-15 16:25:55 +00:00
|
|
|
renderObjects(objBlockHeight);
|
|
|
|
}
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-10-01 10:32:41 +00:00
|
|
|
void EightBit::GameBoy::Display::loadObjectAttributes() {
|
2017-10-03 23:30:59 +00:00
|
|
|
const auto objBlockHeight = (m_control & Bus::ObjectBlockCompositionSelection) ? 16 : 8;
|
2017-10-01 10:32:41 +00:00
|
|
|
for (int i = 0; i < 40; ++i) {
|
2017-10-04 17:00:53 +00:00
|
|
|
m_objectAttributes[i] = ObjectAttribute(m_oam, 4 * i, objBlockHeight);
|
2017-10-01 10:32:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
void EightBit::GameBoy::Display::renderObjects(int objBlockHeight) {
|
|
|
|
|
|
|
|
std::vector<std::array<int, 4>> palettes(2);
|
|
|
|
palettes[0] = createPalette(Bus::OBP0);
|
|
|
|
palettes[1] = createPalette(Bus::OBP1);
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
for (int i = 0; i < 40; ++i) {
|
2017-09-15 17:23:02 +00:00
|
|
|
|
2017-10-01 10:32:41 +00:00
|
|
|
const auto& current = m_objectAttributes[i];
|
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
const auto spriteY = current.positionY();
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto drawY = spriteY - 16;
|
2017-10-01 10:32:41 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
if ((m_scanLine >= drawY) && (m_scanLine < (drawY + objBlockHeight))) {
|
2017-10-01 10:32:41 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto spriteX = current.positionX();
|
|
|
|
const auto drawX = spriteX - 8;
|
2017-10-01 10:32:41 +00:00
|
|
|
|
2017-10-04 14:37:11 +00:00
|
|
|
const auto sprite = current.pattern();
|
2017-10-04 17:00:53 +00:00
|
|
|
const auto definition = CharacterDefinition(&m_vram, 16 * sprite, objBlockHeight);
|
2017-10-01 10:32:41 +00:00
|
|
|
const auto& palette = palettes[current.palette()];
|
|
|
|
const auto flipX = current.flipX();
|
|
|
|
const auto flipY = current.flipY();
|
|
|
|
|
|
|
|
renderTile(
|
|
|
|
objBlockHeight,
|
2017-10-04 00:28:33 +00:00
|
|
|
drawX, drawY,
|
2017-10-01 10:32:41 +00:00
|
|
|
flipX, flipY, true,
|
|
|
|
palette,
|
|
|
|
definition);
|
|
|
|
}
|
2017-09-15 16:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::GameBoy::Display::renderBackground() {
|
|
|
|
|
|
|
|
auto palette = createPalette(Bus::BGP);
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-10-03 23:30:59 +00:00
|
|
|
const auto window = (m_control & Bus::WindowEnable) != 0;
|
2017-10-04 14:37:11 +00:00
|
|
|
const auto bgArea = (m_control & Bus::BackgroundCodeAreaSelection) ? 0x1c00 : 0x1800;
|
|
|
|
const auto bgCharacters = (m_control & Bus::BackgroundCharacterDataSelection) ? 0 : 0x800;
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
const auto wx = m_bus.peekRegister(Bus::WX);
|
|
|
|
const auto wy = m_bus.peekRegister(Bus::WY);
|
2017-08-29 21:23:32 +00:00
|
|
|
|
2017-09-15 16:25:55 +00:00
|
|
|
const auto offsetX = window ? wx - 7 : 0;
|
|
|
|
const auto offsetY = window ? wy : 0;
|
|
|
|
|
|
|
|
const auto scrollX = m_bus.peekRegister(Bus::SCX);
|
|
|
|
const auto scrollY = m_bus.peekRegister(Bus::SCY);
|
|
|
|
|
2017-10-02 14:28:41 +00:00
|
|
|
renderBackground(bgArea, bgCharacters, offsetX - scrollX, offsetY - scrollY, palette);
|
2017-09-15 16:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EightBit::GameBoy::Display::renderBackground(
|
|
|
|
int bgArea, int bgCharacters,
|
|
|
|
int offsetX, int offsetY,
|
|
|
|
const std::array<int, 4>& palette) {
|
|
|
|
|
2017-10-03 23:30:59 +00:00
|
|
|
const int row = (m_scanLine - offsetY) / 8;
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto baseAddress = bgArea + row * BufferCharacterWidth;
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-10-03 22:54:39 +00:00
|
|
|
for (int column = 0; column < BufferCharacterWidth; ++column) {
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto address = baseAddress + column;
|
2017-10-04 17:00:53 +00:00
|
|
|
const auto character = m_vram.peek(address);
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-10-04 17:00:53 +00:00
|
|
|
const auto definition = CharacterDefinition(&m_vram, bgCharacters + 16 * character, 8);
|
2017-10-03 22:54:39 +00:00
|
|
|
renderTile(
|
|
|
|
8,
|
|
|
|
column * 8 + offsetX, row * 8 + offsetY,
|
|
|
|
false, false, false,
|
|
|
|
palette,
|
|
|
|
definition);
|
2017-09-15 17:23:02 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-09-15 17:23:02 +00:00
|
|
|
void EightBit::GameBoy::Display::renderTile(
|
2017-10-04 00:28:33 +00:00
|
|
|
const int height,
|
|
|
|
const int drawX, const int drawY,
|
|
|
|
const bool flipX, const bool flipY, const bool allowTransparencies,
|
2017-09-15 17:23:02 +00:00
|
|
|
const std::array<int, 4>& palette,
|
|
|
|
const CharacterDefinition& definition) {
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-09-17 08:46:28 +00:00
|
|
|
const auto width = 8;
|
2017-09-15 16:25:55 +00:00
|
|
|
|
2017-10-02 14:28:41 +00:00
|
|
|
const auto flipMaskX = width - 1;
|
|
|
|
const auto flipMaskY = height - 1;
|
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto y = m_scanLine;
|
2017-09-17 08:46:28 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
auto cy = y - drawY;
|
|
|
|
if (flipY)
|
|
|
|
cy = ~cy & flipMaskY;
|
2017-10-03 22:54:39 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto rowDefinition = definition.get(cy);
|
2017-10-02 09:12:25 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto lineAddress = y * RasterWidth;
|
|
|
|
for (int cx = 0; cx < width; ++cx) {
|
2017-09-15 17:23:02 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const uint8_t x = drawX + (flipX ? ~cx & flipMaskX : cx);
|
|
|
|
if (x >= RasterWidth)
|
|
|
|
break;
|
2017-09-15 17:23:02 +00:00
|
|
|
|
2017-10-04 00:28:33 +00:00
|
|
|
const auto colour = rowDefinition[cx];
|
|
|
|
if (!allowTransparencies || (allowTransparencies && (colour > 0))) {
|
|
|
|
const auto outputPixel = lineAddress + x;
|
|
|
|
m_pixels[outputPixel] = m_colours->getColour(palette[colour]);
|
2017-08-29 21:23:32 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 17:23:02 +00:00
|
|
|
}
|