0

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?

Peter Panne
  • 475
  • 1
  • 5
  • 15

1 Answers1

1

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.

noone
  • 19,520
  • 5
  • 61
  • 76
  • Ok thank you for this simple example...Another question about the Asset-Manager of libgdx. They use tasks...if you have a look at the code of the update method you will see that they call "nextTask". Why do they use these tasks? – Peter Panne Mar 07 '14 at 08:15
  • The update method is already for another purpose of the AssetManager, namely the loading of the assets. My example was just for storing and retrieving random assets. But the LibGDX AssetManager also has loading functionality. Those can be asynchronous and run in a different thread. Those threads get a Task which describes loading a texture, a sound, or a tiled map for example. – noone Mar 07 '14 at 08:44
  • Ok let me summarize some things and please tell whether I am right or not. The simplest possibility we have is to create a class "Assets" with only static members like this "public static Pixmap background". In our loading routine we load the assets like this "Assets.background = game.getGraphics.newPixmap("background.png", PixmapFormat.RGB565)". Now all assets are loaded and we can use them whenever and wherever we want. But what are the disadvantages? We run pretty fast out of memory because everything has to be loaded at the beginning disregarding when we want to use the specific asset. – Peter Panne Mar 07 '14 at 12:17
  • The second possibility is to use a hash map where we can add and remove assets like shown above. The advantage is we don’t need as much memory because we load just that what we need and we can dispose things we don’t need anymore. And on the other hand it is quite more flexible. These basic principles are what you call store and retrieve. Is that right? – Peter Panne Mar 07 '14 at 12:17
  • The libgdx assetmanager does it in another way. First you have to use the “load” method and then you have to call “update”. I think that is necessary to achieve the asynchronism. Right? After that your assets are available via the “get” method. If I have a look at the “get” method I see that the assets are basically stored in a hash map called “assets”. But when I go the other way around and have a look at the “load” and “update” method I don’t see how the assets will be stored in this hash map. In load they use an array “loadQueue” which gets a new member. – Peter Panne Mar 07 '14 at 12:18
  • But then in “update” “loadQueue” is not in use.That’s why I don’t understand how it works? – Peter Panne Mar 07 '14 at 12:19
  • `update` -> `nextTask` -> `updateTask` -> `addAsset` (which gets `task.getAsset` as a parameter) That's how the asset gets added to the map in the update call. – noone Mar 07 '14 at 21:30
  • I don't find addAsset. The funny thing is that it is in the documentation: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/AssetManager.html#method_summary. But not in the actual code. Are there different versions or am I stupid? – Peter Panne Mar 10 '14 at 09:27
  • @PeterPanne https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/assets/AssetManager.java#L445 That's the current sourcecode. Maybe you are using an older version. The API changes sometimes. – noone Mar 10 '14 at 11:08
  • My very last question: Why do they use "ObjectMap" for the members instead of "HashMap"? In my opinion "ObjectMap" is also just a hashmap. So it would have been easier to use the java stuff. Why do they wrote a hashMap on its own? – Peter Panne Mar 10 '14 at 18:31
  • To reduce the amount of new objects being created. The custom ObjectMap re-uses the iterators for example. Thus the garbage collector won't get invoked at all and there are no lags on mobile devices. PS: after all this discussion I would appreciate if you could at least give me an upvote... ;) – noone Mar 10 '14 at 19:05