SDL_TTF and transparency

I’m migrating an application that used GL to do most of it’s rendering
to SDL and I’m having trouble getting transparency to work. In the
example I have below I’m trying to render a font, give the surface that
returns a particular alpha value, and then blit it to the screen. This
doesn’t seem to work, I still see the font even though it should be
completely transparent. With GL I was simply setting the GL color with
glColor4ub( color->red, color->green, color->blue, color->alpha ) and
then doing any rendering that was necessary. Is what I’m attempting to
do similar to what I was doing in GL? Is there a better way to do
transparency when I want the entire surface to be at a specific alpha level?

#include
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

using namespace std;

int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
cout << "Unable to init SDL: " << SDL_GetError() << endl;
exit(1);
}

if ( TTF_Init() < 0 ) {
    cout <<  "Couldn't initialize TTF: " << SDL_GetError() << endl;
}

SDL_Surface *screen = SDL_SetVideoMode(500, 500, 32, SDL_SWSURFACE);

if (screen == NULL) {
    cout <<  "Error in SDL_SetVideoMode(): " << SDL_GetError() << endl;
    exit(1);
}

SDL_WM_SetCaption("FontTest", NULL);

SDL_Color color;
color.r = 255;
color.g = 255;
color.b = 255;

TTF_Font *theFont = TTF_OpenFont("/aeon/game/fonts/times.ttf", 40);
TTF_SetFontStyle(theFont, 0);

SDL_Surface *temp = TTF_RenderText_Blended(theFont,"Foo Bar",color);
SDL_Surface *font = SDL_DisplayFormatAlpha(temp);

SDL_SetAlpha(font,SDL_SRCALPHA,0);

SDL_Rect destination;
destination.x = 100;
destination.y = 100;
destination.w = 100;
destination.h = 100;

SDL_BlitSurface(font,NULL,screen,&destination);
SDL_UpdateRect(screen, 0, 0, 500, 500);

sleep(3);

}

Joe Keen wrote:

I’m migrating an application that used GL to do most of it’s rendering
to SDL and I’m having trouble getting transparency to work. In the
example I have below I’m trying to render a font, give the surface that
returns a particular alpha value, and then blit it to the screen.

Blended (antialiased) fonts use per-pixel blending. You can’t mix that with
per-surface blending in SDL 1.2. The per-pixel blending will always be used if
it’s available. This is written in the SDL_SetAlpha() docs.

Ilya Olevsky wrote:

Joe Keen wrote:

I’m migrating an application that used GL to do most of it’s rendering
to SDL and I’m having trouble getting transparency to work. In the
example I have below I’m trying to render a font, give the surface that
returns a particular alpha value, and then blit it to the screen.

Blended (antialiased) fonts use per-pixel blending. You can’t mix that with
per-surface blending in SDL 1.2. The per-pixel blending will always be used if
it’s available. This is written in the SDL_SetAlpha() docs.

So, if I’m understanding right, I’d do something along the lines of
replacing the SDL_SetAlpha(font,SDL_SRCALPHA,0) line with the following code

Uint32 colorkey=SDL_MapRGBA(font->format, color.r, color.g, color.b,0);
SDL_SetColorKey(font, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

That seems to give me no different result. Do you have an example that
would show how to give fonts an alpha value?

So, if I’m understanding right, I’d do something along the lines of
replacing the SDL_SetAlpha(font,SDL_SRCALPHA,0) line with the following code

Uint32 colorkey=SDL_MapRGBA(font->format, color.r, color.g, color.b,0);
SDL_SetColorKey(font, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);

That seems to give me no different result. Do you have an example that
would show how to give fonts an alpha value?

Ok as I understand, you’re trying to set opacity (per-surface alpha) of a font
surface that was created with TTF_RenderText_Blended(). This can’t be done in
SDL because this function creates a surface with per-pixel alpha blending to
make a real time antialiased font. You would have to use TTF_RenderText_Solid()
or TTF_RenderText_Shaded() and then you can use per-surface alpha. Those methods
won’t give you a font that gets antialiased in real time though.

And yes, this is something that’s trivial to do in OpenGL or D3D, but SDL is a
software renderer so it has its limitations. There’s another recent topic about
the same thing you can check out: “Alpha blending transparent PNGs” which covers
an alternate method to accomplish what you want to do.