how to create loading screen in libgdx?

10,575

Solution 1

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.

Solution 2

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

Share:
10,575
Aliaaa
Author by

Aliaaa

Updated on June 07, 2022

Comments

  • Aliaaa
    Aliaaa almost 2 years

    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?

    • NotCamelCase
      NotCamelCase over 11 years
      What about AssetManager ? code.google.com/p/libgdx/wiki/AssetManager
    • Aliaaa
      Aliaaa over 11 years
      Thanks for "AssetManager". It solves some part of problem, but not all. Using thread for initializing still is a problem.
    • Matsemann
      Matsemann over 11 years
      When using AssetManager you have to call manager.update() till it's finished in your render(). Why can't you there just do some part of the init per render call?
    • Aliaaa
      Aliaaa over 11 years
      @Matsemann: I call manager.update() per render call. The problem is after finishing update() calls, I still have some other initialization after reading assets; e.g.: initializing physics engine,... . these works also take some times to do.
  • JohnyTex
    JohnyTex over 9 years
    This doesn't work me. If I use this approach the new screen goes black. Any ideas?