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?
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)