Reputation: 109
I'm trying to make a simple Game-Engine using JOGL. But instead of using the EventListener Class I want to create a static Object like in LWJGL. And no I do not use LWJGL because I had a lot of trouble with it. This is my EventListener:
class EventListener implements GLEventListener {
public static GL2 gl;
@Override
public void display(GLAutoDrawable drawable) {
gl = drawable.getGL().getGL2();
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glClearColor(0, 1, 0, 1);
}
@Override
public void dispose(GLAutoDrawable drawable) {
}
@Override
public void init(GLAutoDrawable drawable) {
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
}
}
In the main class, it clears a green screen so it works perfectly. But if I try to use the 'gl' object in an other class it does nothing. Thanks in advance!
Upvotes: 0
Views: 443
Reputation: 4085
Actually, it's a bad idea. You mustn't store a GL instance into a field as it's error prone for several reasons:
That's why you should use a GLEventListener. Otherwise, you can get a valid GL instance by calling GLContext.getCurrentGL() but it throws a GLException if no context is current. You can use GLAutoDrawable.invoke() too, it allows to run your code on the right thread at the right time but GLEventListener is more useful as you can execute some code at initialization time.
By the way, JOGL specific questions should be asked on the official JogAmp forum rather than here. StackOverflow is a better place for general OpenGL questions but most JogAmp contributors never come here. Yes, StackOverflow isn't the panacea.
P.S: I advise you to read this article and this comment if you want to understand the design choices of JOGL, especially the instance design.
Upvotes: 1