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
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
|
#include <unordered_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"
|
2013-10-14 14:01:00 +08:00
|
|
|
|
#include "ZipUtils.h"
|
|
|
|
|
#include "base64.h"
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
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
|
|
|
|
|
2013-11-22 05:43:59 +08:00
|
|
|
|
static const char* valueForKey(const char *key, std::unordered_map<std::string, std::string>* dict)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if (dict)
|
|
|
|
|
{
|
2013-11-22 05:43:59 +08:00
|
|
|
|
std::unordered_map<std::string, std::string>::iterator it = dict->find(key);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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("")
|
2013-11-29 11:36:42 +08:00
|
|
|
|
, _tiles(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
|
, _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-08-22 11:12:09 +08:00
|
|
|
|
_properties = new Dictionary();
|
|
|
|
|
_properties->init();
|
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-08-22 11:12:09 +08:00
|
|
|
|
CCLOGINFO("deallocing TMXLayerInfo: %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-11-12 10:09:47 +08:00
|
|
|
|
free(_tiles);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
_tiles = nullptr;
|
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-08-22 11:05:06 +08:00
|
|
|
|
CCLOGINFO("deallocing TMXTilesetInfo: %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-11-07 09:05:13 +08:00
|
|
|
|
TMXMapInfo * TMXMapInfo::create(const std::string& 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);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
return nullptr;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
TMXMapInfo * TMXMapInfo::createWithXML(const std::string& tmxString, const std::string& 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);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
return nullptr;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
void TMXMapInfo::internalInit(const std::string& tmxFileName, const std::string& resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
|
if (tmxFileName.size() > 0)
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
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
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
if (resourcePath.size() > 0)
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
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-11-29 11:36:42 +08:00
|
|
|
|
_objectGroups.setCapacity(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_properties = new Dictionary();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
_properties->init();
|
2013-06-20 14:13:12 +08:00
|
|
|
|
_tileProperties = new Dictionary();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
_tileProperties->init();
|
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-11-07 09:05:13 +08:00
|
|
|
|
bool TMXMapInfo::initWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
|
internalInit("", resourcePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
return parseXMLString(tmxString);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
bool TMXMapInfo::initWithTMXFile(const std::string& tmxFile)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
|
internalInit(tmxFile, "");
|
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
|
|
|
|
, _layerAttribs(0)
|
|
|
|
|
, _storingCharacters(false)
|
2013-11-29 11:36:42 +08:00
|
|
|
|
, _properties(nullptr)
|
|
|
|
|
, _tileProperties(nullptr)
|
2013-06-15 14:03:30 +08:00
|
|
|
|
, _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-08-22 11:12:09 +08:00
|
|
|
|
CCLOGINFO("deallocing TMXMapInfo: %p", this);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
|
CC_SAFE_RELEASE(_tileProperties);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLString(const std::string& xmlString)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-11-29 11:36:42 +08:00
|
|
|
|
size_t len = xmlString.size();
|
2013-11-07 09:05:13 +08:00
|
|
|
|
if (len <= 0)
|
2012-03-26 16:46:23 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
return parser.parse(xmlString.c_str(), len);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLFile(const std::string& 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;
|
2013-11-22 05:43:59 +08:00
|
|
|
|
std::unordered_map<std::string, std::string> *attributeDict = new std::unordered_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
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
|
pTMXMapInfo->getTilesets().addObject(tileset);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
tileset->release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "tile")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-06-06 02:29:52 +08:00
|
|
|
|
if (pTMXMapInfo->getParentElement() == TMXPropertyLayer)
|
|
|
|
|
{
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
|
2013-07-04 19:28:54 +08:00
|
|
|
|
Size layerSize = layer->_layerSize;
|
2013-06-06 02:29:52 +08:00
|
|
|
|
unsigned int gid = (unsigned int)atoi(valueForKey("gid", attributeDict));
|
2013-07-04 19:28:54 +08:00
|
|
|
|
int tilesAmount = layerSize.width*layerSize.height;
|
2013-08-13 18:32:50 +08:00
|
|
|
|
|
|
|
|
|
do
|
2013-07-04 19:28:54 +08:00
|
|
|
|
{
|
2013-08-13 18:32:50 +08:00
|
|
|
|
// Check the gid is legal or not
|
|
|
|
|
CC_BREAK_IF(gid == 0);
|
|
|
|
|
|
|
|
|
|
if (tilesAmount > 1)
|
|
|
|
|
{
|
|
|
|
|
// Check the value is all set or not
|
|
|
|
|
CC_BREAK_IF(layer->_tiles[tilesAmount - 2] != 0 && layer->_tiles[tilesAmount - 1] != 0);
|
|
|
|
|
|
|
|
|
|
int currentTileIndex = tilesAmount - layer->_tiles[tilesAmount - 1] - 1;
|
|
|
|
|
layer->_tiles[currentTileIndex] = gid;
|
|
|
|
|
|
|
|
|
|
if (currentTileIndex != tilesAmount - 1)
|
|
|
|
|
{
|
|
|
|
|
--layer->_tiles[tilesAmount - 1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(tilesAmount == 1)
|
|
|
|
|
{
|
|
|
|
|
if (layer->_tiles[0] == 0)
|
|
|
|
|
{
|
|
|
|
|
layer->_tiles[0] = gid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (0);
|
2013-06-06 02:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXTilesetInfo* info = pTMXMapInfo->getTilesets().getLastObject();
|
2013-07-03 22:55:32 +08:00
|
|
|
|
Dictionary *dict = new Dictionary();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
dict->init();
|
2013-06-29 08:42:47 +08:00
|
|
|
|
pTMXMapInfo->setParentGID(info->_firstGid + atoi(valueForKey("id", attributeDict)));
|
2013-06-06 02:29:52 +08:00
|
|
|
|
pTMXMapInfo->getTileProperties()->setObject(dict, pTMXMapInfo->getParentGID());
|
|
|
|
|
CC_SAFE_RELEASE(dict);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-06 02:29:52 +08:00
|
|
|
|
pTMXMapInfo->setParentElement(TMXPropertyTile);
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
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
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
|
pTMXMapInfo->getLayers().addObject(layer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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);
|
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
|
pTMXMapInfo->getObjectGroups().addObject(objectGroup);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-11-29 11:36:42 +08:00
|
|
|
|
TMXTilesetInfo* tileset = pTMXMapInfo->getTilesets().getLastObject();
|
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);
|
|
|
|
|
|
2013-06-06 02:29:52 +08:00
|
|
|
|
if (encoding == "")
|
|
|
|
|
{
|
2013-08-13 18:32:50 +08:00
|
|
|
|
pTMXMapInfo->setLayerAttribs(pTMXMapInfo->getLayerAttribs() | TMXLayerAttribNone);
|
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
|
2013-07-03 22:55:32 +08:00
|
|
|
|
Size layerSize = layer->_layerSize;
|
2013-06-06 02:29:52 +08:00
|
|
|
|
int tilesAmount = layerSize.width*layerSize.height;
|
|
|
|
|
|
|
|
|
|
int *tiles = (int *) malloc(tilesAmount*sizeof(int));
|
|
|
|
|
for (int i = 0; i < tilesAmount; i++)
|
2013-08-13 18:32:50 +08:00
|
|
|
|
{
|
2013-07-04 19:28:54 +08:00
|
|
|
|
tiles[i] = 0;
|
2013-08-13 18:32:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Save the special index in tiles[tilesAmount - 1];
|
|
|
|
|
* When we load tiles, we can do this:
|
|
|
|
|
* tiles[tilesAmount - tiles[tilesAmount - 1] - 1] = tileNum;
|
|
|
|
|
* --tiles[tilesAmount - 1];
|
|
|
|
|
* We do this because we can easily contorl how much tiles we loaded without add a "curTilesAmount" into class member.
|
|
|
|
|
*/
|
|
|
|
|
if (tilesAmount > 1)
|
|
|
|
|
{
|
|
|
|
|
tiles[tilesAmount - 1] = tilesAmount - 1;
|
|
|
|
|
}
|
2013-06-06 02:29:52 +08:00
|
|
|
|
|
2013-06-29 08:42:47 +08:00
|
|
|
|
layer->_tiles = (unsigned int*) tiles;
|
2013-06-06 02:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (encoding == "base64")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
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-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "object")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
char buffer[32] = {0};
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
|
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();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
dict->init();
|
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-11-29 11:36:42 +08:00
|
|
|
|
TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
|
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);
|
|
|
|
|
// 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-11-29 11:36:42 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
|
2013-06-20 14:13:12 +08:00
|
|
|
|
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-11-29 11:36:42 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
|
2013-08-22 10:45:47 +08:00
|
|
|
|
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
|
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-11-29 11:36:42 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = _objectGroups.getLastObject();
|
2013-08-22 10:45:47 +08:00
|
|
|
|
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// get points value string
|
|
|
|
|
const char* value = valueForKey("points", attributeDict);
|
|
|
|
|
if(value)
|
|
|
|
|
{
|
2013-08-22 11:12:09 +08:00
|
|
|
|
Array* pointsArray = Array::createWithCapacity(10);
|
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-08-22 11:12:09 +08:00
|
|
|
|
Dictionary* pointDict = new Dictionary;
|
|
|
|
|
pointDict->init();
|
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();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
pointDict->setObject(pStr, "x");
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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();
|
2013-08-22 11:12:09 +08:00
|
|
|
|
pointDict->setObject(pStr, "y");
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to points array
|
2013-08-22 11:12:09 +08:00
|
|
|
|
pointsArray->addObject(pointDict);
|
|
|
|
|
pointDict->release();
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-22 11:12:09 +08:00
|
|
|
|
dict->setObject(pointsArray, "points");
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (elementName == "polyline")
|
|
|
|
|
{
|
|
|
|
|
// find parent object's dict and add polyline-points to it
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = _objectGroups.getLastObject();
|
2013-09-12 07:36:53 +08:00
|
|
|
|
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
|
|
|
|
|
|
|
|
|
|
// get points value string
|
|
|
|
|
const char* value = valueForKey("points", attributeDict);
|
|
|
|
|
if(value)
|
|
|
|
|
{
|
|
|
|
|
Array* pointsArray = Array::createWithCapacity(10);
|
|
|
|
|
|
|
|
|
|
// 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};
|
|
|
|
|
|
|
|
|
|
Dictionary* pointDict = new Dictionary;
|
|
|
|
|
pointDict->init();
|
|
|
|
|
|
|
|
|
|
// set x
|
|
|
|
|
if(std::getline(pointStream, xStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
|
|
|
|
|
sprintf(buffer, "%d", x);
|
|
|
|
|
String* pStr = new String(buffer);
|
|
|
|
|
pStr->autorelease();
|
|
|
|
|
pointDict->setObject(pStr, "x");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set y
|
|
|
|
|
if(std::getline(pointStream, yStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
|
|
|
|
|
sprintf(buffer, "%d", y);
|
|
|
|
|
String* pStr = new String(buffer);
|
|
|
|
|
pStr->autorelease();
|
|
|
|
|
pointDict->setObject(pStr, "y");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to points array
|
|
|
|
|
pointsArray->addObject(pointDict);
|
|
|
|
|
pointDict->release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dict->setObject(pointsArray, "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
|
|
|
|
|
2013-08-13 18:32:50 +08:00
|
|
|
|
if(elementName == "data")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-13 18:32:50 +08:00
|
|
|
|
if (pTMXMapInfo->getLayerAttribs() & TMXLayerAttribBase64)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-13 18:32:50 +08:00
|
|
|
|
pTMXMapInfo->setStoringCharacters(false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
|
2013-08-13 18:32:50 +08:00
|
|
|
|
|
|
|
|
|
std::string currentString = pTMXMapInfo->getCurrentString();
|
|
|
|
|
unsigned char *buffer;
|
|
|
|
|
len = base64Decode((unsigned char*)currentString.c_str(), (unsigned int)currentString.length(), &buffer);
|
|
|
|
|
if( ! buffer )
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-08-13 18:32:50 +08:00
|
|
|
|
CCLOG("cocos2d: TiledMap: decode data error");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2013-08-13 18:32:50 +08:00
|
|
|
|
|
|
|
|
|
if( pTMXMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib) )
|
|
|
|
|
{
|
|
|
|
|
unsigned char *deflated;
|
|
|
|
|
Size s = layer->_layerSize;
|
|
|
|
|
// int sizeHint = s.width * s.height * sizeof(uint32_t);
|
|
|
|
|
int sizeHint = (int)(s.width * s.height * sizeof(unsigned int));
|
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
|
int inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint);
|
2013-08-13 18:32:50 +08:00
|
|
|
|
CCASSERT(inflatedLen == sizeHint, "");
|
|
|
|
|
|
|
|
|
|
inflatedLen = (size_t)&inflatedLen; // XXX: to avoid warnings in compiler
|
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
|
free(buffer);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
buffer = nullptr;
|
2013-08-13 18:32:50 +08:00
|
|
|
|
|
|
|
|
|
if( ! deflated )
|
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: TiledMap: inflate data error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layer->_tiles = (unsigned int*) deflated;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
layer->_tiles = (unsigned int*) buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pTMXMapInfo->setCurrentString("");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-08-13 18:32:50 +08:00
|
|
|
|
else if (pTMXMapInfo->getLayerAttribs() & TMXLayerAttribNone)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-11-29 11:36:42 +08:00
|
|
|
|
TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
|
2013-08-13 18:32:50 +08:00
|
|
|
|
Size layerSize = layer->_layerSize;
|
|
|
|
|
int tilesAmount = layerSize.width * layerSize.height;
|
|
|
|
|
|
|
|
|
|
//reset the layer->_tiles[tilesAmount - 1]
|
|
|
|
|
if (tilesAmount > 1 && layer->_tiles[tilesAmount - 2] == 0)
|
|
|
|
|
{
|
|
|
|
|
layer->_tiles[tilesAmount - 1] = 0;
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 18:32:50 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
|
|
|
|