Creating a transparent rectangle (code)

(sorry… I accidently sent the message early!)

Here’s the code, it returns a Surface so it is used like so,

SDL_Surface *transparent = alphaRect(100, 100, 0xff, 0x00, 0x00);

SDL_Surface *alphaRect(int width, int height, Uint8 red, Uint8 green, Uint8
blue)
{
SDL_Surface *surface, *newImage;
Uint32 rmask, gmask, bmask, amask;

/* SDL interprets each pixel as a 32-bit number, so our masks must

depend
on the endianness (byte order) of the machine */
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif

surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,

rmask, gmask, bmask, amask);

if (surface == NULL) {
	fprintf(stderr, "CreateRGBSurface failed: %s\n",

SDL_GetError());
exit(1);
}

newImage = SDL_DisplayFormat(surface);

SDL_FillRect(newImage, NULL, SDL_MapRGB(newImage->format, red,

green, blue));

SDL_SetAlpha(newImage, SDL_SRCALPHA|SDL_RLEACCEL, 128);

SDL_FreeSurface(surface);

return newImage;

}