Example #1
0
void TMXLoader::loadMap(std::string mapName, std::string filePath)
{
    // String to hold file contents
    std::string fileContents = "";
    
    // Attempt to load file using provided file path
    bool fileLoaded = loadFile(filePath, fileContents);
    
    if (fileLoaded == true)
    {
        // Create new RapidXML document instance to use to parse map data
        rapidxml::xml_document<char> m_currentMap;
        m_currentMap.parse<0>((char*)fileContents.c_str());
        rapidxml::xml_node<> *parentNode = m_currentMap.first_node("map");
        
        // Add new TMXMap to m_mapContainer
        m_mapContainer[mapName] = std::unique_ptr<TMXMap>(new TMXMap());
        
        // Load the map settings, tilesets and layers
        loadMapSettings(m_mapContainer[mapName], parentNode);
        loadTileSets(m_mapContainer[mapName], parentNode);
        loadLayers(m_mapContainer[mapName], parentNode);
        loadObjectLayers(m_mapContainer[mapName], parentNode);

        
        std::cout << "TMXLoader: loaded map '" << mapName << "' from: '" << filePath << "' successfully" << std::endl;
    }
    else
    {
        std::cout << "TMXLoader: map '" << mapName << "' at '" << filePath << "' could not be loaded." << std::endl;
    }
}
Example #2
0
int Level::loadMap(std::string mapName, Graphics &gfx) {
	XMLDocument doc;

	if (loadMap(mapName, &doc))
		return 1;

	XMLElement *mapNode = doc.FirstChildElement("map");
	setSizes(mapNode);

	XMLElement *tileSetNode = mapNode->FirstChildElement("tileset");
	if (loadTileSets(tileSetNode, gfx))
		return 1;

	XMLElement *layerNode = mapNode->FirstChildElement("layer");
	while (layerNode) {
		XMLElement *dataNode = layerNode->FirstChildElement("data");
		while (dataNode) {
			XMLElement *tileNode = dataNode->FirstChildElement("tile");
			appendToTileList(tileNode);
			dataNode = dataNode->NextSiblingElement("data");
		}
		layerNode = layerNode->NextSiblingElement("layer");
	}

	XMLElement *objectGroupNode = mapNode->FirstChildElement("objectgroup");
	while (objectGroupNode) {
		const char *name = objectGroupNode->Attribute("name");
		if (strcmp(name, "collisions") == 0) {
			XMLElement *objectNode = objectGroupNode->FirstChildElement("object");
			loadCollisionRects(objectNode);
		} else if(strcmp(name, "spawn points") == 0){
			XMLElement *objectNode = objectGroupNode->FirstChildElement("object");
			loadSpawnObjects(objectNode);
		}
		objectGroupNode = objectGroupNode->NextSiblingElement("objectgroup");
	}
	
	return 0;
}