Problems with SDL_TTF and surfaces

Hi

I’m having a bit of trouble wrangling with an enhanced font drawing routine.

I have a bit of code which draws (using SDL_TTF) text with a stroked border
(outline), and / or a drop shadow.

Pseudocode:-

Draw Shadow
Blit to destination surface
Draw Outline
Blit to destination surface
Draw Actual Text
Blit to destination surface

For the shadow, I am using a solid font as it is not possible to combine
per-pixel alpha with per-surface alpha. I draw the shadow by drawing the
text a number of times with a low per surface alpha value (around 10). This
gives a nice effect which blends with the destination surface.

For drawing the outline and the actual text, I use a blended font to get the
best out of the nice anti-aliasing which results from the per-pixel alpha.

The routine works fine and looks great but only when the screen is the
destination surface.

Try as I might, I cannot get the code to draw to another SDL surface. I have
tried every possible way I can think of to do this, all without success. I
think I have tried every possible value for the RGB masks, and I have tried
creating the surfaces with and without SDL_SRCALPHA.

I either get no output at all when I blit the new surface to the screen, or
I lose the alpha capabilities completely and get a black box around my text.

Ultimately, the resulting surface will be converted to an OpenGL texture for
final display (please note that I am fully aware of how to do this, I don’t
need any specific advice on this).

Thanks in advance for any possible insight into this!

Steve Lupton**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com


Steve Lupton wrote:

Try as I might, I cannot get the code to draw to another SDL surface. I have
tried every possible way I can think of to do this, all without success. I
think I have tried every possible value for the RGB masks, and I have tried
creating the surfaces with and without SDL_SRCALPHA.

surfaces created with an alpha channel automatically get SDL_SRCALPHA set.
remove that (use SetAlpha) if you want a non-blending (copy) blit

also SDL does not support correctly blending blits to transparent
destination surfaces (RGBA->RGBA blits leave the destination alpha channel
unchanged), so if you need that you have to find a workaround

Steve,

The routine works fine and looks great but only when the screen is the
destination surface.

Try as I might, I cannot get the code to draw to another SDL surface. I have
tried every possible way I can think of to do this, all without success. I
think I have tried every possible value for the RGB masks, and I have tried
creating the surfaces with and without SDL_SRCALPHA.

The reason is that the destination alpha of the surface that you are
blitting the text into is untouched. SDL_Blit does nothing to the
destination alpha channel. So the final blit of the surface to the
screen is done with either alpha=0 (nothing to be seen) or alpha
blending turned off (black box around text).

The solution is to write your own blitting routine that combines the
text surfaces into a new surface transferring some of the alpha from the
source to the destination.

One possible algorithm is to transfer the source alpha into the
destination alpha after your have blitted the text-surface into the
destination-surface using: dA |= sA. This is fast and in most cases
visually acceptable (I know, Mattias, its a bad, bad hack).

Ciao
Andreas