Bookmark GamesCreators

Google
 
Web Games Creators

 
News
Articles
Forum
Downloads
 
Send your article
Send your demo
Send your game
 
Contact Webmaster

Thanks to:

SourceForge.net Logo

 

 

 

 

Replacing Colors

 

    Replacing colors can have a lot of uses, but now I will explain the main one: replacing colors
in sprites to have different players units with different colors.
It is used a lot in strategy games where there are a lot of players and each player have its own color. So player's units and buildings will have player's color in some parts (like flags on the buildings or color of clothes of units). Because a lot of units and buildings are common to every player its not a practical solution to make equal units with different colors. The right solution would be to create units and in stead of player's color, paint these areas with some color and then, when initializing the game, replace this color with player's color. As a result there will be some units that differ only in color, so players can know whose this unit is.
Now I will show and explain the code that makes this replacement and draws 4 viking ships
(from Age of Empires 2 :)) with different colors.

 

main.c (Download the source code here)

//fill the vector with colors to replace
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 65, 0, 0));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 105, 10, 0));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 160, 21, 0));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 230, 11, 0));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 255, 0, 0));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 255, 100, 100));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 255, 160, 160));
ColorFrom.push_back(SDL_MapRGB(newboat1->format, 255, 220, 220));

//these vectors will contain colors that will replace original colors
//each vector position of new colors coresponds to each old color.
//first line tells that the color (RGB) 65,0,0 must be replaced with 0,65,0
//----------------------------------------------------------------------
//replace with green

ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 0, 65, 0));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 0, 105, 10));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 0, 160, 21));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 0, 230, 11));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 0, 255, 0));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 100, 255, 100));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 160, 255, 160));
ColorToGreen.push_back(SDL_MapRGB(newboat1->format, 220, 255, 220));

//replace with yellow
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 65, 65, 0));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 105, 105, 0));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 160, 160, 21));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 230, 230, 11));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 255, 255, 0));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 255, 255, 100));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 255, 255, 160));
ColorToYellow.push_back(SDL_MapRGB(newboat2->format, 255, 255, 220));

//replace with blue
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 0, 0, 65));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 0, 0, 105));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 21, 0, 160));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 11, 0, 230));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 0, 0, 255));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 100, 100, 255));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 160, 160, 255));
ColorToBlue.push_back(SDL_MapRGB(newboat3->format, 220, 220, 255));


//call ReplaceColors for each color to replace.
for (int i = 0; i < ColorFrom.size(); i++)
{
   ReplaceColors(ColorFrom[i], ColorToGreen[i], newboat1); //replace it with green
   ReplaceColors(ColorFrom[i], ColorToYellow[i], newboat2); //replace it with yellow
   ReplaceColors(ColorFrom[i], ColorToBlue[i], newboat3); //replace it with blue
}

   This is the first interesting part of the code. In the first lines of main.c ColorFrom, ColorToGreen, ColorToYellow, ColorToBlue are declared as vector of Uint32. (Read more about C++ vectors here). ColorFrom vector will contain colors that have to be replaces, and ColorTo* vectors contain colors that will replace the original colors. For example 0,65,0 color will be replaced with 65,65,0 color from ColorToGreen vector. In the for cycle the ReplaceColors function is called to replace all colors.

   This function takes 3 parameters. The first one is a vector of Uint32 colors that will be replaced and the second one is a vector of Uint32 colors too, but containing colors that will replace colors from first vector. Finally the third parameter is a pointer to SDL_Surface. The function will look for colors to be replaced in this surface and replace them. This surface will contain the new image with new colors. Lets take a look at ReplaceColors function:

ReplaceColors from main.c

void ReplaceColors(Uint32 colorfrom, Uint32 colorto, SDL_Surface *from)
{
   //This function replaces color: colorfrom with color: colorto on surface: from

   int x, y, i;

   for (x = 0; x < from->w; x++)
   {
      for (y = 0; y < from->h; y++)
      {
         if (GetPixel(from, x, y) == colorfrom) //Check if a pixel has to be replaced
         {
            PutPixel(from, x, y, colorto); //Replace it
         }
      }
   }
}

   Basically it check every pixel of received surface and if it matches with any of colors that need to be replaced, it replaces it. Now lets take a look at GetPixel() and PutPixel() functions:

 

 

Next