Programmers learn & share
0 votes
114 views

Problem :

I am working on a card game, and currently running into an error when I run it in eclipse. I am also using slick 2d.

Here is the error

java.lang.runtimeexception: no opengl context found in the current thread.

by (6.9k points)   | 114 views

2 Answers

0 votes

Solution :

Depending on which LWJGL you're using, init your Display as shown bellow:

LWJGL 3 

if (!glfwInit()) {
   throw new IllegalStateException("Can't init GLFW");
}

LWJGL 2:

try {
   Display.setDisplayMode(new DisplayMode(800, 600));
   Display.create();
} catch (LWJGLException ex) {
   Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

Please dont forget Display.create() .

Further Readings:

https://github.com/nifty-gui/nifty-gui/issues/111

by (36.1k points)  
edited by
0 votes

Solution:

This kind of error is common for LWJGL starters. The OpenGL context gets bound to a thread when it's been created. So you can only access this context from the same thread.

Explanation:

As it does not look that you're working with different threads there might be another cause. Slick2D seems to need a valid OpenGL context for its Image class.

So my first try would be to initialize an OpenGL context before initializing your card images.

Another Reason for occuring this error may be

This happened to me once, and I couldn't figure out what to do until I realized I was calling the image loader before OpenGL had initialized. Make sure that you aren't defining any variables with an image loader in the constructor(or any other method called) before OpenGL inits(what I did).

If you are not able to find solution yet then try this

In lwjgl 3.x and higher

GLContext.createFromCurrent();

If you're using libgdx, there is also

Gdx.app.postRunnable(...)

to post a

Runnable

on the render thread.

 

You can add old thread depending on which LWJGL you're using, init your Display:

LWJGL 3 (uses GLFW):

if (!glfwInit()) {
   throw new IllegalStateException("Can't init GLFW");
}

LWJGL 2:

try {
   Display.setDisplayMode(new DisplayMode(800, 600));
   Display.create();
} catch (LWJGLException ex) {
   Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

 

Never forget 
Display.create() :)

 

by (11.2k points)  
2,227 questions
2,734 answers
59 comments
241 users