Clean up code, handle error, fix integer-overflow for centering windowed fullscreen

This commit is contained in:
Christian Haas 2024-05-01 08:14:34 +02:00
parent 4f44c806d4
commit a41f07949f

View File

@ -1371,12 +1371,13 @@ int InitPlatform(void)
// REF: https://github.com/raysan5/raylib/issues/1554 // REF: https://github.com/raysan5/raylib/issues/1554
glfwSetJoystickCallback(NULL); glfwSetJoystickCallback(NULL);
GLFWmonitor *monitor = NULL;
if (CORE.Window.fullscreen) if (CORE.Window.fullscreen)
{ {
// According to glfwCreateWindow(), if the user does not have a choice, fullscreen applications // According to glfwCreateWindow(), if the user does not have a choice, fullscreen applications
// should default to the primary monitor. // should default to the primary monitor.
GLFWmonitor *monitor = glfwGetPrimaryMonitor(); monitor = glfwGetPrimaryMonitor();
if (!monitor) if (!monitor)
{ {
TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor"); TRACELOG(LOG_WARNING, "GLFW: Failed to get primary monitor");
@ -1442,15 +1443,17 @@ int InitPlatform(void)
else else
{ {
// No-fullscreen window creation // 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 // If we are windowed fullscreen, ensures that window does not minimize when focus is lost.
// TODO: 3693 This code will not work if the user already specified the correct monitor dimensions; // 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?) // at this point we don't know the monitor's dimensions. (Though, how did the user then?)
if (((CORE.Window.screen.height == 0) && (CORE.Window.screen.width == 0))) if (wantWindowedFullscreen)
{ {
glfwWindowHint(GLFW_AUTO_ICONIFY, 0); glfwWindowHint(GLFW_AUTO_ICONIFY, 0);
} }
// 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 creationWidth = CORE.Window.screen.width != 0 ? CORE.Window.screen.width : 1;
int creationHeight = CORE.Window.screen.height != 0 ? CORE.Window.screen.height : 1; int creationHeight = CORE.Window.screen.height != 0 ? CORE.Window.screen.height : 1;
@ -1465,22 +1468,21 @@ int InitPlatform(void)
if (monitorIndex < monitorCount) if (monitorIndex < monitorCount)
{ {
// If window screen dimensions are zero (from user), then prepare to resize to monitor size. monitor = monitors[monitorIndex];
bool resizeWindow = CORE.Window.screen.width == 0 && CORE.Window.screen.height == 0; SetDimensionsFromMonitor(monitor);
SetDimensionsFromMonitor(monitors[monitorIndex]);
if (resizeWindow) 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); glfwSetWindowSize(platform.handle, CORE.Window.screen.width, CORE.Window.screen.height);
} }
} }
else else
{ {
// TODO: 3693 If the monitor is wrong/not known, what now? Core.Window.display.* will not be set to the current monitor then. // The monitor for the window-manager-created window can not be determined, so it can not be centered.
int width = 0; glfwTerminate();
int height = 0; TRACELOG(LOG_WARNING, "GLFW: Failed to determine Monitor to center Window");
glfwGetWindowSize(platform.handle, &width, &height); return -1;
CORE.Window.screen.width = width;
CORE.Window.screen.height = height;
} }
if (platform.handle) if (platform.handle)
@ -1565,8 +1567,8 @@ int InitPlatform(void)
int monitorHeight = 0; int monitorHeight = 0;
glfwGetMonitorWorkarea(monitor, &monitorX, &monitorY, &monitorWidth, &monitorHeight); glfwGetMonitorWorkarea(monitor, &monitorX, &monitorY, &monitorWidth, &monitorHeight);
int posX = monitorX + (monitorWidth - CORE.Window.screen.width)/2; int posX = monitorX + (monitorWidth - (int)CORE.Window.screen.width)/2;
int posY = monitorY + (monitorHeight - CORE.Window.screen.height)/2; int posY = monitorY + (monitorHeight - (int)CORE.Window.screen.height)/2;
if (posX < monitorX) posX = monitorX; if (posX < monitorX) posX = monitorX;
if (posY < monitorY) posY = monitorY; if (posY < monitorY) posY = monitorY;
SetWindowPosition(posX, posY); SetWindowPosition(posX, posY);