How to replace a color with another

Hi guys,

    In my game i want my sprite to show in various

colors and obviously i dont wana create sprite in each
color.
Is there any method in SDL that lets me change sprite
color on the fly ?

thnx in advance,
Visu________________________________________________________________________
Yahoo! India Mobile: Download the latest polyphonic ringtones.
Go to http://in.mobile.yahoo.com

Vishwanath V wrote:

    In my game i want my sprite to show in various

colors and obviously i dont wana create sprite in each
color.
Is there any method in SDL that lets me change sprite
color on the fly ?

Is your whole sprite just in one color, and you want it to change?–
Milan Babuskov
http://njam.sourceforge.net

If the images are paletted, it may be possible to change their
color palette before blitting. Of course, this would mean that
conversion from the paletted (e.g., 8bit) version to the display
format (e.g., 16bit or 24bit) would happen each time… SLOW!

What I did in Tux Paint when I wanted to alter colors of objects
(like the Rose stamp, which you can stamp on the screen in any color,
even though the original PNG is red) is ‘tint’ the surface (using my
own routines) when the stamp was chosen, or a new color was picked.

What you might want to do is the same thing, but during initialization.
Like:

  1. Load the sprite (say it’s blue; we can store it as the “blue sprite”)
  2. For each color you want the sprite to show up as,
    a. Create a new surface that’s the same size as the blue sprite
    b. Draw the blue sprite into it, one pixel at a time, but with
    a different color

To me, this is one of those "if it doesn’t take time, it takes memory"
issues. (In either case – whether you have separate images for each
color, or if you have one and then tint it multiple times – it takes
RAM to store the surfaces. So really, it should be “…it takes space.”)

So in other words, you can either:

  1. Have various files with the different sprites
    (you already said you don’t want to do this)

  2. Change the color of the sprites as you draw them
    (your end users won’t want you to do this… SLOW! :^) )

or:

  1. Load one sprite file, and then create, on-the-fly, the other
    sprites when the program first loads up

Hope this helps!

Oh, and if your game is GPL, feel free to yank the color-tinting code
from Tux Paint. :slight_smile: It’s not PERFECT, but it does the job I need it to
for a kids’ paint program. :wink:

-bill!On Tue, Nov 11, 2003 at 01:39:22PM +0000, Vishwanath V wrote:

Hi guys,

    In my game i want my sprite to show in various

colors and obviously i dont wana create sprite in each
color.
Is there any method in SDL that lets me change sprite
color on the fly ?