2010-11-13 11:34:49 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:59:01 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-04 14:11:43 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2010-11-13 11:34:49 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2011-03-23 22:16:20 +08:00
|
|
|
|
|
|
|
|
2010-11-13 11:34:49 +08:00
|
|
|
#ifndef __CC_TM_XML_PARSER__
|
|
|
|
#define __CC_TM_XML_PARSER__
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCArray.h"
|
|
|
|
#include "CCGeometry.h"
|
2012-06-19 13:50:11 +08:00
|
|
|
#include "platform/CCSAXParser.h"
|
2013-11-29 11:36:42 +08:00
|
|
|
#include "CCVector.h"
|
2013-12-03 14:47:35 +08:00
|
|
|
#include "CCValue.h"
|
2012-03-20 17:27:06 +08:00
|
|
|
#include <string>
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
class TMXLayerInfo;
|
2013-06-20 14:13:12 +08:00
|
|
|
class TMXObjectGroup;
|
2013-11-29 11:36:42 +08:00
|
|
|
class TMXTilesetInfo;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
|
|
/** @file
|
|
|
|
* Internal TMX parser
|
|
|
|
*
|
|
|
|
* IMPORTANT: These classed should not be documented using doxygen strings
|
|
|
|
* since the user should not use them.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup tilemap_parallax_nodes
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
enum {
|
2012-04-19 14:35:52 +08:00
|
|
|
TMXLayerAttribNone = 1 << 0,
|
|
|
|
TMXLayerAttribBase64 = 1 << 1,
|
|
|
|
TMXLayerAttribGzip = 1 << 2,
|
|
|
|
TMXLayerAttribZlib = 1 << 3,
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
2012-04-19 14:35:52 +08:00
|
|
|
TMXPropertyNone,
|
|
|
|
TMXPropertyMap,
|
|
|
|
TMXPropertyLayer,
|
|
|
|
TMXPropertyObjectGroup,
|
|
|
|
TMXPropertyObject,
|
|
|
|
TMXPropertyTile
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
|
|
|
|
2014-02-08 10:11:17 +08:00
|
|
|
typedef enum TMXTileFlags_ {
|
|
|
|
kTMXTileHorizontalFlag = 0x80000000,
|
|
|
|
kTMXTileVerticalFlag = 0x40000000,
|
|
|
|
kTMXTileDiagonalFlag = 0x20000000,
|
2014-02-08 10:33:30 +08:00
|
|
|
kTMXFlipedAll = (kTMXTileHorizontalFlag|kTMXTileVerticalFlag|kTMXTileDiagonalFlag),
|
|
|
|
kTMXFlippedMask = ~(kTMXFlipedAll)
|
2014-02-08 10:11:17 +08:00
|
|
|
} TMXTileFlags;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
// Bits on the far end of the 32-bit global tile ID (GID's) are used for tile flags
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TMXLayerInfo contains the information about the layers like:
|
2012-03-20 15:04:53 +08:00
|
|
|
- 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.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TMXLayerInfo : public Object
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-20 04:16:38 +08:00
|
|
|
TMXLayerInfo();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-20 04:16:38 +08:00
|
|
|
virtual ~TMXLayerInfo();
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
void setProperties(ValueMap properties);
|
2014-01-05 18:20:29 +08:00
|
|
|
ValueMap& getProperties();
|
2013-07-20 04:16:38 +08:00
|
|
|
|
2014-02-08 10:11:17 +08:00
|
|
|
ValueMap _properties;
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _name;
|
2013-07-20 04:16:38 +08:00
|
|
|
Size _layerSize;
|
2014-02-08 10:11:17 +08:00
|
|
|
uint32_t *_tiles;
|
2013-06-15 14:03:30 +08:00
|
|
|
bool _visible;
|
|
|
|
unsigned char _opacity;
|
|
|
|
bool _ownTiles;
|
2013-07-20 04:16:38 +08:00
|
|
|
Point _offset;
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TMXTilesetInfo contains the information about the tilesets like:
|
2012-03-20 15:04:53 +08:00
|
|
|
- Tileset name
|
2012-09-17 15:02:24 +08:00
|
|
|
- Tileset spacing
|
2012-03-20 15:04:53 +08:00
|
|
|
- Tileset margin
|
|
|
|
- size of the tiles
|
|
|
|
- Image used for the tiles
|
|
|
|
- Image size
|
|
|
|
|
|
|
|
This information is obtained from the TMX file.
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TMXTilesetInfo : public Object
|
2012-03-20 15:04:53 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _name;
|
2013-12-20 09:50:53 +08:00
|
|
|
int _firstGid;
|
|
|
|
Size _tileSize;
|
|
|
|
int _spacing;
|
|
|
|
int _margin;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! filename containing the tiles (should be spritesheet / texture atlas)
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _sourceImage;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! size in pixels of the image
|
2013-12-20 09:50:53 +08:00
|
|
|
Size _imageSize;
|
2012-03-20 15:04:53 +08:00
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTilesetInfo();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~TMXTilesetInfo();
|
2014-02-08 10:33:30 +08:00
|
|
|
Rect getRectForGID(uint32_t gid);
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
/** @brief TMXMapInfo contains the information about the map like:
|
2012-03-20 15:04:53 +08:00
|
|
|
- Map orientation (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)
|
|
|
|
|
|
|
|
This information is obtained from the TMX file.
|
|
|
|
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
class CC_DLL TMXMapInfo : public Object, public SAXDelegator
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** creates a TMX Format with a tmx file */
|
2013-11-07 09:05:13 +08:00
|
|
|
static TMXMapInfo * create(const std::string& tmxFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** creates a TMX Format with an XML string and a TMX resource path */
|
2013-11-07 09:05:13 +08:00
|
|
|
static TMXMapInfo * createWithXML(const std::string& tmxString, const std::string& resourcePath);
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/** creates a TMX Format with a tmx file */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithTMXFile(const char *tmxFile) { return TMXMapInfo::create(tmxFile); };
|
|
|
|
/** creates a TMX Format with an XML string and a TMX resource path */
|
|
|
|
CC_DEPRECATED_ATTRIBUTE static TMXMapInfo * formatWithXML(const char* tmxString, const char* resourcePath) { return TMXMapInfo::createWithXML(tmxString, resourcePath); };
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
TMXMapInfo();
|
2013-09-13 13:52:42 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
virtual ~TMXMapInfo();
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
/** initializes a TMX format with a tmx file */
|
2013-11-07 09:05:13 +08:00
|
|
|
bool initWithTMXFile(const std::string& tmxFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
/** initializes a TMX format with an XML string and a TMX resource path */
|
2013-11-07 09:05:13 +08:00
|
|
|
bool initWithXML(const std::string& tmxString, const std::string& resourcePath);
|
2012-09-17 15:02:24 +08:00
|
|
|
/** initializes parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file */
|
2013-11-07 09:05:13 +08:00
|
|
|
bool parseXMLFile(const std::string& xmlFilename);
|
2012-09-17 15:02:24 +08:00
|
|
|
/* initializes parsing of an XML string, either a tmx (Map) string or tsx (Tileset) string */
|
2013-11-07 09:05:13 +08:00
|
|
|
bool parseXMLString(const std::string& xmlString);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-26 22:07:20 +08:00
|
|
|
ValueMapIntKey& getTileProperties() { return _tileProperties; };
|
|
|
|
void setTileProperties(const ValueMapIntKey& tileProperties) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_tileProperties = tileProperties;
|
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
/// map orientation
|
|
|
|
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
|
2013-11-29 11:36:42 +08:00
|
|
|
inline const Vector<TMXLayerInfo*>& getLayers() const { return _layers; };
|
|
|
|
inline Vector<TMXLayerInfo*>& getLayers() { return _layers; };
|
|
|
|
inline void setLayers(const Vector<TMXLayerInfo*>& layers) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_layers = layers;
|
|
|
|
};
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/// tilesets
|
2013-11-29 11:36:42 +08:00
|
|
|
inline const Vector<TMXTilesetInfo*>& getTilesets() const { return _tilesets; };
|
|
|
|
inline Vector<TMXTilesetInfo*>& getTilesets() { return _tilesets; };
|
|
|
|
inline void setTilesets(const Vector<TMXTilesetInfo*>& tilesets) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_tilesets = tilesets;
|
|
|
|
};
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/// ObjectGroups
|
2013-11-29 11:36:42 +08:00
|
|
|
inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
|
|
|
|
inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
|
|
|
|
inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_objectGroups = groups;
|
|
|
|
};
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/// parent element
|
|
|
|
inline int getParentElement() const { return _parentElement; };
|
|
|
|
inline void setParentElement(int element) { _parentElement = element; };
|
|
|
|
|
|
|
|
/// parent GID
|
2013-12-20 09:50:53 +08:00
|
|
|
inline int getParentGID() const { return _parentGID; };
|
|
|
|
inline void setParentGID(int gid) { _parentGID = gid; };
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/// layer attribs
|
|
|
|
inline int getLayerAttribs() const { return _layerAttribs; };
|
|
|
|
inline void setLayerAttribs(int layerAttribs) { _layerAttribs = layerAttribs; };
|
|
|
|
|
|
|
|
/// is storing characters?
|
|
|
|
inline bool isStoringCharacters() const { return _storingCharacters; };
|
2013-07-23 21:56:01 +08:00
|
|
|
CC_DEPRECATED_ATTRIBUTE inline bool getStoringCharacters() const { return isStoringCharacters(); };
|
2013-07-23 18:26:26 +08:00
|
|
|
inline void setStoringCharacters(bool storingCharacters) { _storingCharacters = storingCharacters; };
|
|
|
|
|
|
|
|
/// properties
|
2014-01-24 15:37:32 +08:00
|
|
|
inline const ValueMap& getProperties() const { return _properties; }
|
|
|
|
inline ValueMap& getProperties() { return _properties; }
|
2014-01-24 16:05:38 +08:00
|
|
|
inline void setProperties(const ValueMap& properties) {
|
2013-07-23 20:36:41 +08:00
|
|
|
_properties = properties;
|
|
|
|
};
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implement pure virtual methods of SAXDelegator
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void startElement(void *ctx, const char *name, const char **atts);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void endElement(void *ctx, const char *name);
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2012-04-19 14:35:52 +08:00
|
|
|
void textHandler(void *ctx, const char *ch, int len);
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
inline const std::string& getCurrentString() const { return _currentString; }
|
|
|
|
inline void setCurrentString(const std::string& currentString){ _currentString = currentString; }
|
|
|
|
inline const std::string& getTMXFileName() const { return _TMXFileName; }
|
|
|
|
inline void setTMXFileName(const std::string& fileName){ _TMXFileName = fileName; }
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
protected:
|
2013-11-07 09:05:13 +08:00
|
|
|
void internalInit(const std::string& tmxFileName, const std::string& resourcePath);
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
|
|
/// map orientation
|
|
|
|
int _orientation;
|
|
|
|
/// map width & height
|
|
|
|
Size _mapSize;
|
|
|
|
/// tiles width & height
|
|
|
|
Size _tileSize;
|
|
|
|
/// Layers
|
2013-11-29 11:36:42 +08:00
|
|
|
Vector<TMXLayerInfo*> _layers;
|
2013-07-23 18:26:26 +08:00
|
|
|
/// tilesets
|
2013-11-29 11:36:42 +08:00
|
|
|
Vector<TMXTilesetInfo*> _tilesets;
|
2013-07-23 18:26:26 +08:00
|
|
|
/// ObjectGroups
|
2013-11-29 11:36:42 +08:00
|
|
|
Vector<TMXObjectGroup*> _objectGroups;
|
2013-07-23 18:26:26 +08:00
|
|
|
/// parent element
|
|
|
|
int _parentElement;
|
|
|
|
/// parent GID
|
2013-12-20 09:50:53 +08:00
|
|
|
int _parentGID;
|
2013-07-23 18:26:26 +08:00
|
|
|
/// layer attribs
|
|
|
|
int _layerAttribs;
|
|
|
|
/// is storing characters?
|
|
|
|
bool _storingCharacters;
|
|
|
|
/// properties
|
2013-12-04 17:46:57 +08:00
|
|
|
ValueMap _properties;
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//! tmx filename
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _TMXFileName;
|
2012-04-19 14:35:52 +08:00
|
|
|
// tmx resource path
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _resources;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! current string
|
2013-06-15 14:03:30 +08:00
|
|
|
std::string _currentString;
|
2012-04-19 14:35:52 +08:00
|
|
|
//! tile properties
|
2013-12-26 22:07:20 +08:00
|
|
|
ValueMapIntKey _tileProperties;
|
2013-12-20 09:50:53 +08:00
|
|
|
int _currentFirstGID;
|
2013-12-23 10:37:46 +08:00
|
|
|
bool _recordFirstGID;
|
2012-03-20 15:04:53 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of tilemap_parallax_nodes group
|
|
|
|
/// @}
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
NS_CC_END
|
2010-11-13 11:34:49 +08:00
|
|
|
|
2011-03-23 22:16:20 +08:00
|
|
|
#endif
|
2010-11-13 11:34:49 +08:00
|
|
|
|