Sign in to follow this  

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

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3860 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this