How do I use writeElement to save int, float, and bool?

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
carlwryker
Posts: 22
Joined: Sun Jun 12, 2016 8:03 am

How do I use writeElement to save int, float, and bool?

Post by carlwryker »

How do I use irr::io::IXMLWriter::writeElement to save int, float, and bool values to xml?
Code::Blocks
mingw
Windows 10 64-bit
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Re: How do I use writeElement to save int, float, and bool?

Post by Seven »

here is an example of how I write engine creation parameters to / from disk

Code: Select all

 
    bool FSEngine::saveToDisk(stringc filename)
    {
        // log this event
        FS_LOG(FSL_DEBUG, "%s\n %s", "FSEngine::saveParamsToDisk()", getApp()->getDirectory("GameDataDirectory") + filename.c_str());
 
        // remember this
        m_Filename = filename;
 
        // create a null device, setup the attributes and then save to disk
        IrrlichtDevice* device = createDevice(EDT_NULL);
        IXMLWriter* writer = device->getFileSystem()->createXMLWriter(getApp()->getDirectory("GameDataDirectory") + filename);
        if (!writer) { FS_LOG(FSL_DEBUG, "Warning! unable to create engine param file %s", filename.c_str()); return false; }
 
        // write the xml header to the file
        writer->writeXMLHeader();
 
        writer->writeElement(L"ENGINE_PARAMS", true);
        writer->writeLineBreak();
 
        // create an attributes structure
        IAttributes* attr = device->getFileSystem()->createEmptyAttributes();
 
        if (attr)
        {
            // add the variables to the attributes structure
            switch (m_Params.DriverType)
            {
            case EDT_DIRECT3D9: attr->addString("DRIVERTYPE", "EDT_DIRECT3D9"); break;
            case EDT_OPENGL: attr->addString("DRIVERTYPE", "EDT_OPENGL"); break;
            }
            attr->addInt("ANTIALIAS", m_Params.AntiAlias);
            attr->addInt("BITS", m_Params.Bits);
            attr->addInt("DISPLAYADAPTER", m_Params.DisplayAdapter);
            attr->addBool("DOUBLEBUFFER", m_Params.Doublebuffer);
            attr->addBool("DRIVERMULTITHREADED", m_Params.DriverMultithreaded);
            attr->addBool("FULLSCREEN", m_Params.Fullscreen);
            attr->addBool("HANDLESRGB", m_Params.HandleSRGB);
            attr->addBool("HIGHPRECISIONFPU", m_Params.HighPrecisionFPU);
            attr->addBool("IGNOREINPUT", m_Params.IgnoreInput);
            attr->addBool("STENCILBUFFER", m_Params.Stencilbuffer);
            attr->addBool("STEREOBUFFER", m_Params.Stereobuffer);
            attr->addBool("PORFORMANCETIMER", m_Params.UsePerformanceTimer);
            attr->addBool("VSYNC", m_Params.Vsync);
            attr->addDimension2d("WINDOWSIZE", m_Params.WindowSize);
            attr->addRect("WINDOWPOS", m_WindowPos);
 
            attr->addRect("WINDOWINFO", m_WindowInfo);
            attr->addBool("WITHALPHACHANNEL", m_Params.WithAlphaChannel);
            attr->addInt("ZBUFFERBITS", m_Params.ZBufferBits);
 
            // if thee are attributes, write them to the file
            if (attr->getAttributeCount() != 0) attr->write(writer);
 
            // close the file writing
            writer->writeLineBreak();
            writer->writeClosingTag(L"ENGINE_PARAMS");
 
            // drop all of the pointers
            attr->drop();
            writer->drop();
            device->drop();
 
            // everything went fine
            return true;
        }
 
        // something went wrong
        return false;
    }
 
    bool FSEngine::loadFromDisk(stringc filename)
    {
        // log this event
        FS_LOG(FSL_DEBUG, "%s\n %s", "FSEngine::loadFromDisk()", getApp()->getDirectory("GameDataDirectory") + filename.c_str());
 
        if (filename == FS_DEFAULT) return true;
 
        // create a null device, create the attributes and then load from disk
        IrrlichtDevice* device = createDevice(EDT_NULL);
        IXMLReader* reader = device->getFileSystem()->createXMLReader(getApp()->getDirectory("GameDataDirectory") + filename);
        if (!reader) { FS_LOG(FSL_DEBUG, "Warning! unable to load engine param file %s", filename.c_str()); return false; }
 
        // create an attributes structure
        IAttributes* attr = device->getFileSystem()->createEmptyAttributes();
 
        while (reader->read())
            switch (reader->getNodeType())
            {
            case EXN_ELEMENT:
 
                attr->read(reader, false);
 
                // add the variables to the attributes structure
                stringc tempString = attr->getAttributeAsString("DRIVERTYPE");
                if (tempString == "EDT_DIRECT3D9") m_Params.DriverType = EDT_DIRECT3D9;
                if (tempString == "EDT_OPENGL") m_Params.DriverType = EDT_OPENGL;
 
                m_Params.AntiAlias = attr->getAttributeAsInt("ANTIALIAS");
                m_Params.Bits = attr->getAttributeAsBool("BITS");
                m_Params.DisplayAdapter = attr->getAttributeAsBool("DISPLAYADAPTER");
                m_Params.Doublebuffer = attr->getAttributeAsBool("DOUBLEBUFFER");
                m_Params.DriverMultithreaded = attr->getAttributeAsBool("DRIVERMULTITHREADED");
                m_Params.Fullscreen = attr->getAttributeAsBool("FULLSCREEN");
                m_Params.HandleSRGB = attr->getAttributeAsBool("HANDLESRGB");
                m_Params.HighPrecisionFPU = attr->getAttributeAsBool("HIGHPRECISIONFPU");
                m_Params.IgnoreInput = attr->getAttributeAsBool("IGNOREINPUT");
                m_Params.Stencilbuffer = attr->getAttributeAsBool("STENCILBUFFER");
                m_Params.Stereobuffer = attr->getAttributeAsBool("STEREOBUFFER");
                m_Params.UsePerformanceTimer = attr->getAttributeAsBool("PORFORMANCETIMER");
                m_Params.Vsync = attr->getAttributeAsBool("VSYNC");
                m_Params.WindowSize = attr->getAttributeAsDimension2d("WINDOWSIZE");
                m_Params.WithAlphaChannel = attr->getAttributeAsBool("WITHALPHACHANNEL");
                m_Params.ZBufferBits = attr->getAttributeAsInt("ZBUFFERBITS");
                m_WindowPos = attr->getAttributeAsRect("WINDOWPOS");
                m_WindowInfo = attr->getAttributeAsRect("WINDOWINFO");
                break;
            }
 
 
        // drop all of the pointers
        attr->drop();
        reader->drop();
        device->drop();
 
        // everything went fine
        return true;
    }
Post Reply