sdl2-compat: Deal with mouse coords becoming floats in SDL3.

From 075a2b39dfb88032f41ef4b6e0b56c2494289dda Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Thu, 5 Jan 2023 14:51:40 +0300
Subject: [PATCH] Deal with mouse coords becoming floats in SDL3.

---
 src/sdl2_compat.c | 113 +++++++++++++++++++++++++++++++++++++++++++++-
 src/sdl3_syms.h   |  14 +++---
 2 files changed, 119 insertions(+), 8 deletions(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index c7f22ba..64863a5 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -1260,6 +1260,30 @@ Event3to2(const SDL_Event *event3, SDL2_Event *event2)
     event2->common.type = event3->type;
     event2->common.timestamp = (Uint32) SDL_NS_TO_MS(event3->common.timestamp);
     SDL3_memcpy((&event2->common) + 1, (&event3->common) + 1, sizeof (SDL2_Event) - sizeof (SDL2_CommonEvent));
+    switch (event3->type) {
+    case SDL_MOUSEMOTION:
+        event2->motion.x = (Sint32)event3->motion.x;
+        event2->motion.y = (Sint32)event3->motion.y;
+        event2->motion.xrel = (Sint32)event3->motion.xrel;
+        event2->motion.yrel = (Sint32)event3->motion.yrel;
+        break;
+    case SDL_MOUSEBUTTONDOWN:
+    case SDL_MOUSEBUTTONUP:
+        event2->button.x = (Sint32)event3->button.x;
+        event2->button.y = (Sint32)event3->button.y;
+        break;
+    case SDL_MOUSEWHEEL:
+    /* !!! FIXME: The preciseX|Y members were inserted to SDL_MouseWheelEvent in SDL2-2.0.18.  */
+        event2->wheel.x = (Sint32)event3->wheel.x;
+        event2->wheel.y = (Sint32)event3->wheel.y;
+        event2->wheel.preciseX = event3->wheel.x;
+        event2->wheel.preciseY = event3->wheel.y;
+        event2->wheel.mouseX = (Sint32)event3->wheel.mouseX;
+        event2->wheel.mouseY = (Sint32)event3->wheel.mouseY;
+        break;
+    default:
+        break;
+    }
     return event2;
 }
 
@@ -1279,6 +1303,33 @@ Event2to3(const SDL2_Event *event2, SDL_Event *event3)
     event3->common.type = event2->type;
     event3->common.timestamp = (Uint64) SDL_MS_TO_NS(event2->common.timestamp);
     SDL3_memcpy((&event3->common) + 1, (&event2->common) + 1, sizeof (SDL_Event) - sizeof (SDL_CommonEvent));
+    switch (event2->type) {
+    case SDL_MOUSEMOTION:
+        event3->motion.x = (float)event2->motion.x;
+        event3->motion.y = (float)event2->motion.y;
+        event3->motion.xrel = (float)event2->motion.xrel;
+        event3->motion.yrel = (float)event2->motion.yrel;
+        break;
+    case SDL_MOUSEBUTTONDOWN:
+    case SDL_MOUSEBUTTONUP:
+        event3->button.x = (float)event2->button.x;
+        event3->button.y = (float)event2->button.y;
+        break;
+    case SDL_MOUSEWHEEL:
+    /* !!! FIXME: Which member is safe to use here??  */
+    /* The preciseX|Y members were inserted to SDL_MouseWheelEvent in SDL2-2.0.18.  */
+        /*
+        event3->wheel.x = event2->wheel.preciseX;
+        event3->wheel.y = event2->wheel.preciseY;
+        */
+        event3->wheel.x = (float)event2->wheel.x;
+        event3->wheel.y = (float)event2->wheel.y;
+        event3->wheel.mouseX = (float)event2->wheel.mouseX;
+        event3->wheel.mouseY = (float)event2->wheel.mouseY;
+        break;
+    default:
+        break;
+    }
     return event3;
 }
 
@@ -1369,7 +1420,6 @@ EventFilter3to2(void *userdata, SDL_Event *event3)
     }
 
     /* !!! FIXME: Deal with device add events using instance ids instead of indices in SDL3. */
-    /* !!! FIXME: Deal with mouse coords becoming floats in SDL3. */
 
     return 1;
 }
@@ -1505,6 +1555,67 @@ SDL_FilterEvents(SDL2_EventFilter filter2, void *userdata)
     SDL3_FilterEvents(EventFilterWrapper3to2, &wrapperdata);
 }
 
+DECLSPEC Uint32 SDLCALL
+SDL_GetMouseState(int *x, int *y)
+{
+    float fx, fy;
+    Uint32 ret = SDL3_GetMouseState(&fx, &fy);
+    if (x) *x = (int)fx;
+    if (y) *y = (int)fy;
+    return ret;
+}
+
+DECLSPEC Uint32 SDLCALL
+SDL_GetGlobalMouseState(int *x, int *y)
+{
+    float fx, fy;
+    Uint32 ret = SDL3_GetGlobalMouseState(&fx, &fy);
+    if (x) *x = (int)fx;
+    if (y) *y = (int)fy;
+    return ret;
+}
+
+DECLSPEC Uint32 SDLCALL
+SDL_GetRelativeMouseState(int *x, int *y)
+{
+    float fx, fy;
+    Uint32 ret = SDL3_GetRelativeMouseState(&fx, &fy);
+    if (x) *x = (int)fx;
+    if (y) *y = (int)fy;
+    return ret;
+}
+
+DECLSPEC void SDLCALL
+SDL_WarpMouseInWindow(SDL_Window *window, int x, int y)
+{
+    SDL3_WarpMouseInWindow(window, (float)x, (float)y);
+}
+
+DECLSPEC int SDLCALL
+SDL_WarpMouseGlobal(int x, int y)
+{
+    return SDL3_WarpMouseGlobal((float)x, (float)y);
+}
+
+DECLSPEC void SDLCALL
+SDL_RenderWindowToLogical(SDL_Renderer *renderer,
+                          int windowX, int windowY,
+                          float *logicalX, float *logicalY)
+{
+    SDL3_RenderWindowToLogical(renderer, (float)windowX, (float)windowY, logicalX, logicalY);
+}
+
+DECLSPEC void SDLCALL
+SDL_RenderLogicalToWindow(SDL_Renderer *renderer,
+                          float logicalX, float logicalY,
+                          int *windowX, int *windowY)
+{
+    float x, y;
+    SDL3_RenderLogicalToWindow(renderer, logicalX, logicalY, &x, &y);
+    if (windowX) *windowX = (int)x;
+    if (windowY) *windowY = (int)y;
+}
+
 
 /* The SDL3 version of SDL_RWops changed, so we need to convert when necessary. */
 
diff --git a/src/sdl3_syms.h b/src/sdl3_syms.h
index a924d66..10d5356 100644
--- a/src/sdl3_syms.h
+++ b/src/sdl3_syms.h
@@ -250,9 +250,9 @@ SDL3_SYM_PASSTHROUGH(void,SetMainReady,(void),(),)
 SDL3_SYM_PASSTHROUGH(int,ShowMessageBox,(const SDL_MessageBoxData *a, int *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(int,ShowSimpleMessageBox,(Uint32 a, const char *b, const char *c, SDL_Window *d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(SDL_Window*,GetMouseFocus,(void),(),return)
-SDL3_SYM_PASSTHROUGH(Uint32,GetMouseState,(int *a, int *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(Uint32,GetRelativeMouseState,(int *a, int *b),(a,b),return)
-SDL3_SYM_PASSTHROUGH(void,WarpMouseInWindow,(SDL_Window *a, int b, int c),(a,b,c),)
+SDL3_SYM(Uint32,GetMouseState,(float *a, float *b),(a,b),return)
+SDL3_SYM(Uint32,GetRelativeMouseState,(float *a, float *b),(a,b),return)
+SDL3_SYM(void,WarpMouseInWindow,(SDL_Window *a, float b, float c),(a,b,c),)
 SDL3_SYM_PASSTHROUGH(int,SetRelativeMouseMode,(SDL_bool a),(a),return)
 SDL3_SYM_PASSTHROUGH(SDL_bool,GetRelativeMouseMode,(void),(),return)
 SDL3_SYM_PASSTHROUGH(SDL_Cursor*,CreateCursor,(const Uint8 *a, const Uint8 *b, int c, int d, int e, int f),(a,b,c,d,e,f),return)
@@ -565,13 +565,13 @@ SDL3_SYM_PASSTHROUGH(int,WinRTRunApp,(int a, char **b, void *c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(const wchar_t*,WinRTGetFSPathUNICODE,(SDL_WinRT_Path a),(a),return)
 SDL3_SYM_PASSTHROUGH(const char*,WinRTGetFSPathUTF8,(SDL_WinRT_Path a),(a),return)
 #endif
-SDL3_SYM_PASSTHROUGH(int,WarpMouseGlobal,(int a, int b),(a,b),return)
+SDL3_SYM(int,WarpMouseGlobal,(float a, float b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(float,sqrtf,(float a),(a),return)
 SDL3_SYM_PASSTHROUGH(double,tan,(double a),(a),return)
 SDL3_SYM_PASSTHROUGH(float,tanf,(float a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,CaptureMouse,(SDL_bool a),(a),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowHitTest,(SDL_Window *a, SDL_HitTest b, void *c),(a,b,c),return)
-SDL3_SYM_PASSTHROUGH(Uint32,GetGlobalMouseState,(int *a, int *b),(a,b),return)
+SDL3_SYM(Uint32,GetGlobalMouseState,(float *a, float *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(SDL_bool,HasAVX2,(void),(),return)
 SDL3_SYM_PASSTHROUGH(int,QueueAudio,(SDL_AudioDeviceID a, const void *b, Uint32 c),(a,b,c),return)
 SDL3_SYM_PASSTHROUGH(Uint32,GetQueuedAudioSize,(SDL_AudioDeviceID a),(a),return)
@@ -843,8 +843,8 @@ SDL3_SYM_PASSTHROUGH(int,hid_get_serial_number_string,(SDL_hid_device *a, wchar_
 SDL3_SYM_PASSTHROUGH(int,hid_get_indexed_string,(SDL_hid_device *a, int b, wchar_t *c, size_t d),(a,b,c,d),return)
 SDL3_SYM_PASSTHROUGH(int,SetWindowMouseRect,(SDL_Window *a, const SDL_Rect *b),(a,b),return)
 SDL3_SYM_PASSTHROUGH(const SDL_Rect*,GetWindowMouseRect,(SDL_Window *a),(a),return)
-SDL3_SYM_PASSTHROUGH(void,RenderWindowToLogical,(SDL_Renderer *a, int b, int c, float *d, float *e),(a,b,c,d,e),)
-SDL3_SYM_PASSTHROUGH(void,RenderLogicalToWindow,(SDL_Renderer *a, float b, float c, int *d, int *e),(a,b,c,d,e),)
+SDL3_SYM(void,RenderWindowToLogical,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
+SDL3_SYM(void,RenderLogicalToWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
 SDL3_SYM_PASSTHROUGH(SDL_bool,JoystickHasRumble,(SDL_Joystick *a),(a),return)
 SDL3_SYM_PASSTHROUGH(SDL_bool,JoystickHasRumbleTriggers,(SDL_Joystick *a),(a),return)
 SDL3_SYM_RENAMED(SDL_bool,GameControllerHasRumble,GamepadHasRumble,(SDL_GameController *a),(a),return)