redesigned display peripheral

This commit is contained in:
2025-03-14 15:48:15 -04:00
parent af39b2ab07
commit ac5313a6ca
5 changed files with 63 additions and 53 deletions

View File

@@ -1,8 +1,10 @@
#include "DisplaySDL.hpp"
#include "Peripherals.hpp"
#include <stdexcept>
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor) {
DisplaySDL::DisplaySDL(int w, int h, uint32_t fgColor, uint32_t bgColor):
mpFramebuffer{std::make_unique<Framebuffer>()} {
// Create SDL Window
mpWindow = SDL_CreateWindow(
"ChocoChip-8",
@@ -35,43 +37,48 @@ DisplaySDL::~DisplaySDL() {
SDL_DestroyWindow(mpWindow);
}
void DisplaySDL::updateWindow() const {
void DisplaySDL::clear() {
for(auto &scanline : *mpFramebuffer) {
scanline.reset();
}
SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor);
}
int DisplaySDL::blit(const chocochip8::Scanline &spriteScanline, int y) {
using chocochip8::Scanline;
Scanline &targetScanline = mpFramebuffer->at(y);
Scanline bitsToSet = spriteScanline & ~targetScanline;
Scanline bitsToReset = spriteScanline & targetScanline;
targetScanline ^= spriteScanline;
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
SDL_LockSurface(pSurface);
SDL_Rect rect;
// Map framebuffer pixel to SDL surface rectangle
auto mapChipxelToPixel = [&rect, pSurface](int x, int y) {
// Note that the MSB of an scanline's bitset is the leftmost pixel.
x = (chocochip8::gcWidth - 1) - x;
int x1 = ( x * pSurface->w) / chocochip8::gcWidth;
int x2 = ((x + 1) * pSurface->w) / chocochip8::gcWidth;
int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
rect.x = x1;
rect.y = y1;
rect.w = x2 - x1;
rect.h = y2 - y1;
};
int fx, fy; // ChocoChip8 Framebuffer coordinates
int sx, sy; // SDL Surface coordinates
int lx, ly; // Last-used ChocoChip8 Framebuffer coordinates
Uint32 lc; // Last-used SDL color
// Fill the entire SDL surface, one pixel at a time
lx = -1;
ly = -1;
for(sy = 0; sy < pSurface->h; sy++) {
for(sx = 0; sx < pSurface->w; sx++) {
// Map SDL surface coordinates to Chocochip8 Framebuffer coordinates
fx = sx * (double(chocochip8::gcWidth) / pSurface->w);
fy = sy * (double(chocochip8::gcHeight) / pSurface->h);
// Reuse color if this screen pixel maps to the same ChocoChip8 pixel as the last
if(fx != lx || fy != ly) {
lx = fx;
ly = ly;
// Read Chocochip8 Framebuffer and choose appropriate color,
// note that the MSB of an scanline's bitset is the leftmost pixel.
lc = (mpFramebuffer->at(fy)[(chocochip8::gcWidth - 1) - fx] ? mFgColor : mBgColor);
}
// Convert (x, y) indexes into SDL Surface pixel array index
Uint32 *pPixel = static_cast<Uint32*>(static_cast<void*>(
static_cast<char*>(pSurface->pixels)
+ sy * pSurface->pitch
+ sx * pSurface->format->BytesPerPixel
));
*pPixel = lc;
}
for(int x = bitsToReset._Find_first(); x < bitsToReset.size(); x = bitsToReset._Find_next(x)) {
mapChipxelToPixel(x, y);
SDL_FillRect(pSurface, &rect, mBgColor);
}
for(int x = bitsToSet._Find_first(); x < bitsToSet.size(); x = bitsToSet._Find_next(x)) {
mapChipxelToPixel(x, y);
SDL_FillRect(pSurface, &rect, mFgColor);
}
SDL_UnlockSurface(pSurface);
return bitsToReset.any();
}
void DisplaySDL::updateWindow() const {
SDL_UpdateWindowSurface(mpWindow);
}