Sunday 30 November 2008

Serialization of Teemu

One of the reasons why Teemu is taking so long to finish is that I'm doing some of the routines first time. One of them is serialization. The first and easiest place to check how the load and save works was the scoreboard. I don't actually know how other people do serialization, because I usually become confused when I read source code programmed by some random dude.

I was thinking that it would be easier to save the number values as text, because then you don't have to worry about types. Arrays are saved as raw arrays, because they would take more space if saved in text mode (numbers separated by comma). The file mode is binary and it's always containing the next block size loaded in one piece.

I'm using a helper class named Tar_Ball to save and load data for objects. It's a low level class which makes load and save routines in object's class clean:


void Score::Save(K_File *f)
{
Tar_Ball tb;

for (int t=0; t<Max_Player_Name_Len; t++) tb.Put_Char(name[t]);

tb.Put(death_reason);
tb.Put(killed_by);
tb.Put(score);

tb.Save(f);
}

void Score::Load(K_File *f)
{
Tar_Ball tb;

tb.Load(f);

tb.Get_Next_String(name, Max_Player_Name_Len);

death_reason=tb.Get_Next_Value();
killed_by=tb.Get_Next_Value();
score=tb.Get_Next_Value();
}


Now I just have to save the world. Game world, to be more specific.