Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been looking at ways to load data into a game. I even asked a question on stack overflow about reading binary data in a portable manner . The gist of the answer seemed to be that you can use the int types header file to specify an particular integer byte amount, but there's nothing really useful for floats, etc.

I then thought that you could use fixed point, rather than floating point, but I wanted to see how other games did it. Here is one example, a md2 loader: http://tfc.duke.free.fr/old/models/md2.htm . However, it seemed to assume that the size of various types would just work, and that they would be the same on the platform that made the md2, and the one that read it. This seemed to be fairly common in the code that I've seen so far.

So is it really common to just ignore possible different size issues, or have I just been looking at bad (or old) samples? If it's not, could you also demonstrate a better way to do it, or at least link to one?

Thank you.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

Portable? It's common to use the same binary loading code across different platforms, but to have the data creation pipeline output different data sets to match each platform.

share|improve this answer
add comment

Well, the problem is that a binary format is specific for a certain (or several) platforms, otherwise it wouldn't be a 'binary' format!

A non binary, say CSV with values as ASCII numbers or why not XML, could be read by 'any' platform that can read a file and do some simple number crunching.

share|improve this answer
add comment

Floats can be dealt with in an almost platform independent way by assuming that they are IEEE standard representations, which almost all platforms use.

From there you'll have to do another potentially non-portable operation - treat the memory holding the float as an integer and write that integer out. While I believe the standard doesn't actually support doing that portably, most implementations do allow it. Code would look something like:

 void write_float(uint8_t *buf, float x)
 {
    union float_uint_32 { uint32_t i; float f; };
    float_uint_32 value;
    value.f = x;
    write_uint32_t(buf, value.i);
 }

However as said in other answers, most games have tools that output binary data in a format specific to each platform to maximize loading performance. The quantity of CPU work required for loading data should be almost zero when you do that. Making it more portable slows things down.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.