Exemple #1
0
bool StateParser::parseState(const char *stateFile, std::string stateID, std::vector<GameObject *> *pObjects, std::vector<std::string> *pTextureIDs)
{
	// instanciar xmldoc
	TiXmlDocument xmlDoc;

	// mira si esta cargada correctamente
	if (!xmlDoc.LoadFile(stateFile))
	{
		return false;
	}

	// conseguir el root
	TiXmlElement* pRoot = xmlDoc.RootElement();

	//declarar el nodo de <menu>...
	TiXmlElement* pStateRoot = 0;

	//busca el state segun el id que hemos pasado
	for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == stateID)
		{
			pStateRoot = e;
		}
	}

	// declarar el nodo de <texture>
	TiXmlElement* pTextureRoot = 0;


	for (TiXmlElement* e = pStateRoot->FirstChildElement(); e !=
		NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == std::string("TEXTURES"))
		{
			pTextureRoot = e;
		}
	}

	//buscar la textura
	parseTextures(pTextureRoot, pTextureIDs);

	// declarar el nodo <objeto> 
	TiXmlElement* pObjectRoot = 0;
	for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == std::string("OBJECTS"))
		{
			pObjectRoot = e;
		}
	}

	// buscar el objeto
	parseObjects(pObjectRoot, pObjects);
	return true;

}
Exemple #2
0
Level* LevelParser::parseLevel(const char* levelFile, PlayState* newState){
    
    //Create the XML document and load from file
    TiXmlDocument levelDocument;
    levelDocument.LoadFile(levelFile);
    
    //Create the level object
    Level* pLevel = new Level();
    
    //Get the root node
    TiXmlElement* pRoot = levelDocument.RootElement();
    
    pRoot->Attribute("tilewidth", &m_tileSize);
    pRoot->Attribute("width" , &m_width);
    pRoot->Attribute("height", &m_height);
    //ToDo add orientation and version number
    
    
    //Look for then parse textures
    TiXmlElement* textureRoot = findElement(string("properties"), pRoot)->FirstChildElement();
    
    while (textureRoot != NULL) {
        parseTextures(textureRoot);
        textureRoot = textureRoot->NextSiblingElement();
    }
    
    //Look for then parse tilesets
    for (TiXmlElement* currTileSet = pRoot->FirstChildElement();
         currTileSet != NULL ; currTileSet = currTileSet->NextSiblingElement()){
        if ((currTileSet->Value() == string("tileset"))){
            parseTilesets(currTileSet,pLevel->getTilesets());
        }
    }
    
    
    //Look for and Parse level layers
    for (TiXmlElement* e = pRoot->FirstChildElement();
         e != NULL ; e = e->NextSiblingElement()){
        if (e->Value() == string("layer") ||
            e->Value() == string("objectgroup")){
            string layerType = e->Attribute("name");
            
            if (e->FirstChildElement()->Value() == string("object")){
                //Parse Object layer
                parseObjectLayer(e, pLevel->getLayers(),layerType,pLevel,newState);
            } else if (e->FirstChildElement()->Value() == string("properties")){
                //Parse Tile Layer
                parseTileLayer(e, pLevel->getLayers(),pLevel->getCollisionLayers(), pLevel->getTilesets());
            }
        }
    }
    return pLevel;
}
Exemple #3
0
bool StateParser::parseState(const char* stateFile, string stateID,
	std::vector<GameObject*> * pObjects, std::vector<string> * pTextureIDs)
{
	
	TiXmlDocument xmlDoc; // crea el documento xml

	
	if (!xmlDoc.LoadFile(stateFile)) {  // carga el statefile
		return false;
	}

	TiXmlElement* pRoot = xmlDoc.RootElement();

	TiXmlElement* pStateRoot = 0;
	for (TiXmlElement* e = pRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
	{
		if (e->Value() == stateID)
		{
			pStateRoot = e;
			break;
		}
	}
	TiXmlElement* pTextureRoot = 0;


	
	for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
	{
		if (e->Value() == string("TEXTURES"))
		{
			pTextureRoot = e;
			break;
		}
	}

	parseTextures(pTextureRoot, pTextureIDs);

	TiXmlElement* pObjectRoot = 0;

	for (TiXmlElement* e = pStateRoot->FirstChildElement(); e != nullptr; e = e->NextSiblingElement())
	{
		if (e->Value() == string("OBJECTS"))
		{
			pObjectRoot = e;
			break;
		}
	}

	parseObjects(pObjectRoot, pObjects);
	return true;
}
bool StateParser::parseState(const char *stateFile, std::string stateID,
		std::vector<GameObject*> *pObjects,
		std::vector<std::string> *pTextureIDS) {

	cout<<"File ready"<<'\n';
	TiXmlDocument xmlDoc;
	if (!xmlDoc.LoadFile(stateFile)) {
		cout << "not able to load file " << '\n' << stateFile;
		return false;
	}
	TiXmlElement *pRoot = xmlDoc.RootElement();
	TiXmlElement *pStateRoot = 0;
	cout<<"File ready";

	for (TiXmlElement *e = pRoot->FirstChildElement(); e != NULL; e=e->NextSiblingElement()) {
		if (e->Value() == stateID) {
		 pStateRoot = e;
		}

	}

	TiXmlElement *pTextureRoot = 0;
	 cout<<"pTextureRoot ready"<<'\n';

	for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != NULL; e=e->NextSiblingElement()) {
		if (e->Value() == string("TEXTURES"))
		{
			pTextureRoot = e;
		}
	}


	TiXmlElement *pObjectsRoot = 0;

	for (TiXmlElement *e = pStateRoot->FirstChildElement(); e != NULL; e=e->NextSiblingElement()) {
		if (e->Value() == string("OBJECTS")) {
			pObjectsRoot = e;
		}
	}

	parseTextures(pTextureRoot, pTextureIDS);
	 cout<<"pTextureRoot loaded"<<pTextureRoot->Value()<<'\n';
	 cout<<"pObjectsRoot loaded"<<pObjectsRoot->Value()<<'\n';

	parseObjects(pObjectsRoot, pObjects);

	return true;
}
Exemple #5
0
Level* LevelParser::parseLevel(const char *levelFile)
{
	TiXmlDocument levelDocument;
	levelDocument.LoadFile(levelFile);

	Level* pLevel = new Level();

	TiXmlElement* pRoot = levelDocument.RootElement();
	pRoot->Attribute("tilewidth", &m_tileSizew);
	pRoot->Attribute("tileheight", &m_tileSizeh);
	pRoot->Attribute("width", &m_width);
	pRoot->Attribute("height", &m_height);

	for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == std::string("tileset"))
		{
			parseTilesets(e, pLevel->getTilesets());
		}
		if (e->Value() == std::string("properties"))
		{
			TiXmlElement* pproperties = e;
			for (TiXmlElement* e = pproperties->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
			{
				if (e->Value() == std::string("property"))
				{
					parseTextures(e);
				}
			}
		}
		if (e->Value() == std::string("objectgroup") || e->Value() == std::string("layer"))
		{
			if (e->FirstChildElement()->Value() == std::string("object"))
			{
				parseObjectLayer(e, pLevel->getLayers(), pLevel);
			}
			else if (e->FirstChildElement()->Value() == std::string("data") || (e->FirstChildElement()->NextSiblingElement() != 0
				&& e->FirstChildElement()->NextSiblingElement()->Value() == std::string("data")))
			{
				parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers());
			}
		}
	}

	return pLevel;
}
Exemple #6
0
Level* LevelParser::parseLevel(const char *levelFile)
{
	// create a TinyXML document and load the map XML
	TiXmlDocument levelDocument;
	levelDocument.LoadFile(levelFile);
	// create the level object
	Level* pLevel = new Level();
	// get the root node
	TiXmlElement* pRoot = levelDocument.RootElement();
	pRoot->Attribute("tilewidth", &m_tileSize);
	pRoot->Attribute("width", &m_width);
	pRoot->Attribute("height", &m_height);

	// parse the tilesets
	for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{
		if (e->Value() == std::string("properties"))
		{
			parseTextures(e->FirstChildElement());
		}

		if (e->Value() == std::string("tileset"))
		{
			parseTilesets(e, pLevel->getTilesets());
		}
	}

	// parse any object layers
	// parse any object layers
	for (TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
	{

		if (e->Value() == std::string("objectgroup"))
		{
			parseObjectLayer(e, pLevel->getLayers(), pLevel);
		}
		else if (e->Value() == std::string("layer"))
		{
			parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers());
		}

	}

	return pLevel;
}
XMLScene::XMLScene(char *filename, GlobalData &globals, Graph &graphScene)
{

	// Read XML from file

	doc=new TiXmlDocument( filename );
	bool loadOkay = doc->LoadFile();

	if ( !loadOkay )
	{
		printf( "Could not load file '%s'. Error='%s'. Exiting.\n", filename, doc->ErrorDesc() );
		exit( 1 );
	}

	TiXmlElement* anfElement= doc->FirstChildElement( "anf" );

	if (anfElement == NULL)
	{
		printf("Main anf block element not found! Exiting!\n");
		exit(1);
	}

	globalsElement = anfElement->FirstChildElement( "globals" );
	cameraElement = anfElement->FirstChildElement("cameras");
	lightElement = anfElement->FirstChildElement("lights");
	textureElement = anfElement->FirstChildElement("textures");
	appearanceElement = anfElement->FirstChildElement("appearances");
	graphElement = anfElement->FirstChildElement("graph");
	animsElement = anfElement->FirstChildElement("animations");

	parseGlobals(globals);
	parseCameras(graphScene);
	parseLights(graphScene);
	parseTextures(graphScene);
	parseAppearances(graphScene);
	parseAnimations(graphScene);
	parseGraph(graphScene);
}
Exemple #8
0
Level* LevelParser::parseLevel(const char *levelFile)
{
    // create a tinyXML document and load the map xml
    TiXmlDocument levelDocument;
    levelDocument.LoadFile(levelFile);
    
    // create the level object
    Level* pLevel = new Level();
    
    // get the root node and display some values
    TiXmlElement* pRoot = levelDocument.RootElement();
    
    std::cout << "Loading level:\n" << "Version: " << pRoot->Attribute("version") << "\n";
    std::cout << "Width:" << pRoot->Attribute("width") << " - Height:" << pRoot->Attribute("height") << "\n";
    std::cout << "Tile Width:" << pRoot->Attribute("tilewidth") << " - Tile Height:" << pRoot->Attribute("tileheight") << "\n";
    
    pRoot->Attribute("tilewidth", &m_tileSize);
    pRoot->Attribute("width", &m_width);
    pRoot->Attribute("height", &m_height);
    
    //we know that properties is the first child of the root
    TiXmlElement* pProperties = pRoot->FirstChildElement();
    
    // we must parse the textures needed for this level, which have been added to properties
    for(TiXmlElement* e = pProperties->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("property"))
        {
            parseTextures(e);
        }
    }
    
    // we must now parse the tilesets
    for(TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("tileset"))
        {
            parseTilesets(e, pLevel->getTilesets());
        }
    }
    
    // parse any object layers
    for(TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("objectgroup") || e->Value() == std::string("layer"))
        {
            if(e->FirstChildElement()->Value() == std::string("object"))
            {
                parseObjectLayer(e, pLevel->getLayers(), pLevel);
            }
            else if(e->FirstChildElement()->Value() == std::string("data") ||
                    (e->FirstChildElement()->NextSiblingElement() != 0 && e->FirstChildElement()->NextSiblingElement()->Value() == std::string("data")))
            {
                parseTileLayer(e, pLevel->getLayers(), pLevel->getTilesets(), pLevel->getCollisionLayers());
            }
        }
    }
    
    // set the players collision layer
   // pLevel->getPlayer()->setCollisionLayers(pLevel->getCollisionLayers());
    
    return pLevel;
}
Exemple #9
0
bool StateParser::parseState( const char *stateFile, std::string stateID, std::vector<GameObject*> *pObjects, std::vector<std::string> *pTextureIDs )
{
	std::cout << "< StateParser::parseState( " << stateFile << ", " << stateID << ", pObjects, pTextureIDs) >\n";
	// create the XML document
	TiXmlDocument xmlDoc;
	
	// load the state file
	if( !xmlDoc.LoadFile( stateFile ) )
	{
		std::cerr << xmlDoc.ErrorDesc() << "\n";
		return false;
	}
	std::cout << " XML File Loaded\n";

	// get the root element
	TiXmlElement* pRoot = xmlDoc.RootElement();

	std::cout << " Got Document Root\n";

	// pre declare the state's root node
	TiXmlElement* pStateRoot = 0;

	// get this state's root node and assign it to pStateRoot
	for( TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement() )
	{
		if( e->Value() == stateID )
		{
			pStateRoot = e;
		}
	}

	std::cout << " Got State Root\n";

	// pre-declare the texture root
	TiXmlElement* pTextureRoot = 0;

	// get the root of the texture elements
	for( TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL ; e = e->NextSiblingElement() )
	{
		if( e->Value() == std::string("TEXTURES") )
		{
			pTextureRoot = e;
		}
	}

	std::cout << " Got Texture Root\n";

	// now parse the textures
	parseTextures( pTextureRoot, pTextureIDs );

	std::cout << " Parsed textures\n";

	// pre-declare the object root node
	TiXmlElement* pObjectRoot = 0;

	// get the root node and assign it to pObjectRoot
	for( TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement() )
	{
		if( e->Value() == std::string("OBJECTS") )
		{
			pObjectRoot = e;
		}
	}

	std::cout << " Got Object Root\n";

	// now parse the objects
	parseObjects( pObjectRoot, pObjects );

	std::cout << " Parsed objects\n";	
	std::cout << "< END StateParser::parseState( " << stateFile << ", " << stateID << ", pObjects, pTextureIDs) >\n";
	return true;
}
bool ANFparser::parse(Scene * scene,const char* filename){
	this->scene = scene;

	try{
		TiXmlDocument * doc = new TiXmlDocument(filename);

		//First things first, reading the XML/ANF file
		if (!doc->LoadFile()){
			issue("Could not load file '%"+str(filename)+"'. Error='" + str(doc->ErrorDesc()) + "'.", ERROR);
		}

		//Must exist anf root block
		TiXmlElement* anfRoot = doc->FirstChildElement("anf");
		if (!anfRoot){
			issue("Main block 'anf' block not found!",ERROR);
		}

		//Lets process the global variables
		TiXmlElement* anfGlobals = anfRoot->FirstChildElement("globals");
		if(!anfGlobals){
			issue("Block 'globals' not found!",WARNING);
		}else{
			parseGlobals(anfGlobals);
		}
		
		//Lets process the cameras
		TiXmlElement* anfCameras = anfRoot->FirstChildElement("cameras");
		if(!anfCameras){
			issue("Block 'cameras' not found!",ERROR);
		}else{
			std::string firstCameraId = str(anfCameras->Attribute("initial"));
			if(firstCameraId == "" ){
				issue("Cameras block must declare the first camera.. ", WARNING);
			}
			parseCameras(anfCameras,firstCameraId);
		}

		//Lets process the lights
		TiXmlElement* anfLights = anfRoot->FirstChildElement("lights");
		if(!anfLights){
			issue("Block 'lights' not found!",WARNING);
		}else{
			parseLights(anfLights);
		}

		//If textures extist, we process it
		TiXmlElement* anfTextures = anfRoot->FirstChildElement("textures");
		map<std::string,Texture *> textures;
		if(!anfTextures){
			issue("Block 'textures' not found!",WARNING);
		}else{
			parseTextures(anfTextures,textures);
		}


		//If appearances block exist, we call its parser
		TiXmlElement* anfAppearances = anfRoot->FirstChildElement("appearances");
		map<std::string,Appearance *> appearances;
		if(!anfAppearances){
			issue("Block 'appearances' not found!",WARNING);
		}else{
			parseAppearances(anfAppearances,appearances,textures);
		}
		
		//If animations extist, we process it
		TiXmlElement* anfAnimations = anfRoot->FirstChildElement("animations");
		map<std::string,Animation *> animations;
		if(!anfAnimations){
			issue("Block 'animations' not found!",WARNING);
		}else{
			parseAnimations(anfAnimations,animations);
		}


		//If graph block exist, we call its parser
		TiXmlElement* anfGraph = anfRoot->FirstChildElement("graph");
		if(!anfGraph){
			issue("Block 'graph' not found!",ERROR);
		}else{
			this->scene->setRoot(parseGraph(anfGraph,appearances,animations));
		}

		return true; 
	}catch(...){
		return false;
	}
}