SDL2 纹理渲染目标没有 Alpha 透明度

Rok*_*ner 1 c ubuntu sdl-2

我在使用 SDL2 进行 C 语言编程时遇到了问题。我已将中心透明的正方形的简单图像渲染为纹理。但是当我绘制渲染它们的纹理时,它们并不透明。我什至尝试使用 SDL_SetTextureAlphaMod() 更改渲染纹理的透明度,但它没有改变任何内容。如果我更改正在渲染的纹理(方块)的 alpha 值。它们变得更暗,但它们仍然覆盖了它们后面的任何东西。所以我愿意接受建议。

这是一张图像,我降低了正方形纹理上的 alpha: https: //i.stack.imgur.com/46HAz.jpg

kdy*_*dyz 8

如果你想要透明图像,在 SDL2 中有两种方法:

  1. (静态方法)

    使用图像编辑软件并直接更改其中的alpha值,它将继续到SDL2。

  2. (动态法)

    // This sets the texture in blendmode.
    SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
    
    // This section should be where you alter the alpha. 
    // You can make fade in-fade out effects, etc... Just put the changes here.
    Uint8 alpha = 0x7F; 
    
    // Sets the alpha into the texture.
    SDL_SetTextureAlphaMod(texture, alpha); 
    
    // Redraws the image with a fresh, new alpha.
    SDL_RenderCopy(renderer, texture, NULL, &rect);
    
    Run Code Online (Sandbox Code Playgroud)