PSSGCSim
PSSGCSim

Reputation: 1287

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);
    }

Upvotes: 0

Views: 2767

Answers (2)

Francesco
Francesco

Reputation: 650

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, i.e. using this:

SDL_UpdateTexture(img, NULL, pixels, pitch);

If pixels contains raw pixel data without alpha, i.e. ARGB with A 0x00, even if you do this

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

Upvotes: 0

xorguy
xorguy

Reputation: 2744

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

Upvotes: 4

Related Questions