Filling with transparent pixels

Hello,

I’m trying to create a SDL_Surface with SDL_CreateRGBSurface and filling
it in with transparent pixels with SDL_FillRect. I want to use this
surface for the text of a dynamically sized text box which will be put
on top of background elements.

SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0,
0, 0);
SDL_FillRect(surface, NULL, SDL_MapRGBA(mDialogueBox->format, 0,0,0,0));

The above makes the dialog box completely black. This happens even if
I’m not rendering any text. I also tried a few variations of code with
SDL_SetAlpha but that seems to effect the entire surface when I want to
make individually transparent pixels. If I create a surface using
SDL_Image with a .png file the transparencies are fine, but I need to
make the surface dynamically sized.

Thanks.

You need to tell SDL about your pixel format. Also, you shouldn’t use
a different surface’s format when coloring a different surface. Try
something like this:

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    SDL_Surface* surface =

SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,32, 0xFF000000,
0x00FF0000, 0x0000FF00, 0x000000FF);
#else
SDL_Surface* surface =
SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,32, 0x000000FF,
0x0000FF00, 0x00FF0000, 0xFF000000);
#endif

SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0,0,0,0));

If you want to put text on this surface, I suggest calling this first:
SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE);

Put the text on the surface, then call this before you blit the
surface to the screen:
SDL_SetAlpha(surface, SDL_SRCALPHA, SDL_ALPHA_OPAQUE);

What this does is to disable alpha blending when you put the text on
the surface (otherwise you won’t see anything), and then to enable
alpha blending for when you blit this elsewhere.

Jonny DOn Tue, Jul 7, 2009 at 1:23 AM, Travis wrote:

Hello,

I’m trying to create a SDL_Surface with SDL_CreateRGBSurface and filling it
in with transparent pixels with SDL_FillRect. I want to use this surface for
the text of a dynamically sized text box which will be put on top of
background elements.

SDL_Surface *surface = SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0,
0);
SDL_FillRect(surface, NULL, SDL_MapRGBA(mDialogueBox->format, 0,0,0,0));

The above makes the dialog box completely black. This happens even if I’m
not rendering any text. I also tried a few variations of code with
SDL_SetAlpha but that seems to effect the entire surface when I want to make
individually transparent pixels. If I create a surface using SDL_Image with
a .png file the transparencies are fine, but I need to make the surface
dynamically sized.

Thanks.


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

Thank you, this helped greatly. I have what I want working now. I also
had to make use of the color key option to get the exact combination of
solid text and transparent backgrounds I needed.