sdl12-compat: support SDL2 versions down to 2.0.7

From bf73424b68cd2b90de4dc813b2de70e4097d36a5 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Fri, 15 Oct 2021 18:56:20 +0300
Subject: [PATCH] support SDL2 versions down to 2.0.7

Closes: https://github.com/libsdl-org/sdl12-compat/issues/134
---
 README.md          |  2 +-
 src/SDL12_compat.c | 24 +++++++++++-------------
 src/SDL20_syms.h   | 13 ++++++-------
 3 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/README.md b/README.md
index ccf21ca..c2af48c 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@ that.
 will have a drop-in replacement that can be used with any existing binary
 that relies on SDL 1.2. You can copy this library over the existing 1.2 build,
 or force it to take priority over a system copy with LD_LIBRARY_PATH, etc.
-At runtime, sdl12-compat needs to be able to find a copy of SDL2 (v2.0.9 or
+At runtime, sdl12-compat needs to be able to find a copy of SDL2 (v2.0.7 or
 newer -- v2.0.12 or newer for Windows), so plan to include it with the library
 if necessary.
 
diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 403dc84..e9e12b4 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -171,7 +171,7 @@ SDL20_MostSignificantBitIndex32(Uint32 x)
 /* SDL_truncf needs SDL >=2.0.14, so copy it here. */
 static float SDL20_truncf(float x)
 {
-    return (x < 0.0f) ? SDL20_ceilf(x) : SDL20_floorf(x);
+    return (x < 0.0f) ? (float)SDL20_ceil(x) : (float)SDL20_floor(x);
 }
 
 #define SDL12_DEFAULT_REPEAT_DELAY 500
@@ -989,7 +989,7 @@ static char loaderror[256];
     #include <os2.h>
     #define DIRSEP "\\"
     #define SDL20_LIBNAME "SDL2.dll"
-    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,9)
+    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,7)
     #define strcpy_fn  strcpy
     #define sprintf_fn sprintf
     static HMODULE Loaded_SDL20 = NULLHANDLE;
@@ -1017,7 +1017,7 @@ static char loaderror[256];
     #include <unistd.h>
     #define SDL20_LIBNAME "libSDL2-2.0.0.dylib"
     #define SDL20_FRAMEWORK "SDL2.framework/Versions/A/SDL2"
-    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,9)
+    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,7)
     #define strcpy_fn  strcpy
     #define sprintf_fn sprintf
     static void *Loaded_SDL20 = NULL;
@@ -1068,7 +1068,7 @@ static char loaderror[256];
 #elif defined(__unix__)
     #include <dlfcn.h>
     #define SDL20_LIBNAME "libSDL2-2.0.so.0"
-    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,9)
+    #define SDL20_REQUIRED_VER SDL_VERSIONNUM(2,0,7)
     static void *Loaded_SDL20 = NULL;
     #define LoadSDL20Library() ((Loaded_SDL20 = dlopen(SDL20_LIBNAME, RTLD_LOCAL|RTLD_NOW)) != NULL)
     #define LookupSDL20Sym(sym) dlsym(Loaded_SDL20, sym)
@@ -2182,7 +2182,7 @@ GetOpenGLLogicalScalingViewport(int physical_width, int physical_height)
     want_aspect = ((float) OpenGLLogicalScalingWidth) / ((float) OpenGLLogicalScalingHeight);
     real_aspect = ((float) physical_width) / ((float) physical_height);
 
-    if (SDL20_fabsf(want_aspect-real_aspect) < 0.0001f) {
+    if (SDL20_fabs(want_aspect-real_aspect) < 0.0001) {
         /* The aspect ratios are the same, just scale appropriately */
         dstrect.x = 0;
         dstrect.y = 0;
@@ -2193,14 +2193,14 @@ GetOpenGLLogicalScalingViewport(int physical_width, int physical_height)
         const float scale = ((float) physical_width) / OpenGLLogicalScalingWidth;
         dstrect.x = 0;
         dstrect.w = physical_width;
-        dstrect.h = (int)SDL20_floorf(OpenGLLogicalScalingHeight * scale);
+        dstrect.h = (int)SDL20_floor(OpenGLLogicalScalingHeight * scale);
         dstrect.y = (physical_height - dstrect.h) / 2;
     } else {
         /* We want a narrower aspect ratio than is available - use side-bars */
         const float scale = ((float)physical_height) / OpenGLLogicalScalingHeight;
         dstrect.y = 0;
         dstrect.h = physical_height;
-        dstrect.w = (int)SDL20_floorf(OpenGLLogicalScalingWidth * scale);
+        dstrect.w = (int)SDL20_floor(OpenGLLogicalScalingWidth * scale);
         dstrect.x = (physical_width - dstrect.w) / 2;
     }
 
@@ -3429,12 +3429,10 @@ Surface20to12(SDL_Surface *surface20)
     format12->Bmask = surface20->format->Bmask;
     format12->Amask = surface20->format->Amask;
 
-    if (SDL20_HasColorKey(surface20)) {
-        if (SDL20_GetColorKey(surface20, &format12->colorkey) < 0) {
-            format12->colorkey = 0;
-        } else {
-            surface12->flags |= SDL12_SRCCOLORKEY;
-        }
+    if (SDL20_GetColorKey(surface20, &format12->colorkey) < 0) {
+        format12->colorkey = 0;
+    } else {
+        surface12->flags |= SDL12_SRCCOLORKEY;
     }
 
     if (SDL20_GetSurfaceAlphaMod(surface20, &format12->alpha) < 0) {
diff --git a/src/SDL20_syms.h b/src/SDL20_syms.h
index 374b2cc..343ce43 100644
--- a/src/SDL20_syms.h
+++ b/src/SDL20_syms.h
@@ -113,7 +113,6 @@ SDL20_SYM(void,UnlockSurface,(SDL_Surface *a),(a),)
 SDL20_SYM(int,UpperBlit,(SDL_Surface *a,const SDL_Rect *b,SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
 SDL20_SYM(int,LowerBlit,(SDL_Surface *a,const SDL_Rect *b,SDL_Surface *c, SDL_Rect *d),(a,b,c,d),return)
 SDL20_SYM(int,SoftStretch,(SDL_Surface *a,const SDL_Rect *b,SDL_Surface *c,const SDL_Rect *d),(a,b,c,d),return)
-SDL20_SYM(SDL_bool,HasColorKey,(SDL_Surface *a),(a),return)
 SDL20_SYM(int,SetColorKey,(SDL_Surface *a, int b, Uint32 c),(a,b,c),return)
 SDL20_SYM(int,GetColorKey,(SDL_Surface *a, Uint32 *b),(a,b),return)
 SDL20_SYM(void,FreeSurface,(SDL_Surface *a),(a),)
@@ -301,15 +300,15 @@ SDL20_SYM(int,atoi,(const char *a),(a),return)
 #ifdef __WATCOMC__ /* Watcom builds are broken with SDL math functions. */
 #ifndef SDL12_MATH
 #include <math.h>
-#define SDL20_fabsf fabs
-#define SDL20_ceilf ceil
-#define SDL20_floorf floor
+#define SDL20_fabs fabs
+#define SDL20_ceil ceil
+#define SDL20_floor floor
 #define SDL12_MATH
 #endif
 #else
-SDL20_SYM(float,fabsf,(float a),(a),return)
-SDL20_SYM(float,ceilf,(float a),(a),return)
-SDL20_SYM(float,floorf,(float a),(a),return)
+SDL20_SYM(double,fabs,(double a),(a),return)
+SDL20_SYM(double,ceil,(double a),(a),return)
+SDL20_SYM(double,floor,(double a),(a),return)
 #endif
 
 SDL20_SYM(SDL_Renderer *,CreateRenderer,(SDL_Window *a, int b, Uint32 c),(a,b,c),return)