从TMX文件中读取CSV

时间:2013-02-14 22:44:37

标签: c++ xml parsing csv collision

首先,我不确定在读取或其他地方是否出现问题但是当我尝试将值输出到.txt文件中时(为了调试),某些值被写为垃圾值。

我正在尝试加载等距RPG的碰撞图。这是我的工作:首先,我从CSV中删除所有空格,然后调用我的解析函数。

xml::Text t = data->GetText();
std::string::iterator it = std::remove(s.begin(), s.end(), ' ');
s.erase(it, s.end());
parseCSV(mapType, s);

在下面的部分中,我试图将值分开并将它们读入适当的位置。请注意,passabilityMap是属于我正在使用的API的Map2D对象的实例。我认为问题不在于那个类,因为我以前使用它没有任何问题。在调试时我发现Tile :: passable等的值也是正确的,从上面的代码发送的流的内容似乎也是正确的(没有空格并与CSV映射匹配)

我将218的CSValue(等于Tile :: flyable)转换为2(Passability :: flyable) 219(等于Tile :: nonPassable)为0(Passability :: nonPassable) 和0(等于Tile :: passable)到1(Passability :: passable)更方便。首先我认为在转换或不匹配时可能会出现问题,但我在这一部分中没有发现任何错误。

if(mapType == "Collision")
{
    x = y = 0;
    while(std::getline(ss, ID, ','))
    {
        if(ID == Tile::passable)
            passabilityMap(x, y) = Passability::Passable;

        else if(ID == Tile::nonPassable)
            passabilityMap(x, y) = Passability::NonPassable;

        else if(ID == Tile::flyable)
            passabilityMap(x, y) = Passability::Flyable;

        if(x == passabilityMap.GetWidth() - 1)
            x = 0;

        if(y == passabilityMap.GetHeight() - 1)
        {
            y = 0;
            x++;
        }
        y++;
    }
}

最后我得到输出以使用以下代码检查它:

std::ofstream file("Tile Map.txt");
ss.str(std::string());
ss.clear();
for(int x = 0; x < passabilityMap.GetWidth(); x++)
{
    for(int y = 0; y < passabilityMap.GetHeight(); y++)
    {
        ss << passabilityMap(x, y) << ",";
    }
    file << ss.str() << std::endl;
    ss.str(std::string());
    ss.clear();
}
file.close()

.txt输出的一部分

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-842150451,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-842150451,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-842150451,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-842150451,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
-842150451,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,
.
.
.
-842150451,0,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,-842150451,

最后,这里是完整的解析代码

while(layer)
{
    data = layer->FirstChildElement("data");
    while(att)
    {
        std::string attName = att->Name();
        if(attName == "name")
            mapType = att->Value();

        else if(attName == "width")
            size.Width = stoi(att->Value());

        else if(attName == "height")
            size.Height = stoi(att->Value());

        att = att->Next();
    }

    resize(mapType, size);
    xml::Text t = data->GetText();
    std::string s = t.ValueStr();
    std::string::iterator it = std::remove(s.begin(), s.end(), ' ');
    s.erase(it, s.end());
    parseCSV(mapType, s);
    layer = layer->NextSiblingElement("layer");
    if(layer)
        att = layer->FirstAttribute();
}

和XML结构

<map atts...>
    <layer atts...>
        <data atts...>
        1,1,1...
        </data>
     </layer>
     <layer atts...>
        <data atts...>
        1,1,1...
        </data>
     </layer>
</map>

如您所见,问题出现在除最后一行之外的每一行的开头。垃圾值应为0.我希望这不是太多的代码。

提前谢谢。

0 个答案:

没有答案