Why are you trying to load from a xml file?
I'm guessing you are using a map editor that outputs the map in this way.
I've found it's easy to load a simple plain text map in C++ using getline etc.
The load map function below is for a simple 2-d map however it will only load a certain map structure so you would have to find a map editor which creates a plain text map file and modify the code to load it.
I'm sure you could find map editor which will do this if you decide to do it this way.
I'm somewhat new to programming C++ and SFML so bear in mind the following code may not be the best way to do this or particularly correct though I hope it is, it works for me.
bool Map::loadMap(ImageManager* imageManager, std::string filename) {
std::ifstream mapfile;
mapfile.open (filename.c_str());
if ( mapfile.is_open() == false ) {
std::cout << "Unable to open map file: " << filename << "\n";
return false;
}
mapfile.ignore(3000,'#'); // should skip any notes at the top and find start
mapfile.ignore(1); // ignores # as above line stops before it
int imagenumber,width,height,tilew,tileh,temp; // to hold the map info
std::string line; // for storing each line of the map
std::string tile; // for storing individual tiles parsed from the line
std::string integer; // for storing the individual tile integers but as strings
imageManager->addResourceDirectory("resources/map/terrain/"); // image manager like on SFML website
mapfile >> imagenumber; // how mnany images to load
mapfile.ignore(1); // ignore newline
std::vector < std::string > tempVector; // to hold the filenames of the tile images
for ( int i = 0; i < imagenumber; i++) {
getline(mapfile, line); // get the currect line and store it in string line
tempVector.push_back(line); // add image name to the vector
}
mapfile >> width; // store map width height etc
mapfile >> height;
mapfile >> tilew;
mapfile >> tileh;
mapfile.ignore(1);
Tile temptile; // create an empty tile
// create map. Tile** m_MapTiles; - Member of class Map.
//needs to be deleted later on - handled in Map class destructor;
// allocation of map
m_MapTiles = new Tile*[height];
for(int i = 0; i < height; i++)
m_MapTiles[i] = new Tile[width];
// initialization of map
for(int j = 0; j < height; j++)
for(int i = 0; i < width; i++)
m_MapTiles[j][i] = temptile;
for ( int j = 0; j < height; ++j ) {
getline(mapfile, line);
std::stringstream ss1( line );
for ( int i = 0; i < width; ++i ) {
getline(ss1, tile, '|');
// in the string stream ss1 get the info in the string before the | and put it into string tile
std::stringstream ss2( tile ); // put the info in tile into string stream ss2
getline(ss2, integer, ':');
// in the string stream ss2 get the info in the string before the : and put it into string integer
std::stringstream ss3( integer ); // put the info in integer into string stream ss3
ss3 >> temp; // convert the string in ss3 to int and store it in the integer variable temp
temptile.setObjectParams(temp,i*tilew,j*tileh,tilew,tileh ); // set the tiles parameters
getline(ss2, integer); // get the info after the : in ss2 and put it into string integer
std::stringstream ss4( integer ); // put the info in integer into string stream ss4
ss4 >> temp; // convert the string in ss4 to int and store it in the integer variable temp
temptile.setWalkable(temp); // set the temp tiles walkable value based on this value
temptile.setTexture( imageManager->getResource( tempVector.at( temptile.getID() ) ) );
// getResource returns a reference to the texture given by the filename found in tempVector
// note set texture is a class Tile function and simply sets the sprite it contains to having this texture
m_MapTiles[j][i] = temptile; // finally add it to the map
}
}
mapfile.ignore(3000,'#'); // find end - somewhat unnecessary
mapfile.close(); // make sure to close the map file
return true;
}
Loads a map of this format
hash = start / finish, text before/after is ignored
structure:
hash
number of filenames following
filenames of images ...
...
MapW
MapH
TileW
TileH
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
hash
where 1:1 -> L:K
L = tile type - corresponding to the images at the start 0=first etc
K = walkable ( 0=no)
#
4
notile.png
grass.png
green.png
red.png
4
4
40
40
2:1|2:1|2:1|2:1|
2:1|1:0|1:0|1:0|
2:1|1:0|1:1|1:0|
2:1|1:0|1:0|1:0|
#