SDL: Convert mouse wheel coordinates to renderer view (f13d6)

From f13d6d4b0f6948f72152ac0c1185468f1602c8ef Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 20 Feb 2024 15:44:26 -0800
Subject: [PATCH] Convert mouse wheel coordinates to renderer view

Fixes https://github.com/libsdl-org/SDL/issues/9097

(cherry picked from commit 903d888cc31d283ccaaaa7e5b5a324c50a103e87)
---
 src/render/SDL_render.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 6bf57b18707b..f7fcc52c9953 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -799,6 +799,20 @@ static int SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
                 event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
             }
         }
+    } else if (event->type == SDL_MOUSEWHEEL) {
+        SDL_Window *window = SDL_GetWindowFromID(event->wheel.windowID);
+        if (window == renderer->window) {
+            int logical_w, logical_h;
+            SDL_DRect viewport;
+            SDL_FPoint scale;
+            GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
+            if (logical_w) {
+                event->wheel.mouseX -= (int)(viewport.x * renderer->dpi_scale.x);
+                event->wheel.mouseY -= (int)(viewport.y * renderer->dpi_scale.y);
+                event->wheel.mouseX = (int)(event->wheel.mouseX / (scale.x * renderer->dpi_scale.x));
+                event->wheel.mouseY = (int)(event->wheel.mouseY / (scale.y * renderer->dpi_scale.y));
+            }
+        }
     } else if (event->type == SDL_FINGERDOWN ||
                event->type == SDL_FINGERUP ||
                event->type == SDL_FINGERMOTION) {