From e05eb27f2c8741cbb8051e35758c7bc66e48d72d Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 10 Sep 2026 11:17:51 -0700
Subject: [PATCH] Use the surface palette directly for the texture in
SDL_CreateTextureFromSurface()
This is a better fix for https://github.com/libsdl-org/SDL/issues/16227. It also allows you to create a whole set of indexed textures from surfaces that share a single palette and then modify that palette to change all textures.
---
src/render/SDL_render.c | 16 +---------------
1 file changed, 1 insertion(+), 15 deletions(-)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index c6351f9b5ac9b..54dadec545d18 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -1700,21 +1700,6 @@ SDL_Texture *SDL_CreateTexture(SDL_Renderer *renderer, SDL_PixelFormat format, S
static bool SDL_UpdateTextureFromSurface(SDL_Texture *texture, SDL_Rect *rect, SDL_Surface *surface)
{
- if (SDL_ISPIXELFORMAT_INDEXED(texture->format) && surface->palette) {
- // Copy the palette to the new texture
- SDL_Palette *existing = surface->palette;
- SDL_Palette *palette = SDL_CreatePalette(existing->ncolors);
- if (palette &&
- SDL_SetPaletteColors(palette, existing->colors, 0, existing->ncolors) &&
- SDL_SetTexturePalette(texture, palette)) {
- // The texture has a reference to the palette now
- SDL_DestroyPalette(palette);
- } else {
- SDL_DestroyPalette(palette);
- return false;
- }
- }
-
bool direct_update;
if (surface->format == texture->format &&
SDL_GetSurfaceColorspace(surface) == texture->colorspace) {
@@ -1884,6 +1869,7 @@ SDL_Texture *SDL_CreateTextureFromSurface(SDL_Renderer *renderer, SDL_Surface *s
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STATIC);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, surface->w);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, surface->h);
+ SDL_SetPointerProperty(props, SDL_PROP_TEXTURE_CREATE_PALETTE_POINTER, surface->palette);
texture = SDL_CreateTextureWithProperties(renderer, props);
SDL_DestroyProperties(props);