(C++) Settings.xml Reader

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Buck1000
Posts: 93
Joined: Sun Dec 14, 2008 8:02 pm
Location: Seattle, WA

(C++) Settings.xml Reader

Post by Buck1000 »

I've used this function in all of my projects, to make my program settings editable from outside of the source code. It simply reads in values from an XML file, and creates an IrrlichtDevice* from them -

Code: Select all

IrrlichtDevice* Setup(std::string filename, CEventReceiver* receiver)
{
    IrrXMLReader* xml = createIrrXMLReader(filename.c_str());

	std::string d;

	int x,y,z,f,vs,sb,aa;

    while(xml && xml->read())
    {
        switch(xml->getNodeType())
        {
            case EXN_ELEMENT:
            {
                if (!strcmp("Screen", xml->getNodeName()))
                {
                    x = xml->getAttributeValueAsInt("X");
                    y = xml->getAttributeValueAsInt("Y");
                    z = xml->getAttributeValueAsInt("Z");
                    f = xml->getAttributeValueAsInt("F");
                }
                else
                if (!strcmp("Render", xml->getNodeName()))
                {
                    aa = xml->getAttributeValueAsInt("AA");
                    sb = xml->getAttributeValueAsInt("SB");
                    vs = xml->getAttributeValueAsInt("VS");
                    d = xml->getAttributeValue("Driver");
                }
            }
            break;
        }
    }
    delete xml;

    E_DRIVER_TYPE driver;

	if(d!="gl")
#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_ 
		if(d!="dx")
#endif 
			d="so";

    if(d=="gl")
        driver=EDT_OPENGL;
    if(d=="dx")
        driver=EDT_DIRECT3D9;
	if(d=="so")
		driver=EDT_BURNINGSVIDEO;

	SIrrlichtCreationParameters params;
	params.AntiAlias=aa;
	params.Stencilbuffer=sb;
	params.Vsync=vs;
	params.WindowSize=dimension2d<s32>(x,y);
	params.Bits=z;
	params.Fullscreen=f;
	params.EventReceiver=receiver;
	params.DriverType=driver;

	IrrlichtDevice* device=createDeviceEx(params);

	return device;
}
Here is a sample Settings.xml file -

Code: Select all

<!-- Graphics Config -->
<Screen X="1280" Y="1024" Z="32" F="1"/>
<Render AA="1" SB="1" VS="1" Driver="gl"/>

<!--             Valid Values                          -->
<!-- Driver -> gl, dx                                  -->
It is easily expandable, and has many uses. The code can be ported to custom media importers and similar utilities.

Enjoy!
Last edited by Buck1000 on Fri Jul 02, 2010 8:35 pm, edited 1 time in total.
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

if you will use this code in Linux (where no Direct3D render) and will use "dx" in config, than device will fail to create.
So you can improve small part of your code next way:

Code: Select all

    ...

    if(d!="gl") 
#ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
      if(d!="dx") 
#endif
         d="so";

    ...
It will does not recognize "dx" if Direct3D render not included and fall to "so" automatically.
Buck1000
Posts: 93
Joined: Sun Dec 14, 2008 8:02 pm
Location: Seattle, WA

Post by Buck1000 »

Thanks :) I haven't actually used this before in anything other than a windows environment, so thats a useful failsafe to add.

EDIT: I edited my original post to include it.
Post Reply