0

I'm working with SDL and OpenGL. I'm using SDL ttf for create a surface with the text, then create a texture with that surface.

I'm trying to apply an alpha channel (opacity) to the text, I'm using this way:

    SDL_Surface * fontsurf = TTF_RenderUTF8_Blended(font,buff_split.at(i).c_str(),color);
SDL_PixelFormat *format = fontsurf->format;
SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
   fontsurf->w, fontsurf->h, 32,
   0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 );


    Uint32 alpha = 0;
    alpha = SDL_MapRGBA( newSurface->format, 0,0,0, color.unused);

    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.h = fontsurf->h;
    rect.w = fontsurf->w;
    int ret = SDL_FillRect( newSurface, &rect, alpha);

    SDL_SetAlpha(newSurface,SDL_SRCALPHA,SDL_ALPHA_TRANSPARENT);

    ret = SDL_BlitSurface( fontsurf, 0, newSurface, 0 );

The opacity is applied correctly, but the SDL_MapRGBA creates a black background in the "newSurface". How can I apply alpha/opacity to the text, without adding a background, only the text?

Spamdark
  • 1,341
  • 2
  • 19
  • 38

1 Answers1

2

I think you are missing SDL_SetColorKey for newSurface.

Something like this:

SDL_SetColorKey( newSurface , SDL_SRCCOLORKEY, SDL_MapRGB( newSurface->format, 0, 0, 0) );

or even easier would be to skip filling newSurface with a black rectangle

remove this:

SDL_FillRect( newSurface, &rect, alpha);

Here is an example how I make a picture with transparent color:

testImage = IMG_Load( "trans.png" );
// This picture contains color 255,0,255 which will be set as transparent

SDL_SetColorKey( testImage , SDL_SRCCOLORKEY, SDL_MapRGB( screen->format, 255, 0, 255) );
// I set the color 255,0,255 as transparent for that surface

SDL_BlitSurface( testImage , NULL , screen , NULL );
// and I draw it on the main surface (screen)
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
  • Man, I need help in here, the set color key is not working, if I skip the fill rect, the blit does not works to me... – Spamdark Feb 08 '13 at 20:08
  • You better ask a new >specific< question, and don't post that code in that state. –  Feb 08 '13 at 20:44
  • Ok thanks, I'll open a new question, it's not comfortable to work with sdl ttf.... – Spamdark Feb 08 '13 at 20:46