Saving to and loading from disk

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Saving to and loading from disk

Post 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
User avatar
Trask
ES Beta Backer
ES Beta Backer
Posts: 738
Joined: Wed Oct 29, 2008 8:17 pm
Current Project: Building a 2D Engine
Favorite Gaming Platforms: Sega Genesis and Xbox 360
Programming Language of Choice: C/C++
Location: Pittsburgh, PA
Contact:

Re: Saving to and loading from disk

Post by Trask »

Image


Sorry, had to.
MarauderIIC wrote:You know those people that are like "CHECK IT OUT I just made Linux run on this piece of celery [or other random object]!!"? Yeah, that's Falco, but with ES.
Dear god, they actually ported ES to a piece of celery!
Martin Golding wrote: "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Saving to and loading from disk

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Saving to and loading from disk

Post 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
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Re: Saving to and loading from disk

Post 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. :)
User avatar
captjack
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 50
Joined: Fri Sep 18, 2009 4:23 pm
Current Project: engine framework
Favorite Gaming Platforms: PC, XBox 360, PS3
Programming Language of Choice: C, C++
Location: Northern Virginia

Re: Saving to and loading from disk

Post 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
Post Reply