MobileDragon Resource file API

MobileDragon based game is supposed have access to it’s persistent resources like images, sounds, 3d models and user’s custom data files. MobileDragon way is to store all those files compressed in single packed file. Such a file can be created using supplied command-line utility packdir.exe
See packdir utility description for more information how to prepare the compressed file.

Access to this compressed storage provided through PackDir class interface.

Instance of PackDir object is created by the MobileDragon core, it opens the compressed storage file and prepares it for reading, and pointer to that PackDir instance is stored in the system object, all this is done before MDGame::Init() method is called. Name of the packdir file to open is set in MDGamePreferences structure [ see MobileDragon Basic Application Structure] .

Working with this packed resource is very simple. API allows you to check if there is specified file in packdir, allows to determine that file size and to load that whole file into memory.

Suppose you have mdragon::System * system variable pointing to valid MobileDragon system object therefore you can obtain pointer to PackDir object by calling

PackDir * pd = system->GetPackDir();

Let’s for example load the file “data.bin” from the PackDir

// Obtain pointer to the PackDir object
PackDir * pd = md_system->GetPackDir();

// Find the file "data.bin" in PackDir
// FindItem returns an id of that file or -1
// if there is not such file found.

Int id = pd->FindItem( "data.bin" );

// Determine that file size, allocate memory for it and load.
size_type size = pd->GetItemSize( id );
Byte data * = new Byte[ id ];
pd->LoadItem( id, data );

/* ... some data processing stuff here */

//release memory
delete data;


// or another way to load the file is to load it into the Resource object.
id = pd->FindItem( "data2.bin" );
Resource res;
pd->LoadItem( id, res );

/* ... some data processing stuff here */


Note that PackDir uses case-sensitive comparison on file names, therefore it is recommended to keep all file names in lowercase, to avoid situations like calls to
pd->FindItem ( "data2.bin" ) and pd->FindItem ( "Data2.bin" ) provide different results.

packdir utility packs directory contents including subdirectories. To load file “sample.txt” residing in subdirectory “dir” you need supply it’s full name
pd->FindItem( "dir\\sample.txt" );

The Resource class represents a binary resource in memory. Using it simplifies parsing your binary data, it allow simple retrieving of data of all basic numeric types and string.

Note that you must use this API only when loading your custom data files, all MobileDragon objects like Sound, Music, Image, Texture, etc. loaders interrogate with PackDir themselves.

 

 

Copyright 2005-2006 Herocraft Hitech Co. Ltd.