Commit d763a9f64dd653eb7cb52ad3dd80c56cef882ba2

Sam Lantinga 2015-06-19T23:22:53

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.

diff --git a/src/render/software/SDL_rotate.c b/src/render/software/SDL_rotate.c
index 3a8bc21..89a3aac 100644
--- a/src/render/software/SDL_rotate.c
+++ b/src/render/software/SDL_rotate.c
@@ -342,7 +342,7 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
     SDL_Surface *rz_src;
     SDL_Surface *rz_dst;
     int is32bit;
-    int i, src_converted;
+    int i;
     Uint8 r,g,b;
     Uint32 colorkey = 0;
     int colorKeyAvailable = 0;
@@ -369,27 +369,15 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
         * Use source surface 'as is'
         */
         rz_src = src;
-        src_converted = 0;
     } else {
-        /*
-        * New source surface is 32bit with a defined RGBA ordering
-        */
-        rz_src =
-            SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32,
+        Uint32 format = SDL_MasksToPixelFormatEnum(32,
 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
             0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
 #else
             0xff000000,  0x00ff0000, 0x0000ff00, 0x000000ff
 #endif
-            );
-        if(colorKeyAvailable)
-            SDL_SetColorKey(src, 0, 0);
-
-        SDL_BlitSurface(src, NULL, rz_src, NULL);
-
-        if(colorKeyAvailable)
-            SDL_SetColorKey(src, SDL_TRUE /* SDL_SRCCOLORKEY */, colorkey);
-        src_converted = 1;
+        );
+        rz_src = SDL_ConvertSurfaceFormat(src, format, src->flags);
         is32bit = 1;
     }
 
@@ -474,6 +462,19 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
             flipx, flipy);
         SDL_SetColorKey(rz_dst, /* SDL_SRCCOLORKEY */ SDL_TRUE | SDL_RLEACCEL, _colorkey(rz_src));
     }
+
+    /* copy alpha mod, color mod, and blend mode */
+    {
+      SDL_BlendMode blendMode;
+      Uint8 alphaMod, r, g, b;
+      SDL_GetSurfaceAlphaMod(src, &alphaMod);
+      SDL_GetSurfaceBlendMode(src, &blendMode);
+      SDL_GetSurfaceColorMod(src, &r, &g, &b);
+      SDL_SetSurfaceAlphaMod(rz_dst, alphaMod);
+      SDL_SetSurfaceBlendMode(rz_dst, blendMode);
+      SDL_SetSurfaceColorMod(rz_dst, r, g, b);
+    }
+
     /*
     * Unlock source surface
     */
@@ -484,7 +485,7 @@ SDLgfx_rotateSurface(SDL_Surface * src, double angle, int centerx, int centery, 
     /*
     * Cleanup temp surface
     */
-    if (src_converted) {
+    if (rz_src != src) {
         SDL_FreeSurface(rz_src);
     }