summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorChristian Haas <[email protected]>2024-05-05 20:22:58 +0200
committerGitHub <[email protected]>2024-05-05 20:22:58 +0200
commit3d9aafed3b5d183f35a1e8060d0985475101f6db (patch)
tree0243727dfd7fd084ee8d90ecc32fdefbe76f66bf /src
parent23385231c6c5ed6e9ace97e7305d3b7002c62171 (diff)
downloadraylib-3d9aafed3b5d183f35a1e8060d0985475101f6db.tar.gz
raylib-3d9aafed3b5d183f35a1e8060d0985475101f6db.zip
[rcore_desktop] Fix 3693 initial window geometry (#3950)
* Rework window placement and dimensions for multi-monitor setups; - fullscreen apps use primary monitor, exclusively - non-fullscreen apps come in two variants: a) pre-determined window size by user b) use-active-monitor dimensions by user specifying 0x0 Either way, the window shall be centred at the monitor where it was created This may have been the original intent, yet the primary monitor was used also for the second case, regardless where the window opened. * Clean up code, handle error, fix integer-overflow for centering windowed fullscreen
Diffstat (limited to 'src')
-rw-r--r--src/platforms/rcore_desktop.c91
1 files changed, 67 insertions, 24 deletions
diff --git a/src/platforms/rcore_desktop.c b/src/platforms/rcore_desktop.c
index de289ca0..f0877416 100644
--- a/src/platforms/rcore_desktop.c
+++ b/src/platforms/rcore_desktop.c
@@ -1218,6 +1218,19 @@ void PollInputEvents(void)
// Module Internal Functions Definition
//----------------------------------------------------------------------------------
+static void SetDimensionsFromMonitor(GLFWmonitor *monitor)
+{
+ const GLFWvidmode *mode = glfwGetVideoMode(monitor);
+
+ // Default display resolution to that of the current mode
+ CORE.Window.display.width = mode->width;
+ CORE.Window.display.height = mode->height;
+
+ // Set screen width/height to the display width/height if they are 0
+ if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
+ if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
+}
+
// Initialize platform: graphics, inputs and more
int InitPlatform(void)
{
@@ -1358,26 +1371,22 @@ int InitPlatform(void)
// REF: https://github.com/raysan5/raylib/issues/1554
glfwSetJoystickCallback(NULL);
- // Find monitor resolution
- GLFWmonitor *monitor = glfwGetPrimaryMonitor();
- if (!monitor)
+ GLFWmonitor *monitor = NULL;
+ if (CORE.Window.fullscreen)
{
- TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
- return -1;
- }
+ // According to glfwCreateWindow(), if the user does not have a choice, fullscreen applications
+ // should default to the primary monitor.
- const GLFWvidmode *mode = glfwGetVideoMode(monitor);
-
- CORE.Window.display.width = mode->width;
- CORE.Window.display.height = mode->height;
+ monitor = glfwGetPrimaryMonitor();
+ if (!monitor)
+ {
+ TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
+ return -1;
+ }
- // Set screen width/height to the display width/height if they are 0
- if (CORE.Window.screen.width == 0) CORE.Window.screen.width = CORE.Window.display.width;
- if (CORE.Window.screen.height == 0) CORE.Window.screen.height = CORE.Window.display.height;
+ SetDimensionsFromMonitor(monitor);
- if (CORE.Window.fullscreen)
- {
- // Remember center for switchinging from fullscreen to window
+ // Remember center for switching from fullscreen to window
if ((CORE.Window.screen.height == CORE.Window.display.height) && (CORE.Window.screen.width == CORE.Window.display.width))
{
// If screen width/height equal to the display, we can't calculate the window pos for toggling full-screened/windowed.
@@ -1396,7 +1405,7 @@ int InitPlatform(void)
// Obtain recommended CORE.Window.display.width/CORE.Window.display.height from a valid videomode for the monitor
int count = 0;
- const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
+ const GLFWvidmode *modes = glfwGetVideoModes(monitor, &count);
// Get closest video mode to desired CORE.Window.screen.width/CORE.Window.screen.height
for (int i = 0; i < count; i++)
@@ -1426,21 +1435,55 @@ int InitPlatform(void)
// HighDPI monitors are properly considered in a following similar function: SetupViewport()
SetupFramebuffer(CORE.Window.display.width, CORE.Window.display.height);
- platform.handle = glfwCreateWindow(CORE.Window.display.width, CORE.Window.display.height, (CORE.Window.title != 0)? CORE.Window.title : " ", glfwGetPrimaryMonitor(), NULL);
+ platform.handle = glfwCreateWindow(CORE.Window.display.width, CORE.Window.display.height, (CORE.Window.title != 0)? CORE.Window.title : " ", monitor, NULL);
// NOTE: Full-screen change, not working properly...
//glfwSetWindowMonitor(platform.handle, glfwGetPrimaryMonitor(), 0, 0, CORE.Window.screen.width, CORE.Window.screen.height, GLFW_DONT_CARE);
}
else
{
- // If we are windowed fullscreen, ensures that window does not minimize when focus is lost
- if ((CORE.Window.screen.height == CORE.Window.display.height) && (CORE.Window.screen.width == CORE.Window.display.width))
+ // No-fullscreen window creation
+ bool wantWindowedFullscreen = (CORE.Window.screen.height == 0) && (CORE.Window.screen.width == 0);
+
+ // If we are windowed fullscreen, ensures that window does not minimize when focus is lost.
+ // This hinting code will not work if the user already specified the correct monitor dimensions;
+ // at this point we don't know the monitor's dimensions. (Though, how did the user then?)
+ if (wantWindowedFullscreen)
{
glfwWindowHint(GLFW_AUTO_ICONIFY, 0);
}
- // No-fullscreen window creation
- platform.handle = glfwCreateWindow(CORE.Window.screen.width, CORE.Window.screen.height, (CORE.Window.title != 0)? CORE.Window.title : " ", NULL, NULL);
+ // Default to at least one pixel in size, as creation with a zero dimension is not allowed.
+ int creationWidth = CORE.Window.screen.width != 0 ? CORE.Window.screen.width : 1;
+ int creationHeight = CORE.Window.screen.height != 0 ? CORE.Window.screen.height : 1;
+
+ platform.handle = glfwCreateWindow(creationWidth, creationHeight, (CORE.Window.title != 0)? CORE.Window.title : " ", NULL, NULL);
+
+ // After the window was created, determine the monitor that the window manager assigned.
+ // Derive display sizes, and, if possible, window size in case it was zero at beginning.
+
+ int monitorCount = 0;
+ int monitorIndex = GetCurrentMonitor();
+ GLFWmonitor **monitors = glfwGetMonitors(&monitorCount);
+
+ if (monitorIndex < monitorCount)
+ {
+ monitor = monitors[monitorIndex];
+ SetDimensionsFromMonitor(monitor);
+
+ TRACELOG(LOG_INFO, "wantWindowed: %d, size: %dx%d", wantWindowedFullscreen, CORE.Window.screen.width, CORE.Window.screen.height);
+ if (wantWindowedFullscreen)
+ {
+ glfwSetWindowSize(platform.handle, CORE.Window.screen.width, CORE.Window.screen.height);
+ }
+ }
+ else
+ {
+ // The monitor for the window-manager-created window can not be determined, so it can not be centered.
+ glfwTerminate();
+ TRACELOG(LOG_WARNING, "GLFW: Failed to determine Monitor to center Window");
+ return -1;
+ }
if (platform.handle)
{
@@ -1524,8 +1567,8 @@ int InitPlatform(void)
int monitorHeight = 0;
glfwGetMonitorWorkarea(monitor, &monitorX, &monitorY, &monitorWidth, &monitorHeight);
- int posX = monitorX + (monitorWidth - CORE.Window.screen.width)/2;
- int posY = monitorY + (monitorHeight - CORE.Window.screen.height)/2;
+ int posX = monitorX + (monitorWidth - (int)CORE.Window.screen.width)/2;
+ int posY = monitorY + (monitorHeight - (int)CORE.Window.screen.height)/2;
if (posX < monitorX) posX = monitorX;
if (posY < monitorY) posY = monitorY;
SetWindowPosition(posX, posY);