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 <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(){
}
@ -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<unsigned int> Converter::newComposition(unsigned int type, unsigned int posX, unsigned int posZ){
//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;
}
@ -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<unsigned int> Converter::getNextId(){
return nextId;
}
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
#define CONVERTER_INCLUDED
#include <vector>
#include <string>
#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<unsigned int> newComposition(unsigned int type, unsigned int posX, unsigned int posZ);
void deleteComposition(unsigned int idG, unsigned int idB);
void save();
std::vector<unsigned int> getNextId();
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);
};
#endif

View File

@ -4,26 +4,25 @@
#include <vector>
#include <iostream>
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<unsigned char> 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();
}