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
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...
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>