[SDL] surface transparency

Patricia Curtis patricia.curtis at gmail.com
Mon Jul 16 10:25:20 PDT 2007


Hi Guys
        A surface or an image AFAIK can not be faded, as we cant change the
Alpha of a surface with transparent bits, so i use this i wrote to allow me
to fade a temp SW surface such as an image, the output from SDL_TTF
Blended or Rotozoom, and i thought i would share it, it steps through the
whole surface adjusting the alpha byte of each pixel. Yes it could be a lot
faster, like only adjusting the alpha byte and stepping over the colour
bytes, but it does what it says on the tin, so for now here it is.

void SurfaceTrans(SDL_Surface * Src,double PercentTrans)
{
 Uint8 Sbpp = Src->format->BytesPerPixel;
 Uint8 *Sbits;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    int amask = 0x000000ff;
    int cmask = 0xffffff00;
    int Shift = 0;
#else
    int amask = 0xff000000;
    int cmask = 0x00ffffff;
    int Shift = 24;
#endif
 int x,y;
 Uint32 Pixels;
 Uint32 Alpha;
 for(y=0;y<Src->h;y++)
 {
  for(x=0;x<Src->w;x++)
  {
   Sbits = ((Uint8 *)Src->pixels+(y*Src->pitch)+(x*Sbpp));
   Pixels = *((Uint32 *)(Sbits));
   Alpha = (Pixels&amask)>>Shift;
   Alpha*=PercentTrans;
   *((Uint32 *)(Sbits)) = (Pixels & cmask)|(Alpha<<Shift);
  }
 }
}

i hope this works for you, and if anyone wants to make it faster , please
post the code.

Trish x
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070717/fdfa07b2/attachment.htm 


More information about the SDL mailing list