Game Development Reference
In-Depth Information
also tracks the size of the memory block. If the resource cache gets full, the resource
handle is discarded and removed from the resource cache.
The destructor of ResHandle makes a call to a ResCache member, MemoryHas-
BeenFreed() . ResHandle objects are always managed through a shared_ptr
and can therefore be actively in use at the moment the cache tries to free them.
This is fine, but when the ResHandle object goes out of scope, it needs to inform
the resource cache that it is time to adjust the amount of memory actually in use.
There
'
s a useful side effect of holding a pointer to the resource cache in the ResHan-
dle : it is possible to have multiple resource caches in your game. One may control a
specific type of resource, such as sound effects, whereas another may control level
geometry and textures.
Most resources can be used exactly as they exist in the Zip file; they can be loaded
into memory and sent to whatever game subsystem needs them. Other resources
need to be processed when they are loaded. A resource might need a special decom-
pression method or processing to extract some important data from it. A good exam-
ple of this might be to store the length and format of a sound file. This is the reason
that the resource file defines loaders
classes that implement the IResourceLoader
interface.
IResourceLoader Interface and the DefaultResourceLoader
Here
'
s the definition of the IResourceLoader interface:
class IResourceLoader
{
public:
virtual std::string VGetPattern()=0;
virtual bool VUseRawFile()=0;
virtual unsigned int VGetLoadedResourceSize(
char *rawBuffer, unsigned int rawSize)=0;
virtual bool VLoadResource(char *rawBuffer, unsigned int rawSize,
shared_ptr<ResHandle> handle)=0;
};
The first method returns a wildcard pattern that the resource cache uses to distin-
guish which loaders are used with which files. You might define a loader for all
OGG files, if you wanted to decompress the music file, or all XML files, to parse
the XML data as the resource was loaded. The next method, VUseRawFile()
returns true if the resource loader can use the bits stored in the raw file, no extra
processing needed. The next two methods define the size of the loaded resource if it
 
 
Search WWH ::




Custom Search