Sign in to follow this  
  • entries
    19
  • comments
    29
  • views
    11146

Resource Manager

Sign in to follow this  
mcguile25

94 views

I've been pretty dang efficient as of late in terms of programming and getting things done for the game. I just finished implementing a resource manager for the game. It turned out very well and it wasn't as hard as I thought it was going to be:

I have a base class for resources called CResource which every resource will derive from. A resource holds a name (filename) and a reference count. So that was pretty easy. Next I've got a templated resource manager called CResourceManager. This class allows for creating and retrieving resources and releasing them. Like DirectX, the resource manager increments the reference count when you retrieve or create a resource and decrements it when you release a resource. When that count gets to 0, the manager deletes that resource from its memory pool. The other cool thing is, since this is a resource manager, is that the manager will return you a pointer to existing resource memory if you try to call Create on a resource that already exists. Here's the skeleton class for anyone interested:


template <class T>
class CResourceManager
{
private:

// this hash map represent our resource list
std::map m_resources;

public:

// destructor deallocates all dynamically created resources that remain
~CResourceManager();

// creates a resource given a filename; if it already exists this
// function simply returns the allocated resource, otherwise if it doesn't
// exist we create it and return the pointer
T *CreateResource( const char *resourceName );

// retrieves the resource given a resource name and increments
// the reference count on the resource; returns NULL if the resource doesn't exist
T *GetResource( const char *resourceName );

// releases resource with the given resource name if it's reference
// count has reached zero
void ReleaseResource( const char *resourceName );

// returns the number of resources we have allocated
int GetResourceCount();
};






Tomorrow I'm going to start redoing the way I am rendering objects. I need to sort my objects by texture so as only to call SetTexture when I really need to call it. I'm just glad that I got the resource manager implemented as it will save a ton of memory in the game (imagine having 50 walls in the game having the same texture, meaning 50 instances of the same texture -- UGH!).
Sign in to follow this  


1 Comment


Recommended Comments

What if you decide that you want your resources to be packed in ZIP archives? Or even some more complex? You'd probably have to rewrite/redesign a lot. I'd recommend looking at the Abstract Factory design pattern and redesign your resource manager.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now