0

I'm creating a game with JOGL and I've come across an error which I can't for the life of me figure out.

In the game I'm using two GLCanvases (GLJpanels actually), one for the menu and one for the actual game. The idea being that when a game is started from the menu, the menu GLCanvas is removed from the gamewindow and swapped for the game GLCanvas. So far I've gotten the menu to work pretty much perfectly, but whenever I attempt to switch over to the game canvas, I get this error:

Catched Exception on thread AWT-EventQueue-0
javax.media.opengl.GLException: AWT-EventQueue-0: Context not current on thread, obj 0x2ab44e2d, ctx 0x0, surf 0x0, inDestruction: false, <53f7c06e, 2e7aa0d3>[count 0, qsz 0, owner <NULL>]

The code I'm using to switch between canvases is:

public void start()
{
    canvas.addGLEventListener(this);
    animator.start();
    window.add(canvas);
    canvas.requestFocus();
}
public void stop()
{
    window.remove(canvas);
    animator.stop();
    canvas.removeGLEventListener(this);
}

and the switch function:

public void switchToCanvas(String canvasName)
{
    currentCanvas = canvasName;

    if(canvasName.equals("GameCanvas"))
    {
        menu.stop();
        game.start();
    }
    else
    {
        game.stop();
        menu.start();
    }
}

I've done some googling and I came around this question: How can I create my own openGL context and bind it to a GLCanvas?

But none of the solution posted there worked for me.

Community
  • 1
  • 1
user1870238
  • 447
  • 7
  • 20

1 Answers1

1

At first, I would rather use a single GLCanvas instead of 2 instances of GLJPanel. GLJPanel has an higher memory footprint and should only be used when GLWindow or AWT/SWT GLCanvas can't be used, when there are some issues with mixing heavyweight and lightweight components.

Secondly, your error message means that there is no OpenGL context current on this thread. You should use GLAutoDrawable.invoke() to put OpenGL tasks into the queue or you should make the context current when you need it. I advise you to look at jogl-demos to see how we do that in our examples.

Edit.: JogAmp maintainers including me can be easily contacted on the official forum (http://forum.jogamp.org/) and on IRC.

gouessej
  • 3,640
  • 3
  • 33
  • 67
  • Thanks for the answer, I'll look at some examples and if that fails I'll hit the forums. I believe the reason I used a GLJPanel instead of a GLCanvas, was because a GLCanvas didn't play well with a JFrame. Do you happen to know anything about this? Also, where exactly would you use GLAutoDrawable.invoke(), inside the init() function? – user1870238 Feb 06 '14 at 22:26
  • You're welcome. Mixing AWT (heavyweight) and Swing (lightweight) components should be well supported since Java 1.6 update 10 but there are a few cases in which it is still problematic and this is not related to JOGL in particular. It depends on what you really do in your JFrame. If you just put a single JPanel into your JFrame and your GLCanvas or NewtCanvasAWT into this JPanel, it should work. Rather use GLWindow for games if you can. – gouessej Feb 06 '14 at 22:30