Cocos2d-x源码解析(1)——地图模块(3)
来源:互联网 发布:网络电视盒什么牌子好 编辑:程序博客网 时间:2024/06/04 19:37
接上一章《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)
- Cocos2d-x源码解析(1)——地图模块(1)
- Cocos2d-x源码解析(1)——地图模块(2)
- Cocos2d-x学习笔记(九)—— 地图系统(地图编辑器,事件响应)
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(1)----cocos2D-X渲染结构
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(1)----cocos2D-X渲染结构
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(1)----cocos2D-X渲染结构
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(1)----cocos2D-X渲染结构
- cocos2d-x 源码剖析(3)
- cocos2d-x 源码剖析(1)
- cocos2d-x游戏实例(3)-获得地图索引
- cocos2d-x游戏实例(3)-获得地图索引
- cocos2d-x游戏实例(3)-获得地图索引
- cocos2d-x植物大战僵尸(1) (地图的加载)
- cocos2d-x大型地图实现(续)
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND
- cocos2D-X源码分析之从cocos2D-X学习OpenGL(3)----BATCH_COMMAND
- javascript: typeof
- 再看数据库——(3)触发器
- Android之Inflate()方法用途
- cocos2dx 3.1从零学习(三)——Touch事件(回调,反向传值)
- Lucene创建索引入门案例
- Cocos2d-x源码解析(1)——地图模块(3)
- Qt 5.3 正式版发布下载
- 远程访问路由器下的mac os(ssh+vnc)
- 【android】Activity,Window,View的关系
- web前端开发工程师需要学习的东西
- UVa 10382 - Watering Grass(贪心算法)
- cannot be cast to javax.servlet.Filter 报错, 原因servlet-api.jar冲突
- Android实现多个TextView同时显示跑马灯效果
- MDK+JLINK