diff --git a/converter/converter.cc b/converter/converter.cc index 373c73d..01d6685 100644 --- a/converter/converter.cc +++ b/converter/converter.cc @@ -26,12 +26,6 @@ Converter::Converter(std::string level){ dst << src.rdbuf(); } - //Create a backup of the current Level xml file - std::string backupXML = "../Levels/ObjectSetups/BackupLevel" + level + ".xml"; - std::ifstream src(xmlFile, std::ios::binary); - std::ofstream dst(backupXML, std::ios::binary); - - //Load the Level xml file nextID.push_back(0); nextID.push_back(1); @@ -43,8 +37,19 @@ Converter::Converter(std::string level){ idUsed[0][0] = true; const char* charXmlFile = xmlFile.c_str(); doc->LoadFile(charXmlFile); + //check if the xml file did not already exist if (doc->ErrorID()!=0){ - printf("Could not open xml, creating new xml.\n"); + std::string answer; + printf("Could not open xml, do you want to create a new xml? (y/n)\n"); + std::cin >> answer; + while(answer.compare("y") != 0 && answer.compare("n") != 0){ + printf("Answer with y or n"); + std::cin >> answer; + } + if(answer.compare("n") == 0){ + exit(-1); + } + printf("Creating new xml.\n"); //Create all global Lightingparameters with Dummy-Values std::vector lightAttributes; lightAttributes.push_back(doc->NewElement("xOffset")); @@ -112,7 +117,7 @@ Converter::Converter(std::string level){ physics->InsertEndChild(playerStrength); doc->InsertEndChild(physics); - //create positionConstraint Dummy + //Create positionConstraint Dummy XMLElement* positionConstraint = doc->NewElement("positionConstraint"); XMLElement* positionConstraintObjectNum = doc->NewElement("objectNum"); XMLElement* positionConstraintXPos = doc->NewElement("xPosition"); @@ -131,7 +136,12 @@ Converter::Converter(std::string level){ positionConstraint->InsertEndChild(positionConstraintStrength); doc->InsertEndChild(positionConstraint); }else{ + //Create a backup of the current Level xml file + std::string backupXML = "../Levels/ObjectSetups/BackupLevel" + level + ".xml"; + std::ifstream src(xmlFile, std::ios::binary); + std::ofstream dst(backupXML, std::ios::binary); dst << src.rdbuf(); + //Check what IDs are already in use XMLElement* thisComposition = doc->FirstChildElement("composition"); for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ int idGreen = 0, idBlue = 0; @@ -149,8 +159,6 @@ Converter::~Converter(){ } std::vector Converter::newComposition(int type, float posX, float posZ){ - XMLElement* newComposition = doc->NewElement("composition"); - doc->InsertFirstChild(newComposition); while(idUsed[nextID[0]][nextID[1]]){ nextID[1] += 1; if (nextID[1] == 256){ @@ -162,7 +170,7 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ } } } - + XMLElement* newComposition = doc->NewElement("composition"); XMLElement* typeID = doc->NewElement("typeID"); XMLElement* idBlue = doc->NewElement("idBlue"); XMLElement* idGreen = doc->NewElement("idGreen"); @@ -245,14 +253,15 @@ std::vector Converter::newComposition(int type, float posX, float posZ){ trigger->InsertEndChild(toChangeObjNum); idUsed[nextID[0]][nextID[1]] = true; + doc->InsertFirstChild(newComposition); return nextID; } void Converter::updateComposition(int idG, int idB, float posX, float posZ){ XMLElement* thisComposition = doc->FirstChildElement("composition"); - int idGreen = 0, idBlue = 0; bool compositionExists = false; for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ + int idGreen = 0, idBlue = 0; errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue)); if(idGreen == idG && idBlue == idB){ @@ -278,7 +287,7 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){ void Converter::deleteComposition(int idG, int idB){ XMLElement* thisComposition = doc->FirstChildElement("composition"); for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){ - int idGreen = 0, idBlue = 0; + int idGreen = 0, idBlue = 0; errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen)); errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue)); if(idGreen == idG && idBlue == idB){ @@ -292,10 +301,6 @@ void Converter::save(){ doc->SaveFile(charXmlFile); } -std::vector Converter::getNextID(){ - return nextID; -} - void Converter::errorCheck(XMLError error){ if (error) { printf("XMLError: "); diff --git a/converter/converter.hh b/converter/converter.hh index 92f1c43..318f944 100644 --- a/converter/converter.hh +++ b/converter/converter.hh @@ -15,7 +15,6 @@ class Converter { std::vector newComposition(int type, float posX, float posZ);//creates a new composition and returns its ID void deleteComposition(int idG, int idB); void save(); //writes the xml to file - std::vector getNextID(); //returns the next unused ID private: void errorCheck(XMLError error); diff --git a/converter/main.cc b/converter/main.cc index 2e881e6..f492d0a 100644 --- a/converter/main.cc +++ b/converter/main.cc @@ -35,11 +35,11 @@ int main( int argc, char *argv[] ){ //if there is a composition here, adjust the xml and image if(image[pixel]!=0 && image[pixel]!=255){ if(image[pixel+1]==0 && image[pixel+2]==0){//composition has no ID - std::vector temp; - temp = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); - idFound[temp[0]][temp[1]] = true; - image[pixel+1] = temp[0]; - image[pixel+2] = temp[1]; + std::vector newID; + newID = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); + idFound[newID[0]][newID[1]] = true; + image[pixel+1] = newID[0]; + image[pixel+2] = newID[1]; }else{//composition has an ID conv.updateComposition(image[pixel+1], image[pixel+2], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width); idFound[image[pixel+1]][image[pixel+2]] = true;