Is there a way to toggle fullscreen without destroying window/context?

2.1k Views Asked by At

I currently have a game with OGL 4.5 and GLFW 3.

I'm trying to make a function that allows toggling between fullscreen and windowed modes. However, it seems in order to accomplish this, I have to destroy the current window and then create a new one in the same pointer (I red this in the GLFW documentation).

Although this works and I can swap between the two modes, it causes some crucial parts of the engine to stop drawing once the swap happens.

Due to this, I would like to know if there is some kind of way to enable / disable fullscreen without destroying the window and creating a new one.

This is the code of the function:

void TMooseEngine::toggleFullscreen()
{
    _fullscreen = !_fullscreen;

    glfwDestroyWindow(window);
    delete _shader;
    delete _skybox;
    //delete _particulas;




    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    if(_fullscreen){ //change to fullscreen
       window = glfwCreateWindow(_width, _height, "Fate Warriors", glfwGetPrimaryMonitor(), NULL);
       glfwMakeContextCurrent(window);
       glViewport(0,0,_width,_height);
       //culling
       glEnable(GL_DEPTH_TEST);
       glViewport(0,0,_width,_height);
       glEnable(GL_CULL_FACE);
       glCullFace(GL_BACK); 
       glFrontFace(GL_CCW);
       _shader = new Shader();
       _skybox = new Skybox();

       initUI();
    }

    else{ //change to windowed
        window = glfwCreateWindow(_width, _height, "Fate Warriors", NULL, NULL);
        glfwMakeContextCurrent(window);
        glViewport(0,0,_width,_height);
        //culling
        glEnable(GL_DEPTH_TEST);
        glViewport(0,0,_width,_height);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK); 
        glFrontFace(GL_CCW);
        _shader = new Shader();
        _skybox = new Skybox();

        initUI();
    }
}
1

There are 1 best solutions below

1
On BEST ANSWER

According to the documentation of GLFW, you can use the function glfwSetWindowMonitor() to toggle the fullscreen mode of an existing window, without having to destroy and recreate it.

The following command will change the fullscreen status depending on the _fullscreen variable:

glfwSetWindowMonitor(window, _fullscreen ? glfwGetPrimaryMonitor() : NULL, 0, 0, _width, _height, GLFW_DONT_CARE);