How to multiple windows?


#1

I want create another window where will be new GUI content. But program must handle many windows, not only two. Windows can be 2, 5 or as many as program needs.
I tried create window myself, but without success. Created window is empty. When I try do something with window, like glfwGetBuffer and other window functions, program crash.

When I looked in to documentation, if I understand it, I need use multithreading for new Window and his context?

Is there some examples how to use multiple window?


#2

You don’t need to use multi threading.

What you need to do is to manage OpenGL context explicitly. OpenGL commands are executed in global context (per thread). So if want to draw to specific window then you need to make context of this window current. That is done with glfwMakeContextCurrent call: http://www.glfw.org/docs/latest/group__context.html#ga1c04dc242268f827290fe40aa1c91157


#3

I tried this:
This is pseudo code

Create two windows;
if(bool)
{
glfwMakeContextCurrent(w1);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.3f, 0.5f, 0.8f, 1.0f);
glfwSwapBuffers(w1);
glfwPollEvents();
if(glfwWindowShouldClose(w1)) { close program; }

glfwMakeContextCurrent(w2);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.3f, 0.5f, 0.8f, 1.0f);
glfwSwapBuffers(w2);
glfwPollEvents();
if(glfwWindowShouldClose(w2)) { close program; }

}

This code is working, but am Idoing it right?

But I have problem when I create window with my function:

void createGLFWWindow(GLFWwindow * windowGLFW,
                      const unsigned int width,
                      const unsigned int height,
                      const std::string nameWindow,
                      const unsigned int ww = 0,
                      GLFWmonitor * monitor = NULL,
                      GLFWwindow * share = NULL)
{
    windowGLFW = glfwCreateWindow(width, height, nameWindow.c_str(), monitor, share);
    if (!windowGLFW)
    {
        glfwTerminate();
        pravda = false;
    }
    else {
        if(ww == 1) {
            pravda = true;
            std::cout << "Window 2 created\n";
        }
    }
}

then program crash. I want create dynamic window creation, if it is possible.


#4

Ok, I solve problem, instead
GLFWwindow * windowGLFW argument I need
GLFWwindow ** windowGLFW,
*windowGLFW = glfwCreateWindow();
and createGLFWWindow(&window);
but I am still interesting, is my code is ok? Thanks.


#5

Yes, that is ok. When you assign pointer value you are not changing the contents of the value it points to. Here’s and example.

int x = 12; // variable x will have value 12
int* y = &x; // y will point to x

y = value;   // this will NOT assign "value" to x, it will only make y to
             // point to new place

             // if instead of previous line you would do this:
*y = value;  // this will write "value" to location where y points to
             // thus it will change value of x

And at any time when program crashes start debugging - look at call stack in debugger, examine values of variables and arguments. In this case you would see that value of window you used outside of createGLFWWindow function is not changed. That immediately mean you wrongly tried to assign new value to window. Debugger makes to find this kind of mistakes very easily.