display now only draws chipxels that have changed
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
#include "DisplaySDL.hpp"
|
||||
#include "Peripherals.hpp"
|
||||
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <stdexcept>
|
||||
|
||||
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
|
||||
mpWindow = SDL_CreateWindow(
|
||||
"ChocoChip-8",
|
||||
@@ -41,44 +44,65 @@ void DisplaySDL::clear() {
|
||||
for(auto &scanline : *mpFramebuffer) {
|
||||
scanline.reset();
|
||||
}
|
||||
SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor);
|
||||
mDoClear = true;
|
||||
}
|
||||
|
||||
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;
|
||||
bool collision = (spriteScanline & targetScanline).any();
|
||||
targetScanline ^= spriteScanline;
|
||||
return collision;
|
||||
}
|
||||
|
||||
void DisplaySDL::updateWindow(bool doWindowUpdate) const {
|
||||
using chocochip8::Scanline;
|
||||
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
|
||||
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;
|
||||
if(mDoClear) {
|
||||
for(auto &scanline : *mpDisplayState) {
|
||||
scanline.reset();
|
||||
}
|
||||
SDL_FillRect(pSurface, NULL, mBgColor);
|
||||
mDoClear = false;
|
||||
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 y2 = ((y + 1) * pSurface->h) / chocochip8::gcHeight;
|
||||
SDL_Rect rect;
|
||||
rect.x = x1;
|
||||
rect.y = y1;
|
||||
rect.w = x2 - x1;
|
||||
rect.h = y2 - y1;
|
||||
};
|
||||
|
||||
for(int x = bitsToReset._Find_first(); x < bitsToReset.size(); x = bitsToReset._Find_next(x)) {
|
||||
mapChipxelToPixel(x, y);
|
||||
SDL_FillRect(pSurface, &rect, mBgColor);
|
||||
Uint32 color = (isSet ? mFgColor : mBgColor);
|
||||
SDL_FillRect(pSurface, &rect, color);
|
||||
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();
|
||||
}
|
||||
|
||||
void DisplaySDL::updateWindow() const {
|
||||
if(doWindowUpdate) {
|
||||
*mpDisplayState = *mpFramebuffer;
|
||||
SDL_UpdateWindowSurface(mpWindow);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,12 +13,14 @@ public:
|
||||
~DisplaySDL() override;
|
||||
void clear() override;
|
||||
int blit(const chocochip8::Scanline &scanline, int y) override;
|
||||
void updateWindow() const;
|
||||
void updateWindow(bool forceWindowUpdate = false) const;
|
||||
|
||||
private:
|
||||
using Framebuffer = std::array<chocochip8::Scanline, chocochip8::gcHeight>;
|
||||
std::unique_ptr<Framebuffer> mpFramebuffer;
|
||||
mutable std::unique_ptr<Framebuffer> mpDisplayState;
|
||||
SDL_Window *mpWindow;
|
||||
Uint32 mFgColor;
|
||||
Uint32 mBgColor;
|
||||
mutable bool mDoClear;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user