Now using xml query functions in the converter. Also adjusted the converter to use config.xml. Running the converter would delete all triggers and position constraints though, because the IDs in the png were manually overwritten with zeros.

This commit is contained in:
Steffen Fündgens 2015-02-20 13:11:05 +01:00
parent c2621752fc
commit 563bde0616
7 changed files with 107 additions and 8251 deletions

View File

@ -1,16 +1,16 @@
#include "converter.hh" #include "converter.hh"
#include <fstream> #include <fstream>
#include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <iostream> #include <iostream>
using namespace tinyxml2; using namespace tinyxml2;
Converter::Converter(std::string level){ Converter::Converter(std::string levelPath, std::string compositionsPath){
xmlFile = "../Levels/ObjectSetups/Level" + level + ".xml"; xmlFile = levelPath + ".xml";
//Load Compositions //Load Compositions
const char* charCompositions = "../Levels/ObjectSetups/Compositions.xml"; std::string stringCompositions = compositionsPath;
const char* charCompositions = stringCompositions.c_str();
compositions->LoadFile(charCompositions); compositions->LoadFile(charCompositions);
if (compositions->ErrorID()!=0){ if (compositions->ErrorID()!=0){
printf("Could not open Compositions!\n"); printf("Could not open Compositions!\n");
@ -18,8 +18,8 @@ Converter::Converter(std::string level){
} }
//Create a backup of the current Level png file, if no backup exists //Create a backup of the current Level png file, if no backup exists
std::string pngFile = "../Levels/ObjectSetups/Level" + level + ".png"; std::string pngFile = levelPath + ".png";
std::string backupPNG = "../Levels/ObjectSetups/BackupLevel" + level + ".png"; std::string backupPNG = levelPath + "Backup.png";
struct stat buf; struct stat buf;
if(stat(backupPNG.c_str(), &buf) != 0){ if(stat(backupPNG.c_str(), &buf) != 0){
std::ifstream src(pngFile, std::ios::binary); std::ifstream src(pngFile, std::ios::binary);
@ -157,16 +157,15 @@ Converter::Converter(std::string level){
doc->InsertEndChild(positionConstraint); doc->InsertEndChild(positionConstraint);
}else{ }else{
//Create a backup of the current Level xml file //Create a backup of the current Level xml file
std::string backupXML = "../Levels/ObjectSetups/BackupLevel" + level + ".xml"; std::string backupXML = levelPath + "Backup.xml";
std::ifstream src(xmlFile, std::ios::binary); std::ifstream src(xmlFile, std::ios::binary);
std::ofstream dst(backupXML, std::ios::binary); std::ofstream dst(backupXML, std::ios::binary);
dst << src.rdbuf(); dst << src.rdbuf();
//Check what IDs are already in use //Check what IDs are already in use
XMLElement* thisComposition = doc->FirstChildElement("composition"); XMLElement* thisComposition = doc->FirstChildElement("composition");
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
int idGreen = 0, idBlue = 0; int idGreen = queryInt(thisComposition, "idGreen");
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); int idBlue = queryInt(thisComposition, "idBlue");
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
idUsed[idGreen][idBlue] = true; idUsed[idGreen][idBlue] = true;
} }
} }
@ -286,16 +285,14 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){
XMLElement* thisComposition = doc->FirstChildElement("composition"); XMLElement* thisComposition = doc->FirstChildElement("composition");
bool compositionExists = false; bool compositionExists = false;
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
int idGreen = 0, idBlue = 0; int idGreen = queryInt(thisComposition, "idGreen");
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); int idBlue = queryInt(thisComposition, "idBlue");
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
if(idGreen == idG && idBlue == idB){ if(idGreen == idG && idBlue == idB){
if (compositionExists){ if (compositionExists){
std::cout << "The ID " << idGreen << "," << idBlue << " is used for multiple compositions in the xml." << std::endl; std::cout << "The ID " << idGreen << "," << idBlue << " is used for multiple compositions in the xml." << std::endl;
exit(-1); exit(-1);
} }
bool manualPos; bool manualPos = queryBool(thisComposition, "manualPos");
errorCheck(thisComposition->FirstChildElement("manualPos")->QueryBoolText(&manualPos));
if(!manualPos){ if(!manualPos){
thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str()); thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str());
thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str()); thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str());
@ -312,9 +309,8 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){
void Converter::deleteComposition(int idG, int idB){ void Converter::deleteComposition(int idG, int idB){
XMLElement* thisComposition = doc->FirstChildElement("composition"); XMLElement* thisComposition = doc->FirstChildElement("composition");
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
int idGreen = 0, idBlue = 0; int idGreen = queryInt(thisComposition, "idGreen");
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); int idBlue = queryInt(thisComposition, "idBlue");
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
if(idGreen == idG && idBlue == idB){ if(idGreen == idG && idBlue == idB){
doc->DeleteChild(thisComposition); doc->DeleteChild(thisComposition);
} }
@ -326,6 +322,50 @@ void Converter::save(){
doc->SaveFile(charXmlFile); doc->SaveFile(charXmlFile);
} }
int Converter::queryInt(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
int ret;
errorCheck(attributeElement->QueryIntText(&ret));
return ret;
}
int Converter::queryInt(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
int ret;
errorCheck(attributeElement->QueryIntText(&ret));
return ret;
}
bool Converter::queryBool(XMLElement* element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
bool ret;
errorCheck(attributeElement->QueryBoolText(&ret));
return ret;
}
bool Converter::queryBool(XMLDocument*& element, const char* attribute){
XMLElement* attributeElement = element->FirstChildElement(attribute);
if (attributeElement == NULL){
std::cout << "XMLError: Attribute " << attribute << " does not exist." << std::endl;
exit(-1);
}
bool ret;
errorCheck(attributeElement->QueryBoolText(&ret));
return ret;
}
void Converter::errorCheck(XMLError error){ void Converter::errorCheck(XMLError error){
if (error) { if (error) {
printf("XMLError: "); printf("XMLError: ");

View File

@ -8,20 +8,25 @@
using namespace tinyxml2; using namespace tinyxml2;
class Converter { class Converter {
public: public:
Converter(std::string level); Converter(std::string levelPath, std::string compositionsPath);
Converter(); Converter();
~Converter(); ~Converter();
void updateComposition(int idG, int idB, float posX, float posZ); //updates the position of a composition void updateComposition(int idG, int idB, float posX, float posZ); //updates the position of a composition
std::vector<int> newComposition(int type, float posX, float posZ);//creates a new composition and returns its ID std::vector<int> newComposition(int type, float posX, float posZ); //creates a new composition and returns its ID
void deleteComposition(int idG, int idB); void deleteComposition(int idG, int idB);
void save(); //writes the xml to file void save(); //writes the xml to file
private: private:
void errorCheck(XMLError error);
std::vector<int> nextID; std::vector<int> nextID;
bool idUsed[256][256]; bool idUsed[256][256];
std::string xmlFile; std::string xmlFile;
XMLDocument* doc = new XMLDocument(); XMLDocument* doc = new XMLDocument();
XMLDocument* compositions = new XMLDocument(); XMLDocument* compositions = new XMLDocument();
int queryInt(XMLElement* element, const char* attribute);
int queryInt(XMLDocument*& element, const char* attribute);
bool queryBool(XMLElement* element, const char* attribute);
bool queryBool(XMLDocument*& element, const char* attribute);
void errorCheck(XMLError error);
}; };
#endif #endif

View File

@ -3,15 +3,18 @@
#include "converter.hh" #include "converter.hh"
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include "tinyxml2.hh"
using namespace tinyxml2;
int main( int argc, char *argv[] ){ int main( int argc, char *argv[] ){
if (argc <= 1){ if (argc <= 1){
std::cout << "Converter needs the level (1,2,...) as input." << std::endl; std::cout << "Converter needs the levels name (Level1) as input." << std::endl;
exit(-1); exit(-1);
} }
printf("Initializing.\n"); printf("Initializing.\n");
std::string levelString = argv[1]; std::string levelString = argv[1];
Converter conv = Converter(levelString);
bool idFound[256][256]; bool idFound[256][256];
for (int i=0; i<256; i++){ for (int i=0; i<256; i++){
@ -20,14 +23,48 @@ int main( int argc, char *argv[] ){
} }
} }
XMLDocument* config = new XMLDocument();
const char* xmlFile = "../data/config.xml";
config->LoadFile(xmlFile);
if (config->ErrorID()!=0){
printf("Could not open config.xml!\n");
exit(-1);
}
XMLElement* xmlLevelPath = config->FirstChildElement("levelXmlPath");
if (xmlLevelPath == NULL){
std::cout << "XMLError: Attribute levelXmlPath does not exist." << std::endl;
exit(-1);
}
const char* charLevelPath = xmlLevelPath->GetText();
if(charLevelPath == NULL){
std::cout << "XMLError: Attribute levelXmlPath could not be loaded." << std::endl;
exit(-1);
}
std::string levelPath = charLevelPath;
XMLElement* xmlCompositionsPath = config->FirstChildElement("compositionsPath");
if (xmlCompositionsPath == NULL){
std::cout << "XMLError: Attribute compositionsPath does not exist." << std::endl;
exit(-1);
}
const char* charCompositionsPath = xmlCompositionsPath->GetText();
if(charCompositionsPath == NULL){
std::cout << "XMLError: Attribute compositionsPath could not be loaded." << std::endl;
exit(-1);
}
std::string compositionsPath = charCompositionsPath;
Converter conv = Converter("../" + levelPath + levelString, "../" + compositionsPath);
//read the setup png //read the setup png
printf("Loading the png.\n"); printf("Loading the png.\n");
std::string filePath = "../Levels/ObjectSetups/Level" + levelString + ".png"; std::string filePath = "../" + levelPath + levelString + ".png";
std::vector<unsigned char> image; //the raw pixels std::vector<unsigned char> image; //the raw pixels
unsigned int width, height; unsigned int width, height;
unsigned error = lodepng::decode(image, width, height, filePath); unsigned error = lodepng::decode(image, width, height, filePath);
if (error) { if (error) {
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
std::cout << "Converter needs the levels name (Level1) as input." << std::endl;
exit(-1); exit(-1);
} }
printf("Iterating over the png.\n"); printf("Iterating over the png.\n");
@ -70,4 +107,3 @@ int main( int argc, char *argv[] ){
//save the xml //save the xml
conv.save(); conv.save();
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 158 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

File diff suppressed because it is too large Load Diff