Java threads in libgdx

Hey guys, I'm making a game with libgdx. I really want to operate the game, so I run a drawing loop and a logic loop on different threads, just like you create a simple Java Swing Game with paintcomponent loop and runnable

I have experience with threads in C, but not so much in Java. The only way I can create a thread is to create an extension thread and then create a class to run a loop there

But the key to making a running loop is to allow each screen to freely calculate logic, so I finally need some abstract class with custom thread classes

I asked if there was a simpler or more standard way to implement this thread

Solution

The libgdx library has run a separate rendering thread for OpenGL context updates See http://code.google.com/p/libgdx/wiki/TheArchitecture#The_Graphics_Module

For each screen refresh, this rendering thread calls applicationlistener. On the main GameObject Render () method (so it should be about 60 Hz), so just put the body of the render loop in the implementation of this method

You can create an additional background thread in the Create method of applicationlistener (for example, for game logic) (make sure it is cleared in the dispose method) Except for rendering threads, I don't think any pre - existing threads are the right place for game logic

For communication between threads, you can use any existing Java synchronization method I have used Java's arrayblockingqueue < > to send requests to background threads I use GDx app. PostRunnable () to get the background thread to push the data to the rendering thread (such Runnables runs on the next frame, and then calls render ()).

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>