2014-11-21 14:43:27 +00:00
|
|
|
#include "converter.hh"
|
2014-11-25 12:51:25 +00:00
|
|
|
#include <fstream>
|
2014-12-01 13:09:55 +00:00
|
|
|
#include <string>
|
2014-12-05 15:18:48 +00:00
|
|
|
#include <sys/stat.h>
|
2014-11-25 12:51:25 +00:00
|
|
|
|
|
|
|
using namespace tinyxml2;
|
|
|
|
|
|
|
|
Converter::Converter(std::string level){
|
|
|
|
xmlFile = "../Levels/ObjectSetups/Level" + level + ".xml";
|
|
|
|
|
2014-11-28 16:05:49 +00:00
|
|
|
//Load Compositions
|
|
|
|
const char* charCompositions = "../Levels/ObjectSetups/Compositions.xml";
|
|
|
|
compositions->LoadFile(charCompositions);
|
|
|
|
if (compositions->ErrorID()!=0){
|
|
|
|
printf("Could not open Compositions!!!\n");
|
|
|
|
}
|
|
|
|
|
2014-12-05 15:18:48 +00:00
|
|
|
//Create a backup of the current Level png file, if no backup exists
|
|
|
|
std::string pngFile = "../Levels/ObjectSetups/Level" + level + ".png";
|
|
|
|
std::string backupPNG = "../Levels/ObjectSetups/BackupLevel" + level + ".png";
|
|
|
|
struct stat buf;
|
|
|
|
if(stat(backupPNG.c_str(), &buf) != 0){
|
|
|
|
std::ifstream src(pngFile, std::ios::binary);
|
|
|
|
std::ofstream dst(backupPNG, std::ios::binary);
|
|
|
|
dst << src.rdbuf();
|
|
|
|
}
|
|
|
|
|
2014-11-28 16:05:49 +00:00
|
|
|
//Create a backup of the current Level xml file
|
2014-12-05 15:18:48 +00:00
|
|
|
std::string backupXML = "../Levels/ObjectSetups/BackupLevel" + level + ".xml";
|
2014-11-25 12:51:25 +00:00
|
|
|
std::ifstream src(xmlFile, std::ios::binary);
|
2014-12-05 15:18:48 +00:00
|
|
|
std::ofstream dst(backupXML, std::ios::binary);
|
2014-11-25 12:51:25 +00:00
|
|
|
dst << src.rdbuf();
|
|
|
|
|
2014-11-28 16:05:49 +00:00
|
|
|
//Load the Level xml file
|
2014-11-25 12:51:25 +00:00
|
|
|
const char* charXmlFile = xmlFile.c_str();
|
|
|
|
doc->LoadFile(charXmlFile);
|
2014-12-01 13:09:55 +00:00
|
|
|
nextID.push_back(1);
|
|
|
|
nextID.push_back(1);
|
2014-11-25 12:51:25 +00:00
|
|
|
if (doc->ErrorID()!=0){
|
2014-11-28 16:05:49 +00:00
|
|
|
printf("Could not open xml, creating new xml.\n");
|
2014-12-01 13:09:55 +00:00
|
|
|
}else{
|
|
|
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
|
|
|
int idGreen, idBlue;
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
2014-12-01 13:09:55 +00:00
|
|
|
thisComposition->QueryIntAttribute("idGreen", &idGreen);
|
|
|
|
thisComposition->QueryIntAttribute("idBlue", &idBlue);
|
|
|
|
if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){
|
|
|
|
nextID[0] = idGreen;
|
|
|
|
nextID[1] = idBlue;
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 12:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 14:43:27 +00:00
|
|
|
|
|
|
|
Converter::Converter(){
|
|
|
|
}
|
|
|
|
|
|
|
|
Converter::~Converter(){
|
|
|
|
}
|
|
|
|
|
2014-12-01 13:09:55 +00:00
|
|
|
std::vector<unsigned int> Converter::newComposition(unsigned int type, float posX, float posZ){
|
|
|
|
XMLElement* newComposition = doc->NewElement("composition");
|
|
|
|
doc->InsertFirstChild(newComposition);
|
|
|
|
|
|
|
|
XMLElement* typeID = doc->NewElement("typeID");
|
|
|
|
XMLElement* idBlue = doc->NewElement("idBlue");
|
|
|
|
XMLElement* idGreen = doc->NewElement("idGreen");
|
|
|
|
XMLElement* zPos = doc->NewElement("zPos");
|
|
|
|
XMLElement* yOffset = doc->NewElement("yOffset");
|
|
|
|
XMLElement* xPos = doc->NewElement("xPos");
|
|
|
|
XMLElement* zRot = doc->NewElement("zRot");
|
|
|
|
XMLElement* yRot = doc->NewElement("yRot");
|
|
|
|
XMLElement* xRot = doc->NewElement("xRot");
|
|
|
|
XMLElement* scale = doc->NewElement("scale");
|
|
|
|
|
|
|
|
typeID->SetText(std::to_string(type).c_str());
|
|
|
|
idBlue->SetText(std::to_string(nextID[1]).c_str());
|
|
|
|
idGreen->SetText(std::to_string(nextID[0]).c_str());
|
|
|
|
zPos->SetText(std::to_string(posZ).c_str());
|
|
|
|
yOffset->SetText("0.0");
|
|
|
|
xPos->SetText(std::to_string(posX).c_str());
|
|
|
|
zRot->SetText("0.0");
|
|
|
|
yRot->SetText("0.0");
|
|
|
|
xRot->SetText("0.0");
|
|
|
|
scale->SetText("1.0");
|
|
|
|
|
|
|
|
newComposition->InsertFirstChild(typeID);
|
|
|
|
newComposition->InsertFirstChild(idBlue);
|
|
|
|
newComposition->InsertFirstChild(idGreen);
|
|
|
|
newComposition->InsertFirstChild(zPos);
|
|
|
|
newComposition->InsertFirstChild(yOffset);
|
|
|
|
newComposition->InsertFirstChild(xPos);
|
|
|
|
newComposition->InsertFirstChild(zRot);
|
|
|
|
newComposition->InsertFirstChild(yRot);
|
|
|
|
newComposition->InsertFirstChild(xRot);
|
|
|
|
newComposition->InsertFirstChild(scale);
|
|
|
|
|
|
|
|
std::vector<unsigned int> ret = nextID;
|
|
|
|
nextID[1] += 1;
|
|
|
|
if (nextID[1] == 255){
|
|
|
|
nextID[1] = 0;
|
|
|
|
nextID[0] +=1;
|
|
|
|
}
|
|
|
|
return ret;
|
2014-11-21 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-01 13:09:55 +00:00
|
|
|
void Converter::updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ){
|
|
|
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
|
|
|
int idGreen, idBlue;
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
2014-12-01 13:09:55 +00:00
|
|
|
thisComposition->QueryIntAttribute("idGreen", &idGreen);
|
|
|
|
thisComposition->QueryIntAttribute("idBlue", &idBlue);
|
|
|
|
if(idGreen == idG && idBlue == idB){
|
|
|
|
thisComposition->FirstChildElement("xPos")->SetText(std::to_string(posX).c_str());
|
|
|
|
thisComposition->FirstChildElement("zPos")->SetText(std::to_string(posZ).c_str());
|
2014-11-28 16:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Converter::deleteComposition(unsigned int idG, unsigned int idB){
|
2014-12-01 13:09:55 +00:00
|
|
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
|
|
|
int idGreen, idBlue;
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
2014-12-01 13:09:55 +00:00
|
|
|
thisComposition->QueryIntAttribute("idGreen", &idGreen);
|
|
|
|
thisComposition->QueryIntAttribute("idBlue", &idBlue);
|
|
|
|
if(idGreen == idG && idBlue == idB){
|
|
|
|
doc->DeleteChild(thisComposition);
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 12:51:25 +00:00
|
|
|
void Converter::save(){
|
|
|
|
const char* charXmlFile = xmlFile.c_str();
|
|
|
|
doc->SaveFile(charXmlFile);
|
|
|
|
}
|
|
|
|
|
2014-12-01 13:09:55 +00:00
|
|
|
std::vector<unsigned int> Converter::getNextID(){
|
|
|
|
return nextID;
|
2014-11-25 12:51:25 +00:00
|
|
|
}
|
2014-11-21 14:43:27 +00:00
|
|
|
|
2014-12-01 13:09:55 +00:00
|
|
|
/* finding a typeID in compositions:
|
|
|
|
XMLElement* thisComposition = compositions->FirstChildElement("composition");
|
2014-12-05 11:47:02 +00:00
|
|
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
2014-12-01 13:09:55 +00:00
|
|
|
int thisType;
|
|
|
|
thisComposition->QueryIntAttribute("typeID", &thisType);
|
|
|
|
if(thisType == type){
|
|
|
|
...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|