I wrote this function that I use to “dye” surfaces different colors.
Works with all color depths I believe (focused on above 32bit). This
gives a blending effect by the offset values (RGB) that you give to the
function. It’s fairly fast and could probably be used in a lot of
applications. I have found it very useful myself. It probably needs a
little tweaking… but I dunno. Seen a few people asking around on the
net for something like this.======================================================================
void BlitRect_ColorChange_to_dest(SDL_Surface *src, SDL_Rect rect, int
r, int g, int b, int x, int y, SDL_Surface *dest)
{
int x2 = rect.x;
int y2 = rect.y;
SDL_Rect rect2;
SDL_Color clr;
SDL_Color colorkey;
Uint32 col = 0;
Uint32 col2 = 0;
char *gpos = NULL;
SDL_Surface *temp;
rect2.x = x;
rect2.y = y;
temp = SDL_ConvertSurface(src, src->format, src->flags);
SDL_SetColorKey(temp, SDL_SRCCOLORKEY, src->format->colorkey);
SDL_GetRGB(src->format->colorkey, src->format, &colorkey.r,
&colorkey.g, &colorkey.b);
SDL_LockSurface(temp);
while(y2 < rect.y + rect.h)
{
x2 = rect.x;
while(x2 < rect.x + rect.w)
{
gpos = (char *)temp->pixels;
gpos += (temp->pitch * y2);
gpos += (temp->format->BytesPerPixel * x2);
memcpy(&col, gpos, temp->format->BytesPerPixel);
SDL_GetRGB(col, temp->format, &clr.r, &clr.g, &clr.b);
if(clr.r != colorkey.r || clr.g != colorkey.g || clr.b != colorkey.b)
{
if(clr.r+r > 255)
clr.r = 255;
else if(clr.r+r < 0)
clr.r = 0;
else
clr.r += r;
if(clr.g+g > 255)
clr.g = 255;
else if(clr.g+g < 0)
clr.g = 0;
else
clr.g += g;
if(clr.b+b > 255)
clr.b = 255;
else if(clr.b+b < 0)
clr.b = 0;
else
clr.b += b;
col2 = SDL_MapRGB(temp->format, clr.r, clr.g, clr.b);
memcpy(gpos, &col2, temp->format->BytesPerPixel);
}
x2++;
}
y2++;
}
SDL_UnlockSurface(temp);
SDL_BlitSurface(temp, &rect, dest, &rect2);
SDL_FreeSurface(temp);
}
=========================================================================
Happy Holidays
John Josef
Wyled
Crazed Monkeys Inc www.crazedmonkeys.com