🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Read/write map file

Started by
7 comments, last by ZomeonE 24 years, 5 months ago
I wan''t to create a map editor to my game, which works with tiles. What is the best way to read/write the map data to/from a mapfile? It would really helpfull if you could give me some small bits of code =)
Advertisement
If your tiles are objects, first write out the number of tiles, followed by all the data, then write the dims of the map, followed by all the data:

itn handle = open("nifty.map", O_CREAT / O_TRUNC / O_BINARY / O_RDWR, S_IWRITE / S_IREAD);

int temp = GetNumberOfTiles();
write(handle, &temp, sizeof(temp));
for (int q=0; q < temp; q++) {
WriteTile(handle); // save a tile struct to handle
}

int sizex = GetMapXDim(); write(handle, &sizex, sizeof(sizex));
int sizey = GetMapYDim(); write(handle, &sizey, sizeof(sizey));
for (int x=0; x < sizex; x++)
for (int y=0; y < sizey; y++)
WriteMapSpot(x,y,handle); // write out map coord (x,y)

close(handle);

Something like that. There are all sorts of things you could do to make the above code better, but that''s the general idea.


Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Thanks, great code! =)

What do I have to include to use open, write and close?
I think:

io.h
fcntl.h
sys\stat.h




Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
Thanks, the code works great!

I''ve just got a little question:

How do I read data from files? Using read(...)?
How does read work?
Ok, read seams to be working...but...
I use this to write the dimensions of the map in the beginning of the file:

int sizex = 100;
write(handle, &sizex, sizeof(sizex));
int sizey = 100;
write(handle, &sizey, sizeof(sizey));

and I use this to read the dimensions:

int sizex = read(handle, &sizex, sizeof(sizex));
int sizey = read(handle, &sizey, sizeof(sizey));

but it only reads a 4x4 map instead of, in mycase, a 100x100 map. What do you think is wrong?
Hmm I dont have any experince with the windows save and read file functions. So I recommend looking at fwrite and fread. They might be from dos and are old ,but they work for me.

As far as tile map data goes I normaly just have a array of tile structs. Something like this.

typedef struct map{
char type; //what type of tile is it grass, water, ect
int flag; //any flags walkable,teleport,whatever
}map;

map tilemap[sizeofworldx][sizeofworldy];

Then i can just write to file the size of the world and with one call write tilemap to file. And reading it back is done but with 3 calls to. Of course this is only if you want fixed sized maps. Dynamic sized maps you need to save differntly.
>int sizex = read(handle, &sizex, sizeof(sizex));
>int sizey = read(handle, &sizey, sizeof(sizey));

don''t set sizex and sizey to be the return value of read. read probably returns the number of bytes read, not what was in those bytes.

--If Train A leave San Francisco at 8:30am EST travelling 25mph and Train B leaves Chicago at 1:30pm MST travelling at 40mph, and they're 3000 miles apart when they start, what is the capital of Bulgaria?
Oh Yes, how could I be so stupid???

Thanks for that Strabbi!

This topic is closed to new replies.

Advertisement