0
votes

I got to that point when I have to replace all new Sprite(texture) with new Sprite(manager.get(imgPath,Texture.class)) in order to optimize sprites creating and do all the loading before starting the game.

The problem is that when I use a common AssetManager to load all textures I get black rectangles.

Still, using a new AssetManager() each time I need to load a Texture works fine (maybe because this manager is quickly garbadge-collected and doesn't have time to dispose the texture it created?) but that way there is no point of using it.

The setupBody() method below is called at the end of my sprite-body wrapper constructor.

the working code (yet useless) ─ one new AssetManager for each sprite:

protected void setupBody(String path){
    BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal(path));
    BodyDef bDef = new BodyDef();
    bDef.type = BodyDef.BodyType.DynamicBody;
    bDef.position.set(this.position);
    bDef.angle = this.rotation;
    body = mWorld.createBody(bDef);
    body.setUserData(this);
    FixtureDef fix = new FixtureDef();
    fix.density = 0.1f;
    fix.friction = 0f;
    fix.restitution = 0.5f;
    fix.filter.categoryBits = this.getCategory();
    fix.filter.maskBits = this.getMask();
    origin = loader.getOrigin("base"+identifier, 1).cpy();
    String imgPath = loader.getImagePath("base"+identifier);

    AssetManager manager = new AssetManager();
    manager.load(imgPath,Texture.class);
    manager.finishLoading();
    Texture baseSpriteTexture = manager.get(imgPath,Texture.class);
    Texture baseSpriteTexture = new Texture(imgPath);
    baseSprite = new Sprite(baseSpriteTexture);

    loadHighlightedSprite(loader, identifier);
    attachFixture(loader, identifier, fix);
}

the code keeping me sleepless ─ the idea is to have a single non-static manager :

protected void setupBody(String path){
    /*no changes before */
    String imgPath = loader.getImagePath("base"+identifier);

    AssetManager manager = SmallIntestineDemoGame.getAssets();

    manager.load(imgPath,Texture.class);
    /* no changes after */
}

SmallIntestineDemoGame$getAssets() :

public class SmallIntestineDemoGame extends Game {

    public AssetManager assets;



    @Override
    public void create() {
        setScreen(new GameScreen());
    }

    public static AssetManager getAssets() {
        return ((SmallIntestineDemoGame) Gdx.app.getApplicationListener()).assets;
    }

    public void setupAssets(){
        this.assets = new AssetManager();
        Texture.setAssetManager(assets);
    }

}

DEBUGGING :

Debugging my asset manager got me confused. size of values of values of assets map of manager gets stuck at 33 even if I'm adding more:


(source: hostingpics.net)

I tested both cases ─ the app with a new AssetManager() (followed by finishloading and all required stuff) for each sprite creation. AND with a unique asset manager passed to all sprite creations :

I set a breakpoint at draw method and there was almost no difference between the two sprites, except for sprite.texture.glhandle ! When all sprites textures are created using the same manager their textures glhandle is set to 0 and this explains the black rectangles. But when each sprite is created using a locally newly created asset manager, the glhandle has some proper value (2269).

1
You haven't shown enough of your relevant code to see what's wrong. Like where you load everything and where you acquire Texture references to make sprites with. - Tenfour04
I don't load everything yet. I just tried using one single AssetManager. For the moment I do load one by one (you'll see the finishLoading() inside setupBody() ). Actually setupBody() is where everything is done. The game starts by creating a lot of OrganPart instances. OrganPart is the sprite-body wrapper. It has a box2d body and a sprite. all initialized at the setupBody() called at the end of OrganPart constructor. - Achraf Amil
if you need more information I'm here. - Achraf Amil

1 Answers

0
votes
 Texture.setAssetManager(assets);

When you set AssetManager then you need to call update() method of AssetManager each time when your game resumed.

If you don't set the AssetManager, the usual managed texture mechanism will kick in, so you don't have to worry about anything.