Game Development Reference
In-Depth Information
The constructor is pretty basic. It simply sets a few member variables. The destructor
frees every resource in the cache by making repeated calls to FreeOneResource
until there
'
s nothing left in the cache.
ResCache::ResCache(const unsigned int sizeInMb, IResourceFile *resFile )
{
m_cacheSize = sizeInMb * 1024 * 1024;
// total memory size
m_allocated = 0;
// total memory allocated
m_file = resFile;
}
ResCache::
˜
ResCache()
{
while (!m_lru.empty())
{
FreeOneResource();
}
SAFE_DELETE(m_file);
}
To initialize the resource cache, call the Init() method:
bool ResCache::Init()
{
bool retValue = false;
if ( m_file->VOpen() )
{
RegisterLoader(shared_ptr<IResourceLoader>(GCC_NEW DefaultResourceLoader()));
retValue = true;
}
return retValue;
}
Besides opening the resource file, a default resource loader is created and registered.
The RegisterLoader method simply pushes the loader onto the front of the loader
list. The idea is that the most generic loaders come last in the list and the most spe-
cific loaders come first. This scheme allows you to define a specific loader for a given
file but still use another loader of other files with the same extension.
To get the bits for a resource, you call GetHandle() :
shared_ptr<ResHandle> ResCache::GetHandle(Resource * r)
{
shared_ptr<ResHandle> handle(Find(r));
if (handle==NULL)
handle = Load(r);
Search WWH ::




Custom Search