Search code examples
javaandroidlibgdxasset-management

I look for an explanation of libgdx's asset manager


I would like to create an asset-manager. I know the asset-manager from "libgdx" but I don't wanna use libgdx because I am just programming for studying purposes. So I wanna understand my entire program and write everything from scratch.

Now I need an asset-manager because I ran out of memory. I took a look at the asset-manager of libgdx but it is pretty much code and I don't understood it completly.

I would like to know what is the basic principle/idea of libgdx's asset-manager. I know that it works with hashmaps and in its own thread to be asyncron. But that is pretty much all I know.

Could you help me?


Solution

  • There isn't much more to know, other than that it's using Maps to store and retrieve all assets.

    Basically you have something like this:

    class AssetManager {
    
        private Map<String, Object> assets = new HashMap<String, Object>();
    
        public void storeAsset(String key, Object asset) {
            assets.put(key, asset);
        }
    
        public <T> T getAsset(String key, Class<T> clazz) {
            return (T) assets.get(key);
        }
    
        public void freeAsset(String key) {
            assets.remove(key);
        }
    }
    

    That generic getter is optional, you could also do the casts yourself, but like this it's more convenient. Of course there is error handling and everything missing, but that's how a very very basic AssetManager might work.