hi,
im working on a 2d RPG and recently have tried changing the way i read / write my map files. since i changed it so that the map file is now a whopping 1 meg each, i decided to switch to binary files (from 1 meg to 96k!). anyway, for some reason im having a problem reading in the file. the program crashes because one of the values i read in is something like 2 million, and then i go on to resize a vector to fit 2 million Tile's, which crash's the game. anyway, is there something special i have to do to get this to work that im missing? id appreciate it greatly if anyone could spot out what exactly im doing wrong... heres the Save() code which writes out the file:
void Map::Save()
{
//start the file out
ofstream fout(map_path.c_str(), ios::binary);
//the first thing we do, is write to this file, its own file path and handle
fout.write((char *)(&map_path), sizeof(map_path)); //fout << map_path <<' ';
fout.write((char *)(&map_handle), sizeof(map_handle));//fout << map_handle <<' ';
//first at the top of this map, we will write out how many MST's are on this map
int t = map_swap_tiles.size();
fout.write((char *)(&t), sizeof(t));
//now we will write out the handle and path to each MST, and its coordinates, and of course its source (repeating code here =()
for(int i = 0; i < t; i++)
{
vector<Map_Swap_Tile> &tmp = map_swap_tiles;
fout.write((char *)(&tmp.dst_path), sizeof(tmp.dst_path));
fout.write((char *)(&tmp.dst_handle), sizeof(tmp.dst_handle));
fout.write((char *)(&tmp.src_path), sizeof(tmp.src_path));
fout.write((char *)(&tmp.src_handle), sizeof(tmp.src_handle));
fout.write((char *)(&tmp.xpos), sizeof(tmp.xpos));
fout.write((char *)(&tmp.ypos), sizeof(tmp.ypos));
fout.write((char *)(&tmp.land_x), sizeof(tmp.land_x));
fout.write((char *)(&tmp.land_y), sizeof(tmp.land_y));
}
//first print out which tilemap this map uses...
fout.write((char *)(¤t_tmap), sizeof(current_tmap));//fout << current_tmap << ' ';
//now print out the height/width of this map
fout.write((char *)(&height), sizeof(height));//fout << height << ' ';
fout.write((char *)(&width), sizeof(width));//fout << width << ' ';
// now output how many NPC's are in the map, the NPC's script, and the NPC's location
t = npcs.size();
fout.write((char *)(t), sizeof(t));
//for each NPC output that npc's data
for(int i = 0; i < npcs.size(); i++)
{
//first the script
fout.write((char *)(&npcs.script_path), sizeof(npcs.script_path));
//now its position
fout.write((char *)(&npcs.xpos), sizeof(npcs.xpos));
fout.write((char *)(&npcs.ypos), sizeof(npcs.ypos));
}
//now print the ID #'s for the tiles along with any data with them
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
//write out how many layers this tile has
int p = current_map[y][x].layers.size();
fout.write((char *)(&p), sizeof(p));
//write out all data for each layer
for(int i = 0; i < p; i++)
{
fout.write((char *)(¤t_map[y][x].layers.x_map_loc), sizeof(current_map[y][x].layers.x_map_loc));
fout.write((char *)(¤t_map[y][x].layers.y_map_loc), sizeof(current_map[y][x].layers.y_map_loc));
fout.write((char *)(¤t_map[y][x].layers.z), sizeof(current_map[y][x].layers.z));
}
fout.write((char *)(¤t_map[y][x].flags), sizeof(current_map[y][x].flags));
}
}
//lastly close the file!
fout.close();
}
now, heres the code i use to load in a map.. it *should* be doing exactly what im doing in save....
void Map::Open()
{
//open the file
ifstream file_in(map_path.c_str(), ios::binary);
// Error Check
if(!file_in)
{
cout << "COULDNT OPEN MAP FROM INSIDE OF MAP::OPEN, FILENAME: " + map_path<<endl;
systm.Log("COULDNT OPEN MAP FROM INSIDE OF MAP::OPEN, FILENAME: " + map_path);
file_in.close();
return;
}
Clear_Before_Load();
//the first thing we do, is read to this file, its own file path and handle
file_in.read((char *)(&map_path), sizeof(map_path)); //file_in << map_path <<' ';
file_in.read((char *)(&map_handle), sizeof(map_handle));//file_in << map_handle <<' ';
int t;
//read how many MST's are on the map
file_in.read((char *)(&t), sizeof(t));//file_in << map_swap_tiles.size() << ' ';
vector<Map_Swap_Tile> &tmp = map_swap_tiles;
tmp.resize(t);
//now we will read out the handle and path to each MST, and its coordinates, and of course its source (repeating code here =()
for(int i = 0; i < t; i++)
{
file_in.read((char *)(&tmp.dst_path), sizeof(tmp.dst_path));
file_in.read((char *)(&tmp.dst_handle), sizeof(tmp.dst_handle));
file_in.read((char *)(&tmp.src_path), sizeof(tmp.src_path));
file_in.read((char *)(&tmp.src_handle), sizeof(tmp.src_handle));
file_in.read((char *)(&tmp.xpos), sizeof(tmp.xpos));
file_in.read((char *)(&tmp.ypos), sizeof(tmp.ypos));
file_in.read((char *)(&tmp.land_x), sizeof(tmp.land_x));
file_in.read((char *)(&tmp.land_y), sizeof(tmp.land_y));
}
//read in the tilemap path
file_in.read((char *)(¤t_tmap), sizeof(current_tmap));//file_in << current_tmap << ' ';
//now read in h / w
file_in.read((char *)(&height), sizeof(height));//file_in << height << ' ';
file_in.read((char *)(&width), sizeof(width));//file_in << width << ' ';
//grab how many NPC's are on the map
file_in.read((char *)(t), sizeof(t));
npcs.resize(t);
//for each NPC, read in that NPC's info
for(int i = 0; i < t; i++)
{
//first the script
file_in.read((char *)(&npcs.script_path), sizeof(npcs.script_path));
//now its position
file_in.read((char *)(&npcs.xpos), sizeof(npcs.xpos));
file_in.read((char *)(&npcs.ypos), sizeof(npcs.ypos));
}
//now print the ID #'s for the tiles along with any data with them
for(int y = 0; y < height; y++)
{
for(int x = 0; x < width; x++)
{
//grab how many layers this tile has
int p;
file_in.read((char *)(&p), sizeof(p));
//resize our tile to fit all the layers
current_map[y][x].layers.resize(p);
//read in all the data for each layer
for(int i = 0; i < p; p++)
{
current_map[y][x].layers.texture = this->tilemap_texture[0];
file_in.read((char *)(¤t_map[y][x].layers.x_map_loc), sizeof(current_map[y][x].layers.x_map_loc));
file_in.read((char *)(¤t_map[y][x].layers.y_map_loc), sizeof(current_map[y][x].layers.y_map_loc));
file_in.read((char *)(¤t_map[y][x].layers.z), sizeof(current_map[y][x].layers.z));
}
file_in.read((char *)(¤t_map[y][x].flags), sizeof(current_map[y][x].flags));
}
}
file_in.close();
}//end of open()
the part where things get screwed up, are in the Open function, towards the end, this line:
//grab how many layers this tile has
int p;
file_in.read((char *)(&p), sizeof(p));
//resize our tile to fit all the layers
current_map[y][x].layers.resize(p);
for some reason, it crashes here because im trying to resize the vector to something like 2,000,232,23.... this number should NOT be here! each tile can only have a max of 5 layers. ive traced it and it seems everything before this is read in properly, its just this one line thats screwed up...
thanks for any help!