How to load Pixmap/Texture into AssetManager?

568 Views Asked by At

I've come across those articles: https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/PixmapLoader.html http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/TextureLoader.html https://github.com/libgdx/libgdx/wiki/Managing-your-assets

And I still didn't find anything about how to load a Texture into the AssetManager. In my game I create a Pixmap and wrap it around a Texture. Then I dispose the Pixmap. Then I draw the Texture with SpriteBatch. Every Texture created in my game has been created by a Pixmap, so it's created in runtime. How do I load those Textures into the AssetManager?

The links above and everything I've come across only show how to load Textures from already available files.

1

There are 1 best solutions below

1
On BEST ANSWER

From your comment

AssetManager can be helpful to prevent taking up memory twice or more.

You probably don't need to use asset manager to prevent this from happening. Instead you can just add a conditional in your code that checks if the asset is already loaded e.g.

if(myTexture != null){
    // not loaded
    pmap = new Pixmap(10, 10,Format.RGBA8888);
    pmap.setColor(Color.RED);
    pmap.fill();
    myTexture = new Texture(pmap);
    pmap.dispose()
}else{
    // already loaded, do nothing (or add to count of times used)
}

If you still want to use asset manager you have a couple of options.

  1. You can save your current texture/pixmap images into actual files an load them normally with the asset manager. (simplest option)
  2. Extend the AssetManager (code) and add your own method to add your assets to the assets objectMap and assetTypes objectMap.