Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create loading screen in libgdx?

I have a GameScreen class that renders my game. but before starting to render the game, it needs to reading files and initializing that is time consuming.

So I need to show/render another Screen class called LoadingScreen in order to spending some time and concurrently read my files and do initializing process for my GameScreen, and after initializing completed changing the screen by calling setScreen(gameScreen).

I need to use thread for making this concurrent work, now the problem is that if I use a thread to read files and initializing; When switching to the GameScreen the openGl gives me this error:

javax.media.opengl.GLException: Error: no OpenGL buffer object appears to be bound to target 0x8892
at com.sun.opengl.impl.GLBufferSizeTracker.setBufferSize(GLBufferSizeTracker.java:118)

I am aware of not both of threads use the graphic resources simultaneously.

I have found that the problem causes with Meshes. Initializing a Mesh in initializer thread and rendering in main thread causes this error. But I don't know how to solve it.

Do you have any ideas to solve this problem?

like image 565
Aliaaa Avatar asked Aug 18 '12 07:08

Aliaaa


2 Answers

As suggested in the comments, the AssetManager is the way to load most libGDX resources (audio, textures, etc) asynchronously while showing a splash or loading screen.

For other operations, running them in a background thread (or using one of the other Android or Java background task execution facilities) should be sufficient. To invoke libGDX routines like setScreen or others that need to be executed on the libGDX render thread, use Gdx.app.postRunnable, like this:

Gdx.app.postRunnable(new Runnable() {
     @Override
     public void run() {
         // Do something on the main thread
         myGame.setScreen(postSplashGameScreen);
     }
  });

Depending on the visibility of myGame and postSplashGameScreen it may be easier to construct the Runnable in a different context and then pass it over to the background thread to post when its done.

like image 65
P.T. Avatar answered Nov 10 '22 04:11

P.T.


My workouround is using Actions in my loading screen method:

@Override
public void show() {
    stage.addAction(Actions.sequence(Actions.delay(0.5f), action_loading_assets_and_other_stuff, Actions.delay(0.5f), action_setScreen));
}

Actions.delay(0.5f) makes the magic - game not freezing

like image 20
kosanake Avatar answered Nov 10 '22 06:11

kosanake