3

I use AssetManager to load all assets in my game. I have problem with displaying loading progress or instead progress I try to set only background colour using this code in my LoadingScreen render method.

    Gdx.gl.glClearColor(0.431f, 0.792f, 0.808f, 0xff / 255.0f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

Everiting loads well but, while loading I get black screen instead colour set in glClearColor. The same happend when implementing loading bar like this example

Here part of Assests class

    public class Assets implements Disposable, AssetErrorListener {

        public static final Assets instance = new Assets();
      .
      .
        public void init (AssetManager assetManager) {
            this.assetManager = assetManager;
        // set asset manager error handler
        assetManager.setErrorListener(this);
        // load texture atlas
        assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
     .
     .
        TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
     .


   }  
     .


}

Here is simple code of my LoadingScreen:

    @Override
    public void show() {
        manager = new AssetManager();

        Assets.instance.init(manager);
   }

    public void render(float deltaTime) {
        Gdx.gl.glClearColor(0.431f, 0.792f, 0.808f, 0xff / 255.0f);//light blue
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        if (manager.update()) {
            ScreenTransition transition = ScreenTransitionFade.init(0.5f);
            game.setScreen(new HomeScreen(game), transition);
        }
}

Here I expected light blue empty screen to show while loading assets, but I get black screen.

I guess some code blocks main thread but I don't know how to fix this.

Thanks for help

Jovan
  • 4,684
  • 13
  • 64
  • 98
  • The "cleaning" part is OK, probably the problem is somewhere else. IF you remove the code after the cleaning part is working [do you have a non black screen ? ] ? – dawez Oct 29 '14 at 16:23
  • `Assets.instance.init(manager);` blocks my UI.If I remove the code after the cleaning part its the same (black screen while loading). Any suggestion? – Jovan Oct 29 '14 at 19:57

1 Answers1

0

Make sure you know when Screen.show() and Screen.render(float delta) are called:

Screen.show(): is called when you call Game.setScreen().

Screen.render(float delta): is called continuously after Screen.show() is finished. Hence during the loading Screen.show() is not yet finished and the default background (black) is shown.

To avoid that a separate thread would be nice. Here comes AssetManager which loads stuff asynchronously. Now we only have use it properly:

Call AssetManager.load(<your assets>) before you call Game.setScreen(<Loading Screen>). LoadingScreen.render(float delta) is then called directly and you see your background. To check if the loading is done you can use AssetManager.update(), which returns true if done and false else.

Lukas
  • 434
  • 3
  • 14
  • thanks, I call `setScreen(LoadingScreen)` in `public class MyGame extends DirectedGame {}`. If I call `Assets.instance.init(manager);` on ` create()` in MyGame class where to check if assets are loaded(`update()`)? In LoadingScreen `update()` method? I try this but not working again, not I think black screen is displayed in `MyGame` class – Jovan Oct 29 '14 at 20:53
  • In my implementations I check `AssetManager.update()` in `LoadingScreeen.render()` and switch to the new screen if all loading is done. – Lukas Oct 30 '14 at 00:28
  • where you call `Assets.instance.init(manager);` ? in LoadingScreen? tnx – Jovan Oct 30 '14 at 09:44
  • I would call `.init()` right before the setScreen(loadingScreen). I think I know what your problem is: `assetManager.load()` loads your stuff asynchronously. But the rest of the `.init()` method, need this stuff to be loaded and hence waits until loading is finished. I would suggest that you restrucutre your `init()` method that it only contains asynchronous loading stuff. Then in your render method you check for the `assetManager.update()` until loading is finished and then allocate your `TextureAtlas`. For a detailed analysis, I need to see complete `.init()` method. – Lukas Nov 02 '14 at 10:13