Search code examples
c++sdlsdl-2

SDL_Texture opacity


How can I change opacity of the 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);
    }

Solution

  • Use 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).