Maelstrom: Compensate for the screen offset in handling mouse coordinates

https://github.com/libsdl-org/Maelstrom/commit/16d48e4789838ffe8ff25444ecf23277df5856e9

From 16d48e4789838ffe8ff25444ecf23277df5856e9 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Sun, 6 Nov 2011 22:31:10 -0500
Subject: [PATCH] Compensate for the screen offset in handling mouse
 coordinates

---
 screenlib/SDL_FrameBuf.cpp |  7 ++++---
 screenlib/SDL_FrameBuf.h   | 25 +++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index fad485f6..489ca546 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -73,9 +73,10 @@ FrameBuf:: Init(int width, int height, Uint32 window_flags, Uint32 render_flags,
 	SDL_RenderSetViewport(renderer, &rect);
 
 	/* Set the blit clipping rectangle */
-	rect.x = 0;
-	rect.y = 0;
-	clip = rect;
+	clip.x = 0;
+	clip.y = 0;
+	clip.w = width;
+	clip.h = height;
 
 	/* Copy the image colormap */
 	if ( colors ) {
diff --git a/screenlib/SDL_FrameBuf.h b/screenlib/SDL_FrameBuf.h
index 98dfe71a..793f4484 100644
--- a/screenlib/SDL_FrameBuf.h
+++ b/screenlib/SDL_FrameBuf.h
@@ -68,10 +68,31 @@ class FrameBuf : public ErrorBase {
 
 	/* Event Routines */
 	int PollEvent(SDL_Event *event) {
-		return(SDL_PollEvent(event));
+		int result = SDL_PollEvent(event);
+		if (result > 0) {
+			ProcessEvent(event);
+		}
+		return result;
 	}
 	int WaitEvent(SDL_Event *event) {
-		return(SDL_WaitEvent(event));
+		int result = SDL_WaitEvent(event);
+		if (result > 0) {
+			ProcessEvent(event);
+		}
+		return result;
+	}
+	void ProcessEvent(SDL_Event *event) {
+		switch (event->type) {
+			case SDL_MOUSEMOTION:
+				event->motion.x -= rect.x;
+				event->motion.y -= rect.y;
+				break;
+			case SDL_MOUSEBUTTONDOWN:
+			case SDL_MOUSEBUTTONUP:
+				event->button.x -= rect.x;
+				event->button.y -= rect.y;
+				break;
+		}
 	}
 	void ToggleFullScreen(void) {
 		if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {