From c684c38f2d3ec45d730778ce7beb7d2d4881b6c8 Mon Sep 17 00:00:00 2001
From: Daniel Gibson <metalcaedes@gmail.com>
Date: Sat, 5 Sep 2020 20:31:14 +0200
Subject: [PATCH] SDL2: Create window on display the cursor is currently on,
 fix #309

I hope this provides a good enough way to "select" the display to play on.
---
 neo/sys/glimp.cpp | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/neo/sys/glimp.cpp b/neo/sys/glimp.cpp
index 91b1e065..32fa4cf5 100644
--- a/neo/sys/glimp.cpp
+++ b/neo/sys/glimp.cpp
@@ -179,9 +179,30 @@ bool GLimp_Init(glimpParms_t parms) {
 		SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, parms.multiSamples);
 
 #if SDL_VERSION_ATLEAST(2, 0, 0)
+		int displayIndex = 0;
+
+		// try to put the window on the display the mousecursor currently is on
+		{
+			int x, y;
+			SDL_GetGlobalMouseState(&x, &y);
+
+			int numDisplays = SDL_GetNumVideoDisplays();
+			for (i=0; i<numDisplays; ++i) {
+				SDL_Rect rect;
+				if (SDL_GetDisplayBounds(i, &rect) == 0) {
+					if (   x >= rect.x && x < rect.x + rect.w
+						&& y >= rect.y && y < rect.y + rect.h )
+					{
+						displayIndex = i;
+						break;
+					}
+				}
+			}
+		}
+
 		window = SDL_CreateWindow(ENGINE_VERSION,
-									SDL_WINDOWPOS_UNDEFINED,
-									SDL_WINDOWPOS_UNDEFINED,
+									SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex),
+									SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex),
 									parms.width, parms.height, flags);
 
 		if (!window) {