Standard Game Loops for lwjgl?

In beginning my first lwjgl game project today i realized that up until now i had been using a standard while loop for all my game loops (most of my stuff was up until now has just been simple test code to learn the ropes of lwjgl). But after i programmed the loop it i got an error: “Function is not supported” and turns out after doing a bit of googling that OpenGL calls (from one GL context) can only run on one thread. And up until now all my game loops i’ve made/used run on a separate thread to the main one.

My question is, does anyone have any examples of game loops that work well with lwjgl?

Here’s the code if your interested: https://gist.github.com/anonymous/ab50c60a57eca465242e

You can render in any thread you want with OpenGL. Only the thread processing window messages is limited on some platforms. Here is an example of how to process window messages in the “main” thread and rendering in another spawned thread: https://github.com/LWJGL/lwjgl3-demos/blob/master/src/org/lwjgl/demo/opengl/glfw/Multithreaded.java
This is with LWJGL3, though.

And here is a LWJGL forum entry stating how you can even switch between different render threads (also with LWJGL3): http://forum.lwjgl.org/index.php?topic=5965.msg31963#msg31963

i was more looking for examples of what people have done in actual projects. But thanks anyway :slight_smile:

I am curious about this too actually, it felt a bit strange to just begin the game loop in the main thread because it is so heavily recommended against in Java2D. On top of that, I also believe that it is recommended that any time you wanted to create AWT/Swing components that you invoke it on the Event Dispatched thread (I get that LWJGL isn’t associated with AWT/Swing).

Why?

There’s generally no benefit/need to use more than one thread. Unless you know exactly why and what you’re doing in which case you wouldn’t be asking this question.

@mattdesl has some nice tutorials:

I’m actually not exactly sure what the reason is, but every tutorial I had ever gone through had made a point of running your game on its own thread.

Unfortunately, I don’t think that tutorial is really valid anymore for LWJGL 3. I believe that the display class is now deprecated??

i don’t think it is. All the tutorials/posts i’ve seen still use it.

Correction: The Display class is no longer in LWJGL 3. People who do have a Display class simply wrap the GLFW functions in their own wrapper. IIRC SHC did this at one point.

I’m still using the Display class I wrote once in my SilenceEngine. However, I kept the GLFW functions in their own classes such as Window, Monitor, etc., and the Display supports switching windows (between fullscreen and normal mode).

Display.java
Window.java
Monitor.java
VideoMode.java
Rest of GLFW classes