Преглед изворни кода

fixes for switching full-screen and v-sync (#963)

* fixes for switching full-screen and v-sync

* requireVsync flag
MasterZean пре 5 година
родитељ
комит
8eecbbe4aa
1 измењених фајлова са 31 додато и 2 уклоњено
  1. 31 2
      src/core.c

+ 31 - 2
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