Fixed bug 2538 - SDL_SetTextureAlphaMod does not work with SDL_FlipMode or rotation in the software renderer
authorSam Lantinga <slouken@libsdl.org>
Fri, 19 Jun 2015 23:22:53 -0700
changeset 9763c2ba8ab27c1a
parent 9762 5c4a85c5b648
child 9764 eda4a6234d71
Fixed bug 2538 - SDL_SetTextureAlphaMod does not work with SDL_FlipMode or rotation in the software renderer

Adam M.

When setting a texture alpha mod other than 255 and also specifying a flip mode in the software renderer, the rendering fails. When the texture has an alpha channel, it becomes invisible when flipped. When the texture does not have an alpha channel, it is flipped but the colors are wrong: the alpha mod makes the texture darker rather than more translucent.

0) Initialize a software renderer.
1) Load 16-bit 565 or 32-bit texture.
2) Set texture blend mode to BLEND.
3) Set texture alpha mod to 150.
4) Draw the texture flipped horizontally and/or vertically.
src/render/software/SDL_rotate.c
     1.1 --- a/src/render/software/SDL_rotate.c	Fri Jun 19 23:20:43 2015 -0700
     1.2 +++ b/src/render/software/SDL_rotate.c	Fri Jun 19 23:22:53 2015 -0700
     1.3 @@ -342,7 +342,7 @@
     1.4      SDL_Surface *rz_src;
     1.5      SDL_Surface *rz_dst;
     1.6      int is32bit;
     1.7 -    int i, src_converted;
     1.8 +    int i;
     1.9      Uint8 r,g,b;
    1.10      Uint32 colorkey = 0;
    1.11      int colorKeyAvailable = 0;
    1.12 @@ -369,27 +369,15 @@
    1.13          * Use source surface 'as is'
    1.14          */
    1.15          rz_src = src;
    1.16 -        src_converted = 0;
    1.17      } else {
    1.18 -        /*
    1.19 -        * New source surface is 32bit with a defined RGBA ordering
    1.20 -        */
    1.21 -        rz_src =
    1.22 -            SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
    1.23 +        Uint32 format = SDL_MasksToPixelFormatEnum(32,
    1.24  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
    1.25              0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
    1.26  #else
    1.27              0xff000000,  0x00ff0000, 0x0000ff00, 0x000000ff
    1.28  #endif
    1.29 -            );
    1.30 -        if(colorKeyAvailable)
    1.31 -            SDL_SetColorKey(src, 0, 0);
    1.32 -
    1.33 -        SDL_BlitSurface(src, NULL, rz_src, NULL);
    1.34 -
    1.35 -        if(colorKeyAvailable)
    1.36 -            SDL_SetColorKey(src, SDL_TRUE /* SDL_SRCCOLORKEY */, colorkey);
    1.37 -        src_converted = 1;
    1.38 +        );
    1.39 +        rz_src = SDL_ConvertSurfaceFormat(src, format, src->flags);
    1.40          is32bit = 1;
    1.41      }
    1.42  
    1.43 @@ -474,6 +462,19 @@
    1.44              flipx, flipy);
    1.45          SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
    1.46      }
    1.47 +
    1.48 +    /* copy alpha mod, color mod, and blend mode */
    1.49 +    {
    1.50 +      SDL_BlendMode blendMode;
    1.51 +      Uint8 alphaMod, r, g, b;
    1.52 +      SDL_GetSurfaceAlphaMod(src, &alphaMod);
    1.53 +      SDL_GetSurfaceBlendMode(src, &blendMode);
    1.54 +      SDL_GetSurfaceColorMod(src, &r, &g, &b);
    1.55 +      SDL_SetSurfaceAlphaMod(rz_dst, alphaMod);
    1.56 +      SDL_SetSurfaceBlendMode(rz_dst, blendMode);
    1.57 +      SDL_SetSurfaceColorMod(rz_dst, r, g, b);
    1.58 +    }
    1.59 +
    1.60      /*
    1.61      * Unlock source surface
    1.62      */
    1.63 @@ -484,7 +485,7 @@
    1.64      /*
    1.65      * Cleanup temp surface
    1.66      */
    1.67 -    if (src_converted) {
    1.68 +    if (rz_src != src) {
    1.69          SDL_FreeSurface(rz_src);
    1.70      }
    1.71