Sign in to follow this  
ckaos

Resource manager structure

Recommended Posts

ckaos    122
Hi everyone ! I have been slowly chuging along in implementing a resource manager for a simple game engine and i wondered if the following structure seems good to you : -i currently have only one , singleton pattern based resource manager that handles all my resources (returning a pointer to a CResource object if requested ) but i stumbeled across a bit of an impractical part :when i create a resource of a specific type in the manager, i have to have a switch for each new kind of resource . -so would i be better of keeping my central resource manager but encapsulate a certain number of "sub managers" per resource category (sound, text, 3dmodels etc) -or, given the fact that there aren't that many types of resources, should i just keep my current logic? Thanks in advance for any suggestions and criticism ! This is what i have for now ( works fine)
CResource* CResourceManager::get_resource(const string resourcePath,const int type)
{
  string tmpString;

  for (int i =0;i<resourceList.size();i++)
  {
     tmpString=resourceList[i]->get_fileName();
     if(resourcePath==tmpString)
     {
       return resourceList[i];
     }
  }
  if(type==0) //text
  {
      CText* tmpRes;
      tmpRes=new CText(resourcePath);
      tmpRes->load(resourcePath);
      resourceList.push_back(tmpRes);
      return tmpRes;
  }
  if(type==1) // objet3d
  {
      C3dObject* tmpRes;
      tmpRes=new C3dObject;
      tmpRes->load(resourcePath);
      resourceList.push_back(tmpRes);
      *logStream<<"C3dobject created"<<endl;
      return tmpRes;
  }
  if(type==2) //bitmap
  {
      CBitmap* tmpRes;
      tmpRes=new CBitmap;
      tmpRes->load(resourcePath);
      resourceList.push_back(tmpRes);
      *logStream<<"CBitmap created"<<endl;
      return tmpRes;
  }

};



PS: returning pointers is not really safe in this context, i understand, so i will most likely switch to returning smart pointers further down the road

Share this post


Link to post
Share on other sites
KulSeran    3267
I think the term you are looking for is "factory pattern"
but the idea would boil down to instead of an if statement you have a loop, and your manager has a "regesterResourceType()"
function.


CResourceManager::regesterAllocator ( Allocator *x )
{
allocators.push_back ( x );
}

CResource* CResourceManager::get_resource ( const string path, const int type )
{
for (int i =0;i<resourceList.size();i++)
{
tmpString=resourceList[i]->get_fileName();
if(resourcePath==tmpString)
{
return resourceList[i];
}
}

for ( int i=0; i<allocators.size(); i++ )
{
if ( type == allocators[i].type() )
{
CResource* tmpRes;
tmpRes=allocators[i].new_resource();
tmpRes->load(resourcePath);
resourceList.push_back(tmpRes);
return tmpRes;
}
}

}




But That out of the way, I'd also say you could benifit from a GUID system, and managed pointers(smart/auto/ect..)
so that you dont have to search for a string every time you want a resource.
and you dont have to worry
about the pointer returned from get_resource getting lost somewhere (ie removed before you wanted it to)

Share this post


Link to post
Share on other sites
jpetrie    13106
Quote:

when i create a resource of a specific type in the manager, i have to have a switch for each new kind of resource

Look into the "factory" design pattern. In particular, Loki has a solid implementation of an abstract factory.

In this post I cover the general idea behind the factory implementation, in a basic fashion. (I knew I'd find a use for it, even if it turns out the OP in that thread already knew what I was talking about!)

Share this post


Link to post
Share on other sites
Replicon    306
Does it technically count as a factory when you're caching results like in this case? I guess such nomenclature doesn't really matter, unless we start talking about splitting out the factory portion.

Share this post


Link to post
Share on other sites
Telastyn    3777
Maybe not, but you're invariably going to 'grow' the resource manager into handling resources from sources other than filename. Pulling a resource from file or server or some generation function or dependent on some skinning configuration... 'tis now a factory by most any definition (and one of the reasons some people dissuade from 'manager' style classes).

Share this post


Link to post
Share on other sites

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

Sign in to follow this