Game Development Reference
In-Depth Information
}
assert(loader && _T(
Default resource loader not found!
));
return handle;
// ResCache is out of memory!
}
The first thing that happens in Load() is the right resource loader is located in the
STL list. The utility function WildcardMatch() returns true if the loader
s pattern
matches the resource name. WildcardMatch() uses the same matching rules as the
CMD window in Microsoft Windows, so * matches everything, *.JPG matches all
JPG files, and so on. If a loader isn ' t found, an empty ResHandle is returned.
Then the method grabs the size of the raw resource from the resource file and allo-
cates memory for the raw resource. If the resource doesn
'
t need any processing, the
memory is allocated from the cache through the Allocate() method; otherwise, a
temporary buffer is created. If the memory allocation is successful, the raw resource
bits are loaded with the call to VGetRawResource() . If no further processing of the
resource is needed, a ResHandle object is created using the pointers to the raw bits
and the raw resource size.
Other resources need processing and might even be a different size after they are
loaded. This is the job of a specially defined resource loader, which loads the raw
bits from the resource file, calculates the final size of the processed resource, allocates
the right amount of memory in the cache, and finally copies the processed resource
into the new buffer. You
'
ll learn more about this in Chapter 13, which discusses
using the resource system to create sound resources.
After the resource is loaded, the newly created ResHandle is pushed onto the LRU
list, and the resource name is entered into the resource name map.
Next up is the Allocate() method, which makes more room in the cache when it
is needed.
'
char *ResCache::Allocate(unsigned int size)
{
if (!MakeRoom(size))
return NULL;
char *mem = GCC_NEW char[size];
if (mem)
m_allocated += size;
return mem;
}
Search WWH ::




Custom Search