How to create multiple window?

In some games,like Minecraft,they have two windows,one for the game and one launcher to launch the game.
So in GLFW,how do I create multiple window??
I know I need to use glfwMakeContextCurrent to choose which window you are currently using,but how do i program the game loop(poll event and update) for both window???
Example code also,please??
Pseudocode is fine too…

Here is the documentation page example extended to create two windows.

#include <GLFW/glfw3.h>

int main(void)
{
    GLFWwindow* window1;
    GLFWwindow* window2;

    /* Initialize the library */
    if (!glfwInit())
        return -1;

    /* Create a windowed mode window and its OpenGL context */
    window1 = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window1)
    {
        glfwTerminate();
        return -1;
    }

    /* Create a second windowed mode window and its OpenGL context */
    window2 = glfwCreateWindow(640, 480, "World Hello", NULL, NULL);
    if (!window2)
    {
        glfwTerminate();
        return -1;
    }

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window1) && !glfwWindowShouldClose(window2))
    {
        /* Make the window's context current */
        glfwMakeContextCurrent(window1);
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);
        /* Swap front and back buffers */
        glfwSwapBuffers(window1);

        /* Make the second window's context current */
        glfwMakeContextCurrent(window2);
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);
        /* Swap front and back buffers */
        glfwSwapBuffers(window2);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

The order of operations inside the while loop are only an example of how rendering to more than one window could look.

3 Likes

You can also see the test code windows.c on Github.

Note that normally games use two processes for the launcher and the game, rather than two windows with the same process.

3 Likes

not found the source file

The file windows.c has now moved into the examples folder. I will correct the above link as well.

Extra Good to know -
I have just started with OpenGL and was experimenting with such multi window setup and this thread helped. BUT the issue I faced was memory leaks in both my code and the posted window.c. I could see the memory graph of Visual studio rising each frame the windows rendered. Wasted a lot of time but found nothing of relevance, then it occured I had my RivaTuner stat running, killed it and issue gone.
Don’t know if this is due to rivatuner or if glfw does not like overlays.
Apologies as I don’t read documentation to full extent so might’ve missed if this was mentioned in it.

Welcome to the GLFW forum.

Apps like RivaTuner which hook into graphics APIs and add their own overlays have been known cause of many issues, so if you’re observing no problems without it then I’d guess it was the likely culprit. GLFW doesn’t do anything specific which would cause such apps to have problems.

1 Like

A post was split to a new topic: Interacting with multiple windows