skeleton runs now, only xml read/write missing

This commit is contained in:
Steffen Fündgens 2014-11-25 13:51:25 +01:00
parent 91099d91a0
commit 20e7c732e2
5 changed files with 66 additions and 16 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 B

View File

@ -1,4 +1,27 @@
#include "converter.hh" #include "converter.hh"
#include <fstream>
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(){ Converter::Converter(){
} }
@ -6,14 +29,19 @@ Converter::Converter(){
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 //TODO
} }
std::vector<unsigned int> Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){ std::vector<unsigned int> Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){
//TODO //TODO
std::vector<unsigned int> ret = {0,0}; std::vector<unsigned int> ret = nextId;
nextId[1] += 1;
if (nextId[1] == 256){
nextId[1] = 1;
nextId[0] +=1;
}
return ret; return ret;
} }
@ -21,6 +49,14 @@ void Converter::deleteComposition(unsigned int idG, unsigned int idB){
//TODO //TODO
} }
void Converter::save(){
const char* charXmlFile = xmlFile.c_str();
doc->SaveFile(charXmlFile);
}
std::vector<unsigned int> Converter::getNextId(){
return nextId;
}
void Converter::newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){ void Converter::newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ){

View File

@ -1,17 +1,27 @@
#ifndef CONVERTER_INCLUDED #ifndef CONVERTER_INCLUDED
#define CONVERTER_INCLUDED #define CONVERTER_INCLUDED
#include <vector> #include <vector>
#include <string>
#include "tinyxml2.hh" #include "tinyxml2.hh"
using namespace tinyxml2;
class Converter { class Converter {
public: public:
Converter(std::string level);
Converter(); Converter();
~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<unsigned int> newComposition(unsigned int type, unsigned int posX, unsigned int posZ); std::vector<unsigned int> newComposition(unsigned int type, unsigned int posX, unsigned int posZ);
void deleteComposition(unsigned int idG, unsigned int idB); void deleteComposition(unsigned int idG, unsigned int idB);
void save();
std::vector<unsigned int> getNextId();
private: private:
std::vector<unsigned int> 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); void newObject(unsigned int type, unsigned int idG, unsigned int idB, unsigned int posX, unsigned int posZ);
}; };
#endif #endif

View File

@ -4,26 +4,25 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
int main( int argc, char *argv[] ){ int main( int argc, char *argv[] ){
unsigned int level=atoi(argv[1]); std::string levelString = argv[1];
Converter conv; 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++){
for (int j=0; j<256; j++){ for (int j=0; j<256; j++){
idFound[i][j] = false; idFound[i][j] = false;
} }
} }
//read the setup png
char levelChar[2]; //read the setup png
sprintf (levelChar,"%i", level); std::string filePath = "../Levels/ObjectSetups/Level" + levelString + ".png";
std::string levelString = levelChar;
std::string filePath = "../Levels/ObjectSetups/Lvl" + 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;
} }
//iterate over all pixels of the image //iterate over all pixels of the image
for(unsigned int rowNum = 0; rowNum < height; rowNum++){ for(unsigned int rowNum = 0; rowNum < height; rowNum++){
for(unsigned int columnNum = 0; columnNum < width; columnNum++){ 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+1] = temp[0];
image[pixel+2] = temp[1]; image[pixel+2] = temp[1];
}else{ }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; idFound[image[pixel+1]][image[pixel+2]] = true;
} }
} }
} }
} }
//write ids back to the setup png //write ids back to the setup png
error = lodepng::encode(filePath, image, width, height); error = lodepng::encode(filePath, image, width, height);
if(error) { if(error) {
std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl; std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
} }
//delete compositions that were not in the png anymore //delete compositions that were not in the png anymore
for (int i=0; i<256; i++){
for (int j=0; j<256; j++){ for (int i=1; i<=conv.getNextId()[1]; i++){
if (idFound[i][j] == false){ for (int j=1; j<256; j++){
//TODO if (exists)? if (! idFound[i][j]){
conv.deleteComposition(i,j); conv.deleteComposition(i,j);
} }
} }
} }
//save the xml
conv.save();
} }