Say I have this Cache<typename Resource>
class template, which is a resource cache that contains an std::map<std::string, Resource>
, mapping strings to resources.
I can many different kinds of resources: sounds, textures, geometry etc. So I need to have something like a container of all of these caches:
struct ResourceManager {
Cache<Sound*> _soundCache;
Cache<Mesh*> _meshCache;
Cache<Texture2D> _textureCache;
};
A Mesh object might need to query Texture objects from the texture cache, so working with these caches is possible in two ways: Either pass a pointer to _textureCache
to every Mesh object, perhaps via its constructor (this approach is pretty clumsy), or define a ResourceManager object globally, so every class can use its caches all the time.
Has this issue ever been brought up for discussion? How should I deal with it?
What do most engines do? Where do they store these caches? Is it viable to do something like this? :
~GameEngine.h~
...
#include "ResourceManager.h"
//global object
resourceManager g_resourceManager;
class GameEngine {
...
};