gpt4 book ai didi

c++ - 没有背景的透明 SDL_Surface

转载 作者:行者123 更新时间:2023-11-28 03:23:43 29 4
gpt4 key购买 nike

我正在使用 SDL 和 OpenGL。我使用 SDL ttf 创建带有文本的表面,然后使用该表面创建纹理。

我正在尝试将 alpha channel (不透明度)应用于文本,我正在使用这种方式:

    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/不透明度?

最佳答案

我认为您缺少 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)

关于c++ - 没有背景的透明 SDL_Surface,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14718698/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com