C++ 如何将xml属性转换为c++;使用tinyxml初始化

C++ 如何将xml属性转换为c++;使用tinyxml初始化,c++,visual-studio-2010,xml-parsing,glfw,C++,Visual Studio 2010,Xml Parsing,Glfw,嘿,我正试图通过读取.xml文件中的焰火来创建焰火表演,然后该文件将迭代并启动每个焰火 我按照dinomage的教程学习如何使用tinyxml,我知道我可以将属性存储到char指针中,但我不知道如何将它们转换为glfloat以将它们存储在类变量中 我确实尝试过使用atof,但是当我运行程序时,我得到了大量的错误,我猜这是因为指针只存储了我传递给变量的地址 有人能指出我哪里出了问题,我如何着手解决这个问题吗 更新 我发现tinyxml与vs2010不兼容,但tinyxml 2与vs2010不兼容,

嘿,我正试图通过读取.xml文件中的焰火来创建焰火表演,然后该文件将迭代并启动每个焰火

我按照dinomage的教程学习如何使用tinyxml,我知道我可以将属性存储到char指针中,但我不知道如何将它们转换为glfloat以将它们存储在类变量中

我确实尝试过使用atof,但是当我运行程序时,我得到了大量的错误,我猜这是因为指针只存储了我传递给变量的地址

有人能指出我哪里出了问题,我如何着手解决这个问题吗

更新

我发现tinyxml与vs2010不兼容,但tinyxml 2与vs2010不兼容,因此我更改了代码,但仍无法加载属性。我添加了错误检查,并打印出xml已加载,但不会加载根目录

代码更新

代码:Firework.h

#ifndef FIREWORK_H
#define FIREWORK_H
#include <cstdlib>
#include <GL\GL.h>
#include <string>



const GLint particles= 50;

 class Firework

 {

 public:

            GLint x[particles];
            GLint y[particles];
            GLint VelX[particles];
            GLint VelY[particles];
            GLint Xpos;
            GLint Ypos;
            GLint Xspeed;
            GLint Yspeed;



            unsigned char red;
            unsigned char blue;
            unsigned char green;
            GLfloat alpha;
            GLfloat redStart;
            GLfloat blueStart;
            GLfloat greenStart;
            std::string hexColour;
            std::string type;

            GLint timeUntilLaunch;
            GLint startTime;
            GLint endTime;
            GLint duration;
            GLfloat particleSize;
            GLboolean hasExploded;




            GLboolean rocket, fountain;

            static const GLfloat gravity;
            static const GLfloat baseYSpeed;
            static const GLfloat maxYSpeed;

            Firework();

            void initialise();
            void move();
            void explode();




 };




#endif
#如果没有烟火#
#定义焰火
#包括
#包括
#包括
常数闪烁粒子=50;
班级烟花
{
公众:
闪烁x[粒子];
闪烁的y[粒子];
闪光膜[颗粒];
闪烁[粒子];
闪烁的XPO;
闪烁的YPO;
闪烁的X速度;
闪烁的速度;
无符号字符红色;
无符号字符蓝色;
无符号字符绿色;
GLAα;
GLfloat红启动;
GLfloat-blueStart;
GLfloat-greenStart;
标准:字符串六边形颜色;
std::字符串类型;
闪烁时间至闪烁时间;
闪烁的星光;
闪烁结束时间;
闪烁持续时间;
GLfloat颗粒化;
gl爆炸;
火箭、喷泉;
静态恒重力;
静态常数GLfloat baseYSpeed;
静态常数GLfloat最大速度;
烟花();
无效初始化();
无效移动();
空洞爆炸();
};
#恩迪夫
烟花

#include "Firework.h"
#include "tinyxml2.h"
#include<time.h>
#include <string>
#include<iostream>
#include <vector>

using namespace std;

const GLfloat Firework::gravity = 0.05f;
const GLfloat Firework::baseYSpeed = -4.0f;
const GLfloat Firework::maxYSpeed = -4.0f;

//int QueryAttributeStatus = elem->FirstChildElement("begin")- >QueryFloatAttribute(attr,&timeUntillLaunch);





int convertFromHex(string hex)

{

    int value = 0;



    int a = 0;

    int b = hex.length() - 1;

    for (; b >= 0; a++, b--)

    {

        if (hex[b] >= '0' && hex[b] <= '9')

        {

            value += (hex[b] - '0') * (1 << (a * 4));

        }

        else

        {

            switch (hex[b])

            {

                case 'A':

                case 'a':

                    value += 10 * (1 << (a * 4));

                    break;



                case 'B':

                case 'b':

                    value += 11 * (1 << (a * 4));

                    break;



                case 'C':

                case 'c':

                    value += 12 * (1 << (a * 4));

                    break;



                case 'D':

                case 'd':

                    value += 13 * (1 << (a * 4));

                    break;



                case 'E':

                case 'e':

                    value += 14 * (1 << (a * 4));

                    break;



                case 'F':

                case 'f':

                    value += 15 * (1 << (a * 4));

                    break;



                default:

                    cout << "Error, invalid charactare '" << hex[a] << "' in hex number" << endl;

                    break;

            }

        }

    }
        return value;

}


void hextodec(string hex, vector<unsigned char>& rgb)

{

/*  
    since there is no prefix attached to hex, use this code

    string redString = hex.substr(0, 2);

    string greenString = hex.substr(2, 2);

    string blueString = hex.substr(4, 2);
*/



/*

    if the prefix # was attached to hex, use the following code

    string redString = hex.substr(1, 2);

    string greenString = hex.substr(3, 2);

    string blueString = hex.substr(5, 2);

*/





    //if the prefix 0x was attached to hex, use the following code

    string redString = hex.substr(2, 2);

    string greenString = hex.substr(4, 2);

    string blueString = hex.substr(6, 2);





    unsigned char red = (unsigned char)(convertFromHex(redString));

    unsigned char green = (unsigned char)(convertFromHex(greenString));

    unsigned char blue = (unsigned char)(convertFromHex(blueString));



    rgb[0] = red;

    rgb[1] = green;

    rgb[2] = blue;

}


Firework::Firework()
{

    initialise();

}



void Firework::initialise()
{
    tinyxml2::XMLDocument doc;
    doc.LoadFile( "fireworks.xml");
    if (!doc.LoadFile("fireworks.xml"))
    {
        std::cout<<"Failed to load file: no xml"<<endl;
    }

    else
        std::cout<<"loaded xml"<<endl;



        tinyxml2::XMLElement * root = doc.FirstChildElement();
        if (root == NULL)
        {
            std::cout<<"Failed to load file: no root element."<<endl;

        }

        else

            std::cout<<"root node loaded"<<endl;

        for (tinyxml2::XMLElement* elem = root ->FirstChildElement(); elem!=NULL; elem = elem->NextSiblingElement())
        {
            string elemName = elem->Value();
            const char* attr;

            if (elemName == "Firework")
            {
                attr = elem->Attribute("begin");
                if(attr != NULL)
                {
                    elem->QueryIntAttribute("begin",&startTime);
                }

                attr = elem->Attribute("type");
                if (attr != NULL)
                {
                    type = elem->GetText();
                    if (type != "")

                        std::cout<<"have something"<<endl; 


                }

                attr = elem ->Attribute("colour");
                if (attr !=NULL)
                {
                    hexColour= elem->GetText();
                     vector<unsigned char> rgbColor(3);

                     hextodec(hexColour,rgbColor);

                     red =int(rgbColor[0]);
                     blue = int(rgbColor[1]);
                     green= int(rgbColor[2]);


                }

                attr = elem->Attribute("duration");
                if (attr !=NULL)
                {
                    elem->QueryIntAttribute("duration", &endTime);
                }

                for (tinyxml2::XMLElement * e =elem ->FirstChildElement("Position"); e != NULL; e = e->NextSiblingElement("Position"))
                {
                    attr = e->Attribute("x");
                    if (attr != NULL)
                    {
                        Xpos = e->QueryIntAttribute("x", &Xpos);
                    }

                    attr = e->Attribute("y");
                    if (attr != NULL)
                    {
                        Ypos = e->QueryIntAttribute("y", &Ypos);
                    }
                }

                for(tinyxml2::XMLElement * v =elem ->FirstChildElement("Velocity"); v !=NULL; v = v->NextSiblingElement("Velocity"))
                {
                    attr = v -> Attribute("x");
                        if (attr != NULL)
                    {
                        Xspeed = v ->QueryIntAttribute("x", &Xspeed);
                    }

                    attr = v ->Attribute ("y");
                        if (attr !=NULL)
                    {
                        Yspeed = v ->QueryIntAttribute("y", &Yspeed);
                    }
                }
            }

        }









    //Setting initial x/y locations and speeds for each particle
    for (int loop = 0; loop < particles; loop++)
    {
        x[loop] = Xpos;
        y[loop] = Ypos;
        VelX[loop] = Xspeed;
        VelY[loop] = Yspeed;
    }

    //intiallising the colour and full alpha
    redStart   = 0.85f;//((float)rand() / (float)RAND_MAX);
    greenStart = 0.55f;//((float)rand() / (float)RAND_MAX);
    blueStart  = 0.01f;//((float)rand() / (float)RAND_MAX);

    alpha = 1.0f;

    timeUntilLaunch = startTime;
    duration = endTime;
    particleSize = 1.0f + ((float)rand()/(float)RAND_MAX)* 3.0f;

    hasExploded = false;
}

void Firework::move()
{
    for (int loop = 0; loop < particles; loop++)
    {
        if (timeUntilLaunch <= 0)
        {   
            x[loop] += VelX [loop];
            y[loop] += VelY [loop];
            VelY[loop] += Firework::gravity;
            duration --;
        }

    }
    timeUntilLaunch --;

    if (duration <= 0)
    {
        for (int loop2 = 0; loop2 < particles; loop2++)
        {
            VelX[loop2] = -4 + (rand() / (int)RAND_MAX)*8;
            VelY[loop2] = -4 + (rand() / (int)RAND_MAX)*8;
        }
         hasExploded = true;
    }
}


void Firework::explode()
{
       for (int loop = 0; loop < particles; loop++)
    {
        // Dampen the horizontal speed by 1% per frame
       VelX[loop] *= 0.99f;

        // Move the particle
        x[loop] += VelX[loop];
        y[loop] += VelY[loop];

        // Apply gravity to the particle's speed
        VelY[loop] += Firework::gravity;
    }

    // Fade out the particles (alpha is stored per firework, not per particle)
    if (alpha > 0.0f)
    {
        alpha -= 0.01f;
    }
    else // Once the alpha hits zero, then reset the firework
    {
        initialise();
    }
}
#包括“Firework.h”
#包括“tinyxml2.h”
#包括
#包括
#包括
#包括
使用名称空间std;
常量GLfloat烟花::重力=0.05f;
常量GLfloat烟花::基本速度=-4.0f;
常量GLfloat烟花::最大速度=-4.0f;

//int QueryAttributeStatus=elem->FirstChildElement(“开始”)->queryFloataAttribute(attr和timeUntillLaunch); int convertFromHex(字符串十六进制) { int值=0; int a=0; int b=十六进制长度()-1; 对于(;b>=0;a++,b--) { 如果(十六进制[b]>='0'&&hex[b] 这是错误的。它比较的是指针,而不是字符串内容。请使用strcmp。检查每次出现的情况

if ( rocket = true)

这永远是正确的,因为这是一个赋值,请使用==来代替。检查每一次出现。

ooo谢谢,但你检查每一次出现是什么意思?我的意思是相同类型的表达式在代码中出现的次数更多,请全部更正。好的,我现在明白了,我很自然地这样做了,谢谢你的回答。我想出了这个代码来解压t将xml中的数据导出到我的变量中在我更改其他所有内容之前,您是否看到它有任何错误?timeUntillLaunch=elem->FirstChildElement(“begin”)->QueryFloatAttribute(attr,&timeUntillLaunch);QueryFloataAttribute的返回值是状态值、查询是否成功等。因此,将其分配给时间变量不是一个好主意,它将覆盖查询的浮点值。int QueryAttributeStatus=elem->FirstChildElement(“begin”)->queryFloataAttribute(attr,&timeUntillLaunch);if(QueryAttributeStatus!=TIXML\u SUCCESS)/*处理错误*/这个问题让我发痒:)
if ( rocket = true)
if (attr == "Fountain")
if ( rocket = true)