[Best practice]Assetmanager vs TextureAtlas

At the moment i use both in combination but i think in a wrong way:


public class Assets {

	public static final AssetManager manager = new AssetManager();

	public static final String player = "hero/hero.txt";
	public static final String pogo = "enemy/pogopuschel.txt";

	public static void load() {
		
		manager.load(player, TextureAtlas.class);
		manager.load(pogo, TextureAtlas.class);
	}

	public static void dispose() {
		manager.dispose();
	}
}

PlayScreen class


public PlayScreen(Main game) {
		Assets.load();
		Assets.manager.finishLoading();
		atlas = Assets.manager.get(Assets.player, TextureAtlas.class); //Load only one Asset, how to load more?
}

Player class


public Player(PlayScreen screen) {
		super(screen);
		
		
		move_down 	= setAnimation(super.move_down, 32, 32, false);
		move_up		= setAnimation(super.move_up, 32, 32, false);	
		move_right	= setAnimation(super.move_right, 32, 32, false);
		move_left	= setAnimation(super.move_right, 32, 32, true);
		
		stand_down 	= new TextureRegion(screen.getAtlas().findRegion(super.stand_down), 0, 0, 32, 32);
		stand_up 	= new TextureRegion(screen.getAtlas().findRegion(super.stand_up), 0, 0, 32, 32);
		stand_right = new TextureRegion(screen.getAtlas().findRegion(super.stand_right), 0, 0, 32, 32);
		stand_left 	= new TextureRegion(screen.getAtlas().findRegion(super.stand_right), 0, 0, 32, 32);
		stand_left.flip(true, false);
		
		
	}

I have a lot of sprites maybe over 2000 can i only use TexturePacker and TextureAtlas? Or should i use the Assetmanager in combination?

How can i give the TextureAtlas all Assets from the AssetManager?

You appear to be using the asset manager correctly.

[quote]How can i give the TextureAtlas all Assets from the AssetManager?
[/quote]
I assume by this you mean how do you put all images into one atlas?

You can use a texture packer to pack all your images into a larger file.

A guide on AssetManager https://www.gamedevelopment.blog/asset-manager-libgdx-tutorial/

A guide on Texture Packer https://www.gamedevelopment.blog/texture-packer-gui-quick-tutorial/

Thx for the links.

But what is the maximum size for textureatlas?

[quote]But what is the maximum size for textureatlas?
[/quote]
You can put as many images as you want into an atlas but you should try and keep the atlas size 1024 x 1024 for maximum compatibility but 2048 x 2048 will work on most devices.

If you have too many images to fit onto a single 1024x1024 image then you can use split the atlas into several pages of 1024x124

If i split them into more images, is it usefull to instantiate more TextureAtlas class as one?

Thanks for your help.

The main reason to put all your images in a TextureAtlas is to keep as many images as possible in a single image so you only need to read the image file from disk once which is slow. Then when you want a single image you read it from the already loaded TextureAtlas which is loaded in memory which is a lot faster. So in terms of performance, you are best off having as few as possible Images to load from disk.

Usually, if you have a lot of images that won’t fit onto a single 1024x1024 image you would pack them onto multiple 1024x1024 pages all in the same atlas. This would give you one atlas(information on individual images) and several 1024x1024 image files that belong to that atlas.

It is not about disc load time but about minimizing opengl texture switching.
You want to minimize the render state changes between different objects so you can minimize your draw calls.

Sorry again, do i need to create a multiple packages in Texture Packer?
I cant find any Method from TextureAtlas to load more than one Image.

In Libgdx, the asset manager will load all the pages that belong to the atlas automatically as long as they’re numbered.
So for gui.atlas all gui_1.png, gui_2.png and gui_3.png will be loaded.

Have you tried to load a single atlas with multiple pages then use an image from each page? Would be a good way to check if it’s working.

Maybe i miss understand you but i dont load any png file with assetmanager. Only that txt file from TexturPacker:


public static final String player = "hero/split.txt";
public static final String pogo = "enemy/pogopuschel.txt";

	public static void load() {
		
		manager.load(player, TextureAtlas.class);
		manager.load(pogo, TextureAtlas.class);
	}

And that txt file reference to the image regions.

So i dont understand how to load more than more file to the textureAtlas when i need more than one 1024x1024 image.

The text file is also known as an atlas, this text file has the names of all the image files it needs and so when the assetManager loads the text file it will find the names of the images needed and load them.

This is an excerpt of a text file for some packed images from my pet project:

As you can see on the first few lines is the name of the image which contains all the smaller images as well as some image type information. From blockBreakerTitle until the end is one of the images that was packed. It says where this smaller image can be found on the packed image as well as it’s size and offset. The final attribute “index” is to say which page the image is on. This package only has 1 image so -1 is used but if there was more images the index of the page it is on would be used.

Ah ok, i think i got it.

atlas.findRegion(name, index) // now i understand for what the index is.

I only load one txt file into the assetmanger for all my images, right?

Ok, but now i dont understand why using the assetmanger? Why not using at the programm start only TextureAtlas? Once loaded no need of assetManager.

The assetManager not only loads image files, but sounds, music, particleEffects, fonts, skins etc. It also keeps track of all of the items it loads and won’t dispose of the item until all references of it are no longer needed as well as only loading a single item once even if asked to load it twice as it knows it already has one reference to that item. Finally it allows you to load items asynchronously so you can make cool loading screens with progress bars :slight_smile:

Thank you so much! :slight_smile:

At the moment i use more than one Assetmanager , one for music, one for images and so on. I will merge them into one assetmanager.