Maelstrom: Palette fixes

From 7993ae9f2d1fd3b35b51097e91948fd04b3332cd Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 9 Mar 2025 14:06:47 -0700
Subject: [PATCH] Palette fixes

---
 dialog.cpp              |  2 +-
 dialog.h                |  3 +++
 load.cpp                |  2 +-
 maclib/Mac_FontServ.cpp | 11 +++++++----
 4 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/dialog.cpp b/dialog.cpp
index e19d174..a591470 100644
--- a/dialog.cpp
+++ b/dialog.cpp
@@ -31,7 +31,7 @@ Mac_Button::Mac_Button(int x, int y, int width, int height,
 		SetError("%s", SDL_GetError());
 		return;
 	}
-	SDL_Palette* palette = SDL_GetSurfacePalette(button);
+	SDL_Palette* palette = SDL_CreateSurfacePalette(button);
 	if (palette) {
 		palette->colors[0].r = 0xFF;
 		palette->colors[0].g = 0xFF;
diff --git a/dialog.h b/dialog.h
index 76cdb27..16b55a8 100644
--- a/dialog.h
+++ b/dialog.h
@@ -138,6 +138,7 @@ class Mac_Button : public Mac_Dialog {
 
 		/* Map the bitmap image */
 		SDL_Palette* palette = SDL_GetSurfacePalette(button);
+		SDL_assert(palette);
 		if (palette) {
 			palette->colors[0].r = R_bg;
 			palette->colors[0].g = G_bg;
@@ -333,6 +334,7 @@ class Mac_CheckBox : public Mac_Dialog {
 
 		/* Map the checkbox text */
 		SDL_Palette* palette = SDL_GetSurfacePalette(label);
+		SDL_assert(palette);
 		if (palette) {
 			palette->colors[1].r = R_fg;
 			palette->colors[1].g = G_fg;
@@ -458,6 +460,7 @@ class Mac_RadioList : public Mac_Dialog {
 			radio->sensitive.y += Yoff;
 
 			SDL_Palette* palette = SDL_GetSurfacePalette(radio->label);
+			SDL_assert(palette);
 			if (palette) {
 				palette->colors[1].r = R_fg;
 				palette->colors[1].g = G_fg;
diff --git a/load.cpp b/load.cpp
index c693b4c..d8b52b7 100644
--- a/load.cpp
+++ b/load.cpp
@@ -42,7 +42,7 @@ SDL_Surface *Load_Icon(char **xpm)
 	}
 
 	/* Fill in the palette */
-	SDL_Palette* palette = SDL_GetSurfacePalette(icon);
+	SDL_Palette* palette = SDL_CreateSurfacePalette(icon);
 	for ( i=0; i<num_colors; ++i ) {
 		buf = xpm[index++];
 		p = *buf;
diff --git a/maclib/Mac_FontServ.cpp b/maclib/Mac_FontServ.cpp
index 5fd01a9..135be20 100644
--- a/maclib/Mac_FontServ.cpp
+++ b/maclib/Mac_FontServ.cpp
@@ -383,7 +383,7 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style,
 
 	/* Map the image and return */
 	SDL_SetSurfaceColorKey(image, true, 0);
-	SDL_Palette* palette = SDL_GetSurfacePalette(image);
+	SDL_Palette* palette = SDL_CreateSurfacePalette(image);
 	SDL_SetPaletteColors(palette, &background, 0, 1);
 	SDL_SetPaletteColors(palette, &foreground, 1, 1);
 	++text_allocated;
@@ -408,8 +408,11 @@ FontServ:: InvertText(SDL_Surface *text)
 
 	/* Swap background and foreground colors */
 	SDL_Palette* palette = SDL_GetSurfacePalette(text);
-	colors[0] = palette->colors[1];
-	colors[1] = palette->colors[0];
-	SDL_SetPaletteColors(palette, colors, 0, 2);
+	SDL_assert(palette);
+	if (palette) {
+		colors[0] = palette->colors[1];
+		colors[1] = palette->colors[0];
+		SDL_SetPaletteColors(palette, colors, 0, 2);
+	}
 	return(0);
 }