Game Development Reference
In-Depth Information
else
Update(handle);
return handle;
}
ResCache::GetHandle()
is brain-dead simple. If the resource is already loaded in
the cache, update it. If it
'
s not there, you have to take a cache miss and load the
resource from the file.
The process of finding, updating, and loading resources is easy.
n
ResCache::Find()
uses an STL map,
m_resources
, to locate the right
ResHandle
given a
Resource
.
n
ResCache::Update()
removes a
ResHandle
from the LRU list and promotes
it to the front, making sure that the LRU is always sorted properly.
n
ResCache::Free()
finds a resource by its handle and removes it from the
cache.
The other members,
Load()
,
Allocate()
,
MakeRoom()
,and
FreeOneResource()
,
are the core of how the cache works:
shared_ptr<ResHandle> ResCache::Load(Resource *r)
{
shared_ptr<IResourceLoader> loader;
shared_ptr<ResHandle> handle;
for (ResourceLoaders::iterator it = m_resourceLoaders.begin();
it != m_resourceLoaders.end(); ++it)
{
shared_ptr<IResourceLoader> testLoader = *it;
if (WildcardMatch(testLoader->VGetPattern().c_str(), r->m_name.c_str()))
{
loader = testLoader;
break;
}
}
if (!loader)
{
assert(loader && _T(
“
Default resource loader not found!
”
));
return handle;
// Resource not loaded!
}
unsigned int rawSize = m_file->VGetRawResourceSize(*r);