Game Development Reference
In-Depth Information
is not a number. The only thing we have to be careful of here is that
the byte ordering in a Lightwave file is in Motorola order. This is not the
same as the byte ordering on an Intel machine, so if you are creating a
parser on an Intel machine then you will have to change the byte
The following shows three functions you can use when reading short,
int and floating-point values from a file that uses Motorola, sometimes
also called network, byte ordering. Each function uses the same strategy.
The function returns true or false depending on whether there was a file
read error. For a short we declare a 2-byte buffer, for an int or float we use
a 4-byte buffer. There are two parameters passed to the function: a file
pointer and a reference to a variable of the type we are reading. In
addition to creating an appropriately sized buffer, we also create a
character pointer that is initialized to the address of the variable reference
passed to the function. Then we read either 2 or 4 bytes from the file,
returning false if there was a read error. Finally, we flip the contents of the
buffer, storing the result in the passed variable using the character pointer
we created.
BOOL CLWObject::ReadShort(FILE *fp, unsigned short &s)
char buf[2], *cs = (char*)&s;
if (fread(&buf, 2, 1, fp)!=1) return FALSE;
cs[0]=buf[1]; cs[1]=buf[0];
return TRUE;
BOOL CLWObject::ReadInt(FILE *fp, int &i)
char rd[4], *ci = (char*)&i;
if (fread(rd,4,1,fp)!=1) return FALSE;
ci[0]=rd[3]; ci[1]=rd[2]; ci[2]=rd[1]; ci[3]=rd[0];
return TRUE;
BOOL CLWObject::ReadFloat(FILE * fp, float &f)
char rd[4], *cf = (char*)&f;
Search Nedrilad ::

Custom Search