本文来自http://blog.csdn.net/runaying ,引用必须注明出处!
温馨提醒:为了大家能更好学习,强烈推荐大家看看本人的这篇博客 Cocos2d-X权威指南笔记
TMX/XML 地图解析 和CCTMXTiledMap 差不多(//cocos2d-x-3.0alpha0/cocos2dx/tilemap_parallax_nodes/CCTMXTiledMap.h)
//cocos2d-x-3.0alpha0/cocos2dx/tilemap_parallax_nodes/ //TMX/XML 地图解析 #ifndef __CC_TM_XML_PARSER__ #define __CC_TM_XML_PARSER__ #include "cocoa/CCArray.h" #include "cocoa/CCDictionary.h" #include "cocoa/CCGeometry.h" #include "platform/CCSAXParser.h" #include <string> NS_CC_BEGIN class TMXObjectGroup; /** @file * 内部TMX 解析器 * * IMPORTANT: 这些类不应该使用 doxygen 字符串生成文旦,因为用于不应该使用它们 * */ /** * @addtogroup tilemap_parallax_nodes * @{ */ enum { TMXLayerAttribNone = 1 << 0, TMXLayerAttribBase64 = 1 << 1, TMXLayerAttribGzip = 1 << 2, TMXLayerAttribZlib = 1 << 3, }; enum { TMXPropertyNone, TMXPropertyMap, TMXPropertyLayer, TMXPropertyObjectGroup, TMXPropertyObject, TMXPropertyTile }; typedef enum ccTMXTileFlags_ { kTMXTileHorizontalFlag = 0x80000000, kTMXTileVerticalFlag = 0x40000000, kTMXTileDiagonalFlag = 0x20000000, kFlipedAll = (kTMXTileHorizontalFlag|kTMXTileVerticalFlag|kTMXTileDiagonalFlag), kFlippedMask = ~(kFlipedAll) } ccTMXTileFlags; // Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags /** @brief TMXLayerInfo contains the information about the layers like: - 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) This information is obtained from the TMX file. */ class CC_DLL TMXLayerInfo : public Object { public: /** * @js ctor */ TMXLayerInfo(); /** * @js NA * @lua NA */ virtual ~TMXLayerInfo(); void setProperties(Dictionary *properties); Dictionary* getProperties(); Dictionary *_properties; std::string _name; Size _layerSize; unsigned int *_tiles; bool _visible; unsigned char _opacity; bool _ownTiles; unsigned int _minGID; unsigned int _maxGID; Point _offset; }; /** @brief TMXTilesetInfo 包含地图信息,例如: - Tileset name - Tileset spacing - Tileset margin - size of the tiles - Image used for the tiles - Image size 此信息是从TMX文件得到的 */ class CC_DLL TMXTilesetInfo : public Object { public: std::string _name; unsigned int _firstGid; Size _tileSize; unsigned int _spacing; unsigned int _margin; //! filename 文件名包含 tiles (should be spritesheet / texture atlas) std::string _sourceImage; //! size in pixels of the image Size _imageSize; public: /** * @js ctor */ TMXTilesetInfo(); /** * @js NA * @lua NA */ virtual ~TMXTilesetInfo(); Rect rectForGID(unsigned int gid); }; /** @brief 包含地图信息,例如: - 地图方向 (hexagonal, isometric or orthogonal) - Tile size - Map size And it also contains: - Layers (an array of TMXLayerInfo objects) - Tilesets (an array of TMXTilesetInfo objects) - ObjectGroups (an array of TMXObjectGroupInfo objects) 此信息是从TMX文件得到的 */ class CC_DLL TMXMapInfo : public Object, public SAXDelegator { public: /** 使用 tmx 文件,创建 TMX 格式 */ static TMXMapInfo * create(const char *tmxFile); /** 使用 XML string 、一个 TMX 资源路径 创建 TMX 格式 */ static TMXMapInfo * createWithXML(const char* tmxString, const char* resourcePath); /** 使用 tmx 文件,创建 TMX 格式 */ CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithTMXFile(const char *tmxFile) { return TMXMapInfo::create(tmxFile); }; /** 使用 XML string 、一个 TMX 资源路径 创建 TMX 格式 */ CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithXML(const char* tmxString, const char* resourcePath) { return TMXMapInfo::createWithXML(tmxString, resourcePath); }; /** * @js ctor */ TMXMapInfo(); /** * @js NA * @lua NA */ virtual ~TMXMapInfo(); /** 使用 tmx 文件,初始化 TMX 格式 */ bool initWithTMXFile(const char *tmxFile); /** 使用 XML string 、一个 TMX 资源路径初始化 TMX 格式 */ bool initWithXML(const char* tmxString, const char* resourcePath); /** 初始化解析一个 XML 文件,无论是 TMX(地图) 文件或TSX(地形设置)文件 */ bool parseXMLFile(const char *xmlFilename); /* 初始化解析一个 XML 文件,无论是 TMX(地图) 字符或TSX(地形设置)字符 */ bool parseXMLString(const char *xmlString); Dictionary* getTileProperties() { return _tileProperties; }; void setTileProperties(Dictionary* tileProperties) { CC_SAFE_RETAIN(tileProperties); CC_SAFE_RELEASE(_tileProperties); _tileProperties = tileProperties; }; /// 地图方向 inline int getOrientation() const { return _orientation; }; inline void setOrientation(int orientation) { _orientation = orientation; }; /// map width & height inline const Size& getMapSize() const { return _mapSize; }; inline void setMapSize(const Size& mapSize) { _mapSize = mapSize; }; /// tiles width & height inline const Size& getTileSize() const { return _tileSize; }; inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; }; /// Layers inline Array* getLayers() const { return _layers; }; inline void setLayers(Array* layers) { CC_SAFE_RETAIN(layers); CC_SAFE_RELEASE(_layers); _layers = layers; }; /// tilesets inline Array* getTilesets() const { return _tilesets; }; inline void setTilesets(Array* tilesets) { CC_SAFE_RETAIN(tilesets); CC_SAFE_RELEASE(_tilesets); _tilesets = tilesets; }; /// ObjectGroups inline Array* getObjectGroups() const { return _objectGroups; }; inline void setObjectGroups(Array* groups) { CC_SAFE_RETAIN(groups); CC_SAFE_RELEASE(_objectGroups); _objectGroups = groups; }; /// parent element inline int getParentElement() const { return _parentElement; }; inline void setParentElement(int element) { _parentElement = element; }; /// parent GID inline unsigned int getParentGID() const { return _parentGID; }; inline void setParentGID(unsigned int gid) { _parentGID = gid; }; /// layer attribs inline int getLayerAttribs() const { return _layerAttribs; }; inline void setLayerAttribs(int layerAttribs) { _layerAttribs = layerAttribs; }; /// 存储字符 inline bool isStoringCharacters() const { return _storingCharacters; }; CC_DEPRECATED_ATTRIBUTE inline bool getStoringCharacters() const { return isStoringCharacters(); }; inline void setStoringCharacters(bool storingCharacters) { _storingCharacters = storingCharacters; }; /// properties inline Dictionary* getProperties() const { return _properties; }; inline void setProperties(Dictionary* properties) { CC_SAFE_RETAIN(properties); CC_SAFE_RELEASE(_properties); _properties = properties; }; // implement pure virtual methods of SAXDelegator /** * @js NA * @lua NA */ void startElement(void *ctx, const char *name, const char **atts); /** * @js NA * @lua NA */ void endElement(void *ctx, const char *name); /** * @js NA * @lua NA */ void textHandler(void *ctx, const char *ch, int len); inline const char* getCurrentString(){ return _currentString.c_str(); } inline void setCurrentString(const char *currentString){ _currentString = currentString; } inline const char* getTMXFileName(){ return _TMXFileName.c_str(); } inline void setTMXFileName(const char *fileName){ _TMXFileName = fileName; } private: void internalInit(const char* tmxFileName, const char* resourcePath); protected: /// 地图方向 int _orientation; /// map width & height Size _mapSize; /// tiles width & height Size _tileSize; /// Layers Array* _layers; /// tilesets Array* _tilesets; /// ObjectGroups Array* _objectGroups; /// parent element int _parentElement; /// parent GID unsigned int _parentGID; /// layer attribs int _layerAttribs; /// 存储字符 bool _storingCharacters; /// properties Dictionary* _properties; //! tmx filename std::string _TMXFileName; // tmx resource path std::string _resources; //! current string std::string _currentString; //! tile properties Dictionary* _tileProperties; unsigned int _currentFirstGID; }; // end of tilemap_parallax_nodes group /// @} NS_CC_END #endif