Help needed with XML reader/writer

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
iZOTOPE
Posts: 23
Joined: Sun Jan 14, 2007 1:57 pm
Location: 6 feet under
Contact:

Help needed with XML reader/writer

Post by iZOTOPE »

i want to make a settings.xml that will keep the info about screen resolution, v-sync, antialiasing, full screen options.

so my code is:

core::stringw AntiA,FullScr,VSync;
core::stringc Width, Height;

....

io::IXMLReader* xml2 = device->getFileSystem()->createXMLReader(
"settings.xml");

while(xml2 && xml2->read())
{
switch(xml2->getNodeType())
{
case io::EXN_TEXT:
Width = xml2->getAttributeValue(L"Width");
Height = xml2->getAttributeValue(L"Height");
FullScr = xml2->getAttributeValue(L"FullScreen");
AntiA = xml2->getAttributeValue(L"AntiAlias");
VSync = xml2->getAttributeValue(L"VSync");
break;
}
}

if (xml2)
xml2->drop();

....
param.WindowSize.Width = Width.c_str();
param.WindowSize.Height = Height.c_str();
param.AntiAlias = AntiA.c_str();
param.Fullscreen = FullScr.c_str();
param.Vsync = VSync.c_str();

and the xml file:

<settings>
Width="800"
Height="600"
AntiAlias="true"
FullScreen="false"
VSync="false"
</settings>


but still gives some errors and doesn't read the xml data :(

also i want to make an xml writer to write new values in settings.xml when new screen resolution is set from the options menu...and the new setting to be loaded next time the program is open.

any suggestions could be usefull

10x
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

I don't know much about Irrlicht's xml reader/writer (I'm using tinyXML), but this is very wrong:

Code: Select all

param.WindowSize.Width = Width.c_str();
param.WindowSize.Height = Height.c_str();
param.AntiAlias = AntiA.c_str();
param.Fullscreen = FullScr.c_str();
param.Vsync = VSync.c_str(); 
the parameters are not alpha numeric values, so you can't assign strings to them !!!
param.WindowSize.Width and .Height are integers and the others are boolean !!!
look at the api, there should be functions for reading/writing integer, float and boolean values, too... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

Post by ehenkes »

The following works:
C++ Source

Code: Select all

CCharacter::CCharacter(ISceneManager* smgr, const char* filename)
{
	// read XML file ////////////////////////
	IrrXMLReader* xml = createIrrXMLReader(filename);

  // strings for storing the data we want to get out of the file
	std::string modelFile;

	// parse the file until end reached
	while(xml && xml->read())
	{
		switch(xml->getNodeType())
		{
			case EXN_ELEMENT:
			{
				// Get data from file	
				if (!strcmp("startUpModel", xml->getNodeName()))	
					modelFile = xml->getAttributeValue("file");
				if (!strcmp("messageText", xml->getNodeName()))		
					caption = xml->getAttributeValue("caption");
				if (!strcmp("Health", xml->getNodeName()))  			
					healthPoints = f32(xml->getAttributeValueAsInt("points"));
				if (!strcmp("Armor", xml->getNodeName()))				
				{
					armorType   = xml->getAttributeValueAsInt("type");   
					armorPoints = xml->getAttributeValueAsInt("points"); 
				}
				if (!strcmp("Attack", xml->getNodeName()))
				{
					attackType  = xml->getAttributeValueAsInt("type");
					attackPower = xml->getAttributeValueAsInt("power");
					attackRange = xml->getAttributeValueAsInt("range");
					attackArea  = xml->getAttributeValueAsInt("area");
				}
				if (!strcmp("Move", xml->getNodeName()))
				{
					moveType  = xml->getAttributeValueAsInt("type");
					moveValue = xml->getAttributeValueAsInt("value");
				}
				if (!strcmp("Scale", xml->getNodeName()))
				{
					scale.X = xml->getAttributeValueAsFloat("x");
					scale.Y = xml->getAttributeValueAsFloat("y");
					scale.Z = xml->getAttributeValueAsFloat("z");
				}
				if (!strcmp("startPos", xml->getNodeName()))
				{
					startPos.X = xml->getAttributeValueAsFloat("x");
					startPos.Y = xml->getAttributeValueAsFloat("y");
					startPos.Z = xml->getAttributeValueAsFloat("z");
				}
			}
			break;
		}
	}
	delete xml;
XML file:

Code: Select all

<?xml version="1.0"?>
<config>
<!--This is a config file for the Irrlicht Engine Mesh Viewer.-->
<startUpModel file="../media/Robot.x"></startUpModel>
<messageText caption="Robot 1.0"></messageText>
<Scale x="0.5" y="0.5" z="0.5"></Scale>
<startPos x="400" y="50" z="-250"></startPos>
<Health points="100"></Health>
<Armor   type="1" points="920"></Armor>
<Attack  power="1020" type="1" range="4" area="1"></Attack>
<Move    type="1" value="1"></Move>
</config>
Hence, with

<settings
Width="800"
Height="600"
AntiAlias="true"
FullScreen="false"
VSync="false">
</settings>

try

Code: Select all

if (!strcmp("settings", xml->getNodeName()))
{
  screenWidth = xml->getAttributeValueAsInt("Width");
  screenHeight = xml->getAttributeValueAsInt("Height");
  //...
}
With "getAttributeValueAsInt" you can directly use

Code: Select all

int screenWidth, screenHeight
in this case. I don't see the need for strings.
Last edited by ehenkes on Sat Aug 23, 2008 2:16 pm, edited 1 time in total.
iZOTOPE
Posts: 23
Joined: Sun Jan 14, 2007 1:57 pm
Location: 6 feet under
Contact:

reply

Post by iZOTOPE »

10x Acki and ehenkes ... really helpfull... i was tired that night and wrote the code all wrong =))

10x again :wink:
iZOTOPE
Posts: 23
Joined: Sun Jan 14, 2007 1:57 pm
Location: 6 feet under
Contact:

reply

Post by iZOTOPE »

hey ehenkes, it worked like a charm with the method you explained :) nice and clean ;)

now i need to figure ou how to generate settings.xml each time the user changes the screen resolution from options menu :)

10x again
ehenkes
Posts: 47
Joined: Sun Aug 03, 2008 2:52 pm
Location: Germany
Contact:

Post by ehenkes »

You need an XML Writer. :)

IXMLWriter.h
CXMLWriter.h
CXMLWriter.cpp
iZOTOPE
Posts: 23
Joined: Sun Jan 14, 2007 1:57 pm
Location: 6 feet under
Contact:

reply

Post by iZOTOPE »

are they builtin irrlicht ??? or they are aditional ?

i think irrlicht has it's own xml writer :)
Post Reply