3
\$\begingroup\$

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.

\$\endgroup\$

3 Answers 3

4
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .