Advertisement

(SDL) Help figuring out RTS style colorswitching

Started by July 14, 2004 07:41 PM
14 comments, last by Pxtl 20 years, 4 months ago
I have been trying on my own for a while, but I can't seem to figure out how to achieve a certain effect in my RTS game... You know how units (tanks & soldiers) in RTS games look the same from player to player, only the color is different? Knowing how to accomplish this would ease up my work a great bit, and also the memoryload because I wont have to store "red soldier" and "blue soldier" and so on... seperately as different surfaces. I would use the same image of the soldier for all the players, only blit it, changing colors between every blit. All units images contain a clear blue field (0,0,255) to be used as the player's unique color. So what I'm essentially trying to accomplish, is changing color <0,0,255> in a SDL-surface to some other color. Any help on how to do this is greatly appreciated!! -Nordquist
----------------------~NQ - semi-pro graphical artist and hobbyist programmer
I have some ideas for you.

#1, not very good, but gets the job done. Use a video mode that requires a pallet. Have one value be specialized color.

#2, not alot better, but create a template image, then set it up to scan for a certain color to replace with the team color. Do this durring loading the level. Its about the same as paleting

To copy a surface, create a new one, then just blit the old data to the new one, or load multiple copies of the template.

just some ideas
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
Advertisement
Can you edit manually edit in SDL? If you can, just do exactly what you are doing, and before each game check each image for (0, 0, 255), and make new surfaces that are exact copies of each image, with the appropriate color changed.
Not giving is not stealing.
I haven't tried this, so you'd have to work the details, but I would just exploit alphablending. Make a surface (the destination) a solid colour - red, blue, fuschia, whatever team colour you want. Then, store the unit/building sprites with an alpha channel (which means TGA/PNG format images). The part you don't want showing should be opaque (I can't remember if SDL uses 255 or 0 for opaque) and the team-coloured part should be a partial alpha (translucent) value, so that the colour on the destination will show through. During level load, blit your sprite sheet on top of background, with alphablending enabled. Voila - team colours.

The best part? By varying the alpha, you can have parts that are different brightnesses (of team colour) for a more realistic look. SDL alpha blending is more than fast enough, and if you do it at load time, it essentially becomes free.

The downside is that you need to store a 32-bit sprite sheet, but depending on what you're using now, this might be the case already. Check out SDL_Image for loading TGA/PNG files (or practically any other format).
Quote: Original post by PnP Bios
#1, not very good, but gets the job done. Use a video mode that requires a pallet. Have one value be specialized color.


One color is not enough, use 20 or so.
It works very well in non plalleted modes, only that you will need the texture to be in a palleted format.
ze_jackal provided with what must be the best working solution so far, however this was just after I had solved it myself in a similiar way.
I figured I could post my solution here in case there comes another who wonders the same thing:
In my case, whatever should contain the team colors is plain blue. So, what to do is make blue=transparent with SDL_SetColorKey(blah), and blit that over a surface filled with the team color. And voilá - everything that used to be blue is now team color. Just blit this finished one instead of the original.

Using the alphachannel for PARTIAL transparency instead of complete is a very smart idea. I might use that in the future.

Thanks for the help guys
----------------------~NQ - semi-pro graphical artist and hobbyist programmer
Advertisement
That way works fine until you want to have some blue on the units, but you don't want that blue to be repalced with the unit color.
How about making the image itself palleted? You can then have different pallets for each color. It's fairly easy to replace the pallete of an image with a different one. The screen can be any color you want, the conversion from palleted->32bit happens when you blit the image.
This way you can have different shades of the color and it's probably faster than alpha blending.
Quote: Original post by Gyrbo
How about making the image itself palleted? You can then have different pallets for each color. It's fairly easy to replace the pallete of an image with a different one. The screen can be any color you want, the conversion from palleted->32bit happens when you blit the image.
This way you can have different shades of the color and it's probably faster than alpha blending.


That's exactly what I was saying :)
Quote: Original post by Raduprv
That way works fine until you want to have some blue on the units, but you don't want that blue to be repalced with the unit color.

That's hardly going to be an issue. If the colour key is (0,0,255), just use (0,0,254) instead of real occurrances of 255 blue. Don't tell me you can tell the difference in-game.

~phil
~phil

This topic is closed to new replies.

Advertisement