2010-12-28 10:23:14 +08:00
|
|
|
|
/****************************************************************************
|
2012-09-24 21:22:20 +08:00
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2012-04-19 14:35:52 +08:00
|
|
|
|
Copyright (c) 2011 Максим Аксенов
|
2011-03-19 14:45:51 +08:00
|
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2011-07-04 14:11:43 +08:00
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2010-12-28 10:23:14 +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-01-15 18:05:35 +08:00
|
|
|
|
|
2010-12-28 10:23:14 +08:00
|
|
|
|
#include <map>
|
2013-01-30 06:45:41 +08:00
|
|
|
|
#include <sstream>
|
2010-12-28 10:23:14 +08:00
|
|
|
|
#include "CCTMXXMLParser.h"
|
|
|
|
|
#include "CCTMXTiledMap.h"
|
|
|
|
|
#include "ccMacros.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
|
#include "platform/CCFileUtils.h"
|
2010-12-31 09:36:53 +08:00
|
|
|
|
#include "support/zip_support/ZipUtils.h"
|
2010-12-28 10:23:14 +08:00
|
|
|
|
#include "support/base64.h"
|
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
|
using namespace std;
|
2011-03-23 22:16:20 +08:00
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
|
NS_CC_BEGIN
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
|
static const char* valueForKey(const char *key, std::map<std::string, std::string>* dict)
|
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if (dict)
|
|
|
|
|
{
|
|
|
|
|
std::map<std::string, std::string>::iterator it = dict->find(key);
|
|
|
|
|
return it!=dict->end() ? it->second.c_str() : "";
|
|
|
|
|
}
|
|
|
|
|
return "";
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
|
// implementation TMXLayerInfo
|
|
|
|
|
TMXLayerInfo::TMXLayerInfo()
|
2013-06-15 14:03:30 +08:00
|
|
|
|
: _name("")
|
|
|
|
|
, _tiles(NULL)
|
|
|
|
|
, _ownTiles(true)
|
|
|
|
|
, _minGID(100000)
|
|
|
|
|
, _maxGID(0)
|
2013-07-12 14:47:36 +08:00
|
|
|
|
, _offset(Point::ZERO)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_properties= new Dictionary();;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXLayerInfo::~TMXLayerInfo()
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
|
CCLOGINFO("cocos2d: deallocing: %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
|
if( _ownTiles && _tiles )
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
delete [] _tiles;
|
|
|
|
|
_tiles = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Dictionary * TMXLayerInfo::getProperties()
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return _properties;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void TMXLayerInfo::setProperties(Dictionary* var)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
CC_SAFE_RETAIN(var);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
|
_properties = var;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
// implementation TMXTilesetInfo
|
|
|
|
|
TMXTilesetInfo::TMXTilesetInfo()
|
2013-06-15 14:03:30 +08:00
|
|
|
|
:_firstGid(0)
|
2013-07-12 14:47:36 +08:00
|
|
|
|
,_tileSize(Size::ZERO)
|
2013-06-15 14:03:30 +08:00
|
|
|
|
,_spacing(0)
|
|
|
|
|
,_margin(0)
|
2013-07-12 14:47:36 +08:00
|
|
|
|
,_imageSize(Size::ZERO)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXTilesetInfo::~TMXTilesetInfo()
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
|
CCLOGINFO("cocos2d: deallocing: %p", this);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-07-23 18:26:26 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Rect TMXTilesetInfo::rectForGID(unsigned int gid)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Rect rect;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
rect.size = _tileSize;
|
2013-06-20 14:13:12 +08:00
|
|
|
|
gid &= kFlippedMask;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
gid = gid - _firstGid;
|
|
|
|
|
int max_x = (int)((_imageSize.width - _margin*2 + _spacing) / (_tileSize.width + _spacing));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// int max_y = (imageSize.height - margin*2 + spacing) / (tileSize.height + spacing);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
rect.origin.x = (gid % max_x) * (_tileSize.width + _spacing) + _margin;
|
|
|
|
|
rect.origin.y = (gid / max_x) * (_tileSize.height + _spacing) + _margin;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
return rect;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
// implementation TMXMapInfo
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
|
TMXMapInfo * TMXMapInfo::create(const char *tmxFile)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo *pRet = new TMXMapInfo();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if(pRet->initWithTMXFile(tmxFile))
|
|
|
|
|
{
|
|
|
|
|
pRet->autorelease();
|
|
|
|
|
return pRet;
|
|
|
|
|
}
|
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
|
return NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
|
TMXMapInfo * TMXMapInfo::createWithXML(const char* tmxString, const char* resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo *pRet = new TMXMapInfo();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if(pRet->initWithXML(tmxString, resourcePath))
|
|
|
|
|
{
|
|
|
|
|
pRet->autorelease();
|
|
|
|
|
return pRet;
|
|
|
|
|
}
|
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
|
return NULL;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void TMXMapInfo::internalInit(const char* tmxFileName, const char* resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_tilesets = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_tilesets->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_layers = Array::create();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_layers->retain();
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2012-03-26 16:46:23 +08:00
|
|
|
|
if (tmxFileName != NULL)
|
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
|
_TMXFileName = FileUtils::getInstance()->fullPathForFilename(tmxFileName);
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2012-03-26 16:46:23 +08:00
|
|
|
|
if (resourcePath != NULL)
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_resources = resourcePath;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_objectGroups = Array::createWithCapacity(4);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_objectGroups->retain();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_properties = new Dictionary();
|
|
|
|
|
_tileProperties = new Dictionary();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// tmp vars
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_currentString = "";
|
|
|
|
|
_storingCharacters = false;
|
|
|
|
|
_layerAttribs = TMXLayerAttribNone;
|
|
|
|
|
_parentElement = TMXPropertyNone;
|
|
|
|
|
_currentFirstGID = 0;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
|
bool TMXMapInfo::initWithXML(const char* tmxString, const char* resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
internalInit(NULL, resourcePath);
|
|
|
|
|
return parseXMLString(tmxString);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
bool TMXMapInfo::initWithTMXFile(const char *tmxFile)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
internalInit(tmxFile, NULL);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
return parseXMLFile(_TMXFileName.c_str());
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo::TMXMapInfo()
|
2013-07-12 14:47:36 +08:00
|
|
|
|
: _mapSize(Size::ZERO)
|
|
|
|
|
, _tileSize(Size::ZERO)
|
2013-06-15 14:03:30 +08:00
|
|
|
|
, _layers(NULL)
|
|
|
|
|
, _tilesets(NULL)
|
|
|
|
|
, _objectGroups(NULL)
|
|
|
|
|
, _layerAttribs(0)
|
|
|
|
|
, _storingCharacters(false)
|
|
|
|
|
, _properties(NULL)
|
|
|
|
|
, _tileProperties(NULL)
|
|
|
|
|
, _currentFirstGID(0)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo::~TMXMapInfo()
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-06 09:21:05 +08:00
|
|
|
|
CCLOGINFO("cocos2d: deallocing: %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_tilesets);
|
|
|
|
|
CC_SAFE_RELEASE(_layers);
|
|
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
|
CC_SAFE_RELEASE(_tileProperties);
|
|
|
|
|
CC_SAFE_RELEASE(_objectGroups);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLString(const char *xmlString)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2012-03-26 16:46:23 +08:00
|
|
|
|
int len = strlen(xmlString);
|
|
|
|
|
if (xmlString == NULL || len <= 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
SAXParser parser;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
|
|
|
|
|
if (false == parser.init("UTF-8") )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
|
|
|
|
return parser.parse(xmlString, len);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLFile(const char *xmlFilename)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
SAXParser parser;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
if (false == parser.init("UTF-8") )
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
|
return parser.parse(FileUtils::getInstance()->fullPathForFilename(xmlFilename).c_str());
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
|
// the XML parser calls here with all the elements
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2012-03-20 15:04:53 +08:00
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo *pTMXMapInfo = this;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string elementName = (char*)name;
|
|
|
|
|
std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (atts && atts[0])
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
for(int i = 0; atts[i]; i += 2)
|
|
|
|
|
{
|
|
|
|
|
std::string key = (char*)atts[i];
|
|
|
|
|
std::string value = (char*)atts[i+1];
|
|
|
|
|
attributeDict->insert(pair<std::string, std::string>(key, value));
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (elementName == "map")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
std::string version = valueForKey("version", attributeDict);
|
|
|
|
|
if ( version != "1.0")
|
|
|
|
|
{
|
2013-05-07 14:36:01 +08:00
|
|
|
|
CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %s", version.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
std::string orientationStr = valueForKey("orientation", attributeDict);
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (orientationStr == "orthogonal")
|
2013-06-20 14:13:12 +08:00
|
|
|
|
pTMXMapInfo->setOrientation(TMXOrientationOrtho);
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (orientationStr == "isometric")
|
2013-06-20 14:13:12 +08:00
|
|
|
|
pTMXMapInfo->setOrientation(TMXOrientationIso);
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if(orientationStr == "hexagonal")
|
2013-06-20 14:13:12 +08:00
|
|
|
|
pTMXMapInfo->setOrientation(TMXOrientationHex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
else
|
|
|
|
|
CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation());
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
s.width = (float)atof(valueForKey("width", attributeDict));
|
|
|
|
|
s.height = (float)atof(valueForKey("height", attributeDict));
|
|
|
|
|
pTMXMapInfo->setMapSize(s);
|
|
|
|
|
|
|
|
|
|
s.width = (float)atof(valueForKey("tilewidth", attributeDict));
|
|
|
|
|
s.height = (float)atof(valueForKey("tileheight", attributeDict));
|
|
|
|
|
pTMXMapInfo->setTileSize(s);
|
|
|
|
|
|
|
|
|
|
// The parent element is now "map"
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyMap);
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "tileset")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// If this is an external tileset then start parsing that
|
|
|
|
|
std::string externalTilesetFilename = valueForKey("source", attributeDict);
|
|
|
|
|
if (externalTilesetFilename != "")
|
|
|
|
|
{
|
2013-02-26 17:42:18 +08:00
|
|
|
|
// Tileset file will be relative to the map file. So we need to convert it to an absolute path
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_TMXFileName.find_last_of("/") != string::npos)
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of("/") + 1);
|
2012-08-23 14:20:32 +08:00
|
|
|
|
externalTilesetFilename = dir + externalTilesetFilename;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2012-08-23 14:20:32 +08:00
|
|
|
|
else
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
externalTilesetFilename = _resources + "/" + externalTilesetFilename;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2013-07-12 06:24:23 +08:00
|
|
|
|
externalTilesetFilename = FileUtils::getInstance()->fullPathForFilename(externalTilesetFilename.c_str());
|
2012-08-23 14:20:32 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_currentFirstGID = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
|
2013-02-26 17:42:18 +08:00
|
|
|
|
|
2012-03-26 16:46:23 +08:00
|
|
|
|
pTMXMapInfo->parseXMLFile(externalTilesetFilename.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXTilesetInfo *tileset = new TMXTilesetInfo();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_name = valueForKey("name", attributeDict);
|
|
|
|
|
if (_currentFirstGID == 0)
|
2013-02-26 17:42:18 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_firstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
|
2013-02-26 17:42:18 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_firstGid = _currentFirstGID;
|
|
|
|
|
_currentFirstGID = 0;
|
2013-02-26 17:42:18 +08:00
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_spacing = (unsigned int)atoi(valueForKey("spacing", attributeDict));
|
|
|
|
|
tileset->_margin = (unsigned int)atoi(valueForKey("margin", attributeDict));
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
s.width = (float)atof(valueForKey("tilewidth", attributeDict));
|
|
|
|
|
s.height = (float)atof(valueForKey("tileheight", attributeDict));
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_tileSize = s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
pTMXMapInfo->getTilesets()->addObject(tileset);
|
|
|
|
|
tileset->release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "tile")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXTilesetInfo* info = (TMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
|
|
|
|
|
Dictionary *dict = new Dictionary();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
pTMXMapInfo->setParentGID(info->_firstGid + atoi(valueForKey("id", attributeDict)));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
pTMXMapInfo->getTileProperties()->setObject(dict, pTMXMapInfo->getParentGID());
|
|
|
|
|
CC_SAFE_RELEASE(dict);
|
|
|
|
|
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyTile);
|
|
|
|
|
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "layer")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXLayerInfo *layer = new TMXLayerInfo();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_name = valueForKey("name", attributeDict);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
s.width = (float)atof(valueForKey("width", attributeDict));
|
|
|
|
|
s.height = (float)atof(valueForKey("height", attributeDict));
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_layerSize = s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
std::string visible = valueForKey("visible", attributeDict);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_visible = !(visible == "0");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
std::string opacity = valueForKey("opacity", attributeDict);
|
|
|
|
|
if( opacity != "" )
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_opacity = (unsigned char)(255 * atof(opacity.c_str()));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_opacity = 255;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float x = (float)atof(valueForKey("x", attributeDict));
|
|
|
|
|
float y = (float)atof(valueForKey("y", attributeDict));
|
2013-07-12 14:11:55 +08:00
|
|
|
|
layer->_offset = Point(x,y);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
pTMXMapInfo->getLayers()->addObject(layer);
|
|
|
|
|
layer->release();
|
|
|
|
|
|
|
|
|
|
// The parent element is now "layer"
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyLayer);
|
|
|
|
|
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "objectgroup")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXObjectGroup *objectGroup = new TMXObjectGroup();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
objectGroup->setGroupName(valueForKey("name", attributeDict));
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Point positionOffset;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
positionOffset.x = (float)atof(valueForKey("x", attributeDict)) * pTMXMapInfo->getTileSize().width;
|
|
|
|
|
positionOffset.y = (float)atof(valueForKey("y", attributeDict)) * pTMXMapInfo->getTileSize().height;
|
|
|
|
|
objectGroup->setPositionOffset(positionOffset);
|
|
|
|
|
|
|
|
|
|
pTMXMapInfo->getObjectGroups()->addObject(objectGroup);
|
|
|
|
|
objectGroup->release();
|
|
|
|
|
|
|
|
|
|
// The parent element is now "objectgroup"
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyObjectGroup);
|
|
|
|
|
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "image")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXTilesetInfo* tileset = (TMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// build full path
|
|
|
|
|
std::string imagename = valueForKey("source", attributeDict);
|
2012-03-26 16:46:23 +08:00
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_TMXFileName.find_last_of("/") != string::npos)
|
2012-08-23 14:20:32 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of("/") + 1);
|
|
|
|
|
tileset->_sourceImage = dir + imagename;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2012-08-23 14:20:32 +08:00
|
|
|
|
else
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_sourceImage = _resources + (_resources.size() ? "/" : "") + imagename;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "data")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
std::string encoding = valueForKey("encoding", attributeDict);
|
|
|
|
|
std::string compression = valueForKey("compression", attributeDict);
|
|
|
|
|
|
|
|
|
|
if( encoding == "base64" )
|
|
|
|
|
{
|
|
|
|
|
int layerAttribs = pTMXMapInfo->getLayerAttribs();
|
|
|
|
|
pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribBase64);
|
|
|
|
|
pTMXMapInfo->setStoringCharacters(true);
|
|
|
|
|
|
|
|
|
|
if( compression == "gzip" )
|
|
|
|
|
{
|
|
|
|
|
layerAttribs = pTMXMapInfo->getLayerAttribs();
|
|
|
|
|
pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribGzip);
|
|
|
|
|
} else
|
|
|
|
|
if (compression == "zlib")
|
|
|
|
|
{
|
|
|
|
|
layerAttribs = pTMXMapInfo->getLayerAttribs();
|
|
|
|
|
pTMXMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib);
|
|
|
|
|
}
|
2013-07-20 13:01:27 +08:00
|
|
|
|
CCASSERT( compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method" );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-07-20 13:01:27 +08:00
|
|
|
|
CCASSERT( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported" );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "object")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
char buffer[32] = {0};
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// The value for "type" was blank or not a valid class name
|
|
|
|
|
// Create an instance of TMXObjectInfo to store the object and its properties
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Dictionary *dict = new Dictionary();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// Parse everything automatically
|
|
|
|
|
const char* pArray[] = {"name", "type", "width", "height", "gid"};
|
|
|
|
|
|
2013-03-03 10:32:09 +08:00
|
|
|
|
for(size_t i = 0; i < sizeof(pArray)/sizeof(pArray[0]); ++i )
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
const char* key = pArray[i];
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String* obj = new String(valueForKey(key, attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if( obj )
|
|
|
|
|
{
|
|
|
|
|
obj->autorelease();
|
|
|
|
|
dict->setObject(obj, key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// But X and Y since they need special treatment
|
|
|
|
|
// X
|
|
|
|
|
|
|
|
|
|
const char* value = valueForKey("x", attributeDict);
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (value)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
int x = atoi(value) + (int)objectGroup->getPositionOffset().x;
|
|
|
|
|
sprintf(buffer, "%d", x);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String* pStr = new String(buffer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
pStr->autorelease();
|
|
|
|
|
dict->setObject(pStr, "x");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Y
|
|
|
|
|
value = valueForKey("y", attributeDict);
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (value) {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
int y = atoi(value) + (int)objectGroup->getPositionOffset().y;
|
|
|
|
|
|
|
|
|
|
// Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
|
2013-06-15 14:03:30 +08:00
|
|
|
|
y = (int)(_mapSize.height * _tileSize.height) - y - atoi(valueForKey("height", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
sprintf(buffer, "%d", y);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String* pStr = new String(buffer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
pStr->autorelease();
|
|
|
|
|
dict->setObject(pStr, "y");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the object to the objectGroup
|
|
|
|
|
objectGroup->getObjects()->addObject(dict);
|
|
|
|
|
dict->release();
|
|
|
|
|
|
|
|
|
|
// The parent element is now "object"
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyObject);
|
|
|
|
|
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "property")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
if ( pTMXMapInfo->getParentElement() == TMXPropertyNone )
|
|
|
|
|
{
|
|
|
|
|
CCLOG( "TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'",
|
|
|
|
|
valueForKey("name", attributeDict), valueForKey("value",attributeDict) );
|
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap )
|
|
|
|
|
{
|
|
|
|
|
// The parent element is the map
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String *value = new String(valueForKey("value", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string key = valueForKey("name", attributeDict);
|
|
|
|
|
pTMXMapInfo->getProperties()->setObject(value, key.c_str());
|
|
|
|
|
value->release();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyLayer )
|
|
|
|
|
{
|
|
|
|
|
// The parent element is the last layer
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->lastObject();
|
|
|
|
|
String *value = new String(valueForKey("value", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string key = valueForKey("name", attributeDict);
|
|
|
|
|
// Add the property to the layer
|
|
|
|
|
layer->getProperties()->setObject(value, key.c_str());
|
|
|
|
|
value->release();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyObjectGroup )
|
|
|
|
|
{
|
|
|
|
|
// The parent element is the last object group
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
|
|
|
|
|
String *value = new String(valueForKey("value", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
const char* key = valueForKey("name", attributeDict);
|
|
|
|
|
objectGroup->getProperties()->setObject(value, key);
|
|
|
|
|
value->release();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyObject )
|
|
|
|
|
{
|
|
|
|
|
// The parent element is the last object
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
|
|
|
|
|
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->lastObject();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
const char* propertyName = valueForKey("name", attributeDict);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String *propertyValue = new String(valueForKey("value", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
dict->setObject(propertyValue, propertyName);
|
|
|
|
|
propertyValue->release();
|
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyTile )
|
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Dictionary* dict = (Dictionary*)pTMXMapInfo->getTileProperties()->objectForKey(pTMXMapInfo->getParentGID());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
const char* propertyName = valueForKey("name", attributeDict);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String *propertyValue = new String(valueForKey("value", attributeDict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
dict->setObject(propertyValue, propertyName);
|
|
|
|
|
propertyValue->release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "polygon")
|
|
|
|
|
{
|
|
|
|
|
// find parent object's dict and add polygon-points to it
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = (TMXObjectGroup*)_objectGroups->lastObject();
|
|
|
|
|
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->lastObject();
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// get points value string
|
|
|
|
|
const char* value = valueForKey("points", attributeDict);
|
|
|
|
|
if(value)
|
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Array* pPointsArray = new Array;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-01-30 06:45:41 +08:00
|
|
|
|
// parse points string into a space-separated set of points
|
|
|
|
|
stringstream pointsStream(value);
|
|
|
|
|
string pointPair;
|
|
|
|
|
while(std::getline(pointsStream, pointPair, ' '))
|
|
|
|
|
{
|
|
|
|
|
// parse each point combo into a comma-separated x,y point
|
|
|
|
|
stringstream pointStream(pointPair);
|
|
|
|
|
string xStr,yStr;
|
|
|
|
|
char buffer[32] = {0};
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Dictionary* pPointDict = new Dictionary;
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// set x
|
|
|
|
|
if(std::getline(pointStream, xStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
|
|
|
|
|
sprintf(buffer, "%d", x);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String* pStr = new String(buffer);
|
2013-01-30 06:45:41 +08:00
|
|
|
|
pStr->autorelease();
|
|
|
|
|
pPointDict->setObject(pStr, "x");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set y
|
|
|
|
|
if(std::getline(pointStream, yStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
|
|
|
|
|
sprintf(buffer, "%d", y);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
String* pStr = new String(buffer);
|
2013-01-30 06:45:41 +08:00
|
|
|
|
pStr->autorelease();
|
|
|
|
|
pPointDict->setObject(pStr, "y");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to points array
|
|
|
|
|
pPointsArray->addObject(pPointDict);
|
|
|
|
|
pPointDict->release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dict->setObject(pPointsArray, "points");
|
|
|
|
|
pPointsArray->release();
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (elementName == "polyline")
|
|
|
|
|
{
|
|
|
|
|
// find parent object's dict and add polyline-points to it
|
2013-06-20 14:13:12 +08:00
|
|
|
|
// TMXObjectGroup* objectGroup = (TMXObjectGroup*)_objectGroups->lastObject();
|
|
|
|
|
// Dictionary* dict = (Dictionary*)objectGroup->getObjects()->lastObject();
|
2012-05-05 10:44:42 +08:00
|
|
|
|
// TODO: dict->setObject:[attributeDict objectForKey:@"points"] forKey:@"polylinePoints"];
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if (attributeDict)
|
|
|
|
|
{
|
|
|
|
|
attributeDict->clear();
|
|
|
|
|
delete attributeDict;
|
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void TMXMapInfo::endElement(void *ctx, const char *name)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo *pTMXMapInfo = this;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string elementName = (char*)name;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
int len = 0;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if(elementName == "data" && pTMXMapInfo->getLayerAttribs()&TMXLayerAttribBase64)
|
|
|
|
|
{
|
|
|
|
|
pTMXMapInfo->setStoringCharacters(false);
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->lastObject();
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string currentString = pTMXMapInfo->getCurrentString();
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
len = base64Decode((unsigned char*)currentString.c_str(), (unsigned int)currentString.length(), &buffer);
|
|
|
|
|
if( ! buffer )
|
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: TiledMap: decode data error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if( pTMXMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib) )
|
|
|
|
|
{
|
|
|
|
|
unsigned char *deflated;
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s = layer->_layerSize;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// int sizeHint = s.width * s.height * sizeof(uint32_t);
|
|
|
|
|
int sizeHint = (int)(s.width * s.height * sizeof(unsigned int));
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
int inflatedLen = ZipUtils::ccInflateMemoryWithHint(buffer, len, &deflated, sizeHint);
|
2013-07-20 13:01:27 +08:00
|
|
|
|
CCASSERT(inflatedLen == sizeHint, "");
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
|
inflatedLen = (size_t)&inflatedLen; // XXX: to avoid warnings in compiler
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
delete [] buffer;
|
|
|
|
|
buffer = NULL;
|
|
|
|
|
|
|
|
|
|
if( ! deflated )
|
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: TiledMap: inflate data error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_tiles = (unsigned int*) deflated;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_tiles = (unsigned int*) buffer;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pTMXMapInfo->setCurrentString("");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "map")
|
|
|
|
|
{
|
|
|
|
|
// The map element has ended
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyNone);
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "layer")
|
|
|
|
|
{
|
|
|
|
|
// The layer element has ended
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyNone);
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "objectgroup")
|
|
|
|
|
{
|
|
|
|
|
// The objectgroup element has ended
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyNone);
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "object")
|
|
|
|
|
{
|
|
|
|
|
// The object element has ended
|
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyNone);
|
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
void TMXMapInfo::textHandler(void *ctx, const char *ch, int len)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
|
|
|
|
CC_UNUSED_PARAM(ctx);
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo *pTMXMapInfo = this;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
std::string pText((char*)ch,0,len);
|
|
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
|
if (pTMXMapInfo->isStoringCharacters())
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
std::string currentString = pTMXMapInfo->getCurrentString();
|
|
|
|
|
currentString += pText;
|
|
|
|
|
pTMXMapInfo->setCurrentString(currentString.c_str());
|
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|