added SDL event filter to track mouse position from mouse motion events
[rocksndiamonds.git] / src / libgame / sdl.c
index 4bd1987331b2a0d342f85c35ed666c2b5468fa33..45f7f2ce69a823b9b4f635bb6a8c334765f87736 100644 (file)
@@ -2362,6 +2362,27 @@ void SDLWaitEvent(Event *event)
   SDL_WaitEvent(event);
 }
 
   SDL_WaitEvent(event);
 }
 
+void SDLCorrectMouseEventXY(int *x, int *y)
+{
+  if (sdl_renderer == NULL)
+    return;
+
+  // this corrects the raw mouse position for logical screen size within event
+  // filters (correction done later by SDL library when handling mouse events)
+
+  SDL_Rect viewport;
+  float scale_x, scale_y;
+
+  SDL_RenderGetViewport(sdl_renderer, &viewport);
+  SDL_RenderGetScale(sdl_renderer, &scale_x, &scale_y);
+
+  *x = (int)(*x / scale_x);
+  *y = (int)(*y / scale_y);
+
+  *x -= viewport.x;
+  *y -= viewport.y;
+}
+
 
 // ============================================================================
 // joystick functions
 
 // ============================================================================
 // joystick functions