Saturday, February 18, 2017

architecture - How to design an AssetManager?


What is the best approach to designing an AssestManager that will hold references to graphics, sounds, etc. of a game?


Should these assets be stored in a key/value Map pair? I.e. I ask for "background" asset and the Map returns the associated bitmap? Is there an even better way?


Specifically I'm writing an Android/Java game, but answers can be generic.



Answer



It depends on the scope of your game. An asset manager is absolutely essential for larger titles, less so for smaller games.


For larger titles you have to manage problems such as the following:




  • Shared assets - is that brick texture being used by multiple models?

  • Asset lifetime - is that asset you loaded 15 minutes ago no longer needed? Reference counting your assets to make sure you know when something is finished with etc

  • In DirectX 9 if certain asset types are loaded and your graphics device gets 'lost' (this happens if you press Ctrl+Alt+Del amongst other things) - your game will need to recreate them

  • Loading assets in advance of needing them - you couldn't build big open world games without this

  • Bulk loading assets - We often pack lots of assets into a single file to improve loading times - seeking around the disc is very time consuming


For smaller titles these things are less of an issue, frameworks like XNA have asset managers within them - there is very little point in re-inventing it.


If you find yourself needing an asset manager, there is no one-size-fits-all solution really, but I've found that a hash map with the key as a hash* of the filename (lowered and separators all 'fixed') works well for the projects I've worked on.


It is usually not advisable to hardcode filenames in your app, it is usually better to have another data format (such as xml) depict filenames to 'IDs'.




  • As an amusing side note, you normally get one hash collision per project.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...