GLFW: size, position & monitorsGLFW 3.2

glfwSetWindowMonitor

Switches a window between full screen on a monitor and a normal window, keeping the same OpenGL context.

Signature

void glfwSetWindowMonitor(GLFWwindow* window, GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);

How it works

Pass a monitor to go full screen on it with the given resolution and refresh rate. Pass NULL to come back to a normal window at (xpos, ypos) with the given size.

The window and its context survive, so your buffers, textures and shaders stay valid. Remember the windowed position and size (glfwGetWindowPos, glfwGetWindowSize) BEFORE going full screen, so you can restore them.

Parameters

windowGLFWwindow*

The window to switch.

monitorGLFWmonitor*

The monitor for full screen, or NULL for windowed mode.

xposint

Windowed mode: x of the content area. Ignored in full screen.

yposint

Windowed mode: y of the content area. Ignored in full screen.

widthint

Content width (windowed) or resolution width (full screen).

heightint

Content height (windowed) or resolution height (full screen).

refreshRateint

Full screen refresh rate in Hz, or GLFW_DONT_CARE for the highest available. Ignored in windowed mode.

Tips & pitfalls

  • The framebuffer size changes, so your framebuffer-size callback runs: the viewport follows if it calls glViewport.

  • On Wayland the windowed position is ignored: applications cannot place their windows there.

Errors

When the call is invalid it does nothing and sets an error, which glGetError returns:

GLFW_NOT_INITIALIZED
Called before glfwInit or after glfwTerminate.
GLFW_PLATFORM_ERROR
The operating system or window system reported a failure; the description says which.

Example

example.cpp
1void toggleFullscreen(GLFWwindow* w) {
2    static int wx, wy, ww, wh;
3    if (glfwGetWindowMonitor(w)) {                          // full screen -> window
4        glfwSetWindowMonitor(w, nullptr, wx, wy, ww, wh, 0);
5    } else {                                                // window -> full screen
6        glfwGetWindowPos(w, &wx, &wy);
7        glfwGetWindowSize(w, &ww, &wh);
8        GLFWmonitor* m = glfwGetPrimaryMonitor();
9        const GLFWvidmode* mode = glfwGetVideoMode(m);
10        glfwSetWindowMonitor(w, m, 0, 0, mode->width, mode->height, mode->refreshRate);
11    }
12}
Official GLFW documentation