sdl12-compat: SDL_SetAlpha now works (more) like it should.

From b4acf4f08fff36dda9d80684cd6e8bd5a9a883f3 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Thu, 27 May 2021 16:14:04 -0400
Subject: [PATCH] SDL_SetAlpha now works (more) like it should.

Reference bug #67.
---
 src/SDL12_compat.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 1db8cef..8fec9e0 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -3776,12 +3776,30 @@ SDL_SoftStretch(SDL12_Surface *src12, SDL12_Rect *srcrect12, SDL12_Surface *dst1
 DECLSPEC int SDLCALL
 SDL_SetAlpha(SDL12_Surface *surface12, Uint32 flags12, Uint8 value)
 {
+    /* note that SDL 1.2 does not check if surface12 is NULL before dereferencing it either */
     const SDL_bool addkey = (flags12 & SDL12_SRCALPHA) ? SDL_TRUE : SDL_FALSE;
-    const int retval = SDL20_SetSurfaceAlphaMod(surface12->surface20, addkey ? value : 255);
-    if (SDL20_GetSurfaceAlphaMod(surface12->surface20, &surface12->format->alpha) < 0) {
-        surface12->format->alpha = 255;
+    int retval = -1;
+
+    if (addkey) {
+        if (!surface12->format->Amask) {  /* whole-surface alpha is ignored if surface has an alpha channel. */
+            retval = SDL20_SetSurfaceAlphaMod(surface12->surface20, value);
+            if (SDL20_GetSurfaceAlphaMod(surface12->surface20, &surface12->format->alpha) < 0) {
+                surface12->format->alpha = 255;
+            }
+        }
+        surface12->flags |= SDL12_SRCALPHA;
+        SDL20_SetSurfaceBlendMode(surface12->surface20, SDL_BLENDMODE_BLEND);
+    } else {
+        if (!surface12->format->Amask) {  /* whole-surface alpha is ignored if surface has an alpha channel. */
+            retval = SDL20_SetSurfaceAlphaMod(surface12->surface20, 255);
+            if (SDL20_GetSurfaceAlphaMod(surface12->surface20, &surface12->format->alpha) < 0) {
+                surface12->format->alpha = 255;
+            }
+        }
+        surface12->flags &= ~SDL12_SRCALPHA;
+        SDL20_SetSurfaceBlendMode(surface12->surface20, SDL_BLENDMODE_NONE);
     }
-    FIXME("Should this set SDL12_SRCALPHA on the surface?");
+
     return retval;
 }