implemented all methods of the converter class, needs testing

This commit is contained in:
Steffen Fündgens 2014-12-01 14:09:55 +01:00
parent 2ed33268cb
commit 74c05cb7ed
3 changed files with 96 additions and 62 deletions

View File

@ -1,5 +1,6 @@
#include "converter.hh" #include "converter.hh"
#include <fstream> #include <fstream>
#include <string>
using namespace tinyxml2; using namespace tinyxml2;
@ -22,12 +23,22 @@ Converter::Converter(std::string level){
//Load the Level xml file //Load the Level xml file
const char* charXmlFile = xmlFile.c_str(); const char* charXmlFile = xmlFile.c_str();
doc->LoadFile(charXmlFile); doc->LoadFile(charXmlFile);
nextID.push_back(1);
nextID.push_back(1);
if (doc->ErrorID()!=0){ if (doc->ErrorID()!=0){
printf("Could not open xml, creating new xml.\n"); printf("Could not open xml, creating new xml.\n");
}else{
XMLElement* thisComposition = doc->FirstChildElement("composition");
int idGreen, idBlue;
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){
thisComposition->QueryIntAttribute("idGreen", &idGreen);
thisComposition->QueryIntAttribute("idBlue", &idBlue);
if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){
nextID[0] = idGreen;
nextID[1] = idBlue;
}
}
} }
nextId.push_back(1);
nextId.push_back(250); //TODO
} }
Converter::Converter(){ Converter::Converter(){
@ -36,33 +47,33 @@ Converter::Converter(){
Converter::~Converter(){ Converter::~Converter(){
} }
void Converter::updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ std::vector<unsigned int> Converter::newComposition(unsigned int type, float posX, float posZ){
//TODO XMLElement* newComposition = doc->NewElement("composition");
}
std::vector<unsigned int> Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){
XMLNode* thisComposition = compositions->FirstChild();
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){
int thisType;
XMLElement* thisCompositionElement = thisComposition->ToElement();
thisCompositionElement->QueryIntAttribute("typeID", &thisType);
if(thisType == type){
//XMLElement* newComposition = doc.NewElement();
//newComposition = thisComposition;
//XMLElement* newComposition = thisComposition->Clone(); //TODO write own clone function
doc->InsertFirstChild(newComposition); doc->InsertFirstChild(newComposition);
XMLElement* idBlue = doc->NewElement("<idBlue>" + itoa(nextId[1]) + "</idBlue>"); XMLElement* typeID = doc->NewElement("typeID");
XMLElement* idGreen = doc->NewElement("<idGreen>" + itoa(nextId[0]) + "</idGreen>"); XMLElement* idBlue = doc->NewElement("idBlue");
XMLElement* zPos = doc->NewElement("<zPos>" + itoa(posZ) + "</zPos>"); XMLElement* idGreen = doc->NewElement("idGreen");
XMLElement* yOffset = doc->NewElement("<yOffset>0.0</yOffset>"); XMLElement* zPos = doc->NewElement("zPos");
XMLElement* xPos = doc->NewElement("<xPos>" + itoa(posX) + "</xPos>"); XMLElement* yOffset = doc->NewElement("yOffset");
XMLElement* zRot = doc->NewElement("<zRot>0.0</zRot>"); XMLElement* xPos = doc->NewElement("xPos");
XMLElement* yRot = doc->NewElement("<yRot>0.0</yRot>"); XMLElement* zRot = doc->NewElement("zRot");
XMLElement* xRot = doc->NewElement("<xRot>0.0</xRot>"); XMLElement* yRot = doc->NewElement("yRot");
XMLElement* scale = doc->NewElement("<scale>1.0</scale>"); 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(idBlue);
newComposition->InsertFirstChild(idGreen); newComposition->InsertFirstChild(idGreen);
newComposition->InsertFirstChild(zPos); newComposition->InsertFirstChild(zPos);
@ -72,20 +83,39 @@ std::vector<unsigned int> Converter::newComposition(unsigned int type, unsigned
newComposition->InsertFirstChild(yRot); newComposition->InsertFirstChild(yRot);
newComposition->InsertFirstChild(xRot); newComposition->InsertFirstChild(xRot);
newComposition->InsertFirstChild(scale); newComposition->InsertFirstChild(scale);
}
}
std::vector<unsigned int> ret = nextId; std::vector<unsigned int> ret = nextID;
nextId[1] += 1; nextID[1] += 1;
if (nextId[1] == 256){ if (nextID[1] == 255){
nextId[1] = 1; nextID[1] = 0;
nextId[0] +=1; nextID[0] +=1;
} }
return ret; return ret;
} }
void Converter::updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ){
XMLElement* thisComposition = doc->FirstChildElement("composition");
int idGreen, idBlue;
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){
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());
}
}
}
void Converter::deleteComposition(unsigned int idG, unsigned int idB){ void Converter::deleteComposition(unsigned int idG, unsigned int idB){
//TODO XMLElement* thisComposition = doc->FirstChildElement("composition");
int idGreen, idBlue;
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){
thisComposition->QueryIntAttribute("idGreen", &idGreen);
thisComposition->QueryIntAttribute("idBlue", &idBlue);
if(idGreen == idG && idBlue == idB){
doc->DeleteChild(thisComposition);
}
}
} }
void Converter::save(){ void Converter::save(){
@ -93,11 +123,17 @@ void Converter::save(){
doc->SaveFile(charXmlFile); doc->SaveFile(charXmlFile);
} }
std::vector<unsigned int> Converter::getNextId(){ std::vector<unsigned int> Converter::getNextID(){
return nextId; return nextID;
} }
/* finding a typeID in compositions:
void Converter::newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ XMLElement* thisComposition = compositions->FirstChildElement("composition");
//TODO for(; thisComposition; thisComposition=thisComposition->NextSiblingElement()){
int thisType;
thisComposition->QueryIntAttribute("typeID", &thisType);
if(thisType == type){
...
} }
}
*/

View File

@ -11,18 +11,17 @@ class Converter {
Converter(std::string level); Converter(std::string level);
Converter(); Converter();
~Converter(); ~Converter();
void updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); void updateComposition(unsigned int idG, unsigned int idB, float posX, float posZ);
std::vector<unsigned int> newComposition(unsigned int type, unsigned int posX, unsigned int posZ); std::vector<unsigned int> newComposition(unsigned int type, float posX, float posZ);
void deleteComposition(unsigned int idG, unsigned int idB); void deleteComposition(unsigned int idG, unsigned int idB);
void save(); void save();
std::vector<unsigned int> getNextId(); std::vector<unsigned int> getNextID();
private: private:
std::vector<unsigned int> nextId; std::vector<unsigned int> nextID;
std::string xmlFile; std::string xmlFile;
XMLDocument* doc = new XMLDocument(); XMLDocument* doc = new XMLDocument();
XMLDocument* compositions = new XMLDocument(); XMLDocument* compositions = new XMLDocument();
void newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ);
}; };
#endif #endif

View File

@ -34,12 +34,12 @@ int main( int argc, char *argv[] ){
if(image[pixel]!=0){ if(image[pixel]!=0){
if(image[pixel+1]==0 && image[pixel+2]==0){ if(image[pixel+1]==0 && image[pixel+2]==0){
std::vector<unsigned int> temp; std::vector<unsigned int> temp;
temp = conv.newComposition(image[pixel], rowNum, columnNum); temp = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width);
idFound[temp[0]][temp[1]] = true; idFound[temp[0]][temp[1]] = true;
image[pixel+1] = temp[0]; image[pixel+1] = temp[0];
image[pixel+2] = temp[1]; image[pixel+2] = temp[1];
}else{ }else{
conv.updateComposition(image[pixel+1], image[pixel+2], rowNum, columnNum); 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; idFound[image[pixel+1]][image[pixel+2]] = true;
} }
} }
@ -53,8 +53,7 @@ int main( int argc, char *argv[] ){
} }
//delete compositions that were not in the png anymore //delete compositions that were not in the png anymore
for (int i=1; i<=conv.getNextID()[1]; i++){
for (int i=1; i<=conv.getNextId()[1]; i++){
for (int j=1; j<256; j++){ for (int j=1; j<256; j++){
if (! idFound[i][j]){ if (! idFound[i][j]){
conv.deleteComposition(i,j); conv.deleteComposition(i,j);