working load and save settings routines

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
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

working load and save settings routines

Post by robertgarand »

Hi All,

Since I haven't found working examples of saving/loading config parameters, I did it all. And those works with version 1.4.2

Hope it helps someone and save time.

I found dealing with writing in xml difficult. spent many hours making it work flawlessly, so you can copy and paste.

I know the code is not very elegent, but it does it :D

It would be easy to call the function with file name and pointers to arrays or struct of elements to save, but I had no use for that...:oops:
felt lazy

regards,
Robert.
PS: my humble smallish game will be available in a few days!!!

Code: Select all

#include "settings.h"
#include <irrXML.h>

using namespace irr;
using namespace core;
using namespace gui;
using namespace io;
SIrrlichtCreationParameters paramIrr;

//==============load settings in paramIrr

void load_settings()
{
     u32 screenWidth, screenHeight;
     io::IrrXMLReader* xml = io::createIrrXMLReader("media/settings.xml");
while(xml && xml->read())
   {
     switch(xml->getNodeType())
      {
     case EXN_ELEMENT:
         {
            if (!strcmp("settings", xml->getNodeName()))
                {
                screenWidth          = xml->getAttributeValueAsInt("width");
                screenHeight         = xml->getAttributeValueAsInt("height");
                paramIrr.Bits = xml->getAttributeValueAsInt("bits");
                paramIrr.Fullscreen    = xml->getAttributeValueAsInt("fullscreen");
               //paramIrr.DriverType  =xml->getAttributeValueSafe ("drivertype");
               }
               else if (!strcmp("options", xml->getNodeName()))
               {paramIrr.Vsync          = xml->getAttributeValueAsInt("vsync");
               paramIrr.AntiAlias = xml->getAttributeValueAsInt("antialias");
               paramIrr.Stencilbuffer = xml->getAttributeValueAsInt("stencilbuffer");
               }
         }
      }//switch
   }//while
   delete xml;
//transfer parameters 
   paramIrr.WindowSize = dimension2d<s32>(screenWidth, screenHeight);
   paramIrr.DriverType = video::EDT_OPENGL;
return;
}

//================save settings from paramIrr
void save_settings(SIrrlichtCreationParameters paramIrr)
{
wchar_t  bits_str[32], videoheigth[32], videowidth[32],fullscreen[32],
vsync[32],antialias[32],stencilbuffer[32];

//formats the strings for XMLwriter

swprintf(videowidth, 5, L"%d", paramIrr.WindowSize.Width);
swprintf(videoheigth, 5, L"%d", paramIrr.WindowSize.Height);
swprintf(bits_str, 4, L"%d", paramIrr.Bits);
swprintf(fullscreen, 2, L"%d", paramIrr.Fullscreen);
swprintf(vsync, 2, L"%d", paramIrr.Vsync);
swprintf(antialias, 2, L"%d", paramIrr.AntiAlias);
swprintf(stencilbuffer, 2, L"%d", paramIrr.Stencilbuffer);
// define xml writer
io::IXMLWriter * xml_write_file = Device->getFileSystem()->createXMLWriter("media/settings.xml");

//must write header of file to xml file
     xml_write_file->writeXMLHeader();
     xml_write_file->writeComment(L"This is a config file for arcade nostalgia");
     xml_write_file->writeLineBreak();
     xml_write_file->writeElement(L"config");
     xml_write_file->writeLineBreak();
//writing maximum 5 elements
     xml_write_file->writeElement(L"settings",false
          ,L"width",videowidth
          ,L"height",videoheigth
          ,L"bits",bits_str
          ,L"fullscreen",fullscreen
          ,L"driver",L"EDT_OPENGL");
//must write closing tag
     xml_write_file->writeClosingTag(L"settings");
     xml_write_file->writeLineBreak();

     //writing optionnal parameters
     xml_write_file->writeElement(L"options",false
          ,L"vsync",vsync
          ,L"antialias",antialias
          ,L"stencilbuffer",stencilbuffer);
     xml_write_file->writeLineBreak();
     xml_write_file->writeClosingTag(L"options");
     xml_write_file->writeLineBreak();
//must write closing tag
     xml_write_file->writeClosingTag(L"config");
     // cleanup and write buffer to disk
     xml_write_file->drop();
}



//===========the config file settings.xml================
<?xml version="1.0"?>
<!--This is a config file for arcade nostalgia-->
<config>
	<settings width="1024" height="768" bits="32" fullscreen="1" driver="EDT_OPENGL">	</settings>
	<options vsync="0" antialias="1" stencilbuffer="0">
	</options>
</config>
If it can't be writen, it can't exist
Virion
Competition winner
Posts: 2149
Joined: Mon Dec 18, 2006 5:04 am

Post by Virion »

thanks this is useful
My company: https://kloena.com
My profile: https://zhieng.com
My co-working space: https://deskspace.info
My game engine: https://kemena3d.com
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Code: Select all

<?xml version="1.0"?> 
<!--This is a config file for arcade nostalgia--> 
<config> 
   <settings width="1024" height="768" bits="32" fullscreen="1" driver="EDT_OPENGL" /> 
   <options vsync="0" antialias="1" stencilbuffer="0" />
</config>
else your xml nodes have text child nodes ^^
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

Dorth, what you mean by text child nodes?

Post by robertgarand »

Hi dorth,
If I take out the writeclosingtag, irrlicht still run,
but browser give error on file format...
I am no expert in xml format, if you can explain.
Can give errors ?
Tia,
Robert
If it can't be writen, it can't exist
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

Did you add the / at the end of the nodes?

<this></this>
is identical to
<this />

but

<this> </this>
means the node
<this>
has a text node child
" "
robertgarand
Posts: 69
Joined: Fri Feb 22, 2008 6:47 pm
Location: montreal
Contact:

It means that xml is looking for a child or device?

Post by robertgarand »

Well,
I didn't take notice of that, anyway I close the writer/reader immediatly.
Everything works fine this way.
And the browser sees it exactly as you wrote it...
Thanks for the comment,

Robert
If it can't be writen, it can't exist
Post Reply