SDL: Cancel any accumulated mouse wheel motion in the opposite direction when the wheel direction changes

From a3e8fd49e64144b4b951206a5bd8b9e6abefb2a5 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 8 Nov 2021 09:58:11 -0800
Subject: [PATCH] Cancel any accumulated mouse wheel motion in the opposite
 direction when the wheel direction changes

Fixes https://github.com/libsdl-org/SDL/issues/2912
---
 src/events/SDL_mouse.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/events/SDL_mouse.c b/src/events/SDL_mouse.c
index 155110ce27..fb8e362274 100644
--- a/src/events/SDL_mouse.c
+++ b/src/events/SDL_mouse.c
@@ -623,20 +623,38 @@ SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, S
         return 0;
     }
 
+    if (x > 0.0f) {
+        if (mouse->accumulated_wheel_x < 0.0f) {
+            mouse->accumulated_wheel_x = 0.0f;
+        }
+    } else if (x < 0.0f) {
+        if (mouse->accumulated_wheel_x > 0.0f) {
+            mouse->accumulated_wheel_x = 0.0f;
+        }
+    }
     mouse->accumulated_wheel_x += x;
-    if (mouse->accumulated_wheel_x > 0) {
+    if (mouse->accumulated_wheel_x > 0.0f) {
         integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
-    } else if (mouse->accumulated_wheel_x < 0) {
+    } else if (mouse->accumulated_wheel_x < 0.0f) {
         integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
     } else {
         integral_x = 0;
     }
     mouse->accumulated_wheel_x -= integral_x;
 
+    if (y > 0.0f) {
+        if (mouse->accumulated_wheel_y < 0.0f) {
+            mouse->accumulated_wheel_y = 0.0f;
+        }
+    } else if (y < 0.0f) {
+        if (mouse->accumulated_wheel_y > 0.0f) {
+            mouse->accumulated_wheel_y = 0.0f;
+        }
+    }
     mouse->accumulated_wheel_y += y;
-    if (mouse->accumulated_wheel_y > 0) {
+    if (mouse->accumulated_wheel_y > 0.0f) {
         integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
-    } else if (mouse->accumulated_wheel_y < 0) {
+    } else if (mouse->accumulated_wheel_y < 0.0f) {
         integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
     } else {
         integral_y = 0;