LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: jpwrunyan on August 21, 2019, 23:53:25

Title: glfwInit() doesn't need to be started on a new thread???
Post by: jpwrunyan on August 21, 2019, 23:53:25
Hi, this may be me misunderstanding something about Java but I was looking at the Truetype example in the LWJGL samples and found this:

Code: [Select]
public static void main(String[] args) {
  ...
  new Truetype(filePath).run("STB Truetype Demo");
}

Which then proceeds to spin up OpenGL and everything else to display the STB True Type demo.
However, isn't this on the main thread?!
Every other example of LWJGL I've seen emphasizes that all access to the GPU must be done on a new thread and so they implement Runnable and then start the graphics logic on a new Thread separate from main()  or some variation.
So why is that not the case here?

Sorry if this is a dumb question, but if I can write my core logic without starting a new Thread from the main method... I mean that seems a little cleaner to me. But on the other hand, I feel like I might be missing something here.

Thanks for taking the time to answer this question.

edit: I just found this quote on the forums at random: "GLFW requires that most of its functions are called from the main thread. On macOS, that thread must also be the first thread in the process (that's why -XstartOnMainThread is needed). This is not a problem with LWJGL and not a problem with GLFW, it's how macOS works."

So have I been misreading something? But the fact remains that up until now every app I've seen with LWJGL has been implementing new Thread() before initializing anything...
Title: Re: glfwInit() doesn't need to be started on a new thread???
Post by: spasi on August 22, 2019, 10:14:49
The only requirement is that most UI calls must happen on the main/first thread. The thread(s) you use for rendering is entirely up to you and what your application demands. It's totally OK to render on the main thread.

I'm actually surprised that you find LWJGL examples that do rendering on secondary threads. There are good reasons to do this (decoupling event handling and gameplay update rate from rendering rate, loading rendering resources in a background thread, etc.), but it is an advanced setup and tutorials usually don't do it for simplicity. Most LWJGL samples (in the official repo) are single-threaded for this reason. One sample that uses multiple threads (UI, graphics, audio) is the Vorbis demo.
Title: Re: glfwInit() doesn't need to be started on a new thread???
Post by: jpwrunyan on August 22, 2019, 21:37:44
Yes, some of the demos I've seen feel like they're jumping ahead too much on optimization and OOP design when I'd like to know just the basics of using the OpenGL stuff. It may have been my novice reading that made me think the tutorials were demanding I start a new thread. I only recently found the demos in the LWJGL package.

I've relied heavily on YouTube to start out, but I'm realizing most videos are outdated (the most popular are 3 years old). I understand the concepts of the game loop and separating rendering from logic, the timing issues for calculating changes in real time between rendering frames, etc... but those are problems I'd rather approach after I can actually render things correctly.

Are you going to write an O'Reilly book for LWJGL 3 anytime soon? (please do, I need it!)