display now only draws chipxels that have changed

This commit is contained in:
2025-03-14 17:35:31 -04:00
parent ac5313a6ca
commit 22526c1b90
2 changed files with 51 additions and 25 deletions

View File

@@ -1,10 +1,13 @@
#include "DisplaySDL.hpp" #include "DisplaySDL.hpp"
#include "Peripherals.hpp" #include "Peripherals.hpp"
#include <SDL2/SDL_surface.h>
#include <stdexcept> #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>()} { mpFramebuffer{std::make_unique<Framebuffer>()},
mpDisplayState{std::make_unique<Framebuffer>()},
mDoClear{true} {
// Create SDL Window // Create SDL Window
mpWindow = SDL_CreateWindow( mpWindow = SDL_CreateWindow(
"ChocoChip-8", "ChocoChip-8",
@@ -41,44 +44,65 @@ void DisplaySDL::clear() {
for(auto &scanline : *mpFramebuffer) { for(auto &scanline : *mpFramebuffer) {
scanline.reset(); scanline.reset();
} }
SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor); mDoClear = true;
} }
int DisplaySDL::blit(const chocochip8::Scanline &spriteScanline, int y) { int DisplaySDL::blit(const chocochip8::Scanline &spriteScanline, int y) {
using chocochip8::Scanline; using chocochip8::Scanline;
Scanline &targetScanline = mpFramebuffer->at(y); Scanline &targetScanline = mpFramebuffer->at(y);
Scanline bitsToSet = spriteScanline & ~targetScanline; bool collision = (spriteScanline & targetScanline).any();
Scanline bitsToReset = spriteScanline & targetScanline;
targetScanline ^= spriteScanline; targetScanline ^= spriteScanline;
return collision;
}
void DisplaySDL::updateWindow(bool doWindowUpdate) const {
using chocochip8::Scanline;
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow); SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
SDL_Rect rect; if(mDoClear) {
// Map framebuffer pixel to SDL surface rectangle for(auto &scanline : *mpDisplayState) {
auto mapChipxelToPixel = [&rect, pSurface](int x, int y) { scanline.reset();
// Note that the MSB of an scanline's bitset is the leftmost pixel. }
x = (chocochip8::gcWidth - 1) - x; SDL_FillRect(pSurface, NULL, mBgColor);
int x1 = ( x * pSurface->w) / chocochip8::gcWidth; mDoClear = false;
int x2 = ((x + 1) * pSurface->w) / chocochip8::gcWidth; doWindowUpdate = true;
}
for(int y = 0; y < mpFramebuffer->size(); y++) {
Scanline &rNewScanline = (*mpFramebuffer)[y];
Scanline &rOldScanline = (*mpDisplayState)[y];
if(rNewScanline == rOldScanline) {
// Skip scanlines that haven't changed since the last update
continue;
}
for(int x = 0; x < rNewScanline.size(); x++) {
bool isSet = rNewScanline._Unchecked_test(x);
bool wasSet = rOldScanline._Unchecked_test(x);
if(isSet == wasSet) {
continue;
}
// Map framebuffer pixel to SDL surface rectangle,
// note that the MSB of an scanline's bitset is the leftmost pixel.
int z = (chocochip8::gcWidth - 1) - x;
int x1 = ( z * pSurface->w) / chocochip8::gcWidth;
int x2 = ((z + 1) * pSurface->w) / chocochip8::gcWidth;
int y1 = ( y * pSurface->h) / chocochip8::gcHeight; int y1 = ( y * pSurface->h) / chocochip8::gcHeight;
int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight; int y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
SDL_Rect rect;
rect.x = x1; rect.x = x1;
rect.y = y1; rect.y = y1;
rect.w = x2 - x1; rect.w = x2 - x1;
rect.h = y2 - y1; rect.h = y2 - y1;
};
for(int x = bitsToReset._Find_first(); x < bitsToReset.size(); x = bitsToReset._Find_next(x)) { Uint32 color = (isSet ? mFgColor : mBgColor);
mapChipxelToPixel(x, y); SDL_FillRect(pSurface, &rect, color);
SDL_FillRect(pSurface, &rect, mBgColor); doWindowUpdate = true;
} }
for(int x = bitsToSet._Find_first(); x < bitsToSet.size(); x = bitsToSet._Find_next(x)) {
mapChipxelToPixel(x, y);
SDL_FillRect(pSurface, &rect, mFgColor);
} }
return bitsToReset.any(); if(doWindowUpdate) {
} *mpDisplayState = *mpFramebuffer;
void DisplaySDL::updateWindow() const {
SDL_UpdateWindowSurface(mpWindow); SDL_UpdateWindowSurface(mpWindow);
} }
}

View File

@@ -13,12 +13,14 @@ public:
~DisplaySDL() override; ~DisplaySDL() override;
void clear() override; void clear() override;
int blit(const chocochip8::Scanline &scanline, int y) override; int blit(const chocochip8::Scanline &scanline, int y) override;
void updateWindow() const; void updateWindow(bool forceWindowUpdate = false) const;
private: private:
using Framebuffer = std::array<chocochip8::Scanline, chocochip8::gcHeight>; using Framebuffer = std::array<chocochip8::Scanline, chocochip8::gcHeight>;
std::unique_ptr<Framebuffer> mpFramebuffer; std::unique_ptr<Framebuffer> mpFramebuffer;
mutable std::unique_ptr<Framebuffer> mpDisplayState;
SDL_Window *mpWindow; SDL_Window *mpWindow;
Uint32 mFgColor; Uint32 mFgColor;
Uint32 mBgColor; Uint32 mBgColor;
mutable bool mDoClear;
}; };