sdl12-compat: Revert "video: Don't ever simulate a hardware palette."

From 35488c6894cb55ae4019392022c418f54dc3eb42 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 14 Oct 2022 13:35:50 -0400
Subject: [PATCH] Revert "video: Don't ever simulate a hardware palette."

This reverts commit 9da8808c03e80a228ee66a714091249f331ebcd7.

Fixes #238.
Fixes #244.
Fixes #246.
---
 src/SDL12_compat.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 468322693..d502c00d1 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -954,6 +954,7 @@ static SDL_Renderer *VideoRenderer20 = NULL;
 static SDL_mutex *VideoRendererLock = NULL;
 static SDL_Texture *VideoTexture20 = NULL;
 static SDL12_Surface *VideoSurface12 = NULL;
+static SDL_Palette *VideoPhysicalPalette20 = NULL;
 static Uint32 VideoSurfacePresentTicks = 0;
 static Uint32 VideoSurfaceLastPresentTicks = 0;
 static SDL_Surface *VideoConvertSurface20 = NULL;
@@ -5338,6 +5339,10 @@ EndVidModeCreate(void)
         SDL20_DestroyWindow(VideoWindow20);
         VideoWindow20 = NULL;
     }
+    if (VideoPhysicalPalette20) {
+        SDL20_FreePalette(VideoPhysicalPalette20);
+        VideoPhysicalPalette20 = NULL;
+    }
     if (VideoSurface12) {
         SDL20_free(VideoSurface12->pixels);
         VideoSurface12->pixels = NULL;
@@ -6121,6 +6126,13 @@ SetVideoModeImpl(int width, int height, int bpp, Uint32 flags12)
                 { const int x = (i & 0x3) | ((i & 0x3) << 2); color->b = x | x << 4; }
                 color->a = 255;
             }
+            if (!VideoPhysicalPalette20) {
+                VideoPhysicalPalette20 = SDL20_AllocPalette(256);
+                if (!VideoPhysicalPalette20) {
+                    return EndVidModeCreate();
+                }
+            }
+            SDL20_SetPaletteColors(VideoPhysicalPalette20, VideoSurface12->format->palette->colors, 0, 256);
         }
     }
 
@@ -6669,6 +6681,7 @@ SDL_UpdateRects(SDL12_Surface *surface12, int numrects, SDL12_Rect *rects12)
      * don't handle it correctly, so we have to work around it. */
     if (surface12 == VideoSurface12) {
         const SDL_bool upload_later = (!ThisIsSetVideoModeThread && !AllowThreadedDraws) ? SDL_TRUE : SDL_FALSE;
+        SDL_Palette *logicalPal = surface12->surface20->format->palette;
         const int pixsize = surface12->format->BytesPerPixel;
         const int srcpitch = surface12->pitch;
         SDL_bool whole_screen = SDL_FALSE;
@@ -6698,6 +6711,7 @@ SDL_UpdateRects(SDL12_Surface *surface12, int numrects, SDL12_Rect *rects12)
                 dstrect20.x = dstrect20.y = 0;
                 dstrect20.w = rect20.w;
                 dstrect20.h = rect20.h;
+                surface12->surface20->format->palette = VideoPhysicalPalette20;
                 VideoConvertSurface20->pixels = pixels;
                 VideoConvertSurface20->pitch = pitch;
                 VideoConvertSurface20->w = rect20.w;
@@ -6718,6 +6732,7 @@ SDL_UpdateRects(SDL12_Surface *surface12, int numrects, SDL12_Rect *rects12)
         }
 
         if (VideoConvertSurface20) {  /* reset some state we messed with */
+            surface12->surface20->format->palette = logicalPal;
             VideoConvertSurface20->pixels = NULL;
             VideoConvertSurface20->pitch = 0;
             VideoConvertSurface20->w = VideoSurface12->w;
@@ -7114,11 +7129,21 @@ SDL_SetPalette(SDL12_Surface *surface12, int flags, const SDL_Color *colors,
         }
     }
 
+    if ((flags & SDL12_PHYSPAL) && (surface12 == VideoSurface12) && VideoPhysicalPalette20) {
+        if (SDL20_SetPaletteColors(VideoPhysicalPalette20, opaquecolors, firstcolor, ncolors) < 0) {
+            retval = -1;
+        }
+    }
+
     SDL20_free(opaquecolors);
 
     /* in case this pointer changed... */
     palette12->colors = palette20->colors;
 
+    if ((surface12 == VideoSurface12) && (flags & SDL12_PHYSPAL)) {
+        SDL_UpdateRect(surface12, 0, 0, 0, 0);   /* force screen to reblit with new palette. */
+    }
+
     return retval;
 }