Jump to content
  • Advertisement
Sign in to follow this  
tinkertom

Serialization libraries in C

This topic is 639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm programming a simple game in C with SDL and I'm trying to make a game save system. From what I understand I need to serialize the data to store the game variables in files.

So I was wondering if anyone knew of some good libraries for this. The libraries I've found so far are libtpl and protobuf-c. libtpl seems like it hasn't been updated in a while, and I don't really like google (though if it's the best option I'm happy to use protobuf-c).

So are there better libraries out there, or should I use one of the aforementioned?

Share this post


Link to post
Share on other sites
Advertisement

I would just use fprintf and fscanf and write a ToFile/FromFile function pair:

void ToFile( FILE* fp, data_t* data )
{
    fprintf( "%d ", data->a );
    fprintf( "%d ", data->b );
    fprintf( "%d ", data->c );
    fprintf( "%s\n", data->string );
}

void FromFile( FILE* fp, data_t* data )
{
    fscanf( "%d", &data->a );
    fscanf( "%d", &data->b );
    fscanf( "%d", &data->c );
    fscanf( "%.*s", MAX_CHARS, data->string );
}

Edited by Randy Gaul

Share this post


Link to post
Share on other sites

Thanks Randy. I didn't think to use that (I tend to overcomplicate).

Is this the most efficient method? The data is not overly complicated but there is and awful lot of it.

 

Share this post


Link to post
Share on other sites

Short answer is yes, this method is efficient, but no not the "most efficient".

 

This method should work pretty well for most all use cases. A good optimization might be to start dumping values in binary instead of in text, but this is a little more difficult and probably not necessary for many projects. In that case fwrite can be used instead of fprintf. But care must be taken to handle endianness (for example always read and write little-endian values, and on big-endian machines perform additional run-time byte swaps).

 

Also some people like to write a tool that outputs serialization functions automatically, as in, a tool that writes the ToFile and FromFile functions code. There are all kinds of ways to implement this stuff, and it can get very complicated. Personally I stick to what I described above.

Share this post


Link to post
Share on other sites

One little addition to what Khatharr sayed. I add an additional chunk id into my serializer/deserializer to make savegames backwards compatible. Adding new states to save go into a new chunk id so older versions of the game would read anything except that new parts and newer versions wouldnt conflict with it without any great overhead of code.

My utility class has just some templated global function definition

template<typename T> inline void Serialize(T& instance, IDataWriter& stream) //uses binary as default
{
  BinarySerializer bs;
  Serialize(instance, stream, bs);
}
template<typename T> void Serialize(T& instance, IDataWriter& stream, ISerializer& writer); //may choose a different serializer like JSON

And any class/type may create its own overload for the second global function so you may call Serialize(myObject, myStream); for whatever it is declared for.
 

ISerializer is an abstract class that implements some data handling like writing/reading JSON file format or simply binary for base types and strings as same as BeginChunk and EndChunk to declare sections. It is simple and works in just a few functions

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!