My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLayer,BgLayerData 这个做也是为了方便从文件读取数据

把背景的数据放在文件里方便修改,也可以做个工具快速绘制背景,下面是这个背景的数据保存的XML文件:

 <RootLayer>
<Layers>
<layer>
<Beziers>
<Bezier bx="0.375" by="0.000" ex="1.000" ey="0.500" c1x="0.750" c1y="1.000" c2x="0.850" c2y="0.875"/>
<Bezier bx="0.375" by="0.500" ex="1.000" ey="0.650" c1x="1.000" c1y="0.400" c2x="0.750" c2y="0.750"/>
<Bezier bx="0.375" by="0.125" ex="1.000" ey="0.375" c1x="0.800" c1y="0.130" c2x="0.700" c2y="0.375"/>
</Beziers>
<Colors>
<color r="0.200" g="0.439" b="0.200" a="1"/>
<color r="0.196" g="0.529" b="0.251" a="1"/>
<color r="0.133" g="0.675" b="0.220" a="1"/>
</Colors>
</layer>
<layer>
<Beziers>
<Bezier bx="0.000" by="0.125" ex="0.750" ey="0.000" c1x="0.250" c1y="0.750" c2x="0.375" c2y="0.750"/>
<Bezier bx="0.000" by="0.250" ex="0.750" ey="0.500" c1x="0.375" c1y="0.250" c2x="0.375" c2y="0.500"/>
</Beziers>
<Colors>
<color r="0.255" g="0.647" b="0.310" a="1"/>
<color r="0.000" g="0.600" b="0.267" a="1"/>
</Colors>
</layer>
</Layers>
</RootLayer>

每个 Layer 下两组元素:贝赛尔曲线和对应的对应的颜色

Resources 下的 ResourcesManage 类读取文件内容,并生存相应的数据结构存储起来,方便使用

读取数据的代码:

BgLayerData * ResourcesManage::getBgLayerDataFromFile( std::string && fileName )
{
tinyxml2::XMLDocument doc;
doc.LoadFile( fileName.c_str( ) );
auto elemRoot = doc.RootElement( );
auto layers = elemRoot->FirstChildElement( "Layers" );
auto elemLayer = layers->FirstChildElement( );
while( elemLayer != 0 )
{
//auto elem = elemLayer->FirstChildElement( );
auto bezier = elemLayer->FirstChildElement( "Beziers" );
auto bz = bezier->FirstChildElement( );
auto color = elemLayer->FirstChildElement( "Colors" );
auto co = color->FirstChildElement( );
auto layer = new LineLayer( );
while( bz != 0 && co != 0 )
{
float bx = atof( bz->Attribute( "bx" ) );
float by = atof( bz->Attribute( "by" ) );
float ex = atof( bz->Attribute( "ex" ) );
float ey = atof( bz->Attribute( "ey" ) );
float c1x = atof( bz->Attribute( "c1x" ) );
float c1y = atof( bz->Attribute( "c1y" ) );
float c2x = atof( bz->Attribute( "c2x" ) );
float c2y = atof( bz->Attribute( "c2y" ) );
float r = atof( co->Attribute( "r" ) );
float g = atof( co->Attribute( "g" ) );
float b = atof( co->Attribute( "b" ) );
float a = atof( co->Attribute( "a" ) ); layer->AddBezier( bx, by, ex, ey, c1x, c1y, c2x, c2y, r, g, b, a );
bz = bz->NextSiblingElement( );
co = co->NextSiblingElement( );
}
if( !layer->isEmpty( ) )
{
_bgLayerData->AddLineLayer( layer );
}
elemLayer = elemLayer->NextSiblingElement( );
}
return _bgLayerData;
}

使用 tinyxml2 的 XMLDocument 读取文件,然后从读取的内容当中提取数据。首先找到根节点,再找到第一个“Layers”节点(也就这么一个,因为只有一个背景),“Layers”节点对应 BgLayerData 类;接下来找到第一个子节点“Layer”,“Layer”对应 LineLayer类,开始循环,从“Layer”中循环获取“Bezier”和“color”创建并添加到 LineLayer 中;循环递增条件是当前节点赋值为下一个节点,当节点为时循环结束。通过两重循环把数据提取出来,下一步就可以使用了。

ResourcesManage 类原本设想会有许多任务,但目前只用到上面这个函数,其他的就先放那里,给将来做个参考。

My Game --文件读取数据的更多相关文章

  1. C#实现从EXCEL文件读取数据到SqlServer数据库

    用第三方组件:NPOI组件实现 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用.使用 NPOI ...

  2. springboot~openfeign从JSON文件读取数据

    对openfeign不清楚的同学可以先看我这篇文章:springboot~openfeign从此和httpClient说再见 对于openfeign来说,帮助我们解决了服务端调用服务端的问题,你不需要 ...

  3. 由已打开的文件读取数据---read

    头文件:#include<unistd.h> 函数原型:ssize_t read(int fd,void *buf,size_t count); 参数说明:fd:文件描述符 buf:存放读 ...

  4. 【Selenium + Python】之 Excel、CSV、XML文件读取数据并运用数据百度查询

    目录 从Excel读取数据进行百度搜索 从CSV读取数据进行百度搜索 从XML读取数据进行登录操作 附:其他学习资料(<xml.etree.ElementTree模块>.<pytho ...

  5. TensorFlow从0到1之TensorFlow csv文件读取数据(14)

    大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 TensorFlow 中读取文件,本节将重点介绍如何从 CSV ...

  6. IOS学习笔记之获取Plist文件读取数据

    @property(nonatomic,strong) NSArray *pic; //创建数组属性 @property(nonatomic,assign) int index; //创建索引属性 @ ...

  7. 【HBase】HBase与MapReduce集成——从HDFS的文件读取数据到HBase

    目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.结果 需求 将HDFS路径 /hbase/input/user.txt 文件的内容读取并写入到HBase 表 ...

  8. python --文件读取数据

    读取整个文件: 首先创建一个文件,例如我创建了一个t x t文件了. 然后我想读取这个文件了,我首先将上面的这个文件保存在我即将要创建的Python的文件目录下, 即读取文件成功. 解析: 函数ope ...

  9. C# WPF 进度条,根据读取数据显示进度条进度,根据Excel文件读取数据,进度条样式

    后台代码: //导入 private void Border_MouseLeftButtonUp_2(object sender, MouseButtonEventArgs e) { var path ...

随机推荐

  1. linux下发现可疑用户时处理办法

    如果发现了linux被可疑用户远程登录了,怎么解决呢? 1.先查看最近系统的登录情况 last -10 表示最近10个用户登录的信息,如果发现有可疑账户,就是密码被破解了 [root@localhos ...

  2. eclipse上修改js后,浏览器上还是出现原来效果的解决方法

    废话不多说,直接上方法: 1.最简单的是清除浏览器缓存.2.换个浏览器试试.3.修改js文件名,换成别的名称,再引用.4.重启eclipse.5.重启电脑.

  3. angular的双向数据绑定

    方向1:模型数据(model) 绑定 到视图(view) 实现方法1:{{model变量名}} $scope.num=10 <p>{{num}}</p> 实现方法2: 常用指令 ...

  4. Cookie对象工具包,对象添加,获取,修改-亲测可用

    先来了解下Cookie 和 Session对象的概念吧. 首先,Cookie是客户端缓存技术,大小一般为4kb左右,主要存储一些比较小的信息,常用的例子有用户名和密码,且是不安全的: Session是 ...

  5. C#自定义大小与改变大下的方法

    在用VS的窗体设计器时,我们可以发现控件都是可以拖动的,并且还可以调整大小.怎么在自己的程序中可以使用上述功能呢? 下面的方法值得借鉴! using System; using System.Wind ...

  6. SQLite一些函数用法

    --格林威治日期时间,比北京时间晚8小时 select datetime('now'); --格林威治日期 select date('now'); --本地时间 select time('now',' ...

  7. LINQ之路 8: 解释查询(Interpreted Queries)

    LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...

  8. RequireJS 快速入门

      说明:本文只提供快速入门内容,方便快速进入实战状态.更高级的配置,请参考官网文档. 当初之所以使用 RequireJS 等工具,是因为想提高js的加载速度,避免不必要的堵塞.但通过一段时间的使用, ...

  9. PHP设计模式

    设计模式总的分为三种,创建型模式.结构性模式.行为型模式 1.创建型模式 创建型模式为根据实际情况来创建对象,创建的模式又分为对象创建模式和类创建模式,对象创建模式会把对象创建的一部分在另一个对象中实 ...

  10. jQuery 查找父元素

    function deletesec1Div5(obj){ $(obj).closest(".sec1-div5").remove();}自己写的一段代码,实现了table中的全选 ...