From 20e7c732e223b3e034591b2843b9ac935ecfe7db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20F=C3=BCndgens?= Date: Tue, 25 Nov 2014 13:51:25 +0100 Subject: [PATCH] skeleton runs now, only xml read/write missing --- Levels/ObjectSetups/Level0.xcf | Bin 0 -> 782 bytes Levels/ObjectSetups/Lvl1.png | Bin 88 -> 0 bytes converter/converter.cc | 40 +++++++++++++++++++++++++++++++-- converter/converter.hh | 12 +++++++++- converter/main.cc | 30 ++++++++++++++----------- 5 files changed, 66 insertions(+), 16 deletions(-) create mode 100644 Levels/ObjectSetups/Level0.xcf delete mode 100644 Levels/ObjectSetups/Lvl1.png diff --git a/Levels/ObjectSetups/Level0.xcf b/Levels/ObjectSetups/Level0.xcf new file mode 100644 index 0000000000000000000000000000000000000000..369414924dd68960ac2ccd3e5e9224575ef45965 GIT binary patch literal 782 zcma)4%TB{E5OhifX-h@R>%_sw^bpkkLOE0%kobkvbzF-)sw5TdflolsVPUov!#s#5~rCWE5Qs@FBtTXt^Tz(+|b~ifclf&?4Mg4&M#=XO#Pj zjtxbgrZTH&&rrO4C}dR01%K7mlFuHWo>3XpR-HylhN{pDXyo=-P*&SS@-k2KV$=;{ zCGsRMcu=RXQ1gh7@5glaG@$&}nH~0l{oa^F@i0f&-3|nt$OWf5@=;6E_YOQzdzlHG)EgdXF^CgTC dW`Iq5FMF0TSQ)GgR%Q%?mBDIdvRd|E>kb!gw-*2a literal 0 HcmV?d00001 diff --git a/Levels/ObjectSetups/Lvl1.png b/Levels/ObjectSetups/Lvl1.png deleted file mode 100644 index e216392e8d5a3ffbf01a109fb950c24be1949b57..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 88 zcmeAS@N?(olHy`uVBq!ia0vp^DIm-UBp4!QuJ{S0 + +using namespace tinyxml2; + +Converter::Converter(std::string level){ + xmlFile = "../Levels/ObjectSetups/Level" + level + ".xml"; + + //Create a backup of the current xml file + std::string backup = "../Levels/ObjectSetups/BackupLevel" + level + ".xml"; + std::ifstream src(xmlFile, std::ios::binary); + std::ofstream dst(backup, std::ios::binary); + dst << src.rdbuf(); + + //Load the xml file + const char* charXmlFile = xmlFile.c_str(); + doc->LoadFile(charXmlFile); + if (doc->ErrorID()!=0){ + printf("Could not open xml, creating new xml."); + } + + nextId.push_back(1); + nextId.push_back(250); //TODO +} Converter::Converter(){ } @@ -6,14 +29,19 @@ Converter::Converter(){ Converter::~Converter(){ } -void Converter::updateComposition(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ +void Converter::updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ //TODO } std::vector Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){ //TODO - std::vector ret = {0,0}; + std::vector ret = nextId; + nextId[1] += 1; + if (nextId[1] == 256){ + nextId[1] = 1; + nextId[0] +=1; + } return ret; } @@ -21,6 +49,14 @@ void Converter::deleteComposition(unsigned int idG, unsigned int idB){ //TODO } +void Converter::save(){ + const char* charXmlFile = xmlFile.c_str(); + doc->SaveFile(charXmlFile); +} + +std::vector Converter::getNextId(){ + return nextId; +} void Converter::newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ diff --git a/converter/converter.hh b/converter/converter.hh index e911a69..dc9253a 100644 --- a/converter/converter.hh +++ b/converter/converter.hh @@ -1,17 +1,27 @@ #ifndef CONVERTER_INCLUDED #define CONVERTER_INCLUDED + #include +#include #include "tinyxml2.hh" +using namespace tinyxml2; class Converter { public: + Converter(std::string level); Converter(); ~Converter(); - void updateComposition(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); + void updateComposition(unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); std::vector newComposition(unsigned int type, unsigned int posX, unsigned int posZ); void deleteComposition(unsigned int idG, unsigned int idB); + void save(); + std::vector getNextId(); private: + std::vector nextId; + std::string xmlFile; + XMLDocument* doc = new XMLDocument(); void newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ); }; + #endif diff --git a/converter/main.cc b/converter/main.cc index 44c81da..3885f2e 100644 --- a/converter/main.cc +++ b/converter/main.cc @@ -4,26 +4,25 @@ #include #include -int main( int argc, char *argv[] ){ - unsigned int level=atoi(argv[1]); - Converter conv; +int main( int argc, char *argv[] ){ + std::string levelString = argv[1]; + Converter conv = Converter(levelString); bool idFound[256][256]; for (int i=0; i<256; i++){ for (int j=0; j<256; j++){ idFound[i][j] = false; } } - //read the setup png - char levelChar[2]; - sprintf (levelChar,"%i", level); - std::string levelString = levelChar; - std::string filePath = "../Levels/ObjectSetups/Lvl" + levelString + ".png"; + + //read the setup png + std::string filePath = "../Levels/ObjectSetups/Level" + levelString + ".png"; std::vector image; //the raw pixels unsigned int width, height; unsigned error = lodepng::decode(image, width, height, filePath); if (error) { std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl; } + //iterate over all pixels of the image for(unsigned int rowNum = 0; rowNum < height; rowNum++){ for(unsigned int columnNum = 0; columnNum < width; columnNum++){ @@ -37,25 +36,30 @@ int main( int argc, char *argv[] ){ image[pixel+1] = temp[0]; image[pixel+2] = temp[1]; }else{ - conv.updateComposition(image[pixel], image[pixel+1], image[pixel+2], rowNum, columnNum); + conv.updateComposition(image[pixel+1], image[pixel+2], rowNum, columnNum); idFound[image[pixel+1]][image[pixel+2]] = true; } } } } + //write ids back to the setup png error = lodepng::encode(filePath, image, width, height); if(error) { std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl; } + //delete compositions that were not in the png anymore - for (int i=0; i<256; i++){ - for (int j=0; j<256; j++){ - if (idFound[i][j] == false){ - //TODO if (exists)? + + for (int i=1; i<=conv.getNextId()[1]; i++){ + for (int j=1; j<256; j++){ + if (! idFound[i][j]){ conv.deleteComposition(i,j); } } } + + //save the xml + conv.save(); }