SDL: Is it safe to change a surfaces palette color key?

Started by
3 comments, last by evillive2 16 years, 11 months ago
I'm working on my first simple 2D game and I'm looking for a way to change a color on a surface. This will only be done once per character so speed isn't a factor. I can see two possible options: 1. Change each pixel on a surface, or 2. Change the palette color itself. I prefer the latter in this case and since I'm a newb in SDL I want to make sure what I am about to do is OK. Here's the steps: 1. Lock surface. 2. Create an object of type SDL_Color associated with the color I wish to change. Create another object of type SDL_Color that I want the color to be changed to. 3. Find the color I wish to replace in the images palette (surfaceId->format.palette). 4. Change the found SDL_Color object in the palette to be the color I wish it to be. 5. Unlock surface. Is this safe to do? The reason I ask is I'm not sure how it will behave when I blit the surface onto another surface. Thanks!
Advertisement
Hmm. If you have palettized images, that's usually because you have a palettized display mode. That means all your images share a single palette and changing one will change all of them.

I don't know if SDL supports individual per-image palettes. You may find they all point to the same object! But if they are individual, and you're rendering to a non-palettized video mode, you can give it a go. Bear in mind that this will always be slower than having the images in the same format as the video mode, because in order to support this (and I'm not sure it does), SDL would have to perform a per-pixel lookup to work out the right colours.
Hmm... yeah, it definitely sounds too dangerous to try out. It seems I'll have to just change each individual pixel color then.
If you're going to be changing the colors on the surfaces, you might consider cacheing the recolored surfaces so you don't have to convert them again.

Additionally, one method I used before to recolor surfaces is to use blitting, rather than per-pixel manipulations to change the colors -

  1. Start with the src surface.
  2. Create a dest surface with the same dimensions as the src surface.
  3. Set the colorkey of the src surface to the color you want to change (srccolor).
  4. Fill the dest surface with the color you want to change to (destcolor).
  5. Blit the src surface onto the dest surface.


This always seemed kind of cheap (performance wise) to me, whereas locking a surface seemed expensive. That's just my general perception, which is completely unfounded with regards to testing.

Anyway, best of luck with your project :3
Quote:Original post by Mushu
If you're going to be changing the colors on the surfaces, you might consider cacheing the recolored surfaces so you don't have to convert them again.

Additionally, one method I used before to recolor surfaces is to use blitting, rather than per-pixel manipulations to change the colors -

  1. Start with the src surface.
  2. Create a dest surface with the same dimensions as the src surface.
  3. Set the colorkey of the src surface to the color you want to change (srccolor).
  4. Fill the dest surface with the color you want to change to (destcolor).
  5. Blit the src surface onto the dest surface.


This always seemed kind of cheap (performance wise) to me, whereas locking a surface seemed expensive. That's just my general perception, which is completely unfounded with regards to testing.

Anyway, best of luck with your project :3


I have done this with bitmap fonts to get a few different colors of a font at runtime (done once when loading the font) and it is noticeably faster than changing it pixel by pixel (for 8 different colors) although I just used the getpixel and putpixel from the SDL help files. I noticed the difference easily on my older computer (AMD K6 II 500MHZ) but not at all on my newer one. Since it is all done in software (never tried hardware surfaces) I would imagine the CPU would make a big difference in how much you notice the difference.

Evillive2

This topic is closed to new replies.

Advertisement