SFML community forums

Help => General => Topic started by: kaB00M on November 25, 2011, 08:06:52 am

Title: Can sfml classes be serialized?
Post by: kaB00M on November 25, 2011, 08:06:52 am
So I can serialize classes compose of simple type, but can sfml classes be serialized?
Title: Can sfml classes be serialized?
Post by: Laurent on November 25, 2011, 08:28:11 am
If you mean "directly" (with memcpy of whatever), then no. But even simple types can't be serialized directly anyway, you always have to do it manually in order to avoid problems with type sizes of endianness. So it doesn't really matter if it's a complex or simple type, you have to write the serialization rules anyway.

hint: use boost.serialize
Title: Can sfml classes be serialized?
Post by: kaB00M on November 25, 2011, 08:40:04 am
I'm using ofstream and ifstream.

As always, thank you.
Title: Can sfml classes be serialized?
Post by: Laurent on November 25, 2011, 09:49:24 am
Quote
I'm using ofstream and ifstream

It's not a serialization interface, it just provides raw read/write. It doesn't help for types sizes and endianness. You should really use something dedicated to this task.
Title: Can sfml classes be serialized?
Post by: zeocyte on November 27, 2011, 07:32:26 am
Maybe this will help:
http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/index.html
Title: Can sfml classes be serialized?
Post by: Naufr4g0 on December 01, 2011, 01:28:36 pm
Quote from: "zeocyte"
Maybe this will help:
http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/index.html


I tried to use serialization with boost, but the simplest way to implement it in your program is to modify sfml code itself. I think there's a complex way to do serialization without modify sfml code, but IMO it doesn't worth it.
I suggest to use xml instead.
I use tinyxml lib to save/load my sfml objects. :)
Title: Can sfml classes be serialized?
Post by: Laurent on December 01, 2011, 01:45:23 pm
Quote
I tried to use serialization with boost, but the simplest way to implement it in your program is to modify sfml code itself. I think there's a complex way to do serialization without modify sfml code

Everything that is relevant to serialize is public, so why would you need to modify SFML itself?
Title: Can sfml classes be serialized?
Post by: Nexus on December 02, 2011, 04:43:29 pm
Quote from: "Naufr4g0"
I think there's a complex way to do serialization without modify sfml code, but IMO it doesn't worth it.
I don't see why this should be complex, either. Just provide the global function overloads required by Boost.Serialization.

On the opposite, the intrusive solution brings you trouble because your SFML version is incompatible to others, revision updates become complicated etc. I would avoid changing the library itself unless it's really necessary, and in this case it isn't.
Title: Can sfml classes be serialized?
Post by: Naufr4g0 on December 02, 2011, 06:07:52 pm
Quote from: "Laurent"

Everything that is relevant to serialize is public, so why would you need to modify SFML itself?


Probabily it's possible, but I dont know how. :)
So I used xml documents.

Quote from: "Nexus"
I don't see why this should be complex, either. Just provide the global function overloads required by Boost.Serialization.

How can I access a sfml function and override it? With a derived class?
Or maybe I don't understand what you want to mean...
Title: Can sfml classes be serialized?
Post by: Nexus on December 02, 2011, 06:52:05 pm
Why do you need to override SFML functions? Most of SFML's classes are not designed as base classes. Contrary to languages like Java, C++ doesn't intend inheritance to be the main and only mechanism to extend existing functionality. Alternative approaches are often far more flexible, the STL is a very good example.

Boost.Serialization doesn't enforce class modifications. For a non-intrusive serialization function, look at the documentation (http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/tutorial.html#nonintrusiveversion). Absolutely no need to modify SFML.
Title: Can sfml classes be serialized?
Post by: Naufr4g0 on December 02, 2011, 07:32:39 pm
Quote from: "Nexus"

Boost.Serialization doesn't enforce class modifications. For a non-intrusive serialization function, look at the documentation (http://www.boost.org/doc/libs/1_48_0/libs/serialization/doc/tutorial.html#nonintrusiveversion). Absolutely no need to modify SFML.


I knew that there was this non-intrusive method, but I preferred xml for simplicity (I've been using it for a while).
Anyway serialization could be my next choice. :)
Title: Can sfml classes be serialized?
Post by: Laurent on December 02, 2011, 07:47:31 pm
XML is serialization. It's just an output format -- other formats could be binary data, JSON, INI, ...

With a well designed library (such as boost.serialization) the serialization functions and the output format are completely separated. You only have to write your serialization functions once, in a generic way, and then you can output the result to many different formats.
Title: Can sfml classes be serialized?
Post by: Naufr4g0 on December 03, 2011, 12:14:13 pm
Quote from: "Laurent"
XML is serialization. It's just an output format -- other formats could be binary data, JSON, INI, ...

With a well designed library (such as boost.serialization) the serialization functions and the output format are completely separated. You only have to write your serialization functions once, in a generic way, and then you can output the result to many different formats.


Your remark is fully correct!
In my previous post with "serialization" I mean binary serialization, and with "using xml" I mean "using xml serialization without boost" :)