我有一个XML文件,用于描述位于Sprite工作表中的Sprite:
<?xml version="1.0"?>
<!-- Generated by darkFunction Editor (www.darkfunction.com) -->
<img name="PaddleGfx.png" w="350" h="340">
<definitions>
<dir name="PaddleGfx">
<dir name="Normal">
<spr name="0" x="0" y="0" w="74" h="20"/>
<spr name="1" x="74" y="0" w="74" h="20"/>
<spr name="2" x="148" y="0" w="74" h="20"/>
<spr name="3" x="222" y="0" w="74" h="20"/>
<spr name="4" x="0" y="20" w="74" h="20"/>
<spr name="5" x="74" y="20" w="74" h="20"/>
<spr name="6" x="148" y="20" w="74" h="20"/>
<spr name="7" x="252" y="40" w="74" h="20"/>
</dir>
<dir name="Big">
<spr name="0" x="222" y="20" w="126" h="20"/>
<spr name="1" x="0" y="40" w="126" h="20"/>
<spr name="2" x="126" y="40" w="126" h="20"/>
<spr name="3" x="0" y="60" w="126" h="20"/>
<spr name="4" x="126" y="60" w="126" h="20"/>
<spr name="5" x="0" y="80" w="126" h="19"/>
<spr name="6" x="0" y="99" w="126" h="20"/>
<spr name="7" x="126" y="99" w="126" h="20"/>
</dir>
<dir name="Small">
<spr name="0" x="252" y="60" w="58" h="20"/>
<spr name="1" x="252" y="99" w="58" h="20"/>
<spr name="2" x="0" y="119" w="58" h="20"/>
<spr name="3" x="126" y="80" w="58" h="19"/>
<spr name="4" x="0" y="139" w="58" h="20"/>
<spr name="5" x="258" y="80" w="58" h="19"/>
<spr name="6" x="0" y="159" w="58" h="20"/>
<spr name="7" x="0" y="179" w="58" h="20"/>
</dir>
<dir name="Normal2">
<spr name="0" x="58" y="119" w="74" h="20"/>
<spr name="1" x="58" y="139" w="74" h="20"/>
<spr name="2" x="58" y="159" w="74" h="20"/>
<spr name="3" x="184" y="80" w="74" h="19"/>
<spr name="4" x="58" y="179" w="74" h="20"/>
<spr name="5" x="58" y="199" w="74" h="20"/>
<spr name="6" x="58" y="219" w="74" h="20"/>
<spr name="7" x="58" y="239" w="74" h="20"/>
</dir>
<dir name="Normal3">
<spr name="0" x="58" y="259" w="74" h="20"/>
<spr name="1" x="58" y="279" w="74" h="20"/>
<spr name="2" x="58" y="299" w="74" h="21"/>
<spr name="3" x="58" y="320" w="74" h="20"/>
<spr name="4" x="132" y="119" w="74" h="21"/>
<spr name="5" x="206" y="119" w="74" h="20"/>
<spr name="6" x="132" y="140" w="74" h="20"/>
<spr name="7" x="206" y="140" w="74" h="20"/>
</dir>
<dir name="NormalToSmall">
<spr name="0" x="132" y="160" w="74" h="20"/>
<spr name="1" x="206" y="160" w="72" h="20"/>
<spr name="2" x="280" y="119" w="70" h="20"/>
<spr name="3" x="280" y="140" w="68" h="20"/>
<spr name="4" x="278" y="160" w="66" h="20"/>
<spr name="5" x="132" y="180" w="64" h="20"/>
<spr name="6" x="132" y="200" w="62" h="20"/>
<spr name="7" x="132" y="220" w="60" h="20"/>
</dir>
<dir name="NormalToBig">
<spr name="0" x="196" y="180" w="74" h="20"/>
<spr name="1" x="270" y="180" w="80" h="20"/>
<spr name="2" x="196" y="200" w="86" h="20"/>
<spr name="3" x="196" y="220" w="93" h="19"/>
<spr name="4" x="196" y="239" w="100" h="18"/>
<spr name="5" x="196" y="257" w="106" h="19"/>
<spr name="6" x="196" y="276" w="112" h="20"/>
<spr name="7" x="197" y="296" w="118" h="20"/>
</dir>
</dir>
</definitions>
</img>
我已经能够使用以下内容在img / definitions / dir / dir下列出内容
pugi::xml_node panels = doc.child("img").child("definitions").child("dir").child("dir");
std::cout <<"panels.name: "<< panels.name() << std::endl;
for (pugi::xml_node panel = panels.first_child(); panel; panel = panel.next_sibling())
{
std::cout <<"panel.name: " << panel.name() << std::endl;
for (pugi::xml_attribute attr = panel.first_attribute(); attr; attr = attr.next_attribute())
{
std::cout << " attr.name: " << attr.name() << "= attr.value: " << attr.value() << std::endl;
}
std::cout << std::endl;
}
输出:
panels.name: dir
panel.name: spr
attr.name: name= attr.value: 0
attr.name: x= attr.value: 0
attr.name: y= attr.value: 0
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 1
attr.name: x= attr.value: 74
attr.name: y= attr.value: 0
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 2
attr.name: x= attr.value: 148
attr.name: y= attr.value: 0
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 3
attr.name: x= attr.value: 222
attr.name: y= attr.value: 0
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 4
attr.name: x= attr.value: 0
attr.name: y= attr.value: 20
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 5
attr.name: x= attr.value: 74
attr.name: y= attr.value: 20
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 6
attr.name: x= attr.value: 148
attr.name: y= attr.value: 20
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
panel.name: spr
attr.name: name= attr.value: 7
attr.name: x= attr.value: 252
attr.name: y= attr.value: 40
attr.name: w= attr.value: 74
attr.name: h= attr.value: 20
Process returned 0 (0x0) execution time : 0.109 s
Press any key to continue.
但是,我发现很难搜索特定的桨形精灵,例如然后使用“大” +“ spr名称= 0”在此处访问属性值,即x,y,w,h,以便我从子画面表中绘制该特定子画面。
最终,我希望将这些值放入以下sfml构造中:
sf::IntRect(10, 10, 32, 32))) //sprite x,y,width,height
我尝试使用cout从pugixml的文档中测试各种方法,但我无法做任何想要的事情,例如
cout<<"test: "<<doc.find_child_by_attribute("spr","name","0").attribute("w").value()<<endl<<endl;
最佳答案
对于您的特定示例,您可以使用两个find_child_by_attribute调用:
pugi::xml_node n1 = doc.child("img").child("definitions").child("dir").find_child_by_attribute("dir", "name", "Big").find_child_by_attribute("spr", "name", "0");
std::cout << n1.attribute("w").value() << std::endl;
或使用XPath:
pugi::xml_node n2 = doc.select_single_node("img/definitions/dir/dir[@name='Big']/spr[@name='0']").node();
std::cout << n2.attribute("w").value() << std::endl
两者都输出126。
在您的示例中,find_child_by_attribute不起作用的原因是,它仅查找节点的子节点,而不查找子树中的任何后代。
关于c++ - pugixml spritesheet导航,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21640989/