LibGDX - How many links were counted in my case's AssetManager?

I created an AssetManger class called AssetsTest.

    package com.program.mydemo;

    import com.badlogic.gdx.assets.AssetManager;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.TextureAtlas;


    public class AssetsTest {

        public static AssetManager manager = new AssetManager();

        public static void load() {     
            manager.load("bgscreen.png", Texture.class);
            manager.load("menu.pack", TextureAtlas.class);      
        }

        public static void unload() {
            manager.unload("bgscreen.png");
            manager.unload("menu.pack");        
        }

        public static boolean update(){     
            return manager.update();
        }

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

    }

      

I also created another class called MyDemo

public class MyDemo extends Game {

    private SpriteBatch batch;
    private Sprite spriteStart1, spriteStart2;
    private Texture texture1, texture2;
    private TextureAtlas atlas1, atlas2;
    private TextureRegion startRegion1, startRegion2;
    private Camera camera;
    private int refCount;

    public void create () {

        batch=new SpriteBatch(); 
        camera = new OrthographicCamera(500,700);

        AssetsTest.load();
        AssetsTest.manager.finishLoading();

        texture1 = AssetsTest.manager.get("bgscreen.png",Texture.class);
        atlas1 = AssetsTest.manager.get("menu.pack", TextureAtlas.class);
        startRegion1 = atlas1.findRegion("startbutton");
        spriteStart1 = new Sprite(startRegion1);
        spriteStart1.setPosition(-210/2,-150);      

        texture2 = AssetsTest.manager.get("bgscreen.png",Texture.class);
        atlas2 = AssetsTest.manager.get("menu.pack", TextureAtlas.class);
        startRegion2 = atlas2.findRegion("startbutton");
        spriteStart2 = new Sprite(startRegion2);
        spriteStart2.setPosition(-210/2,-150);      

        refCount = AssetsTest.manager.getReferenceCount("bgscreen.png");
        System.out.println(refCount);

//      AssetsTest.unload();    

//      AssetsTest.load();
//      AssetsTest.manager.finishLoading();         


    public void render () {
        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.draw(texture2, -500/2,-700/2);
        spriteStart2.draw(batch);
        batch.end();
    }   

    @Override
    public void dispose () {
        super.dispose();
        batch.dispose();
        texture1.dispose();
        AssetsTest.dispose();
    }
}

      

Then I created two texture references (texture1 and texture2) for AssetsTest.manager.get (...);

RefCount is 1. Why?

It should be "2" because I have two references (texture1 and texture2).

If I try to call AssetsTest.load (); twice, I get refCount 2. Calling twice from AssetsTest.load (); should create two objects and store them in a different memory address. Is my concept correct?

+3


source to share


1 answer


Directly from libgdx documentation:

Assets are reference counted. If two assets A and B both depend on another  
asset C, C won't be disposed until A and B have been disposed. 

      

This also means that if you load an asset multiple times, it will actually split and only take memory once!

you download

texture1 = AssetsTest.manager.get("bgscreen.png",Texture.class);

      

and then



texture2 = AssetsTest.manager.get("bgscreen.png",Texture.class);

      

both texture1 and texture2 point to the same texture, which is shared and only takes up memory once. This is why you are getting ref count.

Update

Yes, texture1 and texture2 point to the same object ("bgscreen") that is shared. Therefore, in a sense, they are reference variables that point to the same memory address. Once the item is loaded into the asset manager, it can be reused / split in this way. In your case, this means that no new object will be created every time you refer to "bgscreen.png". This way you will always get ref count.

Regarding the second part of your question. To clarify a few things: Assetmanager.load doesn't load any assets in its path . The method highlights assets . As soon as you call manager.update () or manager.isFinished only then the assets you specified in the load method will be loaded into memory. Therefore, if you call the load twice from the same texture, then when you call manager.update (), you will get two references of the same object ("bgscreen.png"), which is not needed because what you usually want , has only one general reference of the same object for efficiency. If this answers your question, please don't forget to accept the answer.

+2


source







All Articles