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. smarty模板引擎

    1.    使用smarty 1.1 项目引入 // 3, 连接数据库,提取相关数据 $title = "Smarty模板引擎"; $content = "Smarty模 ...

  2. USB OTG插入检测识别

    转载请标注原文地址:http://blog.csdn.net/uranus_wm/article/details/9838847 一 USB引脚一般四根线,定义如下: 为支持OTG功能,mini/mi ...

  3. CAS单点登录和spring securtiy集成

    说明:本文章主要建立在spring-security早已集成在系统中的前提下: 1.需要创建一个spring-security.xml文件并关联在applicationContext.xml文件中:& ...

  4. 使用scp在windows和Linux之间互传文件

    转自:http://yangzhongfei.blog.163.com/blog/static/4610987520103141050918/ 为了进行系统维护操作,有时需要再windows和linu ...

  5. Analyzer中进行货币转换

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 微信iphone7、 ios10播放视频解决方案 2016.11.10

    2016.11.10日更新以下方法 微信最新出同层播放规范 即使是官方的也无法解决所有android手机的问题. 另外iphone 5 .5s 某些手机始终会弹出播放,请继续采用 “以下是老的解决办法 ...

  7. 1006. Sign In and Sign Out (25)

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  8. JavaScript获取浏览器类型与版本

    从网上找到一段使用JavaScript判断浏览器以及浏览器版本的比较好的代码,在此记录一下: <script type="text/javascript"> var S ...

  9. hdu acm 简单暴力1004

    字符串匹配函数strcmp 直接使用来判断两字符串是否完全相等 用数组存每个单词的个数时  初始化为零就错 初始化为一时就正确  也不知道为什么

  10. Centos6.5下的Hadoop安装

    开始进行云计算部分的学习,为了存档,写下现在进行过的步骤 需要用到的主要版本: 虚拟机:Vmware Workstation pro 12.5 Linux系统:CentOS6.4 64bit jdk版 ...