From 8eecbbe4aa644bfcce5778b7f077eb01a0665c6f Mon Sep 17 00:00:00 2001 From: MasterZean Date: Sun, 1 Dec 2019 14:46:09 +0200 Subject: [PATCH] fixes for switching full-screen and v-sync (#963) * fixes for switching full-screen and v-sync * requireVsync flag --- src/core.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/core.c b/src/core.c index 74d5465a3..33b67b551 100644 --- a/src/core.c +++ b/src/core.c @@ -313,6 +313,8 @@ 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 requireVsync = 0; 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) @@ -855,10 +857,24 @@ void ToggleFullscreen(void) { #if defined(PLATFORM_DESKTOP) fullscreen = !fullscreen; // Toggle fullscreen flag + requireVsync = true; // 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) @@ -1759,6 +1775,7 @@ Color Fade(Color color, float alpha) void SetConfigFlags(unsigned int flags) { configFlags = flags; + requireVsync = true; if (configFlags & FLAG_FULLSCREEN_MODE) fullscreen = true; if (configFlags & FLAG_WINDOW_ALWAYS_RUN) alwaysRun = true; @@ -2697,6 +2714,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); @@ -3794,6 +3818,11 @@ static void PollInputEvents(void) static void SwapBuffers(void) { #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) + if (requireVsync) { + glfwMakeContextCurrent(window); + glfwSwapInterval((configFlags & FLAG_VSYNC_HINT) != 0); + requireVsync = false; + } glfwSwapBuffers(window); #endif