How to toggle fullscreen when F11 pressed in glfw c++

Viewed 28

I can't get fullscreen to work while pressing F11 like other game in glfw. Here's the code:

#include <iostream>
#include <GLFW/glfw3.h>

void key_input(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);

    if (key == GLFW_KEY_F11 && action == GLFW_PRESS)
        if (!glfwGetPrimaryMonitor())
            glfwGetPrimaryMonitor();
        if (glfwGetPrimaryMonitor())
            !glfwGetPrimaryMonitor();
}
1 Answers

You know, what glfwGetPrimaryMonitor is doing:

This function returns the primary monitor. This is usually the monitor where elements like the task bar or global menu bar are located.

What you want is a fullscreen window:

Full screen windows

To create a full screen window, you need to specify which monitor the window should use. In most cases, the user's primary monitor is a good choice.

Windowed mode windows can be made full screen by setting a monitor with glfwSetWindowMonitor, and full screen ones can be made windowed by unsetting it with the same function.

Be aware: glfwSetWindowMonitor can only be called from the main thread (and not from within a callback). Therefore, you have to set a flag in the callback and check in the main thread, if that flag was set or unset.

Related