Game Development Reference
In-Depth Information
Working with in-memory iles
Sometimes it is very convenient to be able to treat some arbitrary in-memory runtime
generated data as if it were in a ile. As an example, let's consider using a JPEG image
downloaded from a photo hosting, as an OpenGL texture. We do not need to save it into
the internal storage, as it is a waste of CPU time. We also do not want to write separate
code for loading images from memory. Since we have our abstract iIStream and iRawFile
interfaces, we just implement the latter to support memory blocks as the data source.
Getting ready
In the previous recipes, we already used the Blob class, which is a simple wrapper around
a void* buffer.
How to do it...
1.
Our iRawFile interface consists of two methods: GetFileData() and
GetFileSize() . We just delegate these calls to an instance of Blob :
class ManagedMemRawFile: public iRawFile
{
public:
ManagedMemRawFile(): FBlob( NULL ) {}
virtual const ubyte* GetFileData() const
{ return ( const ubyte* )FBlob->GetData(); }
virtual uint64 GetFileSize() const
{ return FBlob->GetSize(); }
void SetBlob( const clPtr<Blob>& Ptr )
{ FBlob = Ptr; }
private:
clPtr<Blob> FBlob;
};
2.
Sometimes it is useful to avoid the overhead of using a Blob object, and for such
cases we provide another class, MemRawFile , that holds a raw pointer to a memory
block and optionally takes care of the memory allocation:
class MemRawFile: public iRawFile
{
public:
virtual const ubyte* GetFileData() const
 
Search WWH ::




Custom Search