How do Surface and Palette interact?


#1
  1. When a new SDL_Surface is created, does it also get a new SDL_Palette of its own?
  2. The wiki docs for SDL_Surface say that the format field is read only, but the format data is what contains the palette pointer. Do you have to use SDL_SetPaletteColors to change even a single color of that palette and it re-allocates stuff around in the background or something?
  3. When you call SDL_SetSurfacePalette, what happens to the old palette? Is it automatically de-allocated or do I need to record the pointer myself before the call and then de-allocate it myself if the call is successful?
  4. Similarly, the remarks on the wiki page for SDL_SetSurfacePalette say that many surfaces can share the same palette. When does a palette shared among surfaces get de-allocated?

#3

Someone on the SDL2 Discord was kind enough to provide an answer. However, they don’t have a forum account I guess so I’ll cross-post their answer here in case others have this same question later:

1. When a new SDL_Surface is created, does it also get a new SDL_Palette of its own?

Yes it gets an empty palette allocated.

2. The wiki docs for SDL_Surface say that the format field is read only, but the format data is what contains the palette pointer. Do you have to use SDL_SetPaletteColors to change even a single color of that palette and it re-allocates stuff around in the background or something?

Yes, you’ll want to create your own palette, then you can use SDL_SetPaletteColors. Unfortuantely I do not believe there is a way to access the original surface palette, but it’s not a big deal.

3. When you call SDL_SetSurfacePalette, what happens to the old palette? Is it automatically de-allocated or do I need to record the pointer myself before the call and then de-allocate it myself if the call is successful?

The original palette the surface was using gets deallocated for you, keep in mind, I think it actually maintains a copy per surface so it’s not going to delete a palette you gave it.

4. Similarly, the remarks on the wiki page for SDL_SetSurfacePalette say that many surfaces can share the same palette. When does a palette shared among surfaces get de-allocated?

That’s your responsibliity. Use SDL_FreePalette. Keeping in mind, based on SDL2 source I actually think you can call SDL_FreePalette after you’ve set it on all the surfaces that need it, because they maintain their own copies.


#4

The docs for SDL_Palette say: “An SDL_Palette should never need to be created manually. It is automatically created when SDL allocates an SDL_PixelFormat for a surface. The colors values of an SDL_Surface’s palette can be set with SDL_SetPaletteColors.

I can’t see why you would need to “create your own palette”. I have successfully modified a surface’s palette by calling SDL_SetPaletteColors() on surface->format->palette. I don’t believe that the format member being read-only implies that the palette itself is read-only!


#5

:frowning:

Well who knows for sure?


#6

FWIW my code, calling SDL_SetPaletteColors() on surface->format->palette, appears to work correctly in Windows, Linux, MacOS, Android and iOS, using the OpenGL or OpenGLES backend in all cases.


#7

Alright, I’ll support the ability to edit a palette through said pointer I suppose.