From 1d3e843653cc43d1218bb6a24b027a586dc9c30a Mon Sep 17 00:00:00 2001 From: Pablo Rodriguez Date: Fri, 14 Mar 2025 04:53:47 -0400 Subject: [PATCH] display now uses SDL_FillRect --- DisplaySDL.cpp | 58 ++++++++++++++++++++++---------------------------- DisplaySDL.hpp | 1 + 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/DisplaySDL.cpp b/DisplaySDL.cpp index 0949998..c3b5fa1 100644 --- a/DisplaySDL.cpp +++ b/DisplaySDL.cpp @@ -35,6 +35,14 @@ DisplaySDL::~DisplaySDL() { SDL_DestroyWindow(mpWindow); } +void DisplaySDL::clear() { + Display::clear(); + SDL_FillRect(SDL_GetWindowSurface(mpWindow), nullptr, mBgColor); + SDL_UpdateWindowSurface(mpWindow); + miTopDirtyScanline = chocochip8::gcHeight - 1; + miBottomDirtyScanline = 0; +} + void DisplaySDL::updateWindow() const { if(miTopDirtyScanline > miBottomDirtyScanline) { // No changes since the last update @@ -42,48 +50,32 @@ void DisplaySDL::updateWindow() const { } SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow); - SDL_LockSurface(pSurface); - - 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++) { - // Map SDL surface coordinates to Chocochip8 Framebuffer coordinates - fy = sy * (double(chocochip8::gcHeight) / pSurface->h); - if(fy < miTopDirtyScanline || miBottomDirtyScanline < fy) { + for(int y = 0; y < chocochip8::gcHeight; y++) { + if(y < miTopDirtyScanline || miBottomDirtyScanline < y) { // Skip scanlines that haven't changed since the last update continue; } - for(sx = 0; sx < pSurface->w; sx++) { - fx = sx * (double(chocochip8::gcWidth) / pSurface->w); + for(int x = 0; x < chocochip8::gcWidth; x++) { + // Map framebuffer pixel to SDL surface rectangle + 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; + SDL_Rect rect; + rect.x = x1; + rect.y = y1; + rect.w = x2 - x1; + rect.h = y2 - y1; - // 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(static_cast( - static_cast(pSurface->pixels) - + sy * pSurface->pitch - + sx * pSurface->format->BytesPerPixel - )); - *pPixel = lc; + // Read Chocochip8 Framebuffer and choose appropriate color, + // note that the MSB of an scanline's bitset is the leftmost pixel. + Uint32 color = (mpFramebuffer->at(y)[(chocochip8::gcWidth - 1) - x] ? mFgColor : mBgColor); + SDL_FillRect(pSurface, &rect, color); } } miTopDirtyScanline = chocochip8::gcHeight - 1; miBottomDirtyScanline = 0; - SDL_UnlockSurface(pSurface); SDL_UpdateWindowSurface(mpWindow); } diff --git a/DisplaySDL.hpp b/DisplaySDL.hpp index 6fd87e9..40e3a0a 100644 --- a/DisplaySDL.hpp +++ b/DisplaySDL.hpp @@ -9,6 +9,7 @@ class DisplaySDL : public chocochip8::Display { public: DisplaySDL(int w, int h, uint32_t fgCol = 0xffffff, uint32_t bgCol = 0x000000); ~DisplaySDL() override; + void clear() override; void updateWindow() const; private: