[SDL] Using the same palette in multiple surfaces

Mason Wheeler masonwheeler at yahoo.com
Sat Dec 13 05:50:28 PST 2008


>----- Original Message ----

>From: Christian Meyer <tvgenial at web.de>
>Subject: [SDL] Using the same palette in multiple surfaces
>
>Hi!
>
>I have a game which generates small animations by changing the palette of the tiles. For performance reasons it >would be optimal, if only one palette has to be changed.
>
>So, is it possible to use one palette structure in multiple surfaces?
>
>Thanks,
>Christian

Yeah, if you're careful.  The palettes are accessed through a pointer
in the Pixel Format.  Change this on all the new surfaces you create to
the palette of the first surface.

IMPORTANT NOTE:
SDL doesn't expect you to be doing something like this.  When it frees
the first surface, it frees the pixel format, which frees the palette. 
In order to prevent a handful of memory corruption issues, you need a
workaround like this:

1.  Create a pointer list of some sort.  If you're using C++, an STL
queue would work well for this purpose.  If not, use something else. 
It needs to keep track of an arbitrary number of pointers and how many
it currently has in the list.
2. Each time you swap the palette of one SDL_Surface out for another
one, place the original palette pointer for that image on the list.
3. Each time you free an SDL_Surface, before the actual SDL_FreeSurface
call, replace its palette with the first one from the list, and remove
that pointer from the list.  It doesn't have to be the same one the
surface started out with; it just needs a unique palette when you free
it.  (And all the palettes need to get freed or they'll leak.)

Note: I haven't tested any of this.  I'm just writing doing this all
off the top of my head.  It may not work, but it should get you pretty
close.




More information about the SDL mailing list