gpt4 book ai didi

c++ - pugixml spritesheet 导航

转载 作者:行者123 更新时间:2023-11-28 07:07:01 24 4
gpt4 key购买 nike

我有一个描述 Sprite 表中 Sprite 的 xml 文件:

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

但是,我发现很难搜索特定的桨 Sprite ,例如"Big"+ "spr name=0"然后在此处访问属性值,即 x、y、w、h 以允许我从 sprite 表中绘制特定的 sprite。最终,我希望将这些值放入以下 ​​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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com