it's been a while since I posted some thread, and recently I've come to another nice problem - which needs a bit of optimization...
Managing resources (graphics ones - textures, models, skinned meshes, skinned anims, etc.) in game engine is not easy (especially in open-worlds) - as for now I've tried more ways of managing them, some better some worse. As for now the best I tried is this:
Basically you have a map containing \ where string is name of resource and pointer is pointer to Templated class (e.g. manager is re-usable template class - where you supply Class on which it will operate). Also I store number of items and "reference counter" for each item.
So basically the class looks like this:
template <class T>
unsigned int* mRefCounts;
unsigned int mCount;
unsigned int mSize;
map<string, T*> mDb;
Where mData are actual data stored, mSize is current number of allocated items (reallocates all buffers to 2-times the size, when mCount == mSize), mCount is current number of used items, mRefCounts is array which contains reference counter for each item. mDb is actual "database" connecting string and pointer to object.
While this is good (imho) - loading method returns pointer to loaded item (if names match), otherwise schedule async load to "loading thread" (and returns address of new blank resource, till async loader swaps that address with address of loaded resource) - in both cases it actually increases reference counter for that item.
When one removes resource, it just decreases its reference counter (and if zero - then also frees that item - e.g. delete texture for textures, etc.).
So far seems okay, right?
This manager is dynamic, handles multiple same resources at time without problems, and doesn't produce any garbage, it's also pretty fast - because once you got your pointer to item - you just operate with the pointer to item, so you won't call manager until you want to remove that item.
Yeah, but! I still wonder whether there are better ways (of course there are) for dynamic world? And whether they're worth implementation, or this way is good enough.
Note: I know I could use F.e. hash from those strings instead of actually using strings, etc. - those are just details in this technique
- e.g. same technique, using little different types, thats all. I wonder whether there is another good technique.
Also if you have some your way to manage resources, feel free to write it out here . I'd be glad.