void Map::ParseText(const string &text) { // Create a tiny xml document and use it to parse the text. TiXmlDocument doc; doc.Parse(text.c_str()); // Check for parsing errors. if (doc.Error()) { has_error = true; error_code = TMX_PARSING_ERROR; error_text = doc.ErrorDesc(); return; } TiXmlNode *mapNode = doc.FirstChild("map"); TiXmlElement* mapElem = mapNode->ToElement(); // Read the map attributes. mapElem->Attribute("version", &version); mapElem->Attribute("width", &width); mapElem->Attribute("height", &height); mapElem->Attribute("tilewidth", &tile_width); mapElem->Attribute("tileheight", &tile_height); // Read the orientation std::string orientationStr = mapElem->Attribute("orientation"); if (!orientationStr.compare("orthogonal")) { orientation = TMX_MO_ORTHOGONAL; } else if (!orientationStr.compare("isometric")) { orientation = TMX_MO_ISOMETRIC; } // Read the map properties. const TiXmlNode *propertiesNode = mapElem->FirstChild("properties"); if (propertiesNode) { properties.Parse(propertiesNode); } // Iterate through all of the tileset elements. const TiXmlNode *tilesetNode = mapNode->FirstChild("tileset"); while (tilesetNode) { // Allocate a new tileset and parse it. Tileset *tileset = new Tileset(); tileset->Parse(tilesetNode->ToElement()); // Add the tileset to the list. tilesets.push_back(tileset); tilesetNode = mapNode->IterateChildren("tileset", tilesetNode); } // Find all layers and object groups. TiXmlNode *layerNode = mapNode->FirstChild(); while( layerNode != 0 ) { if( std::string("layer") == layerNode->Value() ) { // Allocate a new layer and parse it. TileLayer *layer = new TileLayer(this); layer->Parse(layerNode); // Add the layer to the list. layers.push_back(layer); } if( std::string("objectgroup") == layerNode->Value() ) { // Allocate a new object group and parse it. ObjectLayer *objectGroup = new ObjectLayer(this); objectGroup->Parse(layerNode); // Add the object group to the list. layers.push_back(objectGroup); } layerNode = layerNode->NextSibling(); } }