Converter can now reuse IDs of deleted compositions.
This commit is contained in:
parent
f3580555cd
commit
2dee196307
@ -33,8 +33,14 @@ Converter::Converter(std::string level){
|
|||||||
|
|
||||||
|
|
||||||
//Load the Level xml file
|
//Load the Level xml file
|
||||||
|
nextID.push_back(0);
|
||||||
nextID.push_back(1);
|
nextID.push_back(1);
|
||||||
nextID.push_back(1);
|
for (int i=0; i<256; i++){
|
||||||
|
for (int j=0; j<256; j++){
|
||||||
|
idUsed[i][j] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
idUsed[0][0] = true;
|
||||||
const char* charXmlFile = xmlFile.c_str();
|
const char* charXmlFile = xmlFile.c_str();
|
||||||
doc->LoadFile(charXmlFile);
|
doc->LoadFile(charXmlFile);
|
||||||
if (doc->ErrorID()!=0){
|
if (doc->ErrorID()!=0){
|
||||||
@ -127,14 +133,11 @@ Converter::Converter(std::string level){
|
|||||||
}else{
|
}else{
|
||||||
dst << src.rdbuf();
|
dst << src.rdbuf();
|
||||||
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
||||||
int idGreen, idBlue;
|
|
||||||
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
||||||
|
int idGreen = 0, idBlue = 0;
|
||||||
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen));
|
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen));
|
||||||
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
|
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
|
||||||
if(idGreen > nextID[0] || (idGreen == nextID[0] && idBlue > nextID[1])){
|
idUsed[idGreen][idBlue] = true;
|
||||||
nextID[0] = idGreen;
|
|
||||||
nextID[1] = idBlue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,6 +151,17 @@ Converter::~Converter(){
|
|||||||
std::vector<int> Converter::newComposition(int type, float posX, float posZ){
|
std::vector<int> Converter::newComposition(int type, float posX, float posZ){
|
||||||
XMLElement* newComposition = doc->NewElement("composition");
|
XMLElement* newComposition = doc->NewElement("composition");
|
||||||
doc->InsertFirstChild(newComposition);
|
doc->InsertFirstChild(newComposition);
|
||||||
|
while(idUsed[nextID[0]][nextID[1]]){
|
||||||
|
nextID[1] += 1;
|
||||||
|
if (nextID[1] == 256){
|
||||||
|
nextID[1] = 0;
|
||||||
|
nextID[0] +=1;
|
||||||
|
if (nextID[0] == 256){
|
||||||
|
std::cout << "Can not have more than 65535 compositions." << std::endl;
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
XMLElement* typeID = doc->NewElement("typeID");
|
XMLElement* typeID = doc->NewElement("typeID");
|
||||||
XMLElement* idBlue = doc->NewElement("idBlue");
|
XMLElement* idBlue = doc->NewElement("idBlue");
|
||||||
@ -230,13 +244,8 @@ std::vector<int> Converter::newComposition(int type, float posX, float posZ){
|
|||||||
trigger->InsertEndChild(toChangeIdBlue);
|
trigger->InsertEndChild(toChangeIdBlue);
|
||||||
trigger->InsertEndChild(toChangeObjNum);
|
trigger->InsertEndChild(toChangeObjNum);
|
||||||
|
|
||||||
std::vector<int> ret = nextID;
|
idUsed[nextID[0]][nextID[1]] = true;
|
||||||
nextID[1] += 1;
|
return nextID;
|
||||||
if (nextID[1] == 255){
|
|
||||||
nextID[1] = 0;
|
|
||||||
nextID[0] +=1;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Converter::updateComposition(int idG, int idB, float posX, float posZ){
|
void Converter::updateComposition(int idG, int idB, float posX, float posZ){
|
||||||
@ -249,6 +258,7 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){
|
|||||||
if(idGreen == idG && idBlue == idB){
|
if(idGreen == idG && idBlue == idB){
|
||||||
if (compositionExists){
|
if (compositionExists){
|
||||||
std::cout << "An ID is used for multiple compositions in the xml." << std::endl;
|
std::cout << "An ID is used for multiple compositions in the xml." << std::endl;
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
bool manualPos;
|
bool manualPos;
|
||||||
errorCheck(thisComposition->FirstChildElement("manualPos")->QueryBoolText(&manualPos));
|
errorCheck(thisComposition->FirstChildElement("manualPos")->QueryBoolText(&manualPos));
|
||||||
@ -267,8 +277,8 @@ void Converter::updateComposition(int idG, int idB, float posX, float posZ){
|
|||||||
|
|
||||||
void Converter::deleteComposition(int idG, int idB){
|
void Converter::deleteComposition(int idG, int idB){
|
||||||
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
XMLElement* thisComposition = doc->FirstChildElement("composition");
|
||||||
int idGreen, idBlue;
|
|
||||||
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
for(; thisComposition; thisComposition=thisComposition->NextSiblingElement("composition")){
|
||||||
|
int idGreen = 0, idBlue = 0;
|
||||||
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen));
|
errorCheck(thisComposition->FirstChildElement("idGreen")->QueryIntText(&idGreen));
|
||||||
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
|
errorCheck(thisComposition->FirstChildElement("idBlue")->QueryIntText(&idBlue));
|
||||||
if(idGreen == idG && idBlue == idB){
|
if(idGreen == idG && idBlue == idB){
|
||||||
@ -304,5 +314,6 @@ void Converter::errorCheck(XMLError error){
|
|||||||
else {
|
else {
|
||||||
printf("Unknown error.\n");
|
printf("Unknown error.\n");
|
||||||
}
|
}
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ class Converter {
|
|||||||
private:
|
private:
|
||||||
void errorCheck(XMLError error);
|
void errorCheck(XMLError error);
|
||||||
std::vector<int> nextID;
|
std::vector<int> nextID;
|
||||||
|
bool idUsed[256][256];
|
||||||
std::string xmlFile;
|
std::string xmlFile;
|
||||||
XMLDocument* doc = new XMLDocument();
|
XMLDocument* doc = new XMLDocument();
|
||||||
XMLDocument* compositions = new XMLDocument();
|
XMLDocument* compositions = new XMLDocument();
|
||||||
|
@ -25,6 +25,7 @@ int main( int argc, char *argv[] ){
|
|||||||
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;
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//iterate over all pixels of the image
|
//iterate over all pixels of the image
|
||||||
@ -33,13 +34,13 @@ int main( int argc, char *argv[] ){
|
|||||||
unsigned int pixel = (rowNum*width+columnNum)*4;
|
unsigned int pixel = (rowNum*width+columnNum)*4;
|
||||||
//if there is a composition here, adjust the xml and image
|
//if there is a composition here, adjust the xml and image
|
||||||
if(image[pixel]!=0 && image[pixel]!=255){
|
if(image[pixel]!=0 && image[pixel]!=255){
|
||||||
if((image[pixel+1]==0 && image[pixel+2]==0) || (image[pixel+1]==255 && image[pixel+2]==255)){
|
if(image[pixel+1]==0 && image[pixel+2]==0){//composition has no ID
|
||||||
std::vector<int> temp;
|
std::vector<int> temp;
|
||||||
temp = conv.newComposition(image[pixel], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width);
|
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{//composition has an ID
|
||||||
conv.updateComposition(image[pixel+1], image[pixel+2], 0.5+rowNum-0.5*height, 0.5+columnNum-0.5*width);
|
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;
|
||||||
}
|
}
|
||||||
@ -51,10 +52,11 @@ int main( int argc, char *argv[] ){
|
|||||||
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;
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//delete compositions that were not in the png anymore
|
//delete compositions that were not in the png anymore
|
||||||
for (int i=0; i<=conv.getNextID()[1]; i++){
|
for (int i=0; i<256; i++){
|
||||||
for (int j=0; j<256; j++){
|
for (int j=0; j<256; j++){
|
||||||
if (! idFound[i][j]){
|
if (! idFound[i][j]){
|
||||||
conv.deleteComposition(i,j);
|
conv.deleteComposition(i,j);
|
||||||
|
Loading…
Reference in New Issue
Block a user