Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualNoxil

Posted 29 October 2013 - 05:17 PM

This can surely be done in a simple manner.

// Base class for all resources
class Resource
{
public:

    virtual void Load(const std::string & sourcePath);
    virtual void Unload();
};

// Resource manager
class ResourceManager
{
    std::map< std::string, Resource * > m_Resources;
public:

    template< class T >
    T * GetResource(const std::string & sourcePath)
    {
        if(m_Resources[sourcePath] != 0)
        {
            return dynamic_cast< T * >(m_Resources[sourcePath]);
        }

        T * newT = new T();

        newT->Load(sourcePath);
        m_Resources[sourcePath] = newT;
        return newT;
    }
};

So what happens is when you use the resource manager to get a resource.
It will first check to see if we have already loaded the resource and try to return that one.
If we havent loaded it, load it and store it so we later on can get it.
 

void Engine::Init()
{
    ResourceManager * resourceManager = GetResourceManager();
    TextureResource * textureResource = resourceManager->GetResource< TextureResource  >( "path/to/texture.png" );
}

As you can see above, its very straight forward how to use it smile.png

What is also worth mentioning is that in this example im using std::map to store the resources. You should really experiment with different types of storage to find whats best for your needs. 


#1Noxil

Posted 29 October 2013 - 05:03 PM

This can surely be done in a simple manner.

This is how i have done it in my engine.

 

// Base class for all resources
class Resource
{
public:
virtual void Load(const std::string & sourcePath);
virtual void Unload();
};
 
// Resource manager
class ResourceManager
{
std::map< std::string, Resource * > m_Resources;
public:
 
template< class T >
T * GetResource(const std::string & sourcePath)
{
 
}
};

PARTNERS