From fe87646e624f9199d15c4673c751158250546e9f Mon Sep 17 00:00:00 2001 From: Scott Duensing Date: Wed, 12 Oct 2022 16:31:24 -0500 Subject: [PATCH] Window scaling fixed in SDL 1.2 backend. --- joeylib/src/jSDL12.c | 113 ++++++++++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 34 deletions(-) diff --git a/joeylib/src/jSDL12.c b/joeylib/src/jSDL12.c index b2f2655..f786446 100644 --- a/joeylib/src/jSDL12.c +++ b/joeylib/src/jSDL12.c @@ -93,7 +93,7 @@ static jbool _jlModPlaying = jfalse; static jlPlatformModT *_jlModCurrent = NULL; static jbyte _jlModVolume = 255; static jlSoundPlayingT *_jlSoundList = NULL; -static jint16 _jlWindowZoom = 2; +static jint16 _jlWindowZoom = 3; static Uint32 _jlUtilTimer(Uint32 interval, void *param); @@ -103,10 +103,10 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes); extern jbool _jlSwapChannels; -/* #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wcast-align" +/* Uint32 _jlGetPixel(SDL_Surface *surface, int x, int y) { int bpp = surface->format->BytesPerPixel; @@ -138,6 +138,7 @@ Uint32 _jlGetPixel(SDL_Surface *surface, int x, int y) { return 0; } } +*/ void _jlPutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) { @@ -173,7 +174,6 @@ void _jlPutPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) { } #pragma GCC diagnostic pop -*/ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) { @@ -257,44 +257,89 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) { void jlDisplayPresent(void) { - int i = 0; - int j = 0; - Uint16 *pixels = NULL; - SDL_Rect r; jlUtilIdle(); - // Render 4 bit copy to proper pixel format. - // This extra step preserves palette effects. - SDL_LockSurface(_jlTexture); - pixels = (Uint16 *)_jlTexture->pixels; - for (int y=0; y<200; y++) { - for (int x=0; x<160; x++) { - // We decode this R/L instead of L/R for some reason. NO idea why yet. Endians? - pixels[i++] = SDL_MapRGBA(_jlTexture->format, - _jlBackingStore->palette[_jlBackingStore->pixels[j].r].r * 16, - _jlBackingStore->palette[_jlBackingStore->pixels[j].r].g * 16, - _jlBackingStore->palette[_jlBackingStore->pixels[j].r].b * 16, - 255); - pixels[i++] = SDL_MapRGBA(_jlTexture->format, - _jlBackingStore->palette[_jlBackingStore->pixels[j].l].r * 16, - _jlBackingStore->palette[_jlBackingStore->pixels[j].l].g * 16, - _jlBackingStore->palette[_jlBackingStore->pixels[j].l].b * 16, - 255); - j++; - } - } - SDL_UnlockSurface(_jlTexture); + jint16 sourceX; + jint16 sourceY; + jint16 targetX; + jint16 targetY; + juint16 pixelLeft; + juint16 pixelRight; + juint16 p = 0; + + SDL_LockSurface(_jlWindow); // Border. SDL_FillRect(_jlWindow, NULL, SDL_MapRGB(_jlWindow->format, _jlBorderRGBs[_jlBorderColor][0], _jlBorderRGBs[_jlBorderColor][1], _jlBorderRGBs[_jlBorderColor][2])); - // Display. - r.x = _jlWindowZoom * BORDER_WIDTH; - r.y = _jlWindowZoom * BORDER_WIDTH; - r.w = 320 * _jlWindowZoom; - r.h = 200 * _jlWindowZoom; - SDL_BlitSurface(_jlTexture, NULL, _jlWindow, &r); + // Render 4 bit copy to proper pixel format. + // This extra step preserves palette effects. + targetY = _jlWindowZoom * BORDER_WIDTH; + for (sourceY = 0; sourceY < 200; sourceY++) { + targetX = _jlWindowZoom * BORDER_WIDTH; + for (sourceX = 0; sourceX < 160; sourceX++) { + // We decode this R/L instead of L/R for some reason. NO idea why yet. Endians? + pixelLeft = SDL_MapRGBA(_jlWindow->format, + _jlBackingStore->palette[_jlBackingStore->pixels[p].r].r * 16, + _jlBackingStore->palette[_jlBackingStore->pixels[p].r].g * 16, + _jlBackingStore->palette[_jlBackingStore->pixels[p].r].b * 16, + 255); + pixelRight = SDL_MapRGBA(_jlWindow->format, + _jlBackingStore->palette[_jlBackingStore->pixels[p].l].r * 16, + _jlBackingStore->palette[_jlBackingStore->pixels[p].l].g * 16, + _jlBackingStore->palette[_jlBackingStore->pixels[p].l].b * 16, + 255); + // Copy to display. SDL 1.2 does not scale surfaces, so we have to do it manually. + switch (_jlWindowZoom) { + case 1: + _jlPutPixel(_jlWindow, targetX, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY, pixelRight); + break; + + case 2: + _jlPutPixel(_jlWindow, targetX, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX, targetY + 1, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY + 1, pixelLeft); + + _jlPutPixel(_jlWindow, targetX + 2, targetY, pixelRight); + _jlPutPixel(_jlWindow, targetX + 3, targetY, pixelRight); + _jlPutPixel(_jlWindow, targetX + 2, targetY + 1, pixelRight); + _jlPutPixel(_jlWindow, targetX + 3, targetY + 1, pixelRight); + break; + + case 3: + // There is no way this is efficient. + _jlPutPixel(_jlWindow, targetX, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 2, targetY, pixelLeft); + _jlPutPixel(_jlWindow, targetX, targetY + 1, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY + 1, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 2, targetY + 1, pixelLeft); + _jlPutPixel(_jlWindow, targetX, targetY + 2, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 1, targetY + 2, pixelLeft); + _jlPutPixel(_jlWindow, targetX + 2, targetY + 2, pixelLeft); + + _jlPutPixel(_jlWindow, targetX + 3, targetY, pixelRight); + _jlPutPixel(_jlWindow, targetX + 4, targetY, pixelRight); + _jlPutPixel(_jlWindow, targetX + 5, targetY, pixelRight); + _jlPutPixel(_jlWindow, targetX + 3, targetY + 1, pixelRight); + _jlPutPixel(_jlWindow, targetX + 4, targetY + 1, pixelRight); + _jlPutPixel(_jlWindow, targetX + 5, targetY + 1, pixelRight); + _jlPutPixel(_jlWindow, targetX + 3, targetY + 2, pixelRight); + _jlPutPixel(_jlWindow, targetX + 4, targetY + 2, pixelRight); + _jlPutPixel(_jlWindow, targetX + 5, targetY + 2, pixelRight); + break; + } + // Increment pixel locations. + p++; + targetX += _jlWindowZoom + _jlWindowZoom; + } + targetY += _jlWindowZoom; + } + + SDL_UnlockSurface(_jlWindow); SDL_Flip(_jlWindow); }