SDL_ttf and SDL_SRCALPHA

I’m trying to write a simple program that creates a bitmapped font based
on a truetype font. I want the outputed bitmap to have just the
characters and then a transparent background. The thing is I can’t get
this to work. This is more or less my approach:

For each character
surface = TTF_RenderText_Blended(…);
blit surface onto another 32 bit transparent surface created with
SDL_CreateRGBSurface()

Save final surface to tga

Whenever I load the TGA it’s blank, completely transparent. If I do an
SDL_FillRect() on it first before blitting the characters it works. If I
do an SDL_FillRect() and set the alpha of each pixel to 50% or so, the
letters show up, but at 50% transparency.

I’ve investigated a little and I think the cause is that SDL_SRCALPHA
isn’t set in the surface that SDL_ttf returns, so it ignores that
surface’s alpha values when blitting. Is that the cause? If it is, is
there a reason that SDL_SRCALPHA isn’t set? Is there a workaround that
doesn’t involve me recompiling SDL_ttf?

Hey Brad, have you seen LMNOPC bitmap font builder?

http://www.lmnopc.com/bitmapfontbuilder/

that should be able to do that for you so you don’t have to reinvent the
wheel> ----- Original Message -----

From: braddabug@comcast.net (Brad)
To:
Sent: Tuesday, May 17, 2005 4:11 PM
Subject: [SDL] SDL_ttf and SDL_SRCALPHA

I’m trying to write a simple program that creates a bitmapped font based
on a truetype font. I want the outputed bitmap to have just the
characters and then a transparent background. The thing is I can’t get
this to work. This is more or less my approach:

For each character
surface = TTF_RenderText_Blended(…);
blit surface onto another 32 bit transparent surface created with
SDL_CreateRGBSurface()

Save final surface to tga

Whenever I load the TGA it’s blank, completely transparent. If I do an
SDL_FillRect() on it first before blitting the characters it works. If I
do an SDL_FillRect() and set the alpha of each pixel to 50% or so, the
letters show up, but at 50% transparency.

I’ve investigated a little and I think the cause is that SDL_SRCALPHA
isn’t set in the surface that SDL_ttf returns, so it ignores that
surface’s alpha values when blitting. Is that the cause? If it is, is
there a reason that SDL_SRCALPHA isn’t set? Is there a workaround that
doesn’t involve me recompiling SDL_ttf?


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Brad wrote:

For each character
surface = TTF_RenderText_Blended(…);
blit surface onto another 32 bit transparent surface created with
SDL_CreateRGBSurface()

Save final surface to tga

Whenever I load the TGA it’s blank, completely transparent. If I do an
SDL_FillRect() on it first before blitting the characters it works. If I
do an SDL_FillRect() and set the alpha of each pixel to 50% or so, the
letters show up, but at 50% transparency.

In the manpage for SDL_SetAlpha you’ll read that when blitting
from RGBA to RGBA surfaces the destination alpha channel will
be untouched.
Why are you blitting to another surface anyway?

If I don’t need SDL_SRCAPLHA then what’s the problem? You can see what
I’m trying to do here: http://penguin.agrid.usm.edu/~brad/fonttool.cpp.
It may look a little strange in places since I posted that in the middle
of debugging it.

SDL_SRCALPHA is the per-surface value which is, as far as i know, initially always
set to SDL_ALPHA_OPAQUE, so this is not your problem.

Ok, you’re rendering blended glyphs using SDL_ttf. This will yield RGBA surfaces
which you blit, because you want the outputted TGA to have an alpha layer,
to another RGBA surface. Now according to the manual page of SDL_SetAlpha, blitting
from RGBA surfaces to RGBA surfaces blends the source surface with the destination
surface, using the source alpha channel and leaving the destination alpha channel
untouched. Because you’re operating on a freshly allocated RGBA surface, all its
(per-pixel) alpha values are transparent and won’t change during blitting.

You would need a function which copies the alpha layer (or all channels) of an
RGBA surface to another.On Wed, 18 May 2005 13:09:33 -0500 Brad wrote:

If I don’t need SDL_SRCAPLHA then what’s the problem? You can see what
I’m trying to do here: http://penguin.agrid.usm.edu/~brad/fonttool.cpp.
It may look a little strange in places since I posted that in the middle
of debugging it.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl