2

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();
    }
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Manu
  • 209
  • 1
  • 3
  • 13
  • Ah, that answer is much more extensive than the one I provided. Personally, I think GLFW is horrible in this regard, SDL2 gives you the function `SDL_SetWindowFullscreen()` which just does the right thing. GLFW's `glfwSetWindowMonitor()` conflates toggling fullscreen with window resizing and positioning. – G. Sliepen May 17 '18 at 11:15

1 Answers1

4

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);
G. Sliepen
  • 7,637
  • 1
  • 15
  • 31
  • Thank you very much, I didn't got the scope of the function the first time I red the documentation, but when you mentioned it, I went to the specification of the function itself and everything is crystal clear now. I have tried your solution and it works flawlessly – Manu May 17 '18 at 11:26