MORTAR: wrap palette from loaded SDL3 surfaces

When loading BMP files via MORTAR_LoadBMP, SDL3 attaches a palette to
the surface internally. Previously, create_mortar_surface_from_sdl3
did not extract this palette, causing MORTAR_GetSurfacePalette to
return NULL for 8-bit palettized images.

Create an independent MORTAR palette and copy the colors from the
SDL3 surface's palette to make it accessible via MORTAR_GetSurfacePalette.
This commit is contained in:
Christian Semmler 2026-01-31 10:28:05 -08:00 committed by Anonymous Maarten
parent bbd8f2e489
commit aeda30bef6
3 changed files with 23 additions and 1 deletions

View File

@ -268,6 +268,7 @@
X(SDL_Surface*, SDL_CreateSurfaceFrom, (int width, int height, SDL_PixelFormat format, void* pixels, int pitch)) \
X(void, SDL_DestroySurface, (SDL_Surface * surface)) \
X(bool, SDL_SetSurfacePalette, (SDL_Surface * surface, SDL_Palette * palette)) \
X(SDL_Palette*, SDL_GetSurfacePalette, (SDL_Surface * surface)) \
X(bool, SDL_LockSurface, (SDL_Surface * surface)) \
X(void, SDL_UnlockSurface, (SDL_Surface * surface)) \
X(SDL_Surface*, SDL_LoadBMP_IO, (SDL_IOStream * src, bool closeio)) \
@ -4465,7 +4466,7 @@ static void unload_sdl3_api()
#ifdef SDL_GetSurfacePalette
#undef SDL_GetSurfacePalette
#endif // SDL_GetSurfacePalette
#define SDL_GetSurfacePalette SDL3_symbol_SDL_GetSurfacePalette_is_marked_unused
#define SDL_GetSurfacePalette SDL3.SDL_GetSurfacePalette_symbol
#ifdef SDL_AddSurfaceAlternateImage
#undef SDL_AddSurfaceAlternateImage
#endif // SDL_AddSurfaceAlternateImage

View File

@ -66,6 +66,26 @@ static MORTAR_Surface* create_mortar_surface_from_sdl3(SDL_Surface* sdl3_surface
mortar_surface->mortar.pixels = sdl3_surface->pixels;
mortar_surface->mortar.format = pixelformat_sdl3_to_mortar(sdl3_surface->format);
mortar_surface->sdl3_surface = sdl3_surface;
SDL_Palette* sdl3_palette = SDL_GetSurfacePalette(sdl3_surface);
if (sdl3_palette && sdl3_palette->ncolors > 0) {
MORTAR_Palette* mortar_palette = MORTAR_CreatePalette(sdl3_palette->ncolors);
if (mortar_palette) {
MORTAR_Color* colors = (MORTAR_Color*) SDL_malloc(sizeof(MORTAR_Color) * sdl3_palette->ncolors);
if (colors) {
for (int i = 0; i < sdl3_palette->ncolors; i++) {
colors[i].r = sdl3_palette->colors[i].r;
colors[i].g = sdl3_palette->colors[i].g;
colors[i].b = sdl3_palette->colors[i].b;
colors[i].a = sdl3_palette->colors[i].a;
}
MORTAR_SetPaletteColors(mortar_palette, colors, 0, sdl3_palette->ncolors);
SDL_free(colors);
}
mortar_surface->mortar_palette = (MORTAR_SDL_Palette*) mortar_palette;
}
}
return &mortar_surface->mortar;
}

View File

@ -92,6 +92,7 @@
"SDL_GetPrimaryDisplay",
"SDL_GetRGBA",
"SDL_GetRevision",
"SDL_GetSurfacePalette",
"SDL_GetSystemRAM",
"SDL_GetTicks",
"SDL_GetTicksNS",