Saving to and loading from disk
Posted: Mon Oct 12, 2009 2:57 pm
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:
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
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();
}
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