If anybody more skilfull than me wants to do me the favour and give me a correction here my code. Currently I can't find an error in it, but the xml to be read will increase and I am not satisfied with the clumsyness of my work. At least I have the strong suspicion that this has to be said about it. The problem might be that I switched from little experience with C to c++ just a few weeks ago. here I didn't see a reason to define a class.
Code: Select all
void insstr(char**name,const c8*string)
{
char*mstr=new char[strlen(string)];
strcpy(mstr,string);
*name=mstr;
}
bool TEGRead()
{
//this method produces some lost memory. bog s nim.
initallGData();
std::list<char*> ntree;
std::list<char*>::iterator itn=ntree.begin();
IXMLReaderUTF8*xmlr=irrDevice->getFileSystem()->createXMLReaderUTF8("masterv1.tex.xml");
if (!xmlr)
{
cout<<"TE: TEGRead: No game file found. I leave idle"<<endl;
return false;
}
cout<<"TE: Reading TEG file"<<endl;
int level=0;
while (xmlr->read())
{
if (xmlr->getNodeType()==EXN_ELEMENT)
{
if (!xmlr->getAttributeCount())
{
//create a structure to hold current node hierarchy
char*el=new char[strlen(xmlr->getNodeName())];
strcpy(el,xmlr->getNodeName());
ntree.push_back(el);
itn=ntree.begin();
//allocate structures and include them to the allGData structure.
//we do whole hierarchy check here to provide option of
//reusing nodenames freely
if (!strcmp((*itn),"The_Experiment_Game_File"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"PreGame"))
{
if (!strcmp(xmlr->getNodeName(),"Splash"))
{
if (int i=1)
{
tsplashlist *splashlistentry=new tsplashlist;
allGData.splashlist.push_back(*splashlistentry);
}
}
}
if (itn!=ntree.end()&&!strcmp((*itn),"Game"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"GameObjects"))
{
if (!strcmp(xmlr->getNodeName(),"PickupObject"))
{
tpickupobject *pickupentry=new tpickupobject;
pickupentry->name;
pickupentry->modelfile=0;
pickupentry->spawnsoundfile=0;
pickupentry->permanentsoundfile=0;
pickupentry->pickupsoundfile=0;
pickupentry->spawnsoundvolume=0;
pickupentry->permanentsoundvolume=0;
pickupentry->pickupsoundvolume=0;
pickupentry->addweight=0;
pickupentry->addenergy=0;
pickupentry->addshield=0;
allGData.pickupobjectlist.push_back(*pickupentry);
}
} //(itn!=ntree.end()&&!strcmp((*itn),"GameObjects"))
}//if (itn!=ntree.end()&&!strcmp((*itn),"Game"))
if (itn!=ntree.end()&&!strcmp((*itn),"Level"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"LevelObjectTypes"))
{
if (!strcmp(xmlr->getNodeName(),"Spawnstation"))
{
tspawnstation*spawnentry=new tspawnstation;
spawnentry->name=0;
allGData.level.spawnstationlist.push_back(*spawnentry);
}
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Spawnstation"))
{
if (!strcmp(xmlr->getNodeName(),"Spawnobject"))
{
tspawnobject*spawnoentry=new tspawnobject;
spawnoentry->name=0;
spawnoentry->probabilityToSpawn=0;
std::list<tspawnstation>::iterator itspst=allGData.level.spawnstationlist.end();
itspst--;
(itspst->spawnobjectlist).push_back(*spawnoentry);
}
}
if (!strcmp(xmlr->getNodeName(),"LevelCenter"))
{
tlevelcenter*levelcenterentry=new tlevelcenter;
levelcenterentry->name=0;
levelcenterentry->bbox= vector3df(0,0,0);
allGData.level.levelcenterlist.push_back(*levelcenterentry);
}
}//(itn!=ntree.end()&&!strcmp((*itn),"LevelObjectTypes"))
}//(itn!=ntree.end()&&!strcmp((*itn),"Level"))
if (itn!=ntree.end()&&!strcmp((*itn),"Ship"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"ShipObjects"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Weapon"))
{
tshipobject *shoentry=new tshipobject;
shoentry->name=0;
shoentry->onShotSoundFilename=0;
shoentry->onButtonPressShot=true;
allGData.ship.shipobjectslist.push_back(*shoentry);
}
}
}//Ship
} //(!strcmp((*itn),"The_Experiment_Game_File"))
}//if (!xmlr->getAttributeCount())
//read all input values from gamefile here
else // if (!xmlr->getAttributeCount())
{
itn=ntree.begin();
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"PreGame"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Splash"))
{
if (!strcmp(xmlr->getNodeName(),"string"))
{
std::list<tsplashlist>::iterator itspl=--allGData.splashlist.end();
if (!strcmp(xmlr->getAttributeName(0),"filename"))
{
insstr(&(itspl->filename),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
if (!strcmp(xmlr->getAttributeName(0),"abortkey"))
{
insstr(&(itspl->abortkey),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
}
if (!strcmp(xmlr->getNodeName(),"int"))
{
if (!strcmp(xmlr->getAttributeName(0),"countdown"))
{
std::list<tsplashlist>::iterator itspl=--allGData.splashlist.end();
itspl->countdown=xmlr->getAttributeValueAsInt("countdown");
}
}
}//Splash
}//PreGame
if (itn!=ntree.end()&&!strcmp((*itn),"Game"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Gameplay"))
{
if (!strcmp(xmlr->getNodeName(),"string"))
{
if (!strcmp(xmlr->getAttributeName(0),"gamename"))
{
insstr(&(allGData.gamename),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
}
if (!strcmp(xmlr->getNodeName(),"int"))
if (!strcmp(xmlr->getAttributeName(0),"countdown"))
allGData.countdown=xmlr->getAttributeValueAsInt(0);
}
if (itn!=ntree.end()&&!strcmp((*itn),"GameObjects"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"PickupObject"))
{
if (!strcmp(xmlr->getNodeName(),"string"))
{
std::list<tpickupobject>::iterator itpic=--allGData.pickupobjectlist.end();
if (!strcmp(xmlr->getAttributeName(0),"name"))
insstr(&(itpic->name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
if (!strcmp(xmlr->getAttributeName(0),"modelfile"))
insstr(&(itpic->modelfile),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
if (!strcmp(xmlr->getAttributeName(0),"spawnsoundfile"))
insstr(&(itpic->spawnsoundfile),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
if (!strcmp(xmlr->getAttributeName(0),"permanentsoundfile"))
insstr(&(itpic->permanentsoundfile),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
if (!strcmp(xmlr->getAttributeName(0),"pickupsoundfile"))
insstr(&(itpic->pickupsoundfile),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
if (!strcmp(xmlr->getNodeName(),"float"))
{
std::list<tpickupobject>::iterator itpic=--allGData.pickupobjectlist.end();
if (!strcmp(xmlr->getAttributeName(0),"pickupdistance"))
itpic->pickupdistance=xmlr->getAttributeValueAsFloat("pickupdistance");
if (!strcmp(xmlr->getAttributeName(0),"spawnsoundvolume"))
itpic->spawnsoundvolume=xmlr->getAttributeValueAsFloat("spawnsoundvolume");
if (!strcmp(xmlr->getAttributeName(0),"permanentsoundvolume"))
itpic->permanentsoundvolume=xmlr->getAttributeValueAsFloat("permanentsoundvolume");
if (!strcmp(xmlr->getAttributeName(0),"pickupsoundvolume"))
itpic->pickupsoundvolume=xmlr->getAttributeValueAsFloat("pickupsoundvolume");
if (!strcmp(xmlr->getAttributeName(0),"addweight"))
itpic->addweight=xmlr->getAttributeValueAsFloat("addweight");
if (!strcmp(xmlr->getAttributeName(0),"addenergy"))
itpic->addenergy=xmlr->getAttributeValueAsFloat("addenergy");
if (!strcmp(xmlr->getAttributeName(0),"addshield"))
itpic->addshield=xmlr->getAttributeValueAsFloat("addshield");
}
}//PickupObjects
}//GameObjects
}//Game
if (!strcmp((*itn),"Level"))
{
//we must be sure this is game.level.name and not some other .name.//
if (itn==--ntree.end())
{
if (!strcmp(xmlr->getNodeName(),"string"))
{
insstr(&(allGData.level.name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
}
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"LevelObjectTypes"))
{
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Spawnstation"))
{
std::list<tspawnstation>::iterator itspawn=--allGData.level.spawnstationlist.end();
if (itn==--ntree.end()&&!strcmp(xmlr->getNodeName(),"string"))
{
insstr(&(itspawn->name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
itn++;
if (itn!=ntree.end()&&!strcmp((*itn),"Spawnobject"))
{
std::list<tspawnobject>::iterator itspawno=--itspawn->spawnobjectlist.end();
if (++itn==ntree.end()&&!strcmp(xmlr->getNodeName(),"string"))
{
insstr(&(itspawno->name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
if (!strcmp(xmlr->getNodeName(),"probabilityToSpawn"))
itspawno->probabilityToSpawn=xmlr->getAttributeValueAsInt(0);
}
}//Spawnstation
if (itn!=ntree.end()&&!strcmp((*itn),"LevelCenter"))
{
std::list<tlevelcenter>::iterator itlevc=--allGData.level.levelcenterlist.end();
if (++itn==ntree.end())
{
if (!strcmp(xmlr->getNodeName(),"string"))
{
insstr(&(itlevc->name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
}
if (itn!=ntree.end()&&!strcmp((*itn),"bbox"))
{
if(!strcmp(xmlr->getNodeName(),"vector3df"))
{
itlevc->bbox.X=xmlr->getAttributeValueAsFloat(xmlr->getAttributeName(0));
itlevc->bbox.Y=xmlr->getAttributeValueAsFloat(xmlr->getAttributeName(1));
itlevc->bbox.Z=xmlr->getAttributeValueAsFloat(xmlr->getAttributeName(2));
}
}
} //LevelCenter
}
}//Level
itn=++ntree.begin();
if (!strcmp((*itn),"Ship"))
{
itn++;
if (!strcmp((*itn),"RandomMovements"))
{
if (!strcmp(xmlr->getNodeName(),"float"))
{
if (!strcmp(xmlr->getAttributeName(0),"mintime"))
allGData.ship.ranshipmovements.mintime=xmlr->getAttributeValueAsFloat("mintime");
if (!strcmp(xmlr->getAttributeName(0),"rantime"))
allGData.ship.ranshipmovements.rantime=xmlr->getAttributeValueAsFloat("rantime");
if (!strcmp(xmlr->getAttributeName(0),"minamplitude"))
allGData.ship.ranshipmovements.minamplitude=xmlr->getAttributeValueAsFloat("minamplitude");
if (!strcmp(xmlr->getAttributeName(0),"ranamplitude"))
allGData.ship.ranshipmovements.ranamplitude=xmlr->getAttributeValueAsFloat("ranamplitude");
}
}
if (!strcmp((*itn),"ShipObjects"))
{
itn++;
if (!strcmp((*itn),"Weapon"))
{
std::list<tshipobject>::iterator itweap=--allGData.ship.shipobjectslist.end();
if (!strcmp(xmlr->getNodeName(),"string"))
{
if (!strcmp(xmlr->getAttributeName(0),"name"))
insstr(&(itweap->name),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
if (!strcmp(xmlr->getAttributeName(0),"onShotSoundFilename"))
insstr(&(itweap->onShotSoundFilename),xmlr->getAttributeValueSafe(xmlr->getAttributeName(0)));
}
if (!strcmp(xmlr->getNodeName(),"bool"))
{
if (!strcmp(xmlr->getAttributeName(0),"onButtonPressShot"))
{
if (!strcmp(xmlr->getAttributeValueSafe("onButtonPressShot"),"true"))
itweap->onButtonPressShot=true;
if (!strcmp(xmlr->getAttributeValueSafe("onButtonPressShot"),"false"))
itweap->onButtonPressShot=false;
}
}
}
}
}//Ship
}
//writeout nodehierarchy
if (TES_TEGReadVerboseLevel>1)
{
itn=ntree.begin();
while (itn!=ntree.end())
{
cout<<*itn<<".";
itn++;
}
cout<<endl<<"working node: "<<xmlr->getNodeName()<<" attribute count: "<<xmlr->getAttributeCount()<<endl;
}
}//EXN_ELEMENT
if (xmlr->getNodeType()==EXN_ELEMENT_END)
{
if (TES_TEGReadVerboseLevel>1)cout<<xmlr->getNodeName()<<" end"<<endl;
if (!ntree.empty())
{
itn=--ntree.end();
ntree.pop_back();
// delete *itn;
}
}//EXN_ELEMENT_END
}
//writeout allgamedata
if (TES_TEGReadVerboseLevel)
{
cout<<endl<<endl<<"TEGRead: I have read the following data"<<endl;
cout<<"gameplay.gamename "<<allGData.gamename<<endl;
cout<<"gameplay.countdown "<<allGData.countdown<<endl;
std::list<tsplashlist>::iterator itspl=allGData.splashlist.begin();
int i=1;
while (itspl!=allGData.splashlist.end())
{
cout<<"Splash nr "<<i<<" fn= "<<itspl->filename<<" abortkey= "<<itspl->abortkey<<" countdown= "<<itspl->countdown<<endl;
itspl++;
i++;
}
std::list<tpickupobject>::iterator itpick=allGData.pickupobjectlist.begin();
i=1;
while (itpick!=allGData.pickupobjectlist.end())
{
cout<<"GameObjects number "<<i<<" name = "<<itpick->name<<endl;
if (itpick->spawnsoundfile) cout<<" spawnsoundfile "<<itpick->spawnsoundfile<<endl;
if (itpick->permanentsoundfile) cout<<" permanentsoundfile "<<itpick->permanentsoundfile<<endl;
if (itpick->pickupsoundfile) cout<<" pickupsoundfile "<<itpick->pickupsoundfile<<endl;
if (itpick->spawnsoundvolume) cout<<" spawnsoundvolume "<<itpick->spawnsoundvolume<<endl;
if (itpick->permanentsoundvolume) cout<<" permanentsoundvolume "<<itpick->permanentsoundvolume<<endl;
if (itpick->pickupsoundvolume) cout<<" pickupsoundvolume "<<itpick->pickupsoundvolume<<endl;
if (itpick->pickupdistance) cout<<" pickupdistance "<<itpick->pickupdistance<<endl;
if (itpick->addweight) cout<<" addweight "<<itpick->addweight<<endl;
if (itpick->addenergy) cout<<" addenergy "<<itpick->addenergy<<endl;
if (itpick->addshield) cout<<" addshield "<<itpick->addshield<<endl;
itpick++;
i++;
}
std::list<tspawnstation>::iterator itspawnst=allGData.level.spawnstationlist.begin();
cout<<"level filename = "<<allGData.level.name<<endl;
while (itspawnst!=allGData.level.spawnstationlist.end())
{
cout<<"spawnstation= "<<itspawnst->name<<endl;
std::list<tspawnobject>::iterator itspawno=itspawnst->spawnobjectlist.begin();
while (itspawno!=itspawnst->spawnobjectlist.end())
{
if (itspawno->name) cout<<" SpawnstationObject= "<<itspawno->name<<endl;
else cout<<"noname"<<endl;
itspawno++;
}
itspawnst++;
}//while (itspawnst!=allGData.level.spawnstationlist.end())
std::list<tlevelcenter>::iterator itlevc=allGData.level.levelcenterlist.begin();
while(itlevc!=allGData.level.levelcenterlist.end())
{
cout<<"levelcenter name= "<<itlevc->name<<endl;
cout<<" bounding box= "<<itlevc->bbox.X<<" "<<itlevc->bbox.Y<<" "<<itlevc->bbox.Z<<" "<<endl;
itlevc++;
}
cout<<endl<<"Shipproperties "<<endl;
cout<<"random ship movements= "<<endl;
cout<<" mintime= "<<allGData.ship.ranshipmovements.mintime<<" ";
cout<<"rantime= "<<allGData.ship.ranshipmovements.rantime<<" ";
cout<<"minamplitude= "<<allGData.ship.ranshipmovements.minamplitude<<" ";
cout<<"ranamplitude= "<<allGData.ship.ranshipmovements.ranamplitude<<" "<<endl;
cout<<"Weapons"<<endl;
std::list<tshipobject>::iterator itweap=allGData.ship.shipobjectslist.begin();
while (itweap!=allGData.ship.shipobjectslist.end())
{
cout<<" Weaponname= "<<itweap->name<<" onShotSoundFilename= "<<itweap->onShotSoundFilename;
cout<<" onButtonPressShot= "<<itweap->onButtonPressShot<<endl;
itweap++;
}
cout<<endl<<"TEGRead: TES_TEGReadVerboseLevel "<<TES_TEGReadVerboseLevel<<" end"<<endl;
}// if (TES_TEGReadVerboseLevel)
if (TES_TEGReadVerboseLevel) system("PAUSE");
return true;
}