The tween animations is getting disturbed when I load the TextureAtlas. I wish to load my texture Atlas in a seperate thread.Could you please guide me to the safe usage of a thread in Libgdx. Any example / sample code is highly appreciated.
-
TextureAtlases cannot be loaded in a separate thread because the texture must be loaded into GL memory. If you try it, they will look corrupted. Using AssetManager can help break up the loading over a few frames if your atlas contains multiple pages, but there will probably still be a noticeable stutter. – Tenfour04 May 29 '15 at 12:30
-
1I am not English speaker, but I think they are different questions, one is on a different thread load and another is on the state of charge of the asset. it may or may not use a different thread, be as efficient or not, I do not understand the downvote – Angel Angel May 29 '15 at 15:47
-
Thanks Angel for your understanding ! – iappmaker May 29 '15 at 18:27
1 Answers
The easiest and best way to do this is to use the LibGdx AssetLoader class.
See https://github.com/libgdx/libgdx/wiki/Managing-your-assets
From the page...
Loading a specific asset is simple:
manager.load("data/mytexture.png", Texture.class); manager.load("data/myfont.fnt", BitmapFont.class); manager.load("data/mymusic.ogg", Music.class);
These calls will enqueue those assets for loading. The assets will be loaded in the order we called the AssetManager#load() method. Some loaders allow you to also pass parameters to them via AssetManager#load(). Say we want to specify a non-default filter and mipmapping setting for loading a texture:
TextureParameter param = new TextureParameter(); param.minFilter = TextureFilter.Linear; param.genMipMaps = true; manager.load("data/mytexture.png", Texture.class, param);
Look into the loaders mentioned above to find out about their parameters.
So far we only queued assets to be loaded. The AssetManager does not yet load anything. To kick this off we have to call AssetManager#update() continuously, say in our ApplicationListener#render() method:
public MyAppListener implements ApplicationListener { public void render() { if(manager.update()) { // we are done loading, let's move to another screen! } // display loading information float progress = manager.getProgress() ... left to the reader ... } }
As long as AssetManager#update() returns false you know it's still loading assets. To poll the concrete state of loading you can use AssetManager#getProgress(), which returns a number between 0 and 1 indicating the percentage of assets loaded so far. There are other methods in AssetManager that give you similar information, like AssetManager#getLoadedAssets() or AssetManager#getQueuedAssets(). You have to call AssetManager#update() to keep loading!

- 3,146
- 13
- 24
-
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Dropout May 29 '15 at 08:06
-
1@Dropout - Good idea, thanks... I've edited to include a section from the page. – Phil Anderson May 29 '15 at 08:19
-
-
With this setup, I get a black screen while loading. How to avoid it – iappmaker May 30 '15 at 10:23
-
It could be due to any number of reasons. Probably best to start a new question with relevant code etc. – Phil Anderson May 30 '15 at 16:51