Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Loading a map from a text file

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Loading a map from a text file
ImLeftFooted
Member #3,935
October 2003
avatar

Hm, well the best way i think is to put spaces inbetween each number and then use atoi.

Your map format would be something like:

0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0

and then you'd read the file into a char* and call atoi on that. And then increment past spaces.. yeah in C it kinda sucks. Heres the C++ version to parse the above with spaces.

ifstream in("map.txt");
ofstream out("output.txt");

for(int y = 0; y < 24; y++) {
 for(int x = 0; x < 24; x++) {
  in >> map[x][y];
  out << map[x][y];
 }
}

This code does the same as the C code in my last post did, except that it assumes there are spaces inbetween the numbers.

 1   2 


Go to: