Page 1 of 1

Saving to and loading from disk

Posted: Mon Oct 12, 2009 2:57 pm
by captjack
In a fit of eggheadedness, I managed to totally muck up my storage code. I was using hard coded game objects using .push_back() on five items. I wanted to migrate to using binary files primarily due to my work on the level editor.

See one of my earlier posts for another topic related to vectors of pointers.

However, after committing a cardinal sin of modifying source code without making a copy, I was left with completely buggered file I/O code.

Essentially I'm working with a vector of pointers to a class. I've watered it down to bare essentials to give you the gist:

Code: Select all

class MapObject : public Object {  // the inheritance isn't important, not all game objects are map objects
     ...
private:
     int x;
     int y;
     int layer;
     ...
};

class Map {
     void write_map(vector<MapObject*> mapObjs);
     void read_map(vector<MapObject*> mapObjs);
};

Code: Select all

Map::write_map(vector<MapObject*> mapObjs)
{
     mapFile.open(fileName.c_str(), ios::binary | ios::out);

     mapFile.write(...);     // this is the sticky part

     mapFile.close();
}
Everything worked fine with the hard coded examples, but now that I'm trying for disk storage it's all gone pear shaped. From what I've read so far, one can write plain-old-data structures directly to disk using streams. This technique, however, does not work if you use dynamic allocation. In other words I could have simply used write() on a vector<MapObject>, but I can't with a vector of pointers.

So that leaves me with the idea that I'll need to iterate through all the map objects in the vector to write them out. I worked on that for a few hours but caused more harm than good. Does anyone have any pointers (pardon the pun)?

-capt jack

Re: Saving to and loading from disk

Posted: Mon Oct 12, 2009 3:01 pm
by Trask
Image


Sorry, had to.

Re: Saving to and loading from disk

Posted: Mon Oct 12, 2009 4:09 pm
by K-Bal
captjack wrote: So that leaves me with the idea that I'll need to iterate through all the map objects in the vector to write them out.
What's wrong with that? You might also need to write out the number of objects, to be able to read the same number in again.

I would recommend using [put popular file streaming lib here, i.e. libXML], it will make your life easier.

Re: Saving to and loading from disk

Posted: Mon Oct 12, 2009 4:11 pm
by avansc
think about it... a pointer is just a think that points to some address in memory.
so if you run a program and then save a bunch of pointers, and then load that level again, it will have appeared that it worked flawlessly. thats because
circumstances have ensured that you can do that. because the program never ended, the memory stayed the same.

no if you load that up after the program has closed all bets are off.

id still go for a binary file format. but you would have to iterate through each object that is or has a pointer value.

http://elysianshadows.com/phpBB3/viewto ... ary#p28647

Re: Saving to and loading from disk

Posted: Mon Oct 12, 2009 4:21 pm
by captjack
K-Bal wrote:
captjack wrote: So that leaves me with the idea that I'll need to iterate through all the map objects in the vector to write them out.
What's wrong with that? You might also need to write out the number of objects, to be able to read the same number in again.

I would recommend using [put popular file streaming lib here, i.e. libXML], it will make your life easier.
Great idea. I'm wanting to use XML for config files and such so this is in line with a design goal. Although I'm not sure of doing my maps in that format, it's a worthwhile to pick this up now for the end result.

I've just read about the boost library, but I was hoping to keep this as standard as possible. I'm not familiar with boost so I can't say I'm eager to learn yet-another-library. However, the boost serialization library seems to do what I need (STL container serialization), and has an emphasis on code portability as an added bonus. It's might be what the doctor ordered. Time for a detour from my editor. :)

Re: Saving to and loading from disk

Posted: Mon Oct 12, 2009 4:26 pm
by captjack
That looks promising! I'll have to spend some cycles reading over your posts in that thread. The code looks like what I tried to do (and failed) on this first go-round of object serialization. Fingers crossed.

-capt jack