Maelstrom: Sharing colors was indeed causing problems. Now we hash the textures by color as well.

https://github.com/libsdl-org/Maelstrom/commit/da30b199d8fd3319c41ba321311c5a5e585ab0e4

From da30b199d8fd3319c41ba321311c5a5e585ab0e4 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 24 Oct 2011 23:47:24 -0400
Subject: [PATCH] Sharing colors was indeed causing problems.  Now we hash the
 textures by color as well.

---
 UIElementKeyButton.cpp  | 10 +++++++---
 UIElementKeyButton.h    |  1 +
 UIElementLabel.cpp      | 37 +++++++++++++++++++++++++++----------
 UIElementLabel.h        |  2 +-
 maclib/Mac_FontServ.cpp |  8 +++-----
 5 files changed, 39 insertions(+), 19 deletions(-)

diff --git a/UIElementKeyButton.cpp b/UIElementKeyButton.cpp
index 1177b621..acf3e872 100644
--- a/UIElementKeyButton.cpp
+++ b/UIElementKeyButton.cpp
@@ -8,6 +8,7 @@ UIElementKeyButton::UIElementKeyButton(UIPanel *panel, const char *name) :
 	UIElementButton(panel, name)
 {
 	m_text = NULL;
+	m_textShadow = NULL;
 }
 
 UIElementKeyButton::~UIElementKeyButton()
@@ -15,6 +16,9 @@ UIElementKeyButton::~UIElementKeyButton()
 	if (m_text) {
 		fontserv->FreeText(m_text);
 	}
+	if (m_textShadow) {
+		fontserv->FreeText(m_textShadow);
+	}
 }
 
 bool
@@ -29,6 +33,8 @@ UIElementKeyButton::Load(rapidxml::xml_node<> *node)
 
 	if (m_hotkey != SDLK_UNKNOWN) {
 		m_text = fontserv->TextImage(SDL_GetKeyName(m_hotkey),
+				fonts[GENEVA_9], STYLE_BOLD, 0x00, 0x00, 0x00);
+		m_textShadow = fontserv->TextImage(SDL_GetKeyName(m_hotkey),
 				fonts[GENEVA_9], STYLE_BOLD, 0xFF, 0xFF, 0xFF);
 	}
 	return true;
@@ -42,8 +48,6 @@ UIElementKeyButton::Draw()
 printf("KeyButton: %s at %d,%d\n", SDL_GetKeyName(m_hotkey), m_rect.x+14, m_rect.y+10);
 printf("KeyButton: %s at %d,%d\n", SDL_GetKeyName(m_hotkey), m_rect.x+13, m_rect.y+9);
 #endif
-	SDL_SetTextureColorMod(m_text, 0xFF, 0xFF, 0xFF);
-	m_screen->QueueBlit(m_rect.x+14, m_rect.y+10, m_text, NOCLIP);
-	SDL_SetTextureColorMod(m_text, 0x00, 0x00, 0x00);
+	m_screen->QueueBlit(m_rect.x+14, m_rect.y+10, m_textShadow, NOCLIP);
 	m_screen->QueueBlit(m_rect.x+13, m_rect.y+9, m_text, NOCLIP);
 }
diff --git a/UIElementKeyButton.h b/UIElementKeyButton.h
index 157fe79e..701f799c 100644
--- a/UIElementKeyButton.h
+++ b/UIElementKeyButton.h
@@ -20,6 +20,7 @@ class UIElementKeyButton : public UIElementButton
 
 protected:
 	SDL_Texture *m_text;
+	SDL_Texture *m_textShadow;
 
 protected:
 	static UIElementType s_elementType;
diff --git a/UIElementLabel.cpp b/UIElementLabel.cpp
index e75c0bd3..d5f382ae 100644
--- a/UIElementLabel.cpp
+++ b/UIElementLabel.cpp
@@ -11,14 +11,15 @@ UIElementLabel::UIElementLabel(UIPanel *panel, const char *name) :
 	m_font = NULL;
 	m_style = STYLE_NORM;
 	m_color = m_screen->MapRGB(0xFF, 0xFF, 0xFF);
+	m_text = NULL;
 	m_texture = NULL;
-#ifdef UI_DEBUG
-m_text = NULL;
-#endif
 }
 
 UIElementLabel::~UIElementLabel()
 {
+	if (m_text) {
+		delete[] m_text;
+	}
 	if (m_texture) {
 		fontserv->FreeText(m_texture);
 	}
@@ -94,14 +95,20 @@ printf("Label: '%s' %d,%d\n", m_text, m_rect.x, m_rect.y);
 void
 UIElementLabel::SetText(const char *text)
 {
+	if (m_text && strcmp(text, m_text) == 0) {
+		return;
+	}
+
+	if (m_text) {
+		delete[] m_text;
+	}
 	if (m_texture) {
 		fontserv->FreeText(m_texture);
 	}
 
-#ifdef UI_DEBUG
-m_text = strdup(text);
-#endif
-	m_texture = fontserv->TextImage(text, m_font, m_style, m_color);
+	m_text = new char[strlen(text)+1];
+	strcpy(m_text, text);
+	m_texture = fontserv->TextImage(m_text, m_font, m_style, m_color);
 	m_rect.w = m_screen->GetImageWidth(m_texture);
 	m_rect.h = m_screen->GetImageHeight(m_texture);
 	CalculateAnchor();
@@ -114,10 +121,20 @@ printf("Label: '%s' %d,%d\n", m_text, m_rect.x, m_rect.y);
 void
 UIElementLabel::SetTextColor(Uint8 R, Uint8 G, Uint8 B)
 {
-	if (m_texture) {
-		SDL_SetTextureColorMod(m_texture, R, G, B);
+	Uint32 color;
+
+	color = m_screen->MapRGB(R, G, B);
+	if (color == m_color) {
+		return;
+	}
+	m_color = color;
+
+	if (m_text) {
+		if (m_texture) {
+			fontserv->FreeText(m_texture);
+		}
+		m_texture = fontserv->TextImage(m_text, m_font, m_style, m_color);
 	}
-	m_color = m_screen->MapRGB(R, G, B);
 }
 
 void
diff --git a/UIElementLabel.h b/UIElementLabel.h
index 81abc33a..ac5cf8f3 100644
--- a/UIElementLabel.h
+++ b/UIElementLabel.h
@@ -26,8 +26,8 @@ class UIElementLabel : public UIElement
 	MFont *m_font;
 	Uint8 m_style;
 	Uint32 m_color;
+	char *m_text;
 	SDL_Texture *m_texture;
-char *m_text;
 
 protected:
 	static UIElementType s_elementType;
diff --git a/maclib/Mac_FontServ.cpp b/maclib/Mac_FontServ.cpp
index 7a1f684d..b236539d 100644
--- a/maclib/Mac_FontServ.cpp
+++ b/maclib/Mac_FontServ.cpp
@@ -295,11 +295,10 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
 	int bit;
 
 	/* First see if we can find it in our cache */
-	keysize = strlen(font->name)+1+2+1+1+1+strlen(text)+1;
+	keysize = strlen(font->name)+1+2+1+1+1+6+1+strlen(text)+1;
 	key = SDL_stack_alloc(char, keysize);
-	sprintf(key, "%s:%d:%c:%s", font->name, font->ptsize, '0'+style, text);
+	sprintf(key, "%s:%d:%c:%2.2x%2.2x%2.2x:%s", font->name, font->ptsize, '0'+style, fg.r, fg.g, fg.b, text);
 	if (hash_find(strings, key, (const void**)&image)) {
-		SDL_SetTextureColorMod(image, fg.r, fg.g, fg.b);
 		return image;
 	}
 
@@ -361,7 +360,7 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
 	/* Allocate the text pixels */
 	bitmap = new Uint32[width*height];
 	memset(bitmap, 0, width*height*sizeof(Uint32));
-	color = screen->MapRGB(0xFF, 0xFF, 0xFF);
+	color = screen->MapRGB(fg.r, fg.g, fg.b);
 
 	/* Print the individual characters */
 	/* Note: this could probably be optimized.. eh, who cares. :) */
@@ -412,7 +411,6 @@ FontServ:: TextImage(const char *text, MFont *font, Uint8 style, SDL_Color fg)
 	/* Create the image */
 	image = screen->LoadImage(width, height, bitmap);
 	delete[] bitmap;
-	SDL_SetTextureColorMod(image, fg.r, fg.g, fg.b);
 	SDL_SetTextureBlendMode(image, SDL_BLENDMODE_BLEND);
 
 	/* Add it to our cache */