Blitting a surface in a single color

Hi,

I was wondering if it is possible to blit an SDL_Surface as a single color, such as white. I’m writing a game and want to do this to indicate that something has taken damage. If anyone has played shoot-em-ups such as R-Type then it’s the same effect when a enemy gets hit with a bullet.

I know I could probably just include white versions of all my sprites but this might start to become a hassle after a while.

Any suggestions would be appreciated

Thanks

Richard_________________________________________________________________

Hotmail, Messenger, Photos and more - all with the new Windows Live. Get started!
http://www.download.live.com/

Hello !

I know I could probably just include white versions of all my
sprites but this might start to become a hassle after a while.

Any suggestions would be appreciated

The easiest and most space saving way to have
it is to write your own routine. The routine duplicates the original
surface and then sets every pixel that is not the color key to white.

Something like this :-----------------------------

bool GLB_Screen::Create_BlitMask (GLB_Bitmap *bitmap, GLB_Bitmap **target,
bool transparent)
{
GLB_Bitmap *temp1 = NULL, *temp2 = NULL;
Uint32 video_flags = 0;
Uint32 trans1_color = 0, trans2_color = 0, mask_color = 0, color = 0;
Uint16 index_x = 0, index_y = 0;

 (* target) = NULL;

 if ( bitmap == NULL ) return false;

 if ( _use_hwsurfaces == true )
    video_flags = SDL_HWSURFACE;
 else
    video_flags = SDL_SWSURFACE;

 temp1 = SDL_CreateRGBSurface (	video_flags,
                                     bitmap -> w,
                                     bitmap -> h,
                                     _screen_depth,
                                     0,
                                     0,
                                     0,
                                     0			);

 if ( temp1 == NULL ) return false;

 trans1_color	= SDL_MapRGB (bitmap -> format, 255, 0, 255);
 trans2_color	= SDL_MapRGB (temp1 -> format, 255, 0, 255);
 mask_color		= SDL_MapRGB (temp1 -> format, 255, 255, 255);

 if ( SDL_MUSTLOCK (temp1) )
 {
SDL_LockSurface (temp1);
 }

 if ( SDL_MUSTLOCK (bitmap) )
 {
SDL_LockSurface (bitmap);
 }

 for (index_y = 0; index_y < bitmap -> h; index_y ++)
 {
for (index_x = 0; index_x < bitmap -> w; index_x ++)
{
    color = Get_Pixel (bitmap, index_x, index_y);

    if ( color == trans1_color )
	    Put_Pixel (temp1, index_x, index_y, trans2_color);
    else
	    Put_Pixel (temp1, index_x, index_y, mask_color);
}
 }

 if ( SDL_MUSTLOCK (bitmap) )
 {
SDL_UnlockSurface (bitmap);
 }

 if ( SDL_MUSTLOCK (temp1) )
 {
SDL_LockSurface (temp1);
 }

 Set_Transparency (temp1, transparent);

 temp2 = SDL_DisplayFormat ( temp1 );

 if ( temp2 == NULL )
 {
     Remove_Bitmap (& temp1);

     return false;
 }

 Remove_Bitmap (& temp1);
 (* target) = temp2;

 return true;

}


CU

Excellent, your suggestion worked perfectly! Thank you very much!

Richard> Date: Fri, 27 Feb 2009 17:30:03 +0100

From: wizard at syntheticsw.com
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Blitting a surface in a single color

Hello !

I know I could probably just include white versions of all my
sprites but this might start to become a hassle after a while.

Any suggestions would be appreciated

The easiest and most space saving way to have
it is to write your own routine. The routine duplicates the original
surface and then sets every pixel that is not the color key to white.

Something like this :


bool GLB_Screen::Create_BlitMask (GLB_Bitmap *bitmap, GLB_Bitmap **target,
bool transparent)
{
GLB_Bitmap *temp1 = NULL, *temp2 = NULL;
Uint32 video_flags = 0;
Uint32 trans1_color = 0, trans2_color = 0, mask_color = 0, color = 0;
Uint16 index_x = 0, index_y = 0;

 (* target) = NULL;

 if ( bitmap == NULL ) return false;

 if ( _use_hwsurfaces == true )
  video_flags = SDL_HWSURFACE;
 else
  video_flags = SDL_SWSURFACE;

 temp1 = SDL_CreateRGBSurface (	video_flags,
                                     bitmap -> w,
                                     bitmap -> h,
                                     _screen_depth,
                                     0,
                                     0,
                                     0,
                                     0			);

 if ( temp1 == NULL ) return false;

 trans1_color	= SDL_MapRGB (bitmap -> format, 255, 0, 255);
 trans2_color	= SDL_MapRGB (temp1 -> format, 255, 0, 255);
 mask_color		= SDL_MapRGB (temp1 -> format, 255, 255, 255);

 if ( SDL_MUSTLOCK (temp1) )
 {

SDL_LockSurface (temp1);
}

 if ( SDL_MUSTLOCK (bitmap) )
 {

SDL_LockSurface (bitmap);
}

 for (index_y = 0; index_y < bitmap -> h; index_y ++)
 {

for (index_x = 0; index_x < bitmap -> w; index_x ++)
{
color = Get_Pixel (bitmap, index_x, index_y);

  if ( color == trans1_color )
      Put_Pixel (temp1, index_x, index_y, trans2_color);
  else
      Put_Pixel (temp1, index_x, index_y, mask_color);

}
}

 if ( SDL_MUSTLOCK (bitmap) )
 {

SDL_UnlockSurface (bitmap);
}

 if ( SDL_MUSTLOCK (temp1) )
 {

SDL_LockSurface (temp1);
}

 Set_Transparency (temp1, transparent);

 temp2 = SDL_DisplayFormat ( temp1 );

 if ( temp2 == NULL )
 {
     Remove_Bitmap (& temp1);

     return false;
 }

 Remove_Bitmap (& temp1);
 (* target) = temp2;

 return true;

}


CU


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Twice the fun?Share photos while you chat with Windows Live Messenger. Learn more.
http://www.microsoft.com/uk/windows/windowslive/products/messenger.aspx