From accc47e37791976235c100d23608e4a8ac4c055d Mon Sep 17 00:00:00 2001
From: FunkyFr3sh <cc.red.alert.1@googlemail.com>
Date: Thu, 14 Sep 2023 16:47:28 +0200
Subject: [PATCH] fix cursor position issues with downscaling

---
 inc/dd.h           |  5 +++++
 src/dd.c           |  9 +++++++--
 src/mouse.c        |  8 ++++----
 src/winapi_hooks.c | 20 ++++++++++----------
 src/wndproc.c      | 12 ++++++------
 5 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/inc/dd.h b/inc/dd.h
index 846904a..e4f312c 100644
--- a/inc/dd.h
+++ b/inc/dd.h
@@ -109,6 +109,11 @@ typedef struct CNCDDRAW
 
     struct
     {
+        float scale_x;
+        float scale_y;
+        float unscale_x;
+        float unscale_y;
+
         int y_adjust;
         int x_adjust;
         RECT rc;
diff --git a/src/dd.c b/src/dd.c
index d9d55d0..e49fc00 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -817,11 +817,16 @@ HRESULT dd_SetDisplayMode(DWORD dwWidth, DWORD dwHeight, DWORD dwBPP, DWORD dwFl
     g_ddraw->render.unscale_w = ((float)g_ddraw->width / g_ddraw->render.viewport.width);
     g_ddraw->render.unscale_h = ((float)g_ddraw->height / g_ddraw->render.viewport.height);
 
+    g_ddraw->mouse.scale_x = ((float)(g_ddraw->render.viewport.width - 1) / (g_ddraw->width - 1));
+    g_ddraw->mouse.scale_y = ((float)(g_ddraw->render.viewport.height - 1) / (g_ddraw->height - 1));
+    g_ddraw->mouse.unscale_x = ((float)(g_ddraw->width - 1) / (g_ddraw->render.viewport.width - 1));
+    g_ddraw->mouse.unscale_y = ((float)(g_ddraw->height - 1) / (g_ddraw->render.viewport.height - 1));
+
     g_ddraw->mouse.x_adjust = g_ddraw->render.viewport.x;
     g_ddraw->mouse.y_adjust = g_ddraw->render.viewport.y;
 
-    g_ddraw->mouse.rc.left = g_ddraw->mouse.x_adjust;;
-    g_ddraw->mouse.rc.top = g_ddraw->mouse.y_adjust;;
+    g_ddraw->mouse.rc.left = g_ddraw->mouse.x_adjust;
+    g_ddraw->mouse.rc.top = g_ddraw->mouse.y_adjust;
     g_ddraw->mouse.rc.right = g_ddraw->width + g_ddraw->mouse.x_adjust;
     g_ddraw->mouse.rc.bottom = g_ddraw->height + g_ddraw->mouse.y_adjust;
 
diff --git a/src/mouse.c b/src/mouse.c
index 4cbed30..e709784 100644
--- a/src/mouse.c
+++ b/src/mouse.c
@@ -41,8 +41,8 @@ void mouse_lock()
         int cur_y = InterlockedExchangeAdd((LONG*)&g_ddraw->cursor.y, 0);
 
         real_SetCursorPos(
-            g_ddraw->adjmouse ? (int)(rc.left + (cur_x * g_ddraw->render.scale_w)) : rc.left + cur_x,
-            g_ddraw->adjmouse ? (int)(rc.top + (cur_y * g_ddraw->render.scale_h)) : rc.top + cur_y);
+            g_ddraw->adjmouse ? (int)(rc.left + (cur_x * g_ddraw->mouse.scale_x)) : rc.left + cur_x,
+            g_ddraw->adjmouse ? (int)(rc.top + (cur_y * g_ddraw->mouse.scale_y)) : rc.top + cur_y);
 
         CopyRect(&rc, &g_ddraw->mouse.rc);
         real_MapWindowPoints(g_ddraw->hwnd, HWND_DESKTOP, (LPPOINT)&rc, 2);
@@ -73,8 +73,8 @@ void mouse_unlock()
         int cur_y = InterlockedExchangeAdd((LONG*)&g_ddraw->cursor.y, 0);
 
         real_SetCursorPos(
-            (int)(rc.left + (cur_x * g_ddraw->render.scale_w)),
-            (int)(rc.top + (cur_y * g_ddraw->render.scale_h)));
+            (int)(rc.left + (cur_x * g_ddraw->mouse.scale_x)),
+            (int)(rc.top + (cur_y * g_ddraw->mouse.scale_y)));
 
         real_SetCursor(LoadCursor(NULL, IDC_ARROW));
 
diff --git a/src/winapi_hooks.c b/src/winapi_hooks.c
index d95ef75..aebecd4 100644
--- a/src/winapi_hooks.c
+++ b/src/winapi_hooks.c
@@ -74,8 +74,8 @@ BOOL WINAPI fake_GetCursorPos(LPPOINT lpPoint)
 
         if (g_ddraw->adjmouse)
         {
-            x = min((DWORD)(roundf(pt.x * g_ddraw->render.unscale_w)), g_ddraw->width - 1);
-            y = min((DWORD)(roundf(pt.y * g_ddraw->render.unscale_h)), g_ddraw->height - 1);
+            x = min((DWORD)(roundf(pt.x * g_ddraw->mouse.unscale_x)), g_ddraw->width - 1);
+            y = min((DWORD)(roundf(pt.y * g_ddraw->mouse.unscale_y)), g_ddraw->height - 1);
         }
         else
         {
@@ -299,8 +299,8 @@ BOOL WINAPI fake_SetCursorPos(int X, int Y)
 
     if (g_ddraw->adjmouse)
     {
-        pt.x = (LONG)(roundf(pt.x * g_ddraw->render.scale_w));
-        pt.y = (LONG)(roundf(pt.y * g_ddraw->render.scale_h));
+        pt.x = (LONG)(roundf(pt.x * g_ddraw->mouse.scale_x));
+        pt.y = (LONG)(roundf(pt.y * g_ddraw->mouse.scale_y));
     }
 
     pt.x += g_ddraw->mouse.x_adjust;
@@ -428,8 +428,8 @@ LRESULT WINAPI fake_SendMessageA(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lPar
 
         if (g_ddraw->adjmouse)
         {
-            x = (int)(roundf(x * g_ddraw->render.scale_w));
-            y = (int)(roundf(y * g_ddraw->render.scale_h));
+            x = (int)(roundf(x * g_ddraw->mouse.scale_x));
+            y = (int)(roundf(y * g_ddraw->mouse.scale_y));
         }
 
         lParam = MAKELPARAM(x + g_ddraw->mouse.x_adjust, y + g_ddraw->mouse.y_adjust);
@@ -620,8 +620,8 @@ BOOL WINAPI fake_PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT w
                 }
                 else
                 {
-                    x = (DWORD)((x - g_ddraw->render.viewport.x) * g_ddraw->render.unscale_w);
-                    y = (DWORD)((y - g_ddraw->render.viewport.y) * g_ddraw->render.unscale_h);
+                    x = (DWORD)((x - g_ddraw->render.viewport.x) * g_ddraw->mouse.unscale_x);
+                    y = (DWORD)((y - g_ddraw->render.viewport.y) * g_ddraw->mouse.unscale_y);
                 }
 
                 InterlockedExchange((LONG*)&g_ddraw->cursor.x, x);
@@ -667,8 +667,8 @@ BOOL WINAPI fake_PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT w
                 }
                 else
                 {
-                    x = (DWORD)(roundf(x * g_ddraw->render.unscale_w));
-                    y = (DWORD)(roundf(y * g_ddraw->render.unscale_h));
+                    x = (DWORD)(roundf(x * g_ddraw->mouse.unscale_x));
+                    y = (DWORD)(roundf(y * g_ddraw->mouse.unscale_y));
                 }
             }
 
diff --git a/src/wndproc.c b/src/wndproc.c
index fa753cf..756e815 100644
--- a/src/wndproc.c
+++ b/src/wndproc.c
@@ -758,8 +758,8 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
             }
             else
             {
-                x = (DWORD)((x - g_ddraw->render.viewport.x) * g_ddraw->render.unscale_w);
-                y = (DWORD)((y - g_ddraw->render.viewport.y) * g_ddraw->render.unscale_h);
+                x = (DWORD)((x - g_ddraw->render.viewport.x) * g_ddraw->mouse.unscale_x);
+                y = (DWORD)((y - g_ddraw->render.viewport.y) * g_ddraw->mouse.unscale_y);
             }
 
             InterlockedExchange((LONG*)&g_ddraw->cursor.x, x);
@@ -804,8 +804,8 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
             }
             else
             {
-                x = (DWORD)(roundf(x * g_ddraw->render.unscale_w));
-                y = (DWORD)(roundf(y * g_ddraw->render.unscale_h));
+                x = (DWORD)(roundf(x * g_ddraw->mouse.unscale_x));
+                y = (DWORD)(roundf(y * g_ddraw->mouse.unscale_y));
             }
         }
 
@@ -833,8 +833,8 @@ LRESULT CALLBACK fake_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam
         {
             if (!g_ddraw->devmode && !g_mouse_locked)
             {
-                int x = (DWORD)((GET_X_LPARAM(lParam) - g_ddraw->render.viewport.x) * g_ddraw->render.unscale_w);
-                int y = (DWORD)((GET_Y_LPARAM(lParam) - g_ddraw->render.viewport.y) * g_ddraw->render.unscale_h);
+                int x = (DWORD)((GET_X_LPARAM(lParam) - g_ddraw->render.viewport.x) * g_ddraw->mouse.unscale_x);
+                int y = (DWORD)((GET_Y_LPARAM(lParam) - g_ddraw->render.viewport.y) * g_ddraw->mouse.unscale_y);
 
                 InterlockedExchange((LONG*)&g_ddraw->cursor.x, x);
                 InterlockedExchange((LONG*)&g_ddraw->cursor.y, y);