Game Development Reference
In-Depth Information
is different from the size stored in the file, and then how the resource is actually
loaded from the file.
Many resources in the Zip file require no processing at all, so it is convenient to load
them exactly as-is. This requires the definition of a DefaultResourceLoader .
class DefaultResourceLoader : public IResourceLoader
{
public:
virtual bool VUseRawFile() { return true; }
virtual unsigned int VGetLoadedResourceSize(char *rawBuffer, unsigned int rawSize) {
return rawSize; }
virtual bool VLoadResource(char *rawBuffer, unsigned int rawSize, shared_ptr<Re-
sHandle> handle) { return true; }
virtual std::string VGetPattern() { return
*
;}
};
There ' s not much to this class. Since the resource is loaded exactly as it exists in the
file, there
s not really anything to do. The IResourceFile interface has already
loaded the bits into memory, and the ResHandle already stores those bits. You
'
ll
see a more interesting implementation of the IResourceLoader interface in Chap-
ter 13,
'
Game Audio,
which loads WAV and OGG files.
ResCache : A Simple Resource Cache
Since most of the players are already on the stage,
it
'
s time to bring out the
ResCache class, an ultra-simple resource cache.
First, a few type definitions. While the resource is in memory, a pointer to the
ResHandle exists in two data structures. The first, a linked list, is managed such
that the nodes appear in the order in which the resource was last used. Every time
a resource is used, it is moved to the front of the list, so you can find the most and
least recently used resources.
The second data structure, an STL map, provides a way to quickly find resource data
with the unique resource identifier. The third defines a map to store the resource
loaders.
typedef std::list< shared_ptr <ResHandle > > ResHandleList;
typedef std::map<std::string, shared_ptr < ResHandle > > ResHandleMap;
typedef std::list< shared_ptr < IResourceLoader > > ResourceLoaders;
class ResCache
{
 
 
Search WWH ::




Custom Search