The map of many 2D games is not a whole piece, but a small BMP image (such as 32x32) is spliced by arrangement, and these small images are numbered 0, 1, 2, 3, and so on. In this way, you can write a file to store a map. For example, if a map file is opened, it is like this:
111111111111111111111
110000000000000000011
112222222222222222211
112222222220000000011
113333333333333333311
110000000000000000011
112222222222222222211
Then we can use ifstream to read the data from the file to the array map [] []. however, the map path parameter is a constant. You can read a map and it will not work if you pass through the map, but it does not matter. We can create another array to store the map path.
The following code is written to me:
# Include <iostream>
# Include <fstream>
# Include <string>
# Include <stdio. h>
Using namespace STD;
Char s [10];
Int mapindex = 2;
Int main ()
{
Sprintf (S, "map // % d. Map", mapindex );
Const char * mappath = s;
Ifstream infile (mappath );
Char map [34] [42]; // row (Y axis) = 34, column (X axis) = 42
String STR;
For (INT y = 0; y <34; ++ y)
{
If (Getline (infile, STR )){
Memcpy (Map [Y], str. c_str (), 42); // The second parameter must be a constant pointer. c_str () can change the string variable to a constant of char.
}
}
For (INT y = 0; y <34; ++ y)
{
For (INT x = 0; x <42; ++ X ){
Cout <map [y] [x];
}
Cout <Endl;
}
Cout <s;
Return 0;
}