2014-11-21 14:43:27 +00:00
# include "converter.hh"
2014-11-25 12:51:25 +00:00
# include <fstream>
2014-12-05 15:18:48 +00:00
# include <sys/stat.h>
2014-12-08 13:10:08 +00:00
# include <iostream>
2014-11-25 12:51:25 +00:00
using namespace tinyxml2 ;
2015-03-14 13:54:43 +00:00
Converter : : Converter ( std : : string levelPath , std : : string levelName , std : : string compositionsPath ) {
xmlFile = levelPath + levelName + " .xml " ;
2014-11-25 12:51:25 +00:00
2014-11-28 16:05:49 +00:00
//Load Compositions
2015-02-20 12:11:05 +00:00
std : : string stringCompositions = compositionsPath ;
const char * charCompositions = stringCompositions . c_str ( ) ;
2014-11-28 16:05:49 +00:00
compositions - > LoadFile ( charCompositions ) ;
if ( compositions - > ErrorID ( ) ! = 0 ) {
2014-12-08 12:28:50 +00:00
printf ( " Could not open Compositions! \n " ) ;
2015-01-27 11:21:30 +00:00
exit ( - 1 ) ;
2014-11-28 16:05:49 +00:00
}
2014-12-05 15:18:48 +00:00
//Create a backup of the current Level png file, if no backup exists
2015-03-14 13:54:43 +00:00
std : : string pngFile = levelPath + levelName + " .png " ;
std : : string backupPNG = levelPath + levelName + " Backup.png " ;
2014-12-05 15:18:48 +00:00
struct stat buf ;
if ( stat ( backupPNG . c_str ( ) , & buf ) ! = 0 ) {
std : : ifstream src ( pngFile , std : : ios : : binary ) ;
std : : ofstream dst ( backupPNG , std : : ios : : binary ) ;
dst < < src . rdbuf ( ) ;
}
2014-11-28 16:05:49 +00:00
//Load the Level xml file
2015-01-23 15:02:50 +00:00
nextID . push_back ( 0 ) ;
2014-12-01 13:09:55 +00:00
nextID . push_back ( 1 ) ;
2015-01-23 15:02:50 +00:00
for ( int i = 0 ; i < 256 ; i + + ) {
for ( int j = 0 ; j < 256 ; j + + ) {
idUsed [ i ] [ j ] = false ;
}
}
idUsed [ 0 ] [ 0 ] = true ;
2014-12-12 11:31:24 +00:00
const char * charXmlFile = xmlFile . c_str ( ) ;
doc - > LoadFile ( charXmlFile ) ;
2015-01-27 10:50:51 +00:00
//check if the xml file did not already exist
2014-11-25 12:51:25 +00:00
if ( doc - > ErrorID ( ) ! = 0 ) {
2015-01-27 10:50:51 +00:00
std : : string answer ;
printf ( " Could not open xml, do you want to create a new xml? (y/n) \n " ) ;
std : : cin > > answer ;
while ( answer . compare ( " y " ) ! = 0 & & answer . compare ( " n " ) ! = 0 ) {
2015-01-27 11:21:30 +00:00
printf ( " Answer with y or n \n " ) ;
2015-01-27 10:50:51 +00:00
std : : cin > > answer ;
}
if ( answer . compare ( " n " ) = = 0 ) {
exit ( - 1 ) ;
}
printf ( " Creating new xml. \n " ) ;
2015-01-06 12:14:30 +00:00
//Create all global Lightingparameters with Dummy-Values
2014-12-12 14:41:17 +00:00
std : : vector < XMLElement * > lightAttributes ;
lightAttributes . push_back ( doc - > NewElement ( " xOffset " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " yOffset " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " zOffset " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " rColour " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " gColour " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " bColour " ) ) ;
lightAttributes . push_back ( doc - > NewElement ( " intensity " ) ) ;
XMLElement * rColourAmbient = doc - > NewElement ( " rColour " ) ;
XMLElement * gColourAmbient = doc - > NewElement ( " gColour " ) ;
XMLElement * bColourAmbient = doc - > NewElement ( " bColour " ) ;
2015-03-14 13:54:43 +00:00
XMLElement * rColourFogDay = doc - > NewElement ( " rColour " ) ;
XMLElement * gColourFogDay = doc - > NewElement ( " gColour " ) ;
XMLElement * bColourFogDay = doc - > NewElement ( " bColour " ) ;
XMLElement * alphaFogDay = doc - > NewElement ( " alpha " ) ;
XMLElement * rColourFogRise = doc - > NewElement ( " rColour " ) ;
XMLElement * gColourFogRise = doc - > NewElement ( " gColour " ) ;
XMLElement * bColourFogRise = doc - > NewElement ( " bColour " ) ;
XMLElement * alphaFogRise = doc - > NewElement ( " alpha " ) ;
XMLElement * rColourFogNight = doc - > NewElement ( " rColour " ) ;
XMLElement * gColourFogNight = doc - > NewElement ( " gColour " ) ;
XMLElement * bColourFogNight = doc - > NewElement ( " bColour " ) ;
XMLElement * alphaFogNight = doc - > NewElement ( " alpha " ) ;
2014-12-12 11:31:24 +00:00
2014-12-12 14:41:17 +00:00
lightAttributes [ 0 ] - > SetText ( " -1.0 " ) ;
lightAttributes [ 1 ] - > SetText ( " 1.5 " ) ;
lightAttributes [ 2 ] - > SetText ( " 1.0 " ) ;
lightAttributes [ 3 ] - > SetText ( " 1.0 " ) ;
lightAttributes [ 4 ] - > SetText ( " 1.0 " ) ;
lightAttributes [ 5 ] - > SetText ( " 0.9 " ) ;
lightAttributes [ 6 ] - > SetText ( " 0.2 " ) ;
rColourAmbient - > SetText ( " 1.0 " ) ;
gColourAmbient - > SetText ( " 1.0 " ) ;
bColourAmbient - > SetText ( " 1.0 " ) ;
2015-03-14 13:54:43 +00:00
rColourFogDay - > SetText ( " 0.57 " ) ;
gColourFogDay - > SetText ( " 0.80 " ) ;
bColourFogDay - > SetText ( " 0.98 " ) ;
alphaFogDay - > SetText ( " 1.0 " ) ;
rColourFogRise - > SetText ( " 0.88 " ) ;
gColourFogRise - > SetText ( " 0.38 " ) ;
bColourFogRise - > SetText ( " 0.38 " ) ;
alphaFogRise - > SetText ( " 1.0 " ) ;
rColourFogNight - > SetText ( " 0.09 " ) ;
gColourFogNight - > SetText ( " 0.1 " ) ;
bColourFogNight - > SetText ( " 0.24 " ) ;
alphaFogNight - > SetText ( " 1.0 " ) ;
2014-12-12 14:41:17 +00:00
XMLElement * ambientLight = doc - > NewElement ( " ambientLight " ) ;
2015-03-14 13:54:43 +00:00
XMLElement * fogColourDay = doc - > NewElement ( " fogColourDay " ) ;
XMLElement * fogColourRise = doc - > NewElement ( " fogColourRise " ) ;
XMLElement * fogColourNight = doc - > NewElement ( " fogColourNight " ) ;
2014-12-12 14:41:17 +00:00
XMLElement * directionalLight = doc - > NewElement ( " directionalLight " ) ;
ambientLight - > InsertEndChild ( rColourAmbient ) ;
ambientLight - > InsertEndChild ( gColourAmbient ) ;
ambientLight - > InsertEndChild ( bColourAmbient ) ;
2015-03-14 13:54:43 +00:00
fogColourDay - > InsertEndChild ( rColourFogDay ) ;
fogColourDay - > InsertEndChild ( gColourFogDay ) ;
fogColourDay - > InsertEndChild ( bColourFogDay ) ;
fogColourDay - > InsertEndChild ( alphaFogDay ) ;
fogColourRise - > InsertEndChild ( rColourFogRise ) ;
fogColourRise - > InsertEndChild ( gColourFogRise ) ;
fogColourRise - > InsertEndChild ( bColourFogRise ) ;
fogColourRise - > InsertEndChild ( alphaFogRise ) ;
fogColourNight - > InsertEndChild ( rColourFogNight ) ;
fogColourNight - > InsertEndChild ( gColourFogNight ) ;
fogColourNight - > InsertEndChild ( bColourFogNight ) ;
fogColourNight - > InsertEndChild ( alphaFogNight ) ;
2014-12-12 14:41:17 +00:00
for ( int i = 0 ; i < 7 ; i + + ) {
directionalLight - > InsertEndChild ( lightAttributes [ i ] ) ;
}
2015-01-06 12:14:30 +00:00
doc - > InsertEndChild ( ambientLight ) ;
2015-03-14 13:54:43 +00:00
doc - > InsertEndChild ( fogColourDay ) ;
doc - > InsertEndChild ( fogColourRise ) ;
doc - > InsertEndChild ( fogColourNight ) ;
2015-01-06 12:14:30 +00:00
doc - > InsertEndChild ( directionalLight ) ;
2014-12-12 14:41:17 +00:00
2015-02-06 11:37:00 +00:00
//Create global terrain Element
XMLElement * terrain = doc - > NewElement ( " terrain " ) ;
2015-03-14 13:54:43 +00:00
XMLElement * terrainHeightmap = doc - > NewElement ( " heightmap " ) ;
2015-02-06 11:37:00 +00:00
XMLElement * terrainTexture = doc - > NewElement ( " texture " ) ;
XMLElement * terrainAmbientFactor = doc - > NewElement ( " ambientFactor " ) ;
XMLElement * terrainDiffuseFactor = doc - > NewElement ( " diffuseFactor " ) ;
XMLElement * terrainSpecularFactor = doc - > NewElement ( " specularFactor " ) ;
XMLElement * terrainShininess = doc - > NewElement ( " shininess " ) ;
2015-03-14 13:54:43 +00:00
std : : string heightmapPath = " heightmap " + levelName + " .png " ;
terrainHeightmap - > SetText ( heightmapPath . c_str ( ) ) ;
terrainTexture - > SetText ( " terrainTexture.png " ) ;
2015-02-06 11:37:00 +00:00
terrainAmbientFactor - > SetText ( " 0.1 " ) ;
terrainDiffuseFactor - > SetText ( " 0.8 " ) ;
terrainSpecularFactor - > SetText ( " 0.2 " ) ;
2015-03-14 13:54:43 +00:00
terrainShininess - > SetText ( " 1.0 " ) ;
terrain - > InsertEndChild ( terrainHeightmap ) ;
2015-02-06 11:37:00 +00:00
terrain - > InsertEndChild ( terrainTexture ) ;
terrain - > InsertEndChild ( terrainAmbientFactor ) ;
terrain - > InsertEndChild ( terrainDiffuseFactor ) ;
terrain - > InsertEndChild ( terrainSpecularFactor ) ;
terrain - > InsertEndChild ( terrainShininess ) ;
doc - > InsertEndChild ( terrain ) ;
2015-01-06 12:14:30 +00:00
//Create global skydome Element
2014-12-12 14:41:17 +00:00
XMLElement * skydome = doc - > NewElement ( " skydome " ) ;
2015-03-14 13:54:43 +00:00
XMLElement * skydomeModel = doc - > NewElement ( " model " ) ;
2014-12-12 14:41:17 +00:00
XMLElement * skydomeTexture = doc - > NewElement ( " texture " ) ;
2015-03-14 13:54:43 +00:00
XMLElement * skydomeTextureNight = doc - > NewElement ( " nightTexture " ) ;
skydomeModel - > SetText ( " skydome.obj " ) ;
2014-12-12 14:41:17 +00:00
skydomeTexture - > SetText ( " skydome.png " ) ;
2015-03-14 13:54:43 +00:00
skydomeTextureNight - > SetText ( " skydomeNight.png " ) ;
skydome - > InsertEndChild ( skydomeModel ) ;
2014-12-12 14:41:17 +00:00
skydome - > InsertEndChild ( skydomeTexture ) ;
2015-03-14 13:54:43 +00:00
skydome - > InsertEndChild ( skydomeTextureNight ) ;
2014-12-12 14:41:17 +00:00
doc - > InsertEndChild ( skydome ) ;
2015-01-06 12:14:30 +00:00
//Create global physics parameters
XMLElement * physics = doc - > NewElement ( " physics " ) ;
2015-01-17 13:07:12 +00:00
XMLElement * playerFriction = doc - > NewElement ( " friction " ) ;
XMLElement * playerStrength = doc - > NewElement ( " strength " ) ;
playerFriction - > SetText ( " 0.9 " ) ;
2015-03-14 13:54:43 +00:00
playerStrength - > SetText ( " 300.0 " ) ;
2015-01-17 13:07:12 +00:00
physics - > InsertEndChild ( playerFriction ) ;
physics - > InsertEndChild ( playerStrength ) ;
2015-01-06 12:14:30 +00:00
doc - > InsertEndChild ( physics ) ;
2015-01-27 10:50:51 +00:00
//Create positionConstraint Dummy
2015-01-17 13:07:12 +00:00
XMLElement * positionConstraint = doc - > NewElement ( " positionConstraint " ) ;
XMLElement * positionConstraintObjectNum = doc - > NewElement ( " objectNum " ) ;
XMLElement * positionConstraintXPos = doc - > NewElement ( " xPosition " ) ;
XMLElement * positionConstraintYPos = doc - > NewElement ( " yPosition " ) ;
XMLElement * positionConstraintZPos = doc - > NewElement ( " zPosition " ) ;
XMLElement * positionConstraintStrength = doc - > NewElement ( " strength " ) ;
positionConstraintObjectNum - > SetText ( " 0 " ) ;
positionConstraintXPos - > SetText ( " 0.0 " ) ;
positionConstraintYPos - > SetText ( " 0.0 " ) ;
positionConstraintZPos - > SetText ( " 0.0 " ) ;
positionConstraintStrength - > SetText ( " 100.0 " ) ;
positionConstraint - > InsertEndChild ( positionConstraintObjectNum ) ;
positionConstraint - > InsertEndChild ( positionConstraintXPos ) ;
positionConstraint - > InsertEndChild ( positionConstraintYPos ) ;
positionConstraint - > InsertEndChild ( positionConstraintZPos ) ;
positionConstraint - > InsertEndChild ( positionConstraintStrength ) ;
doc - > InsertEndChild ( positionConstraint ) ;
2014-12-01 13:09:55 +00:00
} else {
2015-01-27 10:50:51 +00:00
//Create a backup of the current Level xml file
2015-03-14 13:54:43 +00:00
std : : string backupXML = levelPath + levelName + " Backup.xml " ;
2015-01-27 10:50:51 +00:00
std : : ifstream src ( xmlFile , std : : ios : : binary ) ;
std : : ofstream dst ( backupXML , std : : ios : : binary ) ;
2014-12-16 12:46:49 +00:00
dst < < src . rdbuf ( ) ;
2015-01-27 10:50:51 +00:00
//Check what IDs are already in use
2014-12-01 13:09:55 +00:00
XMLElement * thisComposition = doc - > FirstChildElement ( " composition " ) ;
2014-12-05 11:47:02 +00:00
for ( ; thisComposition ; thisComposition = thisComposition - > NextSiblingElement ( " composition " ) ) {
2015-02-20 12:11:05 +00:00
int idGreen = queryInt ( thisComposition , " idGreen " ) ;
int idBlue = queryInt ( thisComposition , " idBlue " ) ;
2015-01-23 15:02:50 +00:00
idUsed [ idGreen ] [ idBlue ] = true ;
2014-12-01 13:09:55 +00:00
}
2014-11-25 12:51:25 +00:00
}
}
2014-11-21 14:43:27 +00:00
Converter : : Converter ( ) {
}
Converter : : ~ Converter ( ) {
}
2014-12-08 13:24:26 +00:00
std : : vector < int > Converter : : newComposition ( int type , float posX , float posZ ) {
2015-02-20 14:06:43 +00:00
int oldIDGreen , oldIDBlue ;
bool alreadyExists = false ;
XMLElement * thisComposition = doc - > FirstChildElement ( " composition " ) ;
for ( ; thisComposition ; thisComposition = thisComposition - > NextSiblingElement ( " composition " ) ) {
float xPos = queryFloat ( thisComposition , " xPos " ) ;
float zPos = queryFloat ( thisComposition , " zPos " ) ;
int typeID = queryInt ( thisComposition , " typeID " ) ;
if ( xPos = = posX & & zPos = = posZ & & typeID = = type ) {
if ( alreadyExists ) {
std : : cout < < " At the position " < < xPos < < " , " < < zPos < < " multiple compositions with the ID " < < typeID < < " exist in the xml. " < < std : : endl ;
exit ( - 1 ) ;
}
oldIDGreen = queryInt ( thisComposition , " idGreen " ) ;
oldIDBlue = queryInt ( thisComposition , " idBlue " ) ;
alreadyExists = true ;
}
}
if ( alreadyExists ) {
if ( ! idUsed [ oldIDGreen ] [ oldIDBlue ] ) {
std : : cout < < " The composition with ID " < < oldIDGreen < < " , " < < oldIDBlue < < " exists in the xml but the converter thinks it does not. " < < std : : endl ;
exit ( - 1 ) ;
}
std : : vector < int > oldID ;
oldID . push_back ( oldIDGreen ) ;
oldID . push_back ( oldIDBlue ) ;
return oldID ;
}
2015-01-27 11:21:30 +00:00
bool full = false ;
2015-01-23 15:02:50 +00:00
while ( idUsed [ nextID [ 0 ] ] [ nextID [ 1 ] ] ) {
nextID [ 1 ] + = 1 ;
if ( nextID [ 1 ] = = 256 ) {
nextID [ 1 ] = 0 ;
nextID [ 0 ] + = 1 ;
if ( nextID [ 0 ] = = 256 ) {
2015-01-27 11:21:30 +00:00
if ( full ) {
printf ( " Can not have more than 65535 compositions. \n " ) ;
exit ( - 1 ) ;
}
nextID [ 0 ] = 0 ;
full = true ;
2015-01-23 15:02:50 +00:00
}
}
}
2015-02-20 14:06:43 +00:00
2015-01-27 10:50:51 +00:00
XMLElement * newComposition = doc - > NewElement ( " composition " ) ;
2014-12-01 13:09:55 +00:00
XMLElement * typeID = doc - > NewElement ( " typeID " ) ;
XMLElement * idBlue = doc - > NewElement ( " idBlue " ) ;
XMLElement * idGreen = doc - > NewElement ( " idGreen " ) ;
XMLElement * zPos = doc - > NewElement ( " zPos " ) ;
XMLElement * yOffset = doc - > NewElement ( " yOffset " ) ;
XMLElement * xPos = doc - > NewElement ( " xPos " ) ;
2014-12-08 13:10:08 +00:00
XMLElement * manualPos = doc - > NewElement ( " manualPos " ) ;
2014-12-01 13:09:55 +00:00
XMLElement * zRot = doc - > NewElement ( " zRot " ) ;
XMLElement * yRot = doc - > NewElement ( " yRot " ) ;
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 ( ) ) ;
2014-12-08 13:10:08 +00:00
manualPos - > SetText ( " false " ) ;
2014-12-01 13:09:55 +00:00
zRot - > SetText ( " 0.0 " ) ;
yRot - > SetText ( " 0.0 " ) ;
xRot - > SetText ( " 0.0 " ) ;
scale - > SetText ( " 1.0 " ) ;
newComposition - > InsertFirstChild ( typeID ) ;
newComposition - > InsertFirstChild ( idBlue ) ;
newComposition - > InsertFirstChild ( idGreen ) ;
newComposition - > InsertFirstChild ( zPos ) ;
newComposition - > InsertFirstChild ( yOffset ) ;
newComposition - > InsertFirstChild ( xPos ) ;
2014-12-08 13:10:08 +00:00
newComposition - > InsertFirstChild ( manualPos ) ;
2014-12-01 13:09:55 +00:00
newComposition - > InsertFirstChild ( zRot ) ;
newComposition - > InsertFirstChild ( yRot ) ;
newComposition - > InsertFirstChild ( xRot ) ;
newComposition - > InsertFirstChild ( scale ) ;
2014-12-16 12:46:49 +00:00
//Create a Dummy-trigger
XMLElement * trigger = doc - > NewElement ( " trigger " ) ;
newComposition - > InsertEndChild ( trigger ) ;
XMLElement * name = doc - > NewElement ( " name " ) ;
2015-03-04 14:45:48 +00:00
XMLElement * undo = doc - > NewElement ( " undo " ) ;
2014-12-16 12:46:49 +00:00
XMLElement * xPosition = doc - > NewElement ( " xPosition " ) ;
XMLElement * yPosition = doc - > NewElement ( " yPosition " ) ;
XMLElement * zPosition = doc - > NewElement ( " zPosition " ) ;
2014-12-19 15:15:29 +00:00
XMLElement * targetIdGreen = doc - > NewElement ( " targetIdGreen " ) ;
XMLElement * targetIdBlue = doc - > NewElement ( " targetIdBlue " ) ;
2014-12-16 12:46:49 +00:00
XMLElement * distance = doc - > NewElement ( " distance " ) ;
XMLElement * isBiggerThan = doc - > NewElement ( " isBiggerThan " ) ;
XMLElement * objectNum = doc - > NewElement ( " objectNum " ) ;
2015-01-13 12:52:22 +00:00
XMLElement * luaScript = doc - > NewElement ( " luaScript " ) ;
XMLElement * toChangeIdGreen = doc - > NewElement ( " toChangeIdGreen " ) ;
XMLElement * toChangeIdBlue = doc - > NewElement ( " toChangeIdBlue " ) ;
XMLElement * toChangeObjNum = doc - > NewElement ( " toChangeObjNum " ) ;
2014-12-19 15:15:29 +00:00
2014-12-16 12:46:49 +00:00
name - > SetText ( " - " ) ;
2015-03-04 14:45:48 +00:00
undo - > SetText ( " false " ) ;
2014-12-19 15:15:29 +00:00
xPosition - > SetText ( " 0 " ) ;
yPosition - > SetText ( " 0 " ) ;
zPosition - > SetText ( " 0 " ) ;
targetIdGreen - > SetText ( " - " ) ;
targetIdBlue - > SetText ( " - " ) ;
2014-12-16 12:46:49 +00:00
distance - > SetText ( " 1.0 " ) ;
isBiggerThan - > SetText ( " false " ) ;
2014-12-19 12:35:17 +00:00
objectNum - > SetText ( " 0 " ) ;
2015-01-13 12:52:22 +00:00
luaScript - > SetText ( " - " ) ;
2015-01-13 17:18:13 +00:00
toChangeIdGreen - > SetText ( " 0 " ) ;
toChangeIdBlue - > SetText ( " 0 " ) ;
toChangeObjNum - > SetText ( " 0 " ) ;
2014-12-19 15:15:29 +00:00
2014-12-16 12:46:49 +00:00
trigger - > InsertEndChild ( name ) ;
2015-03-04 14:45:48 +00:00
trigger - > InsertEndChild ( undo ) ;
2014-12-16 12:46:49 +00:00
trigger - > InsertEndChild ( xPosition ) ;
trigger - > InsertEndChild ( yPosition ) ;
trigger - > InsertEndChild ( zPosition ) ;
2014-12-19 15:15:29 +00:00
trigger - > InsertEndChild ( targetIdGreen ) ;
trigger - > InsertEndChild ( targetIdBlue ) ;
2014-12-16 12:46:49 +00:00
trigger - > InsertEndChild ( distance ) ;
trigger - > InsertEndChild ( isBiggerThan ) ;
trigger - > InsertEndChild ( objectNum ) ;
2015-01-13 12:52:22 +00:00
trigger - > InsertEndChild ( luaScript ) ;
trigger - > InsertEndChild ( toChangeIdGreen ) ;
trigger - > InsertEndChild ( toChangeIdBlue ) ;
trigger - > InsertEndChild ( toChangeObjNum ) ;
2014-12-19 15:15:29 +00:00
2015-01-23 15:02:50 +00:00
idUsed [ nextID [ 0 ] ] [ nextID [ 1 ] ] = true ;
2015-01-27 10:50:51 +00:00
doc - > InsertFirstChild ( newComposition ) ;
2015-01-23 15:02:50 +00:00
return nextID ;
2014-11-21 14:43:27 +00:00
}
2014-12-08 12:33:38 +00:00
void Converter : : updateComposition ( int idG , int idB , float posX , float posZ ) {
2014-12-01 13:09:55 +00:00
XMLElement * thisComposition = doc - > FirstChildElement ( " composition " ) ;
2014-12-08 13:10:08 +00:00
bool compositionExists = false ;
2014-12-05 11:47:02 +00:00
for ( ; thisComposition ; thisComposition = thisComposition - > NextSiblingElement ( " composition " ) ) {
2015-02-20 12:11:05 +00:00
int idGreen = queryInt ( thisComposition , " idGreen " ) ;
int idBlue = queryInt ( thisComposition , " idBlue " ) ;
2014-12-01 13:09:55 +00:00
if ( idGreen = = idG & & idBlue = = idB ) {
2015-01-19 10:15:41 +00:00
if ( compositionExists ) {
2015-01-27 11:21:30 +00:00
std : : cout < < " The ID " < < idGreen < < " , " < < idBlue < < " is used for multiple compositions in the xml. " < < std : : endl ;
2015-01-23 15:02:50 +00:00
exit ( - 1 ) ;
2015-01-19 10:15:41 +00:00
}
2015-02-20 12:11:05 +00:00
bool manualPos = queryBool ( thisComposition , " manualPos " ) ;
2014-12-08 13:10:08 +00:00
if ( ! manualPos ) {
thisComposition - > FirstChildElement ( " xPos " ) - > SetText ( std : : to_string ( posX ) . c_str ( ) ) ;
thisComposition - > FirstChildElement ( " zPos " ) - > SetText ( std : : to_string ( posZ ) . c_str ( ) ) ;
}
compositionExists = true ;
2014-11-28 16:05:49 +00:00
}
}
2014-12-08 13:10:08 +00:00
if ( ! compositionExists ) {
2015-02-13 17:17:47 +00:00
std : : cout < < " A composition has the ID " < < idG < < " , " < < idB < < " and the position " < < posX < < " , " < < posZ < < " in the png, but does not exist in the xml. " < < std : : endl ;
2014-12-08 13:10:08 +00:00
exit ( - 1 ) ;
}
2014-11-21 14:43:27 +00:00
}
2014-12-08 12:33:38 +00:00
void Converter : : deleteComposition ( int idG , int idB ) {
2014-12-01 13:09:55 +00:00
XMLElement * thisComposition = doc - > FirstChildElement ( " composition " ) ;
2014-12-05 11:47:02 +00:00
for ( ; thisComposition ; thisComposition = thisComposition - > NextSiblingElement ( " composition " ) ) {
2015-02-20 12:11:05 +00:00
int idGreen = queryInt ( thisComposition , " idGreen " ) ;
int idBlue = queryInt ( thisComposition , " idBlue " ) ;
2014-12-01 13:09:55 +00:00
if ( idGreen = = idG & & idBlue = = idB ) {
doc - > DeleteChild ( thisComposition ) ;
}
}
2014-11-21 14:43:27 +00:00
}
2014-11-25 12:51:25 +00:00
void Converter : : save ( ) {
const char * charXmlFile = xmlFile . c_str ( ) ;
doc - > SaveFile ( charXmlFile ) ;
}
2015-02-20 14:06:43 +00:00
float Converter : : queryFloat ( XMLElement * element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
float ret ;
errorCheck ( attributeElement - > QueryFloatText ( & ret ) ) ;
return ret ;
}
float Converter : : queryFloat ( XMLDocument * & element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
float ret ;
errorCheck ( attributeElement - > QueryFloatText ( & ret ) ) ;
return ret ;
}
2015-02-20 12:11:05 +00:00
int Converter : : queryInt ( XMLElement * element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
int ret ;
errorCheck ( attributeElement - > QueryIntText ( & ret ) ) ;
return ret ;
}
int Converter : : queryInt ( XMLDocument * & element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
int ret ;
errorCheck ( attributeElement - > QueryIntText ( & ret ) ) ;
return ret ;
}
bool Converter : : queryBool ( XMLElement * element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
bool ret ;
errorCheck ( attributeElement - > QueryBoolText ( & ret ) ) ;
return ret ;
}
bool Converter : : queryBool ( XMLDocument * & element , const char * attribute ) {
XMLElement * attributeElement = element - > FirstChildElement ( attribute ) ;
if ( attributeElement = = NULL ) {
std : : cout < < " XMLError: Attribute " < < attribute < < " does not exist. " < < std : : endl ;
exit ( - 1 ) ;
}
bool ret ;
errorCheck ( attributeElement - > QueryBoolText ( & ret ) ) ;
return ret ;
}
2014-12-08 13:10:08 +00:00
void Converter : : errorCheck ( XMLError error ) {
if ( error ) {
printf ( " XMLError: " ) ;
if ( error = = XML_WRONG_ATTRIBUTE_TYPE ) {
printf ( " Wrong attribute type. \n " ) ;
}
else if ( error = = XML_NO_ATTRIBUTE ) {
printf ( " No attribute. \n " ) ;
}
else if ( error = = XML_CAN_NOT_CONVERT_TEXT ) {
printf ( " Can not convert text. \n " ) ;
}
else if ( error = = XML_NO_TEXT_NODE ) {
printf ( " No text. \n " ) ;
}
else {
printf ( " Unknown error. \n " ) ;
2014-12-01 13:09:55 +00:00
}
2015-01-23 15:02:50 +00:00
exit ( - 1 ) ;
2014-12-01 13:09:55 +00:00
}
2014-12-08 13:10:08 +00:00
}