The nessesarity of binary files

Until now AnKi used to read all the assets from text files. The mesh (containing the geometry), the skeleton, the skeleton animation, the light properties, the materials etc etc were all text files. Text files are pretty nice for debugging and importing but they contain lots of unnecessary information and they take lots of time to load, not to mention the size. If we take into account that some of the AnKi assets are automatically generated we see that text files are simple inadequate for some cases.

For that reason we’ve created a versatile class called BinaryStream. Binary stream inherits the well known C++ iostream which allows the class to behave as a text stream as well. On top of that there are a few methods that extract binary data from binary files. It can extract unsigned integers (32bit), floats (32bit) and special strings. One thing to note about the numbers is the endianness also known as byte order. Byte order is a common problem when it comes to memory representation of numbers. BinaryStream class addresses this issue by taking into account the file’s byte order as well as the machine’s byte order. The strings are a special case of strings that resemble Pascal strings. The first 32bits are the string’s length and the rest is the following characters.

AnKi binary files also contain a header before everything. The header is an 8byte identifier and it ensures that we are reading the right file type. For example meshes have the “ANKIMESH” and the skeletons the “ANKISKEL”.

Here is a sample of how we use the BinaryStream in mesh loading:

// Open the file
std::fstream file(filename, std::fstream::in | std::fstream::binary);

if(!file.is_open())
{
	throw EXCEPTION("Cannot open file \"" + filename + "\"");
}

BinaryStream bs(file.rdbuf());

// Magic word
char magic[8];
bs.read(magic, 8);
if(bs.fail() || memcmp(magic, "ANKIMESH", 8))
{
	throw MESH_EXCEPTION("Incorrect magic word");
}

// Mesh name
std::string meshName = bs.readString();

// Material name
std::string materialName = bs.readString();
if(materialName.length() > 0)
{
	material.loadRsrc(materialName.c_str());
}

// Verts num
uint vertsNum = bs.readUint();
vertCoords.resize(vertsNum);

If you want suggestions of how to load binary files check the BinaryStream in the SVN repository

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>