Exemple #1
0
bool SceneLoader::loadObjects(){ //Falta condicao para nao se repetirem ID's <--
	TiXmlElement* objectElement = root->FirstChildElement("objects");
	if(objectElement == NULL)
	{
		cout << "> Error: objects tag not found!" << endl;
		return false;
	}
	TiXmlElement* objectChild = objectElement->FirstChildElement();
	int i=1;
	for( ; objectChild != NULL; objectChild = objectChild->NextSiblingElement()){
		if(i <= maxObjects){
			if(!objectChild->Attribute("type")){
				cout << "	> Error: Could not load type of an Object!" << endl; //Sair imediatamente
				return NULL;
			}
			string type = objectChild->Attribute("type");
			if(type == "compound"){ //Tipo composto
				Compound* compound = new Compound();
				
				if((compound->loadAttributes(objectChild)) == NULL)
					return false;
				if(sceneManager->getObjectbyID(objectChild->Attribute("id")) != NULL){ //Objecto já existente no sistema
					cout << "	> Warning: object with ID " << objectChild->Attribute("id") << " is already in the system!!" << endl;
					continue;
				}

				string material;
				if((material = compound->loadMaterial(objectChild)).empty()) return NULL;
				if(material != "null"){
					Material* mat = this->sceneManager->getMaterialbyID(material);
					if(mat == NULL){
						cout << "	> Error: Material with ID: " << material << " not found for Object: " << compound->id << endl;
						return false;
					}
					compound->material = mat;
				} else{
					Material* mat = new Material();
					mat->id = "null";
					compound->material = mat;
				}

				string texture;
				if((texture = compound->loadTexture(objectChild)).empty()) return NULL;
				if(texture == "clear"){
					Texture* text = new Texture();
					text->id = "clear";
					compound->texture = text;
				} else if(texture != "null"){
					Texture* text = this->sceneManager->getTexturebyID(texture);
					if(text == NULL){
						cout << "	> Error: Texture with ID: " << texture << " not found for Object: " << compound->id << endl;
						return false;
					}
					compound->texture = text;
				} else{
					Texture* text = new Texture();
					text->id = "null";
					compound->texture = text;
				}

				if(objectChild->Attribute("id") == sceneManager->globals->root){
					sceneManager->root = compound;
				}

				sceneManager->addObject(compound);
			} else if(type == "simple"){ //Tipo simples
				Simple* simple = new Simple();
		
				if((simple->loadAttributes(objectChild)) == NULL)
						return false;
				if(sceneManager->getObjectbyID(objectChild->Attribute("id")) != NULL) //Objecto já existente no sistema
				{
					cout << "	> Warning: object with ID " << objectChild->Attribute("id") << " is already in the system!!" << endl;
					continue;
				}

				string material;
				if((material = simple->loadMaterial(objectChild)).empty()) return NULL;
				if(material != "null"){
					Material* mat = this->sceneManager->getMaterialbyID(material);
					if(mat == NULL){
						cout << "	> Error: Material with ID: " << material << " not found for Object: " << simple->id << endl;
						return false;
					}
					simple->material = mat;
				} else{
					Material* mat = new Material();
					mat->id = "null";
					simple->material = mat;
				}

				string texture;
				if((texture = simple->loadTexture(objectChild)).empty()) return NULL;
				if(texture == "clear"){
					Texture* text = new Texture();
					text->id = "clear";
					simple->texture = text;
				} else if(texture != "null"){
					Texture* text = this->sceneManager->getTexturebyID(texture);
					if(text == NULL){
						cout << "	> Error: Texture with ID: " << texture << " not found for Object: " << simple->id << endl;
						return false;
					}
					simple->texture = text;
				} else{
					Texture* text = new Texture();
					text->id = "null";
					simple->texture = text;
				}

				if(objectChild->Attribute("id") == sceneManager->globals->root){
					sceneManager->root = simple;
				}
				sceneManager->addObject(simple);
			}
			i++;
		} else {
			cout << "	> Error: Maximum number of Objects reached!" << endl;
			return false;
		}
			
	}
	if(i == 1){
		cout << "	> Error: You need to have at least 1 object!" << endl;
		return false;
	}
	
	if(sceneManager->root == NULL) //O objecto raiz nao existe
	{
		cout << "	> Error: Cannot load root element. There's no object with ID " << sceneManager->globals->root << endl;
		return false;
	} 
	//Adicionar os objectos filhos aos objectos compostos
	if(!this->sceneManager->setChildrenObjects())
		return false;
	return true;
}