示例#1
0
	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();
		}
	}
示例#2
0
    void Map::Parse(tinyxml2::XMLNode *mapNode)
    {
        tinyxml2::XMLElement* mapElem = mapNode->ToElement();

        // Read the map attributes.
        version = mapElem->IntAttribute("version");
        width = mapElem->IntAttribute("width");
        height = mapElem->IntAttribute("height");
        tile_width = mapElem->IntAttribute("tilewidth");
        tile_height = mapElem->IntAttribute("tileheight");
        next_object_id = mapElem->IntAttribute("nextobjectid");

        if (mapElem->Attribute("backgroundcolor"))
        {
            background_color = mapElem->Attribute("backgroundcolor");
        }

        // 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;
        }
        else if (!orientationStr.compare("staggered"))
        {
            orientation = TMX_MO_STAGGERED;
        }
        else if (!orientationStr.compare("hexagonal"))
        {
            orientation = TMX_MO_HEXAGONAL;
        }

        // Read the render order
        if (mapElem->Attribute("renderorder"))
        {
            std::string renderorderStr = mapElem->Attribute("renderorder");
            if (!renderorderStr.compare("right-down")) 
            {
                render_order = TMX_RIGHT_DOWN;
            } 
            else if (!renderorderStr.compare("right-up")) 
            {
                render_order = TMX_RIGHT_UP;
            }
            else if (!renderorderStr.compare("left-down")) 
            {
                render_order = TMX_LEFT_DOWN;
            }
            else if (!renderorderStr.compare("left-down")) 
            {
                render_order = TMX_LEFT_UP;
            }        
        }

        // Read the stagger axis
        if (mapElem->Attribute("staggeraxis"))
        {
            std::string staggerAxisStr = mapElem->Attribute("staggeraxis");
            if (!staggerAxisStr.compare("x"))
            {
                stagger_axis = TMX_SA_X;
            }
            else if (!staggerAxisStr.compare("y"))
            {
                stagger_axis = TMX_SA_Y;
            }
        }

        // Read the stagger index
        if (mapElem->Attribute("staggerindex"))
        {
            std::string staggerIndexStr = mapElem->Attribute("staggerindex");
            if (!staggerIndexStr.compare("even"))
            {
                stagger_index = TMX_SI_EVEN;
            }
            else if (!staggerIndexStr.compare("odd"))
            {
                stagger_index = TMX_SI_ODD;
            }
        }

        // read the hexside length
        if (mapElem->IntAttribute("hexsidelength"))
        {
            hexside_length = mapElem->IntAttribute("hexsidelength");
        }

        // read all other attributes
        const tinyxml2::XMLNode *node = mapElem->FirstChild();
        while( node )
        {
            // Read the map properties.
            if( strcmp( node->Value(), "properties" ) == 0 )
            {
                properties.Parse(node);         
            }

            // Iterate through all of the tileset elements.
            if( strcmp( node->Value(), "tileset" ) == 0 )
            {
                // Allocate a new tileset and parse it.
                Tileset *tileset = new Tileset();
                tileset->Parse(node->ToElement(), file_path);

                // Add the tileset to the list.
                tilesets.push_back(tileset);
            }

            // Iterate through all of the "layer" (tile layer) elements.           
            if( strcmp( node->Value(), "layer" ) == 0 )
            {
                // Allocate a new tile layer and parse it.
                TileLayer *tileLayer = new TileLayer(this);
                tileLayer->Parse(node);

                // Add the tile layer to the lists.
                tile_layers.push_back(tileLayer);
                layers.push_back(tileLayer);
            }

            // Iterate through all of the "imagelayer" (image layer) elements.            
            if( strcmp( node->Value(), "imagelayer" ) == 0 )
            {
                // Allocate a new image layer and parse it.
                ImageLayer *imageLayer = new ImageLayer(this);
                imageLayer->Parse(node);

                // Add the image layer to the lists.
                image_layers.push_back(imageLayer);
                layers.push_back(imageLayer);
            }

            // Iterate through all of the "objectgroup" (object layer) elements.
            if( strcmp( node->Value(), "objectgroup" ) == 0 )
            {
                // Allocate a new object group and parse it.
                ObjectGroup *objectGroup = new ObjectGroup(this);
                objectGroup->Parse(node);
        
                // Add the object group to the lists.
                object_groups.push_back(objectGroup);
                layers.push_back(objectGroup);
            }

            node = node->NextSibling();
        }
    }
示例#3
0
    void Map::ParseText(const string &text) 
    {
        // Create a tiny xml document and use it to parse the text.
        tinyxml2::XMLDocument doc;
        doc.Parse(text.c_str());
    
        // Check for parsing errors.
        if (doc.Error()) 
        {
            has_error = true;
            error_code = TMX_PARSING_ERROR;
            error_text = doc.GetErrorStr1();
            return;
        }

        tinyxml2::XMLNode *mapNode = doc.FirstChildElement("map");
        tinyxml2::XMLElement* mapElem = mapNode->ToElement();

        // Read the map attributes.
        version = mapElem->IntAttribute("version");
        width = mapElem->IntAttribute("width");
        height = mapElem->IntAttribute("height");
        tile_width = mapElem->IntAttribute("tilewidth");
        tile_height = mapElem->IntAttribute("tileheight");
        next_object_id = mapElem->IntAttribute("nextobjectid");

        if (mapElem->Attribute("backgroundcolor"))
        {
            background_color = mapElem->Attribute("backgroundcolor");
        }

        // 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;
        }
        else if (!orientationStr.compare("staggered")) 
        {
            orientation = TMX_MO_STAGGERED;
        }
        

        // Read the render order
        if (mapElem->Attribute("renderorder"))
        {
            std::string renderorderStr = mapElem->Attribute("renderorder");
            if (!renderorderStr.compare("right-down")) 
            {
                render_order = TMX_RIGHT_DOWN;
            } 
            else if (!renderorderStr.compare("right-up")) 
            {
                render_order = TMX_RIGHT_UP;
            }
            else if (!renderorderStr.compare("left-down")) 
            {
                render_order = TMX_LEFT_DOWN;
            }
            else if (!renderorderStr.compare("left-down")) 
            {
                render_order = TMX_LEFT_UP;
            }        
        }

        const tinyxml2::XMLNode *node = mapElem->FirstChild();
        while( node )
        {
            // Read the map properties.
            if( strcmp( node->Value(), "properties" ) == 0 )
            {
                properties.Parse(node);         
            }

            // Iterate through all of the tileset elements.
            if( strcmp( node->Value(), "tileset" ) == 0 )
            {
                // Allocate a new tileset and parse it.
                Tileset *tileset = new Tileset();
                tileset->Parse(node->ToElement());

                // Add the tileset to the list.
                tilesets.push_back(tileset);
            }

            // Iterate through all of the "layer" (tile layer) elements.           
            if( strcmp( node->Value(), "layer" ) == 0 )
            {
                // Allocate a new tile layer and parse it.
                TileLayer *tileLayer = new TileLayer(this);
                tileLayer->Parse(node);

                // Add the tile layer to the lists.
                tile_layers.push_back(tileLayer);
                layers.push_back(tileLayer);
            }

            // Iterate through all of the "imagelayer" (image layer) elements.            
            if( strcmp( node->Value(), "imagelayer" ) == 0 )
            {
                // Allocate a new image layer and parse it.
                ImageLayer *imageLayer = new ImageLayer(this);
                imageLayer->Parse(node);

                // Add the image layer to the lists.
                image_layers.push_back(imageLayer);
                layers.push_back(imageLayer);
            }

            // Iterate through all of the "objectgroup" (object layer) elements.
            if( strcmp( node->Value(), "objectgroup" ) == 0 )
            {
                // Allocate a new object group and parse it.
                ObjectGroup *objectGroup = new ObjectGroup(this);
                objectGroup->Parse(node);
        
                // Add the object group to the lists.
                object_groups.push_back(objectGroup);
                layers.push_back(objectGroup);
            }

            node = node->NextSibling();
        }
    }