shibukawa / nanovgo

zlib License
149 stars 32 forks source link

Creating multiple windows with separate contexts #8

Closed medvednikov closed 7 years ago

medvednikov commented 7 years ago

Hi,

Thanks for this great library!

I'd like to have two different windows with different stuff drawn on them. I'm struggling with this. Is there a small example of how to handle multiple windows/multiple contexts?

Here's what I'm trying to do looks like in pure OpenGL:


foreach w in windows:
    foreach ctx in w.contexts:
        ctx.make_current(w)
        do_opengl_stuff()
        glFlush()

It's not clear how to define multiple contexts. I assume NewContext() uses current current context, so I tried

w1, _ := glfw.CreateWindow(...)
ctx1, _ := nanovgo.NewContext()

w2, _ := glfw.CreateWindow(...)
ctx2, _ := nanovgo.NewContext()

for {
        for i := 0; i < len(windows); i++ {
                windows[i].MakeContextCurrent()
                gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT
                gl.ClearColor(255, 255, 255, 255)

                ctx[i].BeginFrame(...)
                drawText(ctx[i], "HELLO")
                ctx[i].EndFrame()

                windows[i].SwapBuffers()
        }
        glfw.WaitEvents()
}

But it didn't work. The text is drawn only in the first window.

Thanks

medvednikov commented 7 years ago

Sorry, there was an error in my code. It actually works.

I also found out you can use shared windows for this.