Sign in to follow this  
Aliii

How to write game-data into file?

Recommended Posts

Aliii    1456

When saving game data, how should one write floats for example into the file? Someone quicksaves the game then later loads it back. Should I assume that the save-load will be on the same device or on devices with the same float format/size, same endianness, ...and write it in binary? Or divide it into two integers - mantissa and exponent - and save those in binary? Or convert it to text and parse back to float when loading?

Share this post


Link to post
Share on other sites
haegarr    7372

Saving a floating point number can losslessly be done only without conversion to a decimal number format. This means: Store it either binary "as is" or else the equivalent bit pattern, perhaps hex number, as text. The latter one can be done by reading a float as uint32_t, and a double as uint64_t, perhaps by using a union.

 

It is very likely that a stored game will be reloaded on the same machine. If you want to be safe, you can write an endianess mark at the beginning of the file (so do I). The format of binary floating point number should be no problem, as long as you stick with the 32 and 64 bit numbers and don't try to use any extended format (e.g. the 80 bit internal format).

 

My preference is saving binary data, if possible even loadable "in-place".

Share this post


Link to post
Share on other sites
Glass_Knife    8636

See this for a discussion of testing endianness:

 

http://stackoverflow.com/questions/4239993/determining-endianness-at-compile-time

 

Since many languages like Java always assume big-endian, and everything sent over the network is assumed big-endian, I would always save the file in big-endian, the then check if the current machine needs to convert the values.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this