Better way to Manage Assets... Need a tip :)

Hi Guys… Im trying to optimize my game, and well, its going well :slight_smile:
Im usint TexturePacker2 to optimze the images in game, i dont know well how should i manage them or pack. But i was able to pack the images and gen the atlas.

Anyway,
Heres the Class that i built following tutorials :

package br.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;


public class AssetManagement implements Disposable {

    private static final AssetManagement assetManagement = new AssetManagement();

    public static AssetManagement getAssetManager() {
        return assetManagement;
    }
    
    private AssetManager assetManager;

    
    public AssetManagement() {}

    
    public void init(AssetManager assetManager) {
        this.assetManager = assetManager;
        //load texture atlas
        assetManager.load("", TextureAtlas.class);
        //start loading assets and wait until finished
        assetManager.finishLoading();
    }

    @Override
    public void dispose() {
        assetManager.dispose();

    }

}

The tutorial suggests that i do this to load :

@Override
public void create () {

Assets.instance.init(new AssetManager());


@Override
public void resume () {
Assets.instance.init(new AssetManager());
}

Now i ask, if im creating a new AssetManager, and replacing the old one, the old AssetManager wont dispose what he already had… Wouldnt that cause memory leak?

I mean , in resume he creates a new AssetManager();

I was thinking in making in this way :

package br.assets;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;

public class AssetManagement implements Disposable {

    private static final AssetManagement assetManagement = new AssetManagement();
    private final AssetManager assetManager;

    public AssetManagement() {
        assetManager = new AssetManager();
    }

    public static AssetManagement getInstance() {
        return assetManagement;
    }

    public void init() {

        //load texture atlas
        assetManager.load("packed/archerresources.atlas", TextureAtlas.class);
        //start loading assets and wait until finished
        assetManager.finishLoading();
    }

    @Override
    public void dispose() {
        assetManager.dispose();

    }

}

When i pause/resume, i could do this :

public void resume() {
            AssetManagement.getInstance().init();
        }

        public void pause() {
            AssetManagement.getInstance().dispose();
        }

Am i thinking wrong or im correct?

Its 1 AM here so , im sorry if im lacking something lol

Creating a new AssetManager with out calling its dispose method will in eventual memory issues. Many of the objects that it loads are not lost during the pause/resume of a mobile application. Creating a new one will cause it to reload everything that is not lost during the context switch as well (I believe that this includes any sounds, music and non-image files that were loaded up, though some of these may require a bit of processing. I may be wrong about everything that is saved. I might be wrong about this, though!)

The AssetManager class already provides some support for the pause/resume. The AssetManager can be set to reload the assets that are required to load when a resume is called by doing: Texture.setAssetManager(AssetManager manager).

Then in your screen/application listener class, you can have the resume method either wait for the AssetManager to reload or you can cause it to switch to a loading screen until it’s complete (Look at the very end of: https://github.com/libgdx/libgdx/wiki/Managing-your-assets )

I understand…
But i want to use AssetManager with My TexturePack2… I already packed all my images , i just need to know whats the best way to load them and dispose…

Something simple like this. But, a lot depends on how you’re controlling things: whether you’re just implementing ApplicationListener or making use of Game and Screens.


public class MainClassName implements ApplicationListener {
private final AssetManager assetManager;


public void create() {
assetManager = new AssetManager();
Texture.setAssetManager(assetManager);
// ... Load operations here.
}

public void resume() {
if (assetManager.update()) {
doneLoading();
}
}

public void render(float delta) {
if (assetManager.update()) {
doneLoading();
} else {
// ... Draw Loading thing!
}
}

public void dispose() {
assetManager.dispose();
}

public void doneLoading() {
// ... Get your texture assets here.
// ... Change to your main game screen.
}

}

I see. I just want to create a class that i can call from any class to get an asset that i need. Like an image or sound.

I generated a .atlas with texturepacker 2, so now i want a class to manage all that.