optimized display update region

This commit is contained in:
2025-03-14 02:50:01 -04:00
parent af39b2ab07
commit 4f7083d7c7
4 changed files with 47 additions and 10 deletions

View File

@@ -36,6 +36,11 @@ DisplaySDL::~DisplaySDL() {
}
void DisplaySDL::updateWindow() const {
if(miTopDirtyScanline > miBottomDirtyScanline) {
// No changes since the last update
return;
}
SDL_Surface *pSurface = SDL_GetWindowSurface(mpWindow);
SDL_LockSurface(pSurface);
@@ -48,10 +53,15 @@ void DisplaySDL::updateWindow() const {
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) {
// Skip scanlines that haven't changed since the last update
continue;
}
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) {
@@ -72,6 +82,8 @@ void DisplaySDL::updateWindow() const {
}
}
miTopDirtyScanline = chocochip8::gcHeight - 1;
miBottomDirtyScanline = 0;
SDL_UnlockSurface(pSurface);
SDL_UpdateWindowSurface(mpWindow);
}