SDL_ttf: text with transparency on a surface

I’m trying to use the SDL_ttf library to render subtitles on a video. It works,
but for the moment the subtitle has a colored box around it, and I would like to
set is transparent, if possible. This is my code:

SDL_Color color={255,255,255};
SDL_Surface *text_surface;
SDL_Rect rect, rect2;

TTF_SizeText(font,text,&rect.w,&rect.h);

rect.y=height-rect.h;
rect.x=(width-rect.w)/2;
rect2.x=rect.x;
rect2.y=rect.y;
rect2.w=rect.w;
rect2.h=rect.h;

text_surface=TTF_RenderText_Blended(font,text,color);
	SDL_BlitSurface(text_surface,NULL,display,&rect2);
    SDL_FreeSurface(text_surface);
SDL_UpdateRect(display, rect.x, rect.y, rect.w, rect.h);

The video is rendered with a YUVOverlay, build over the “display” SDL_Surface,
and on it I should render the text. I tried to use SDL_SetColorKey, but there’s
still a black box around text. Does anyone have suggestions for my problem?

Thanks,
Simone

The video is rendered with a YUVOverlay, build over the “display” SDL_Surface,
and on it I should render the text. I tried to use SDL_SetColorKey, but there’s
still a black box around text. Does anyone have suggestions for my problem?

The black box is probably due to you alpha blending against the current
contents of “display”. Try using TTF_RenderText_Shaded(), and doing a
colorkey blit (the colorkey is 0 in these cases). This will probably fix
your problem and be much faster, too.

–ryan.

Simone Bottani wrote:

The video is rendered with a YUVOverlay, build over the "display"
SDL_Surface, and on it I should render the text. I tried to use
SDL_SetColorKey, but there’s still a black box around text. Does
anyone have suggestions for my problem?

this is not how an overlay works. the video is blended in by your grafix
hardware and comes from a different location in the video memory. when
you blit to your screen surface the pixels of your text do not overwrite
the overlay but the pixels “behind” it. what makes this even more
confusing is, that the overlay is more of an “underlay” as you still see
your blits to the screen. to come around this either do not use the
overlay in the first place or convert your subtitles into an YUV surface
and blit it into your video befor sending it to the grafix card. there
was a thread about something like this a few days ago, have a look in
the archives.

best regards …
clemens

this is not how an overlay works. the video is blended in by your grafix
hardware and comes from a different location in the video memory. when
you blit to your screen surface the pixels of your text do not overwrite
the overlay but the pixels “behind” it. what makes this even more
confusing is, that the overlay is more of an “underlay” as you still see
your blits to the screen. to come around this either do not use the
overlay in the first place or convert your subtitles into an YUV surface
and blit it into your video befor sending it to the grafix card. there
was a thread about something like this a few days ago, have a look in
the archives.

Oh, he’s trying to blit over an overlay? Yeah, the above quote is good
advice, then.

–ryan.

I submitted a patch to SDL_ttf last year to get rid of a few different
problems caused by inconsistent colors in SDL_ttf surfaces, but I never
got an answer to it. This behavior might have been related to that.
There are a few places where the black background interferes with things,
and the one that got on my nerves was that resizing the bitmaps made ugly
dark pixels on the edges of the letters. Those dark pixels came from the
letters being blended with invisible black pixels, and it was fixed by
making the entire background a single color before rendering the text. If
anyone is interested, I could resend a copy of that e-mail (if I can find
it), or you might be able to find it in the archives of last December or
so. The patch wasn’t all that sophisticated (inserting 2 lines), but it
was all it took :slight_smile:

RyanOn Mon, 17 Oct 2005, Ryan C. Gordon wrote:

this is not how an overlay works. the video is blended in by your grafix
hardware and comes from a different location in the video memory. when
you blit to your screen surface the pixels of your text do not overwrite
the overlay but the pixels “behind” it. what makes this even more
confusing is, that the overlay is more of an “underlay” as you still see
your blits to the screen. to come around this either do not use the
overlay in the first place or convert your subtitles into an YUV surface
and blit it into your video befor sending it to the grafix card. there
was a thread about something like this a few days ago, have a look in
the archives.

Oh, he’s trying to blit over an overlay? Yeah, the above quote is good
advice, then.

–ryan.


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

I submitted a patch to SDL_ttf last year to get rid of a few different
problems caused by inconsistent colors in SDL_ttf surfaces, but I never
got an answer to it. This behavior might have been related to that.
There are a few places where the black background interferes with things,
and the one that got on my nerves was that resizing the bitmaps made ugly
dark pixels on the edges of the letters. Those dark pixels came from the
letters being blended with invisible black pixels, and it was fixed by
making the entire background a single color before rendering the text. If
anyone is interested, I could resend a copy of that e-mail (if I can find
it), or you might be able to find it in the archives of last December or
so. The patch wasn’t all that sophisticated (inserting 2 lines), but it
was all it took :slight_smile:

You mean this:
http://www.libsdl.org/pipermail/sdl/2004-December/066455.html

…I just committed this to CVS, thanks!

–ryan.