简体   繁体   English

SDL_Texture 不透明度

[英]SDL_Texture opacity

How can I change opacity of the SDL_Texture?如何更改 SDL_Texture 的不透明度? And I don't know how to apply the opacity number in my function.而且我不知道如何在我的函数中应用不透明度数。

My code我的代码

    void drawTexture(SDL_Texture *img, int x, int y, int width, int height, double opacity)
    {
        SDL_Rect SrcR;
        SDL_Rect DestR;

        SrcR.x = 0;
        SrcR.y = 0;
        SrcR.w = width;
        SrcR.h = height;

        DestR.x = x;
        DestR.y = y;
        DestR.w = width;
        DestR.h = height;

        SDL_RenderCopy(_main::_main_renderer, img, &SrcR, &DestR);
    }

Use SDL_SetTextureAlphaMod :使用SDL_SetTextureAlphaMod

SDL_SetTextureAlphaMod(img, opacity);

This will set the opacity (alpha) of the texture, the alpha value must be a Uint8 from 0 (totally transparent aka invisible) to 255 (fully opaque).这将设置纹理的不透明度(alpha),alpha 值必须是从0 (完全透明又名不可见)到255 (完全不透明)的Uint8

Opening question doesn't have info on the origin of img texture so the chosen answer it not correct it the texture is created from raw pixel data that doesn't have Alpha, ie using this:开场问题没有关于img纹理来源的信息,所以选择的答案它不会纠正纹理是从没有 Alpha 的原始像素数据创建的,即使用这个:

SDL_UpdateTexture(img, NULL, pixels, pitch);

If pixels contains raw pixel data without alpha, ie ARGB with A 0x00, even if you do this如果像素包含没有 alpha 的原始像素数据,即带有A 0x00 的 ARGB,即使您这样做

SDL_UpdateTexture(img, NULL, pixels, pitch);
SDL_SetTextureAlphaMod(img, opacity);

you will not see the texture (in this case alpha is 0x00) or you'll see garbage你不会看到纹理(在这种情况下 alpha 是 0x00)或者你会看到垃圾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM