sdl12-compat: Mouse position should be consistant in relative mode

From cf47f88e169cc9680e2dd1a89f775e2da129cded Mon Sep 17 00:00:00 2001
From: David Gow <[EMAIL REDACTED]>
Date: Mon, 28 Jun 2021 14:02:19 +0800
Subject: [PATCH] Mouse position should be consistant in relative mode

When the mouse is in Relative Mode (MouseInputIsRelative is true), we
"fake" the mouse position by integrating the relative coordinates
(adding them to the MousePosition variable, which accumulates the
position over time).

However, only SDL_GetMouseState() would actually use these
coordinates, events still used the raw position from the system
(possibly scaled, as both the SDL_RenderSetLogicalSize() and OpenGL
based scaling only affects the position, not the relative values). This
creates a mismatch: mouse coordinates received from events don't match
those received from SDL_GetMouseState().

Instead, we should update the mouse coordinates of the SDL 1.2 events
produced to match what we're keeing in the MousePosition variable. We
also do this for mouse button events, as otherwise clicks would hit a
different target from the visible cursor if a software cursor is
rendered.

This is still not perfect: the real and fake mouse positions are
different, so toggling relative mode can result in a "jump" in the mouse
coordinates. Relative mouse movements are also not scaled, so the mouse
movement speed can be different in relative mode.

Tested this against Ren'py SDL 1.2's software mouse cursor option, and
verified no regressions in Wolf4SDL and Multiwinia.
---
 src/SDL12_compat.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index f3e9499..67fb133 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -3138,6 +3138,7 @@ EventFilter20to12(void *data, SDL_Event *event20)
                     } else if (MousePosition.axis >= VideoSurface12->dim) { \
                         MousePosition.axis = (VideoSurface12->dim - 1); \
                     } \
+                    event12.motion.axis = MousePosition.axis; \
                 }
                 ADJUST_RELATIVE(x, xrel, w);
                 ADJUST_RELATIVE(y, yrel, h);
@@ -3156,9 +3157,15 @@ EventFilter20to12(void *data, SDL_Event *event20)
                 event12.button.button += 2; /* SDL_BUTTON_X1/2 */
             }
             event12.button.state = event20->button.state;
-            AdjustOpenGLLogicalScalingPoint(&event20->button.x, &event20->button.y);
-            event12.button.x = (Uint16) event20->button.x;
-            event12.button.y = (Uint16) event20->button.y;
+            if (MouseInputIsRelative) {
+                /* If we're using relative mouse input, we need to use our "fake" position. */
+                event12.button.x = MousePosition.x;
+                event12.button.y = MousePosition.y;
+            } else {
+                AdjustOpenGLLogicalScalingPoint(&event20->button.x, &event20->button.y);
+                event12.button.x = (Uint16) event20->button.x;
+                event12.button.y = (Uint16) event20->button.y;
+            }
             break;
 
         case SDL_MOUSEBUTTONUP:
@@ -3169,9 +3176,15 @@ EventFilter20to12(void *data, SDL_Event *event20)
                 event12.button.button += 2; /* SDL_BUTTON_X1/2 */
             }
             event12.button.state = event20->button.state;
-            AdjustOpenGLLogicalScalingPoint(&event20->button.x, &event20->button.y);
-            event12.button.x = (Uint16) event20->button.x;
-            event12.button.y = (Uint16) event20->button.y;
+            if (MouseInputIsRelative) {
+                /* If we're using relative mouse input, we need to use our "fake" position. */
+                event12.button.x = MousePosition.x;
+                event12.button.y = MousePosition.y;
+            } else {
+                AdjustOpenGLLogicalScalingPoint(&event20->button.x, &event20->button.y);
+                event12.button.x = (Uint16) event20->button.x;
+                event12.button.y = (Uint16) event20->button.y;
+            }
             break;
 
         case SDL_MOUSEWHEEL: