Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can sfml classes be serialized?  (Read 7302 times)

0 Members and 1 Guest are viewing this topic.

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Can sfml classes be serialized?
« on: November 25, 2011, 08:06:52 am »
So I can serialize classes compose of simple type, but can sfml classes be serialized?



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can sfml classes be serialized?
« Reply #1 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
Laurent Gomila - SFML developer

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Can sfml classes be serialized?
« Reply #2 on: November 25, 2011, 08:40:04 am »
I'm using ofstream and ifstream.

As always, thank you.



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can sfml classes be serialized?
« Reply #3 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.
Laurent Gomila - SFML developer

zeocyte

  • Newbie
  • *
  • Posts: 9
    • View Profile
Can sfml classes be serialized?
« Reply #4 on: November 27, 2011, 07:32:26 am »
Everything is predetermined.

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Can sfml classes be serialized?
« Reply #5 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. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can sfml classes be serialized?
« Reply #6 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?
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Can sfml classes be serialized?
« Reply #7 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Can sfml classes be serialized?
« Reply #8 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...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Can sfml classes be serialized?
« Reply #9 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. Absolutely no need to modify SFML.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Can sfml classes be serialized?
« Reply #10 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. 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. :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can sfml classes be serialized?
« Reply #11 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.
Laurent Gomila - SFML developer

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
Can sfml classes be serialized?
« Reply #12 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" :)