Maelstrom: Added a MacOS cursor and draw it ourselves if necessary

From 919a4210dcdf572f34c9a21c429f1919ef8c2749 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 23 Apr 2026 00:40:15 -0700
Subject: [PATCH] Added a MacOS cursor and draw it ourselves if necessary

---
 Data/Images/cursor.png     | Bin 0 -> 260 bytes
 game/init.cpp              |  17 +++++++++++++++
 screenlib/SDL_FrameBuf.cpp |  41 +++++++++++++++++++++++++++++++++++++
 screenlib/SDL_FrameBuf.h   |   7 +++++++
 4 files changed, 65 insertions(+)
 create mode 100644 Data/Images/cursor.png

diff --git a/Data/Images/cursor.png b/Data/Images/cursor.png
new file mode 100644
index 0000000000000000000000000000000000000000..6624c4804d0b49ad15df310bc187dec605ef3177
GIT binary patch
literal 260
zcmV+f0sH=mP)<h;3K|Lk000e1NJLTq001BW001Be1^@s6b9#F80002XNkl<ZScUD<
zL9qiN5Jl0~RbN0$h!mtCVI0H)QNTF(5|A)XNF3ZOlBt@cCQD$$T{Wvd1qirmWV{-O
z0NA!|90I`I#~}bDjYI5!VTdDO2;v*i9dQQeiueWShWP%3l1M3WQX9k$$T?F=K@uSZ
zfYM~)86f8jumUB7fFw#O0HF&2Sl{!KQbH0jMu7hwfb}qYt@ZBixI4AhJK!z%|Cw3b
z4`%iNZp*S<0hyUgijq=G#7Ds8{Vb)+ANYZ2dhN5>Y&M(Cjy(ZTx(aOLu^Vy#0000<
KMNUMnLSTY?8)!-Z

literal 0
HcmV?d00001

diff --git a/game/init.cpp b/game/init.cpp
index 074a83d2..2a3602d0 100644
--- a/game/init.cpp
+++ b/game/init.cpp
@@ -834,6 +834,17 @@ static bool LoadIcon(SDL_Surface **icon)
 	return true;
 }
 
+static bool LoadCursor(SDL_Surface **cursor)
+{
+	SDL_Surface *surface = SDL_LoadSurface_IO(OpenRead("Images/cursor.png"), true);
+	if (!surface) {
+		error("Fatal: Couldn't load cursor: %s\n", SDL_GetError());
+		return false;
+	}
+	*cursor = surface;
+	return true;
+}
+
 static void ShowLoadingPanel(int stage)
 {
 	gInitializing = true;
@@ -860,6 +871,7 @@ bool StartInitialization(int window_width, int window_height, Uint32 window_flag
 {
 	int w, h;
 	SDL_Surface *icon = nullptr;
+	SDL_Surface *cursor = nullptr;
 
 	gInitializing = true;
 
@@ -905,6 +917,11 @@ bool StartInitialization(int window_width, int window_height, Uint32 window_flag
 	SDL_ShowWindow(screen->GetWindow());
 	SDL_DestroySurface(icon);
 
+	if (LoadCursor(&cursor)) {
+		screen->SetCursor(cursor, 0, 0);
+		SDL_DestroySurface(cursor);
+	}
+
 	/* Load the Font Server and fonts */
 	fontserv = new FontServ(screen, "Maelstrom Fonts");
 	if (fontserv->Error()) {
diff --git a/screenlib/SDL_FrameBuf.cpp b/screenlib/SDL_FrameBuf.cpp
index bf1336c4..15f6db23 100644
--- a/screenlib/SDL_FrameBuf.cpp
+++ b/screenlib/SDL_FrameBuf.cpp
@@ -92,6 +92,9 @@ FrameBuf::~FrameBuf()
 	for (unsigned int i = 0; i < m_gamepads.length(); ++i) {
 		SDL_CloseGamepad(m_gamepads[i]);
 	}
+	if (m_cursor) {
+		SDL_DestroyCursor(m_cursor);
+	}
 	if (m_renderer) {
 		SDL_DestroyRenderer(m_renderer);
 	}
@@ -309,6 +312,32 @@ FrameBuf::ConvertTouchCoordinates(const SDL_TouchFingerEvent &finger, int *x, in
 	return true;
 }
 
+void
+FrameBuf::SetCursor(SDL_Surface *image, int hotX, int hotY)
+{
+	if (m_cursor) {
+		SDL_DestroyCursor(m_cursor);
+	}
+	if (m_cursorTexture) {
+		SDL_DestroyTexture(m_cursorTexture);
+	}
+
+#if defined(SDL_PLATFORM_APPLE) && !defined(SDL_PLATFORM_MACOS)
+	// We need to draw our own cursor
+#else
+	m_cursor = SDL_CreateColorCursor(image, hotX, hotY);
+#endif
+	if (m_cursor) {
+		SDL_SetCursor(m_cursor);
+	} else {
+		m_cursorTexture = SDL_CreateTextureFromSurface(m_renderer, image);
+		m_cursorWidth = image->w;
+		m_cursorHeight = image->h;
+		m_cursorOffsetX = -hotX;
+		m_cursorOffsetY = -hotY;
+	}
+}
+
 void
 FrameBuf::GetCursorPosition(int *x, int *y)
 {
@@ -460,6 +489,18 @@ FrameBuf::Update(void)
 		return;
 	}
 
+	if (m_cursorTexture && SDL_CursorVisible()) {
+		int x, y;
+		GetCursorPosition(&x, &y);
+
+		SDL_FRect dstrect;
+		dstrect.x = (float)x + m_cursorOffsetX;
+		dstrect.y = (float)y + m_cursorOffsetY;
+		dstrect.w = (float)m_cursorWidth;
+		dstrect.h = (float)m_cursorHeight;
+		SDL_RenderTexture(m_renderer, m_cursorTexture, NULL, &dstrect);
+	}
+
 	SDL_RenderPresent(m_renderer);
 }
 
diff --git a/screenlib/SDL_FrameBuf.h b/screenlib/SDL_FrameBuf.h
index fa114604..3bfff7fe 100644
--- a/screenlib/SDL_FrameBuf.h
+++ b/screenlib/SDL_FrameBuf.h
@@ -189,6 +189,7 @@ class FrameBuf : public ErrorBase {
 	int ScreenDump(const char *prefix, int x, int y, int w, int h);
 
 	/* Cursor handling routines */
+	void SetCursor(SDL_Surface *image, int hotX, int hotY);
 	void ShowCursor(void) {
 		SDL_ShowCursor();
 	}
@@ -205,6 +206,12 @@ class FrameBuf : public ErrorBase {
 	SDL_Window *m_window = nullptr;
 	SDL_Renderer *m_renderer = nullptr;
 	SDL_Texture *m_fadeTexture = nullptr;
+	SDL_Cursor *m_cursor = nullptr;
+	SDL_Texture *m_cursorTexture = nullptr;
+	int m_cursorWidth = 0;
+	int m_cursorHeight = 0;
+	int m_cursorOffsetX = 0;
+	int m_cursorOffsetY = 0;
 	int m_fadeStep = 0;
 	bool m_faded = false;
 	SDL_FRect m_clip;