接上一章《Cocos2d-x源代码解析(1)——地图模块(2)》

通过前面两章的分析,我们能够知道cocos将tmx的信息结构化到 CCTMXMapInfo。CCTMXTilesetInfo,CCTMXLayerInfo之中。

当中CCTMXMapInfo存储地图的信息包扩下面几块信息:

- Map orientation (hexagonal, isometric or orthogonal)

- Tile size

- Map size



- Layers (an array of TMXLayerInfo objects)

- Tilesets (an array of TMXTilesetInfo objects)

- ObjectGroups (an array of TMXObjectGroupInfo objects)

CCTMXTilesetInfo存储瓦片信息包含:

- Tileset name

- Tileset spacing

- Tileset margin

- size of the tiles

- Image used for the tiles

- Image size

CCTMXLayerInfo存储层的信息包含:

-
Layer name

- Layer size

- Layer opacity at creation time (it can be modified at runtime)

- Whether the layer is visible (if it's not visible, then the CocosNode won't be created)

接下来,我们分析cocos怎样通过这些信息,生成地图的图像:

首先我们看一个重要的函数buildWithMapInfo

void CCTMXTiledMap::buildWithMapInfo(CCTMXMapInfo* mapInfo)
{
m_tMapSize = mapInfo->getMapSize();
m_tTileSize = mapInfo->getTileSize();
m_nMapOrientation = mapInfo->getOrientation(); CC_SAFE_RELEASE(m_pObjectGroups);
m_pObjectGroups = mapInfo->getObjectGroups();
CC_SAFE_RETAIN(m_pObjectGroups); CC_SAFE_RELEASE(m_pProperties);
m_pProperties = mapInfo->getProperties();
CC_SAFE_RETAIN(m_pProperties); CC_SAFE_RELEASE(m_pTileProperties);
m_pTileProperties = mapInfo->getTileProperties();
CC_SAFE_RETAIN(m_pTileProperties); int idx=0; CCArray* layers = mapInfo->getLayers();
if (layers && layers->count()>0)
{
CCTMXLayerInfo* layerInfo = NULL;
CCObject* pObj = NULL;
CCARRAY_FOREACH(layers, pObj)
{
layerInfo = (CCTMXLayerInfo*)pObj;
if (layerInfo && layerInfo->m_bVisible)
{
CCTMXLayer *child = parseLayer(layerInfo, mapInfo);
addChild((CCNode*)child, idx, idx); // update content size with the max size
const CCSize& childSize = child->getContentSize();
CCSize currentSize = this->getContentSize();
currentSize.width = MAX( currentSize.width, childSize.width );
currentSize.height = MAX( currentSize.height, childSize.height );
this->setContentSize(currentSize); idx++;
}
}
}
}

这个函数首相将地图信息赋给map类。接下来对于每一个layer进行处理,重要的函数是parseLayer,这个函数是将layer的信息处理好而且加上sprite,并生成CTMXlayer对象。

因此我们须要分析parseLayer函数和CTMXlayer类。

首先我们看CTMXlayer的声明:

class CC_DLL CCTMXLayer : public CCSpriteBatchNode
{
/** size of the layer in tiles */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tLayerSize, LayerSize);
/** size of the map's tile (could be different from the tile's size) */
CC_SYNTHESIZE_PASS_BY_REF(CCSize, m_tMapTileSize, MapTileSize);
/** pointer to the map of tiles */
CC_SYNTHESIZE(unsigned int*, m_pTiles, Tiles);
/** Tileset information for the layer */
CC_PROPERTY(CCTMXTilesetInfo*, m_pTileSet, TileSet);
/** Layer orientation, which is the same as the map orientation */
CC_SYNTHESIZE(unsigned int, m_uLayerOrientation, LayerOrientation);
/** properties from the layer. They can be added using Tiled */
CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
public:
/**
* @js ctor
* @lua NA
*/
CCTMXLayer();
/**
* @js NA
* @lua NA
*/
virtual ~CCTMXLayer(); /** creates a CCTMXLayer with an tileset info, a layer info and a map info */
static CCTMXLayer * create(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); /** initializes a CCTMXLayer with a tileset info, a layer info and a map info
* @lua NA
*/
bool initWithTilesetInfo(CCTMXTilesetInfo *tilesetInfo, CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); /** dealloc the map that contains the tile position from memory.
Unless you want to know at runtime the tiles positions, you can safely call this method.
If you are going to call layer->tileGIDAt() then, don't release the map
*/
void releaseMap(); /** returns the tile (CCSprite) at a given a tile coordinate.
The returned CCSprite will be already added to the CCTMXLayer. Don't add it again.
The CCSprite can be treated like any other CCSprite: rotated, scaled, translated, opacity, color, etc.
You can remove either by calling:
- layer->removeChild(sprite, cleanup);
- or layer->removeTileAt(ccp(x,y));
@js getTileGIDAt
*/
CCSprite* tileAt(const CCPoint& tileCoordinate); /** returns the tile gid at a given tile coordinate.
if it returns 0, it means that the tile is empty.
This method requires the the tile map has not been previously released (eg. don't call layer->releaseMap())
@js tileGIDAt
*/
unsigned int tileGIDAt(const CCPoint& tileCoordinate); /** returns the tile gid at a given tile coordinate. It also returns the tile flags.
This method requires the the tile map has not been previously released (eg. don't call [layer releaseMap])
@js tileGIDAt
@lua NA
*/
unsigned int tileGIDAt(const CCPoint& tileCoordinate, ccTMXTileFlags* flags); /** sets the tile gid (gid = tile global id) at a given tile coordinate.
The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
If a tile is already placed at that position, then it will be removed.
*/
void setTileGID(unsigned int gid, const CCPoint& tileCoordinate); /** sets the tile gid (gid = tile global id) at a given tile coordinate.
The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
If a tile is already placed at that position, then it will be removed. Use withFlags if the tile flags need to be changed as well
*/ void setTileGID(unsigned int gid, const CCPoint& tileCoordinate, ccTMXTileFlags flags); /** removes a tile at given tile coordinate */
void removeTileAt(const CCPoint& tileCoordinate); /** returns the position in points of a given tile coordinate
* @js getPositionAt
*/
CCPoint positionAt(const CCPoint& tileCoordinate); /** return the value for the specific property name
* @js getProperty
*/
CCString *propertyNamed(const char *propertyName); /** Creates the tiles */
void setupTiles(); /** CCTMXLayer doesn't support adding a CCSprite manually.
* @warning addchild(z, tag); is not supported on CCTMXLayer. Instead of setTileGID.
* @lua NA
*/
virtual void addChild(CCNode * child, int zOrder, int tag);
/** super method
* @lua NA
*/
void removeChild(CCNode* child, bool cleanup); inline const char* getLayerName(){ return m_sLayerName.c_str(); }
inline void setLayerName(const char *layerName){ m_sLayerName = layerName; }
private:
CCPoint positionForIsoAt(const CCPoint& pos);
CCPoint positionForOrthoAt(const CCPoint& pos);
CCPoint positionForHexAt(const CCPoint& pos); CCPoint calculateLayerOffset(const CCPoint& offset); /* optimization methods */
CCSprite* appendTileForGID(unsigned int gid, const CCPoint& pos);
CCSprite* insertTileForGID(unsigned int gid, const CCPoint& pos);
CCSprite* updateTileForGID(unsigned int gid, const CCPoint& pos); /* The layer recognizes some special properties, like cc_vertez */
void parseInternalProperties();
void setupTileSprite(CCSprite* sprite, CCPoint pos, unsigned int gid);
CCSprite* reusedTileWithRect(CCRect rect);
int vertexZForPos(const CCPoint& pos); // index
unsigned int atlasIndexForExistantZ(unsigned int z);
unsigned int atlasIndexForNewZ(int z);
protected:
//! name of the layer
std::string m_sLayerName;
//! TMX Layer supports opacity
unsigned char m_cOpacity; unsigned int m_uMinGID;
unsigned int m_uMaxGID; //! Only used when vertexZ is used
int m_nVertexZvalue;
bool m_bUseAutomaticVertexZ; //! used for optimization
CCSprite *m_pReusedTile;
ccCArray *m_pAtlasIndexArray; // used for retina display
float m_fContentScaleFactor;
};

尽管有一大堆的属性和函数,可是归根结底他是一个CCSpriteBatchNode对象,因为地图中非常多图片是同样的。CCSpriteBatchNode就是cocos2d-x为了减少渲染批次而建立的一个专门管理精灵的类。

然后是函数parseLayer

CCTMXLayer * CCTMXTiledMap::parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo)
{
CCTMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
CCTMXLayer *layer = CCTMXLayer::create(tileset, layerInfo, mapInfo); // tell the layerinfo to release the ownership of the tiles map.
layerInfo->m_bOwnTiles = false;
layer->setupTiles(); return layer;
}

这个函数又分为3步

第一步tilesetForLayer是拿到随意一块属于这个layer的tile。

第二步是依据layerInfo创建一个真正的层信息。

第三步是将tiled增加到这个层中去。

Cocos2d-x源代码解析(1)——地图模块(3)的更多相关文章

  1. Android xUtils3源代码解析之网络模块

    本文已授权微信公众号<非著名程序猿>原创首发,转载请务必注明出处. xUtils3源代码解析系列 一. Android xUtils3源代码解析之网络模块 二. Android xUtil ...

  2. Cocos2d-x源代码解析(1)——地图模块(1)

    cocos通过加载tiled 生成的tmx文件来生成游戏地图.本文主要分析cocos加载地图模块的源代码.   如图所看到的,地图加载模块由以上几个类组成. 对外的入口是类CCTMXTiledMap, ...

  3. thttpd源代码解析 定时器模块

    thttpd源代码解析 定时器模块 thttpd是很轻量级的httpserver,可运行文件仅50kB.名称中的第一个t表示tiny, turbo, 或throttling 与lighttpd.mem ...

  4. 《nginx源代码解析》系列分享专栏

    <nginx源代码解析>系列分享专栏 解析nginx源代码,从main函数开始,一步步解读nginx运行原理,同时进行nginx第三方模块的开发,努力做到知其然,知其所以然 <ngi ...

  5. linux device tree源代码解析--转

    //Based on Linux v3.14 source code Linux设备树机制(Device Tree) 一.描述 ARM Device Tree起源于OpenFirmware (OF), ...

  6. Android源代码解析之(十三)--&gt;apk安装流程

    转载请标明出处:一片枫叶的专栏 上一篇文章中给大家分析了一下android系统启动之后调用PackageManagerService服务并解析系统特定文件夹.解析apk文件并安装的过程,这个安装过程实 ...

  7. Spark技术内幕:Client,Master和Worker 通信源代码解析

    Spark的Cluster Manager能够有几种部署模式: Standlone Mesos YARN EC2 Local 在向集群提交计算任务后,系统的运算模型就是Driver Program定义 ...

  8. Spring源代码解析

    Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的启动:http://www.itey ...

  9. python 解析XML python模块xml.dom解析xml实例代码

    分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...

随机推荐

  1. pygame系列

    在接下来的blog中,会有一系列的文章来介绍关于pygame的内容,pygame系列偷自http://www.cnblogs.com/hongten/p/hongten_pygame_install. ...

  2. vim ctl+v批量添加/删除

    vim编辑器---批量注释与反注释 在使用vim编写代码的时候,经常需要用到批量注释与反注释一段代码.下面简要介绍其操作. 方法一 块选择模式 插入注释: 用v进入virtual模式 用上下键选中需要 ...

  3. WPF换肤之一:创建圆角窗体

    原文:WPF换肤之一:创建圆角窗体 我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感.这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文 ...

  4. HPUX平台经常使用命令列举

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...

  5. Ubuntu 如何重新安裝 Unity ?

    阿舍在刪除 Qt 的時候下錯指令,結果,就把 Unity 給移除掉了,雖然,阿舍從此就知道 Unity 和 Qt 有不分離的關係,不過,就沒有Unity可以用了,這樣阿舍要試東西就不是很方便了哩 ! ...

  6. 性能是全新的 SEO

    作为一个前端project师,那不只就是公开地处理那些美丽的html5, css3 和javascript特效.小而重要的一部分工作就是要让项目朝着代码稳定和代码标准方向进展.设计.信息结构以及后台限 ...

  7. 【从翻译mos文章】Weblogic AdminServer 启动fail,报错&quot;unable to get file lock, will retry&quot;

    Weblogic AdminServer 启动fail,报错"unable to get file lock, will retry" 参考原始: Weblogic AdminSe ...

  8. 3Dmax+blend+WPF综合运用

    原文:3Dmax+blend+WPF综合运用 赛后总结 本人小菜,WPF刚入门,只是写一下最近的项目心得.欢迎各位前辈们前来拍砖指正,感激不敬!先申明,小弟我入门仓促,很多东西也是一知半解,所以很多问 ...

  9. Spring Boot 基础

    Spring Boot 基础 Spring Boot 项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置.使用Spring Boot ...

  10. 用Linux/Unix命令把十六进制转换成十进制(转)

    那天写个脚本,需要把十六进制的数字转成十进制的打出来,发现不知道要怎么弄,搜一下,原来还是很简单的,比用C语言什么的容易多了,就一些现成的命令就解决了. 先列两种简单的方法: 1) echo 自己就能 ...