【问题标题】:Transparent SDL_Surface without background没有背景的透明 SDL_Surface
【发布时间】:2013-02-05 23:08:46
【问题描述】:

我正在使用 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 应用于文本,而不添加背景,只添加文本?​​

【问题讨论】:

    标签: c++ opengl sdl alpha sdl-ttf


    【解决方案1】:

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

    【讨论】:

    • 伙计,我需要帮助,设置颜色键不起作用,如果我跳过填充矩形,blit 对我不起作用...
    • 您最好提出一个新的>特定
    • 好的,谢谢,我会开一个新问题,使用 sdl ttf 不太舒服....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 2011-03-14
    • 1970-01-01
    • 2011-03-08
    • 1970-01-01
    • 2013-01-24
    • 2015-02-08
    相关资源
    最近更新 更多