我正在使用SDL和OpenGL。 我正在使用SDL ttf创建带有文本的表面,然后使用该表面创建纹理。
我正在尝试将alpha通道(不透明度)应用于文本,我正在使用这种方式:
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 );
正确应用了不透明度,但SDL_MapRGBA在“newSurface”中创建了黑色背景。 如何在不添加背景的情况下对文本应用alpha / opacity,只显示文本?
答案 0 :(得分:2)
我认为你缺少newSurface的SDL_SetColorKey。
这样的事情:
SDL_SetColorKey( newSurface , SDL_SRCCOLORKEY, SDL_MapRGB( newSurface->format, 0, 0, 0) );
甚至更容易跳过用黑色矩形填充newSurface
删除它:
SDL_FillRect( newSurface, &rect, alpha);
以下是我如何使用透明色制作图片的示例:
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)