My Game --文件读取数据
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 --文件读取数据的更多相关文章
- C#实现从EXCEL文件读取数据到SqlServer数据库
用第三方组件:NPOI组件实现 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用.使用 NPOI ...
- springboot~openfeign从JSON文件读取数据
对openfeign不清楚的同学可以先看我这篇文章:springboot~openfeign从此和httpClient说再见 对于openfeign来说,帮助我们解决了服务端调用服务端的问题,你不需要 ...
- 由已打开的文件读取数据---read
头文件:#include<unistd.h> 函数原型:ssize_t read(int fd,void *buf,size_t count); 参数说明:fd:文件描述符 buf:存放读 ...
- 【Selenium + Python】之 Excel、CSV、XML文件读取数据并运用数据百度查询
目录 从Excel读取数据进行百度搜索 从CSV读取数据进行百度搜索 从XML读取数据进行登录操作 附:其他学习资料(<xml.etree.ElementTree模块>.<pytho ...
- TensorFlow从0到1之TensorFlow csv文件读取数据(14)
大多数人了解 Pandas 及其在处理大数据文件方面的实用性.TensorFlow 提供了读取这种文件的方法. 前面章节中,介绍了如何在 TensorFlow 中读取文件,本节将重点介绍如何从 CSV ...
- IOS学习笔记之获取Plist文件读取数据
@property(nonatomic,strong) NSArray *pic; //创建数组属性 @property(nonatomic,assign) int index; //创建索引属性 @ ...
- 【HBase】HBase与MapReduce集成——从HDFS的文件读取数据到HBase
目录 需求 步骤 一.创建maven工程,导入jar包 二.开发MapReduce程序 三.结果 需求 将HDFS路径 /hbase/input/user.txt 文件的内容读取并写入到HBase 表 ...
- python --文件读取数据
读取整个文件: 首先创建一个文件,例如我创建了一个t x t文件了. 然后我想读取这个文件了,我首先将上面的这个文件保存在我即将要创建的Python的文件目录下, 即读取文件成功. 解析: 函数ope ...
- C# WPF 进度条,根据读取数据显示进度条进度,根据Excel文件读取数据,进度条样式
后台代码: //导入 private void Border_MouseLeftButtonUp_2(object sender, MouseButtonEventArgs e) { var path ...
随机推荐
- win7的HOST文件夹具体位置
win7的HOST文件位置为C:\WINDOWS\system32\drivers\etc\文件夹下,快捷查看方法如下: 1.按win+r,输入C:\WINDOWS\system32\drivers\ ...
- jQuery实践——选择器篇
一.基本 #id: html:<div id="demo1">demo1</div> jQuery:$("#demo1").css( ...
- Sublime Text 3 提高工作效率的使用技巧
Sublime Text 3对于Sublime Text 2压倒性的优势就是秒启动,启动非常非常快,所以从2012年到2016年我一直用Sublime Text 2,但是安装了3并且启动试用后,我再也 ...
- js创建节点及其属性
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- python基础编程
1.if else var1 = 100 if var1: print ("1 - if 表达式条件为 true") print (var1) #为0时,条件不成立 var2 = ...
- 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, ...
- PHP Cookie Session
这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...
- 远程重装centos6
写得比较简略,也是综合网络上的文章,总结一下实操的经验 获取启动内核 wget -P /boot/ http://mirrors.163.com/centos/6.8/os/x86_64/images ...
- 用流来读取文件(getline,istringstream)
ifstream infile("fileanme"); 原型:getline(istream &infile, string &line); 函数说明:读取文件中 ...
- 可以结合react的ui组件
https://ant.design/components/switch-cn/