From 1a956208e248971ca4acb75fbec4b2b485ee614e Mon Sep 17 00:00:00 2001 From: MasterZean Date: Wed, 11 Sep 2019 16:39:40 +0300 Subject: [PATCH] fixes for switching full-screen and v-sync --- src/core.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core.c b/src/core.c index fad2c485f..553a6a9e2 100644 --- a/src/core.c +++ b/src/core.c @@ -311,6 +311,7 @@ static unsigned int displayWidth, displayHeight;// Display width and height (mon static int screenWidth, screenHeight; // Screen width and height (used render area) static int renderWidth, renderHeight; // Framebuffer width and height (render area, including black bars if required) static int currentWidth, currentHeight; // Current render width and height, it could change on BeginTextureMode() +static int windowLeftPos, windowTopPos; static int renderOffsetX = 0; // Offset X from render area (must be divided by 2) static int renderOffsetY = 0; // Offset Y from render area (must be divided by 2) static bool fullscreen = false; // Fullscreen mode (useful only for PLATFORM_DESKTOP) @@ -853,8 +854,21 @@ void ToggleFullscreen(void) fullscreen = !fullscreen; // Toggle fullscreen flag // NOTE: glfwSetWindowMonitor() doesn't work properly (bugs) - if (fullscreen) glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE); - else glfwSetWindowMonitor(window, NULL, 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE); + if (fullscreen) { + glfwGetWindowPos(window, &windowLeftPos, &windowTopPos); + + GLFWmonitor *monitor = glfwGetPrimaryMonitor(); + if (!monitor) + { + TraceLog(LOG_WARNING, "Failed to get monitor"); + glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, GLFW_DONT_CARE); + return; + } + + const GLFWvidmode *mode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(window, glfwGetPrimaryMonitor(), 0, 0, screenWidth, screenHeight, mode->refreshRate); + } + else glfwSetWindowMonitor(window, NULL, windowLeftPos, windowTopPos, screenWidth, screenHeight, GLFW_DONT_CARE); #endif #if defined(PLATFORM_ANDROID) || defined(PLATFORM_RPI) @@ -2672,6 +2686,13 @@ static bool InitGraphicsDevice(int width, int height) if (fullscreen) { + // remember center for switchinging from fullscreen to window + windowLeftPos = displayWidth/2 - screenWidth/2; + windowTopPos = displayHeight/2 - screenHeight/2; + + if (windowLeftPos < 0) windowLeftPos = 0; + if (windowTopPos < 0) windowTopPos = 0; + // Obtain recommended displayWidth/displayHeight from a valid videomode for the monitor int count = 0; const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count); @@ -3768,6 +3789,7 @@ static void PollInputEvents(void) static void SwapBuffers(void) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + glfwSwapInterval((configFlags & FLAG_VSYNC_HINT) != 0 ? 1 : 0); glfwSwapBuffers(window); #endif