Cocos2d-x源代码解析(1)——地图模块(1)
cocos通过加载tiled 生成的tmx文件来生成游戏地图。本文主要分析cocos加载地图模块的源代码。
如图所看到的,地图加载模块由以上几个类组成。
对外的入口是类CCTMXTiledMap,在使用该类时。程序猿不须要知道其底层的其它类便可解析tmx文件生成地图。
那么。我们首先分析类CCTMXTiledMap是怎样调用其它类进行解析,该类的声明例如以下:
class CC_DLL CCTMXTiledMap :public CCNode
{
/** the map's size property measured intiles */
CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tMapSize, MapSize);
/** the tiles's size property measured inpixels */
CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tTileSize, TileSize);
/** map orientation */
CC_SYNTHESIZE(int, m_nMapOrientation,MapOrientation);
/** object groups */
CC_PROPERTY(CCArray*, m_pObjectGroups,ObjectGroups);
/** properties */
CC_PROPERTY(CCDictionary*, m_pProperties,Properties);
public:
/**
* @js ctor
*/
CCTMXTiledMap();
/**
* @js NA
* @lua NA
*/
virtual ~CCTMXTiledMap(); /** creates a TMX Tiled Map with a TMXfile.*/
static CCTMXTiledMap* create(const char*tmxFile); /** initializes a TMX Tiled Map with a TMXformatted XML string and a path to TMX resources */
static CCTMXTiledMap* createWithXML(constchar* tmxString, const char* resourcePath); /** initializes a TMX Tiled Map with a TMXfile */
bool initWithTMXFile(const char *tmxFile); /** initializes a TMX Tiled Map with a TMXformatted XML string and a path to TMX resources */
bool initWithXML(const char* tmxString,const char* resourcePath); /** return the TMXLayer for the specificlayer
* @js getLayer
*/
CCTMXLayer* layerNamed(const char*layerName); /** return the TMXObjectGroup for thespecific group
* @js getObjectGroup
*/
CCTMXObjectGroup* objectGroupNamed(constchar *groupName); /** return the value for the specificproperty name
* @js getProperty
*/
CCString *propertyNamed(const char*propertyName); /** return properties dictionary for tileGID */
CCDictionary* propertiesForGID(int GID); private:
CCTMXLayer *parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
CCTMXTilesetInfo *tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
void buildWithMapInfo(CCTMXMapInfo*mapInfo);
protected:
//! tile properties
CCDictionary* m_pTileProperties; };
通过查看函数实现,我们发现函数
/** creates a TMX Tiled Map with a TMX file.*/
static CCTMXTiledMap* create(const char*tmxFile);
通过调用函数
/** initializes a TMX Tiled Mapwith a TMX file */
bool initWithTMXFile(const char *tmxFile);
来实现文件的解析。并创建一个CCTMXTiledMap对象返回给使用者。
boolCCTMXTiledMap::initWithTMXFile(const char *tmxFile)
{
CCAssert(tmxFile != NULL &&strlen(tmxFile)>0, "TMXTiledMap: tmx file should not bi NULL"); setContentSize(CCSizeZero); CCTMXMapInfo *mapInfo =CCTMXMapInfo::formatWithTMXFile(tmxFile); if (! mapInfo)
{
return false;
}
CCAssert(mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found.Please check the filename.");
buildWithMapInfo(mapInfo); return true;
}
通过看源代码。我们发现有2个重要的函数 CCTMXMapInfo::formatWithTMXFile(tmxFile)和buildWithMapInfo(mapInfo),看名字应该能知道formatWithTMXFile是解析XML文件将其结构化。buildWithMapInfo是将结构化的数据变成CCNode从而显示在游戏界面上。
接下来,我们查看CCTMXMapInfo是怎样组织数据以及怎样解析XML文件的,首先我们还是查看CCTMXMapInfo的声明
class CC_DLL CCTMXMapInfo :public CCObject, public CCSAXDelegator
{
public:
/// map orientation
CC_SYNTHESIZE(int, m_nOrientation, Orientation);
/// map width & height
CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tMapSize, MapSize);
/// tiles width & height
CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tTileSize, TileSize);
/// Layers
CC_PROPERTY(CCArray*, m_pLayers, Layers);
/// tilesets
CC_PROPERTY(CCArray*, m_pTilesets,Tilesets);
/// ObjectGroups
CC_PROPERTY(CCArray*, m_pObjectGroups,ObjectGroups);
/// parent element
CC_SYNTHESIZE(int, m_nParentElement,ParentElement);
/// parent GID
CC_SYNTHESIZE(unsigned int, m_uParentGID,ParentGID);
/// layer attribs
CC_SYNTHESIZE(int, m_nLayerAttribs,LayerAttribs);
/// is storing characters?
CC_SYNTHESIZE(bool, m_bStoringCharacters,StoringCharacters);
/// properties
CC_PROPERTY(CCDictionary*, m_pProperties,Properties);
public:
/**
* @js ctor
* @lua NA
*/
CCTMXMapInfo();
/**
* @js NA
* @lua NA
*/
virtual ~CCTMXMapInfo();
/** creates a TMX Format with a tmx file */
static CCTMXMapInfo *formatWithTMXFile(const char *tmxFile);
/** creates a TMX Format with an XML stringand a TMX resource path */
static CCTMXMapInfo * formatWithXML(constchar* tmxString, const char* resourcePath);
/** initializes a TMX format with a tmx file
* @lua NA
*/
bool initWithTMXFile(const char *tmxFile);
/** initializes a TMX format with an XMLstring and a TMX resource path
* @lua NA
*/
bool initWithXML(const char* tmxString,const char* resourcePath);
/** initializes parsing of an XML file,either a tmx (Map) file or tsx (Tileset) file */
bool parseXMLFile(const char *xmlFilename);
/* initializes parsing of an XML string,either a tmx (Map) string or tsx (Tileset) string */
bool parseXMLString(const char *xmlString); CCDictionary* getTileProperties();
void setTileProperties(CCDictionary*tileProperties); /** implement pure virtual methods ofCCSAXDelegator
* @js NA
*/
void startElement(void *ctx, const char*name, const char **atts);
/**
* @js NA
*/
void endElement(void *ctx, const char*name);
/**
* @js NA
*/
void textHandler(void *ctx, const char *ch,int len); inline const char* getCurrentString(){return m_sCurrentString.c_str(); }
inline void setCurrentString(const char*currentString){ m_sCurrentString = currentString; }
inline const char* getTMXFileName(){ returnm_sTMXFileName.c_str(); }
inline void setTMXFileName(const char*fileName){ m_sTMXFileName = fileName; }
private:
void internalInit(const char* tmxFileName,const char* resourcePath);
protected:
//! tmx filename
std::string m_sTMXFileName;
// tmx resource path
std::string m_sResources;
//! current string
std::string m_sCurrentString;
//! tile properties
CCDictionary* m_pTileProperties;
unsigned int m_uCurrentFirstGID;
};
通过查看声明 我们发现CCTMXMapInfo主要包含下面几个数据:
- TMXLayerInfo的数组,用来存放地图信息
- TMXTilesetInfo的数组,用来存放块信息
- ObjectGroups的数组
另外。CCTMXMapInfo中有几个重要的函数:
bool initWithXML(const char* tmxString, constchar* resourcePath);
/** initializes parsing of an XML file,either a tmx (Map) file or tsx (Tileset) file */
bool parseXMLFile(const char *xmlFilename);
/* initializes parsing of an XML string, eithera tmx (Map) string or tsx (Tileset) string */
bool parseXMLString(const char *xmlString);
这3个函数是libxml2中所须要解析xml的代理函数,分别表示标签的開始,标签的结束,标签的内容
解析xml的标签就在这几个函数中。
Cocos2d-x源代码解析(1)——地图模块(1)的更多相关文章
- Android xUtils3源代码解析之网络模块
本文已授权微信公众号<非著名程序猿>原创首发,转载请务必注明出处. xUtils3源代码解析系列 一. Android xUtils3源代码解析之网络模块 二. Android xUtil ...
- Cocos2d-x源代码解析(1)——地图模块(3)
接上一章<Cocos2d-x源代码解析(1)--地图模块(2)> 通过前面两章的分析,我们能够知道cocos将tmx的信息结构化到 CCTMXMapInfo.CCTMXTilesetInf ...
- thttpd源代码解析 定时器模块
thttpd源代码解析 定时器模块 thttpd是很轻量级的httpserver,可运行文件仅50kB.名称中的第一个t表示tiny, turbo, 或throttling 与lighttpd.mem ...
- 《nginx源代码解析》系列分享专栏
<nginx源代码解析>系列分享专栏 解析nginx源代码,从main函数开始,一步步解读nginx运行原理,同时进行nginx第三方模块的开发,努力做到知其然,知其所以然 <ngi ...
- linux device tree源代码解析--转
//Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...
- Android源代码解析之(十三)-->apk安装流程
转载请标明出处:一片枫叶的专栏 上一篇文章中给大家分析了一下android系统启动之后调用PackageManagerService服务并解析系统特定文件夹.解析apk文件并安装的过程,这个安装过程实 ...
- Spark技术内幕:Client,Master和Worker 通信源代码解析
Spark的Cluster Manager能够有几种部署模式: Standlone Mesos YARN EC2 Local 在向集群提交计算任务后,系统的运算模型就是Driver Program定义 ...
- Spring源代码解析
Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
随机推荐
- 【BZOJ 2665】 2665: [cqoi2012]编号(暴力?)
2665: [cqoi2012]编号 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 347 Solved: 217 Description 你需要给一 ...
- HDU 1698 Just a Hook (线段树)
Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...
- 「CTSC2016」单调上升路径
「CTSC2016」单调上升路径 解题思路:根据提示可以得到答案的下界是 \(n - 1\) ,然后打表发现这个下界好像一定可以取到. 事实上考虑 \(n\) 个点完全图的边数是 \(\frac{n( ...
- 【离散化】【DFS】Gym - 101617H - Security Badges
题意:给你一张有向图,每条边有个限制范围,只有权值在限制范围内的人能走这条边,问你权值不超过K的人中,有多少人能从S到T. K很大,因此我们只处理边的范围的上下界这O(m)个权值能否到达,以防万一,还 ...
- [HAOI2015]数组游戏
题目大意: 有一排n个格子,每个格子上都有一个白子或黑子,在上面进行游戏,规则如下: 选择一个含白子的格子x,并选择一个数k,翻转x,2x,...,kx格子上的子. 不能操作者负. 思路: 将“某个格 ...
- 零配置文件搭建SpringMvc
零配置文件搭建SpringMvc SpringMvc 流程原理 (1)用户发送请求至前端控制器DispatcherServlet:(2) DispatcherServlet收到请求后,调用Handle ...
- 【8.22校内测试】【数学】【并查集】【string】
今天的t2t3能打出来80分的暴力都好满足啊QwQ.(%%%$idy$ 今天的签到题,做的时候一眼就看出性质叻qwq.大于11的所有数分解合数都可以用4.6.9表示,乱搞搞就可以了. #include ...
- bzoj 4836: [Lydsy2017年4月月赛]二元运算 -- 分治+FFT
4836: [Lydsy2017年4月月赛]二元运算 Time Limit: 8 Sec Memory Limit: 128 MB Description 定义二元运算 opt 满足 现在给定一 ...
- Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形
题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...
- combotree的总结(这个好)
1:最近有这个需求,就是ext下的combo下拉是树,网上的例子很多,封装的也很好,基本都可以满足下拉框下出现想要的树,但是我的使用情况是比如在用户编辑的时候,要把用户已经选择过的数值自动再赋值到下拉 ...