GetCurrentMonitor() - use closest monitor (#3472)

This commit is contained in:
Alexandre Almeida 2023-10-27 12:01:05 -03:00 committed by GitHub
parent 3afd0a55b9
commit 2db7c727b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -751,15 +751,19 @@ int GetCurrentMonitor(void)
} }
else else
{ {
int x = 0; int closestDist = 0x7FFFFFFF;
int y = 0;
glfwGetWindowPos(platform.handle, &x, &y); // Window center position
x += (int)CORE.Window.screen.width / 2; int wcx = 0;
y += (int)CORE.Window.screen.height / 2; 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++) for (int i = 0; i < monitorCount; i++)
{ {
// Monitor top-left position
int mx = 0; int mx = 0;
int my = 0; int my = 0;
@ -769,17 +773,46 @@ int GetCurrentMonitor(void)
if (mode) if (mode)
{ {
const int width = mode->width; const int right = mx + mode->width - 1;
const int height = mode->height; const int bottom = my + mode->height - 1;
if ((x >= mx) && if ((wcx >= mx) &&
(x < (mx + width)) && (wcx <= right) &&
(y >= my) && (wcy >= my) &&
(y < (my + height))) (wcy <= bottom))
{ {
index = i; index = i;
break; 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"); else TRACELOG(LOG_WARNING, "GLFW: Failed to find video mode for selected monitor");
} }