summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlexandre Almeida <[email protected]>2023-10-27 12:01:05 -0300
committerGitHub <[email protected]>2023-10-27 17:01:05 +0200
commit2db7c727b653fc526b7da07fd337de02b7156e37 (patch)
tree7ab206497c8573fb6b75f12a29d6b9750ed15999
parent3afd0a55b9f243d1a3f0fb6be026deb085828c9d (diff)
downloadraylib-2db7c727b653fc526b7da07fd337de02b7156e37.tar.gz
raylib-2db7c727b653fc526b7da07fd337de02b7156e37.zip
GetCurrentMonitor() - use closest monitor (#3472)
-rw-r--r--src/platforms/rcore_desktop.c55
1 files changed, 44 insertions, 11 deletions
diff --git a/src/platforms/rcore_desktop.c b/src/platforms/rcore_desktop.c
index 1114181a..f60a0e56 100644
--- a/src/platforms/rcore_desktop.c
+++ b/src/platforms/rcore_desktop.c
@@ -751,15 +751,19 @@ int GetCurrentMonitor(void)
}
else
{
- int x = 0;
- int y = 0;
+ int closestDist = 0x7FFFFFFF;
- glfwGetWindowPos(platform.handle, &x, &y);
- x += (int)CORE.Window.screen.width / 2;
- y += (int)CORE.Window.screen.height / 2;
+ // Window center position
+ int wcx = 0;
+ int wcy = 0;
+
+ glfwGetWindowPos(platform.handle, &wcx, &wcy);
+ wcx += (int)CORE.Window.screen.width / 2;
+ wcy += (int)CORE.Window.screen.height / 2;
for (int i = 0; i < monitorCount; i++)
{
+ // Monitor top-left position
int mx = 0;
int my = 0;
@@ -769,17 +773,46 @@ int GetCurrentMonitor(void)
if (mode)
{
- const int width = mode->width;
- const int height = mode->height;
+ const int right = mx + mode->width - 1;
+ const int bottom = my + mode->height - 1;
- if ((x >= mx) &&
- (x < (mx + width)) &&
- (y >= my) &&
- (y < (my + height)))
+ if ((wcx >= mx) &&
+ (wcx <= right) &&
+ (wcy >= my) &&
+ (wcy <= bottom))
{
index = i;
break;
}
+
+ int xclosest = wcx;
+ if (wcx < mx)
+ {
+ xclosest = mx;
+ }
+ else if (wcx > right)
+ {
+ xclosest = right;
+ }
+
+ int yclosest = wcy;
+ if (wcy < my)
+ {
+ yclosest = my;
+ }
+ else if (wcy > bottom)
+ {
+ yclosest = bottom;
+ }
+
+ int dx = wcx - xclosest;
+ int dy = wcy - yclosest;
+ int dist = (dx * dx) + (dy * dy);
+ if (dist < closestDist)
+ {
+ index = i;
+ closestDist = dist;
+ }
}
else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
}