SDL Color-masking Blitting?

I was curious if there was a built-in way to mask a surface and then blit it. Basically, say I have a font or an image, and it is black and white – nothing else. I want to be able to mask the image (say with color 255,0,128, so I have a kind of dark red-magenta), and then blit it. Is there a way that is built in? If not, then am I basically just needing to write my own specialized Blit function to do this? Or if anyone’s already done this and is kind and doesn’t mind divulging some open-source code, that’d be pretty cool too!_________________________________________________________________
Need to know the score, the latest news, or you need your Hotmail?-get your “fix”.
http://www.msnmobilefix.com/Default.aspx

If it’s a paletted image, you can set its palette before blitting. If
it’s not, you’re in for a fun time…
-:sigma.SBOn Feb 4, 2008 9:58 AM, Taylor Jeude wrote:

I was curious if there was a built-in way to mask a surface and then blit
it. Basically, say I have a font or an image, and it is black and white –
nothing else. I want to be able to mask the image (say with color
255,0,128, so I have a kind of dark red-magenta), and then blit it. Is
there a way that is built in? If not, then am I basically just needing to
write my own specialized Blit function to do this? Or if anyone’s already
done this and is kind and doesn’t mind divulging some open-source code,
that’d be pretty cool too!

Hey Taylor,

There are at least two ways about this. If you are restricting yourself to a single-color swap like black for dark magenta, you should use a colorkeyed surface:

SDL_Surface* colorSwapSingle(SDL_Surface* surface, Uint32 oldColor, Uint32 newColor)
{
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, oldColor);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#else
SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
#endif
SDL_FillRect(result, NULL, newColor);
SDL_BlitSurface(surface, NULL, result, NULL);
return result;
}

Otherwise, attached is some code for a very slow blitter that you can tweak to fit your needs. I think I included all the code you need. It’s all in SPriG at http://pubpages.unh.edu/~jmb97 in case you’re interested in that. If there are any problems, just tell me.

Jonny DFrom: terinfire@hotmail.com
To: sdl at lists.libsdl.org
Date: Mon, 4 Feb 2008 10:58:33 -0600
Subject: [SDL] SDL Color-masking Blitting?

I was curious if there was a built-in way to mask a surface and then blit it. Basically, say I have a font or an image, and it is black and white – nothing else. I want to be able to mask the image (say with color 255,0,128, so I have a kind of dark red-magenta), and then blit it. Is there a way that is built in? If not, then am I basically just needing to write my own specialized Blit function to do this? Or if anyone’s already done this and is kind and doesn’t mind divulging some open-source code, that’d be pretty cool too!

Need to know the score, the latest news, or you need your Hotmail?-get your “fix”. Check it out.


Need to know the score, the latest news, or you need your Hotmail?-get your “fix”.
http://www.msnmobilefix.com/Default.aspx
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: Custom Blitter.txt
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080204/285fd4d8/attachment.txt

Oops,

That surface creation part of the function in my last email should be generalized like so:

SDL_Surface* colorSwapSingle(SDL_Surface* surface, Uint32 oldColor, Uint32 newColor)
{
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, oldColor);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN

SDL_Surface* result =
SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,surface->format->BytesPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
#else

SDL_Surface* result =
SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,surface->format->BytesPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
#endif
SDL_FillRect(result, NULL, newColor);
SDL_BlitSurface(surface, NULL, result, NULL);
return result;
}

Jonny DFrom: grimfang4@hotmail.com
To: sdl at lists.libsdl.org
Date: Mon, 4 Feb 2008 18:55:46 -0500
Subject: Re: [SDL] SDL Color-masking Blitting?

Hey Taylor,

There are at least two ways about this. If you are restricting yourself to a single-color swap like black for dark magenta, you should use a colorkeyed surface:

SDL_Surface* colorSwapSingle(SDL_Surface* surface, Uint32 oldColor, Uint32 newColor)
{
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, oldColor);
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
#else
SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
#endif
SDL_FillRect(result, NULL, newColor);
SDL_BlitSurface(surface, NULL, result, NULL);
return result;
}

Otherwise, attached is some code for a very slow blitter that you can tweak to fit your needs. I think I included all the code you need. It’s all in SPriG at http://pubpages.unh.edu/~jmb97 in case you’re interested in that. If there are any problems, just tell me.

Jonny D

From: terinfire@hotmail.com
To: sdl at lists.libsdl.org
Date: Mon, 4 Feb 2008 10:58:33 -0600
Subject: [SDL] SDL Color-masking Blitting?

I was curious if there was a built-in way to mask a surface and then blit it. Basically, say I have a font or an image, and it is black and white – nothing else. I want to be able to mask the image (say with color 255,0,128, so I have a kind of dark red-magenta), and then blit it. Is there a way that is built in? If not, then am I basically just needing to write my own specialized Blit function to do this? Or if anyone’s already done this and is kind and doesn’t mind divulging some open-source code, that’d be pretty cool too!

Need to know the score, the latest news, or you need your Hotmail?-get your “fix”. Check it out.

Need to know the score, the latest news, or you need your Hotmail?-get your “fix”. Check it out.


Helping your favorite cause is as easy as instant messaging.?You IM, we give.
http://im.live.com/Messenger/IM/Home/?source=text_hotmail_join