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-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-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
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict 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-12-03 14:47:35 +08:00
|
|
|
|
void TMXLayerInfo::setProperties(ValueDict var)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_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
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
, _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);
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict attributeDict;
|
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];
|
2013-12-03 14:47:35 +08:00
|
|
|
|
attributeDict.insert(std::make_pair(key, Value(value)));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (elementName == "map")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string version = attributeDict["version"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
|
|
|
}
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string orientationStr = attributeDict["orientation"].asString();
|
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;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
s.width = attributeDict["width"].asFloat();
|
|
|
|
|
s.height = attributeDict["height"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
pTMXMapInfo->setMapSize(s);
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
s.width = attributeDict["tilewidth"].asFloat();
|
|
|
|
|
s.height = attributeDict["tileheight"].asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string externalTilesetFilename = attributeDict["source"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
_currentFirstGID = (unsigned int)attributeDict["firstgid"].asInt();
|
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-12-03 14:47:35 +08:00
|
|
|
|
tileset->_name = attributeDict["name"].asString();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
if (_currentFirstGID == 0)
|
2013-02-26 17:42:18 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
tileset->_firstGid = (unsigned int)attributeDict["firstgid"].asInt();
|
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-12-03 14:47:35 +08:00
|
|
|
|
tileset->_spacing = (unsigned int)attributeDict["spacing"].asInt();
|
|
|
|
|
tileset->_margin = (unsigned int)attributeDict["margin"].asInt();
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
s.width = attributeDict["tilewidth"].asFloat();
|
|
|
|
|
s.height = attributeDict["tileheight"].asFloat();
|
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-12-03 14:47:35 +08:00
|
|
|
|
unsigned int gid = (unsigned int)attributeDict["gid"].asInt();
|
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-12-03 14:47:35 +08:00
|
|
|
|
pTMXMapInfo->setParentGID(info->_firstGid + attributeDict["id"].asInt());
|
|
|
|
|
//FIXME:XXX Why insert an empty dict?
|
|
|
|
|
pTMXMapInfo->getTileProperties().insert(std::make_pair(pTMXMapInfo->getParentGID(), Value()));
|
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-12-03 14:47:35 +08:00
|
|
|
|
layer->_name = attributeDict["name"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Size s;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
s.width = attributeDict["width"].asFloat();
|
|
|
|
|
s.height = attributeDict["height"].asFloat();
|
2013-06-15 14:03:30 +08:00
|
|
|
|
layer->_layerSize = s;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
layer->_visible = attributeDict["visible"].asBool();
|
|
|
|
|
|
|
|
|
|
Value& opacityValue = attributeDict["opacity"];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
if( !opacityValue.isNull() )
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
layer->_opacity = (unsigned char)(255.0f * opacityValue.asFloat());
|
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
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
float x = attributeDict["x"].asFloat();
|
|
|
|
|
float y = attributeDict["y"].asFloat();
|
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();
|
2013-12-03 14:47:35 +08:00
|
|
|
|
objectGroup->setGroupName(attributeDict["name"].asString());
|
2013-06-20 14:13:12 +08:00
|
|
|
|
Point positionOffset;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
positionOffset.x = attributeDict["x"].asFloat() * pTMXMapInfo->getTileSize().width;
|
|
|
|
|
positionOffset.y = attributeDict["y"].asFloat() * pTMXMapInfo->getTileSize().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string imagename = attributeDict["source"].asString();
|
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
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string encoding = attributeDict["encoding"].asString();
|
|
|
|
|
std::string compression = attributeDict["compression"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
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
|
|
|
|
{
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict dict;
|
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-12-03 14:47:35 +08:00
|
|
|
|
Value value = attributeDict[key];
|
|
|
|
|
dict[key] = value;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// But X and Y since they need special treatment
|
|
|
|
|
// X
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
int x = attributeDict["x"].asInt() + (int)objectGroup->getPositionOffset().x;
|
|
|
|
|
dict["x"] = Value(x);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// Y
|
2013-12-03 14:47:35 +08:00
|
|
|
|
int y = attributeDict["y"].asInt() + (int)objectGroup->getPositionOffset().y;
|
|
|
|
|
|
|
|
|
|
// Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
|
|
|
|
|
y = (int)(_mapSize.height * _tileSize.height) - y - attributeDict["height"].asInt();
|
|
|
|
|
dict["y"] = Value(y);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// Add the object to the objectGroup
|
2013-12-03 14:47:35 +08:00
|
|
|
|
objectGroup->getObjects().push_back(Value(dict));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// 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'",
|
2013-12-03 14:47:35 +08:00
|
|
|
|
attributeDict["name"].asString().c_str(), attributeDict["value"].asString().c_str() );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap )
|
|
|
|
|
{
|
|
|
|
|
// The parent element is the map
|
2013-12-03 14:47:35 +08:00
|
|
|
|
Value value = attributeDict["value"];
|
|
|
|
|
std::string key = attributeDict["name"].asString();
|
|
|
|
|
pTMXMapInfo->getProperties().insert(std::make_pair(key, value));
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
Value value = attributeDict["value"];
|
|
|
|
|
std::string key = attributeDict["name"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// Add the property to the layer
|
2013-12-03 14:47:35 +08:00
|
|
|
|
layer->getProperties().insert(std::make_pair(key, value));
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
Value value = attributeDict["value"];
|
|
|
|
|
std::string key = attributeDict["name"].asString();
|
|
|
|
|
objectGroup->getProperties().insert(std::make_pair(key, value));
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict& dict = objectGroup->getObjects().rbegin()->asDict();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string propertyName = attributeDict["name"].asString();
|
|
|
|
|
dict[propertyName] = attributeDict["value"];
|
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
else if ( pTMXMapInfo->getParentElement() == TMXPropertyTile )
|
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
IntValueDict& dict = pTMXMapInfo->getTileProperties().at(pTMXMapInfo->getParentGID()).asIntKeyDict();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
int propertyName = attributeDict["name"].asInt();
|
|
|
|
|
dict[propertyName] = attributeDict["value"];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict& dict = objectGroup->getObjects().rbegin()->asDict();
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// get points value string
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string value = attributeDict["points"].asString();
|
|
|
|
|
if (!value.empty())
|
2013-01-30 06:45:41 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
ValueArray pointsArray;
|
|
|
|
|
pointsArray.reserve(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;
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
ValueDict pointDict;
|
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;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointDict["x"] = Value(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;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointDict["y"] = Value(y);
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to points array
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointsArray.push_back(Value(pointDict));
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
dict["points"] = Value(pointsArray);
|
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-12-03 14:47:35 +08:00
|
|
|
|
ValueDict& dict = objectGroup->getObjects().rbegin()->asDict();
|
2013-09-12 07:36:53 +08:00
|
|
|
|
|
|
|
|
|
// get points value string
|
2013-12-03 14:47:35 +08:00
|
|
|
|
std::string value = attributeDict["points"].asString();
|
|
|
|
|
if (!value.empty())
|
2013-09-12 07:36:53 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
ValueArray pointsArray;
|
|
|
|
|
pointsArray.reserve(10);
|
2013-09-12 07:36:53 +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;
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
ValueDict pointDict;
|
2013-09-12 07:36:53 +08:00
|
|
|
|
|
|
|
|
|
// set x
|
|
|
|
|
if(std::getline(pointStream, xStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointDict["x"] = Value(x);
|
2013-09-12 07:36:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set y
|
|
|
|
|
if(std::getline(pointStream, yStr, ','))
|
|
|
|
|
{
|
|
|
|
|
int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointDict["y"] = Value(y);
|
2013-09-12 07:36:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// add to points array
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointsArray.push_back(Value(pointDict));
|
2013-09-12 07:36:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
dict["polylinePoints"] = Value(pointsArray);
|
2013-09-12 07:36:53 +08:00
|
|
|
|
}
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
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) )
|
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
unsigned char *deflated = nullptr;
|
2013-08-13 18:32:50 +08:00
|
|
|
|
Size s = layer->_layerSize;
|
|
|
|
|
// int sizeHint = s.width * s.height * sizeof(uint32_t);
|
|
|
|
|
int sizeHint = (int)(s.width * s.height * sizeof(unsigned int));
|
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
int CC_UNUSED inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint);
|
2013-08-13 18:32:50 +08:00
|
|
|
|
CCASSERT(inflatedLen == sizeHint, "");
|
|
|
|
|
|
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
|
|
|
|
|