Game Development Reference
In-Depth Information
n ResHandle , a handle to track loaded resources
n ResCache , a simple resource cache
IResourceFile Interface
A resource file should be able to be opened and closed and provide the application
programmer access to resources. Here
'
s a simple interface that defines just that:
class IResourceFile
{
public:
virtual bool VOpen()=0;
virtual int VGetRawResourceSize(const Resource &r)=0;
virtual int VGetRawResource(const Resource &r, char *buffer)=0;
virtual int VGetNumResources() const = 0;
virtual std::string VGetResourceName(int num) const = 0;
virtual ~IResourceFile() { }
};
There are only five pure virtual functions to implement. I told you it was simple. The
implementation of VOpen() should open the file and return success or failure based
on the file
'
s existence and integrity. VGetRawResourceSize() should return the
size of the resource based on the name of the resource, and VGetRawResource()
should read the resource from the file. The VGetNumResources() method should
tell you how many resources are in the file, and the VGetResourceName() method
should tell you the name of the nth resource. The last two methods enable you to
iterate through every resource by number or by name.
The accompanying source code implements the IResourceFile interface with a
ZipFile implementation. This is a convenient file format since it is supported by
so many off-the-shelf and open source tools on many platforms. This is a great
example of using interfaces to hide the technical implementation of something
while maintaining a consistent API. If you wanted to, you could implement this
interface using a completely different file structure, like CAB or WAD.
ResHandle : Tracking Loaded Resources
For the cache to do its work, it must keep track of all the loaded resources. A useful
class, ResHandle , encapsulates the resource identifier with the loaded resource data:
class ResHandle
{
 
 
 
Search WWH ::




Custom Search