2010-12-28 10:23:14 +08:00
|
|
|
|
/****************************************************************************
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Copyright (c) 2011 Максим Аксенов
|
2011-03-19 14:45:51 +08:00
|
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-04 14:11:43 +08:00
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
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
|
|
|
|
|
2014-08-28 17:03:29 +08:00
|
|
|
|
#include "2d/CCTMXXMLParser.h"
|
2013-11-22 05:43:59 +08:00
|
|
|
|
#include <unordered_map>
|
2013-01-30 06:45:41 +08:00
|
|
|
|
#include <sstream>
|
2020-08-28 15:42:40 +08:00
|
|
|
|
// #include "2d/CCTMXTiledMap.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
|
#include "base/ZipUtils.h"
|
|
|
|
|
#include "base/base64.h"
|
|
|
|
|
#include "base/CCDirector.h"
|
2014-08-29 16:44:20 +08:00
|
|
|
|
#include "platform/CCFileUtils.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
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXLayerInfo::TMXLayerInfo() : _name(""), _tiles(nullptr), _ownTiles(true) {}
|
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);
|
2015-03-11 21:54:22 +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
|
|
|
|
|
2014-01-05 18:20:29 +08:00
|
|
|
|
ValueMap& 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
|
|
|
|
}
|
2015-03-11 21:54:22 +08:00
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
|
void TMXLayerInfo::setProperties(ValueMap 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
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXTilesetInfo::TMXTilesetInfo() : _firstGid(0), _tileSize(Vec2::ZERO), _spacing(0), _margin(0), _imageSize(Vec2::ZERO)
|
|
|
|
|
{}
|
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
|
|
|
|
|
2014-02-08 10:33:30 +08:00
|
|
|
|
Rect TMXTilesetInfo::getRectForGID(uint32_t 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;
|
2014-02-08 10:33:30 +08:00
|
|
|
|
gid &= kTMXFlippedMask;
|
2013-06-15 14:03:30 +08:00
|
|
|
|
gid = gid - _firstGid;
|
2015-09-22 16:08:23 +08:00
|
|
|
|
// max_x means the column count in tile map
|
2015-07-23 11:01:51 +08:00
|
|
|
|
// in the origin:
|
|
|
|
|
// max_x = (int)((_imageSize.width - _margin*2 + _spacing) / (_tileSize.width + _spacing));
|
2015-09-22 16:08:23 +08:00
|
|
|
|
// but in editor "Tiled", _margin variable only effect the left side
|
|
|
|
|
// for compatible with "Tiled", change the max_x calculation
|
2015-07-23 11:01:51 +08:00
|
|
|
|
int max_x = (int)((_imageSize.width - _margin + _spacing) / (_tileSize.width + _spacing));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
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
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
TMXMapInfo* TMXMapInfo::create(std::string_view tmxFile)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXMapInfo* ret = new TMXMapInfo();
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (ret->initWithTMXFile(tmxFile))
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
ret->autorelease();
|
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
|
CC_SAFE_DELETE(ret);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
return nullptr;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
TMXMapInfo* TMXMapInfo::createWithXML(std::string_view tmxString, std::string_view resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXMapInfo* ret = new TMXMapInfo();
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (ret->initWithXML(tmxString, resourcePath))
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
ret->autorelease();
|
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
|
CC_SAFE_DELETE(ret);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
return nullptr;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
void TMXMapInfo::internalInit(std::string_view tmxFileName, std::string_view resourcePath)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2015-03-11 22:12:23 +08:00
|
|
|
|
if (!tmxFileName.empty())
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2015-03-11 22:12:23 +08:00
|
|
|
|
if (!resourcePath.empty())
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-05 10:35:10 +08:00
|
|
|
|
_objectGroups.reserve(4);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// tmp vars
|
2021-12-25 10:04:45 +08:00
|
|
|
|
_currentString = "";
|
2013-06-15 14:03:30 +08:00
|
|
|
|
_storingCharacters = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
_layerAttribs = TMXLayerAttribNone;
|
|
|
|
|
_parentElement = TMXPropertyNone;
|
|
|
|
|
_currentFirstGID = -1;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2015-03-11 21:54:22 +08:00
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
bool TMXMapInfo::initWithXML(std::string_view tmxString, std::string_view 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
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
bool TMXMapInfo::initWithTMXFile(std::string_view tmxFile)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
|
internalInit(tmxFile, "");
|
2016-02-03 23:12:37 +08:00
|
|
|
|
return parseXMLFile(_TMXFileName);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
|
TMXMapInfo::TMXMapInfo()
|
2021-12-25 10:04:45 +08:00
|
|
|
|
: _orientation(TMXOrientationOrtho)
|
|
|
|
|
, _staggerAxis(TMXStaggerAxis_Y)
|
|
|
|
|
, _staggerIndex(TMXStaggerIndex_Even)
|
|
|
|
|
, _hexSideLength(0)
|
|
|
|
|
, _mapSize(Vec2::ZERO)
|
|
|
|
|
, _tileSize(Vec2::ZERO)
|
|
|
|
|
, _parentElement(0)
|
|
|
|
|
, _parentGID(0)
|
|
|
|
|
, _layerAttribs(0)
|
|
|
|
|
, _storingCharacters(false)
|
|
|
|
|
, _xmlTileIndex(0)
|
|
|
|
|
, _currentFirstGID(-1)
|
|
|
|
|
, _recordFirstGID(true)
|
|
|
|
|
{}
|
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
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLString(std::string_view 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
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (false == parser.init("UTF-8"))
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
return parser.parse(xmlString.data(), len);
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
bool TMXMapInfo::parseXMLFile(std::string_view xmlFilename)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
|
SAXParser parser;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
if (false == parser.init("UTF-8"))
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
|
parser.setDelegator(this);
|
|
|
|
|
|
2016-02-03 23:12:37 +08:00
|
|
|
|
return parser.parse(FileUtils::getInstance()->fullPathForFilename(xmlFilename));
|
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
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts)
|
|
|
|
|
{
|
|
|
|
|
TMXMapInfo* tmxMapInfo = this;
|
2015-03-11 22:39:04 +08:00
|
|
|
|
std::string elementName = name;
|
2013-12-04 17:46:57 +08:00
|
|
|
|
ValueMap attributeDict;
|
2013-02-26 17:42:18 +08:00
|
|
|
|
if (atts && atts[0])
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2015-03-11 21:54:22 +08:00
|
|
|
|
for (int i = 0; atts[i]; i += 2)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
std::string key = atts[i];
|
|
|
|
|
std::string value = atts[i + 1];
|
2016-11-08 11:50:00 +08:00
|
|
|
|
attributeDict.emplace(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
|
|
|
|
{
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string version = attributeDict["version"].asString();
|
2020-08-31 17:13:04 +08:00
|
|
|
|
CCLOG("cocos2d: TMXFormat: TMX version: %s", version.c_str());
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string orientationStr = attributeDict["orientation"].asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (orientationStr == "orthogonal")
|
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setOrientation(TMXOrientationOrtho);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (orientationStr == "isometric")
|
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setOrientation(TMXOrientationIso);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (orientationStr == "hexagonal")
|
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setOrientation(TMXOrientationHex);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (orientationStr == "staggered")
|
|
|
|
|
{
|
2014-10-10 17:52:13 +08:00
|
|
|
|
tmxMapInfo->setOrientation(TMXOrientationStaggered);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else
|
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", tmxMapInfo->getOrientation());
|
2015-03-11 21:54:22 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string staggerAxisStr = attributeDict["staggeraxis"].asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (staggerAxisStr == "x")
|
|
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tmxMapInfo->setStaggerAxis(TMXStaggerAxis_X);
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (staggerAxisStr == "y")
|
|
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tmxMapInfo->setStaggerAxis(TMXStaggerAxis_Y);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string staggerIndex = attributeDict["staggerindex"].asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (staggerIndex == "odd")
|
|
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tmxMapInfo->setStaggerIndex(TMXStaggerIndex_Odd);
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (staggerIndex == "even")
|
|
|
|
|
{
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tmxMapInfo->setStaggerIndex(TMXStaggerIndex_Even);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
auto hexSideLength = attributeDict["hexsidelength"].asInt();
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tmxMapInfo->setHexSideLength(hexSideLength);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
|
Vec2 s;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
s.width = attributeDict["width"].asFloat();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
s.height = attributeDict["height"].asFloat();
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setMapSize(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
s.width = attributeDict["tilewidth"].asFloat();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
s.height = attributeDict["tileheight"].asFloat();
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setTileSize(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// The parent element is now "map"
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyMap);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
else if (elementName == "tileset")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// If this is an external tileset then start parsing that
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string externalTilesetFilename = attributeDict["source"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
if (externalTilesetFilename != "")
|
|
|
|
|
{
|
2015-11-12 09:49:49 +08:00
|
|
|
|
_externalTilesetFilename = externalTilesetFilename;
|
2015-11-16 11:08:20 +08:00
|
|
|
|
|
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
|
2019-04-22 09:44:19 +08:00
|
|
|
|
if (_TMXFileName.find_last_of('/') != string::npos)
|
2012-03-26 16:46:23 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +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
|
|
|
|
}
|
2021-12-25 10:04:45 +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
|
|
|
|
}
|
2016-02-03 23:12:37 +08:00
|
|
|
|
externalTilesetFilename = FileUtils::getInstance()->fullPathForFilename(externalTilesetFilename);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
_currentFirstGID = attributeDict["firstgid"].asInt();
|
2013-12-23 10:37:46 +08:00
|
|
|
|
if (_currentFirstGID < 0)
|
|
|
|
|
{
|
|
|
|
|
_currentFirstGID = 0;
|
|
|
|
|
}
|
|
|
|
|
_recordFirstGID = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2016-02-03 23:12:37 +08:00
|
|
|
|
tmxMapInfo->parseXMLFile(externalTilesetFilename);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXTilesetInfo* tileset = new TMXTilesetInfo();
|
|
|
|
|
tileset->_name = attributeDict["name"].asString();
|
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
|
if (_recordFirstGID)
|
2013-02-26 17:42:18 +08:00
|
|
|
|
{
|
2013-12-23 10:37:46 +08:00
|
|
|
|
// unset before, so this is tmx file.
|
2020-08-18 11:33:18 +08:00
|
|
|
|
tileset->_firstGid = attributeDict["firstgid"].asInt();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-23 10:37:46 +08:00
|
|
|
|
if (tileset->_firstGid < 0)
|
|
|
|
|
{
|
|
|
|
|
tileset->_firstGid = 0;
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_firstGid = _currentFirstGID;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
_currentFirstGID = 0;
|
2013-02-26 17:42:18 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
tileset->_spacing = attributeDict["spacing"].asInt();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
tileset->_margin = attributeDict["margin"].asInt();
|
2021-10-23 23:27:14 +08:00
|
|
|
|
Vec2 s;
|
2021-12-25 10:04:45 +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-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->getTilesets().pushBack(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-12-18 17:47:20 +08:00
|
|
|
|
if (tmxMapInfo->getParentElement() == TMXPropertyLayer)
|
2013-06-06 02:29:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Vec2 layerSize = layer->_layerSize;
|
|
|
|
|
uint32_t gid = static_cast<uint32_t>(attributeDict["gid"].asUnsignedInt());
|
2021-12-26 23:26:34 +08:00
|
|
|
|
int tilesAmount = static_cast<int>(layerSize.width * layerSize.height);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2014-02-19 10:41:12 +08:00
|
|
|
|
if (_xmlTileIndex < tilesAmount)
|
2013-07-04 19:28:54 +08:00
|
|
|
|
{
|
2014-02-19 10:41:12 +08:00
|
|
|
|
layer->_tiles[_xmlTileIndex++] = gid;
|
|
|
|
|
}
|
2013-06-06 02:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
tmxMapInfo->setParentGID(info->_firstGid + attributeDict["id"].asInt());
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->getTileProperties()[tmxMapInfo->getParentGID()] = Value(ValueMap());
|
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyTile);
|
2013-06-06 02:29:52 +08:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXLayerInfo* layer = new TMXLayerInfo();
|
|
|
|
|
layer->_name = attributeDict["name"].asString();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2021-10-23 23:27:14 +08:00
|
|
|
|
Vec2 s;
|
2021-12-25 10:04:45 +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
|
|
|
|
|
2014-05-02 02:51:11 +08:00
|
|
|
|
Value& visibleValue = attributeDict["visible"];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
layer->_visible = visibleValue.isNull() ? true : visibleValue.asBool();
|
2013-12-03 14:47:35 +08:00
|
|
|
|
|
|
|
|
|
Value& opacityValue = attributeDict["opacity"];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
layer->_opacity = opacityValue.isNull() ? 255 : (unsigned char)(255.0f * opacityValue.asFloat());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
float x = attributeDict["x"].asFloat();
|
|
|
|
|
float y = attributeDict["y"].asFloat();
|
2015-04-20 01:40:52 +08:00
|
|
|
|
layer->_offset.set(x, y);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->getLayers().pushBack(layer);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
layer->release();
|
|
|
|
|
|
|
|
|
|
// The parent element is now "layer"
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyLayer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "objectgroup")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = new TMXObjectGroup();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
objectGroup->setGroupName(attributeDict["name"].asString());
|
2014-05-15 01:07:09 +08:00
|
|
|
|
Vec2 positionOffset;
|
2020-08-18 11:33:18 +08:00
|
|
|
|
positionOffset.x = attributeDict["x"].asFloat() * tmxMapInfo->getTileSize().width;
|
|
|
|
|
positionOffset.y = attributeDict["y"].asFloat() * tmxMapInfo->getTileSize().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
objectGroup->setPositionOffset(positionOffset);
|
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->getObjectGroups().pushBack(objectGroup);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
objectGroup->release();
|
|
|
|
|
|
|
|
|
|
// The parent element is now "objectgroup"
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyObjectGroup);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2016-07-02 06:51:00 +08:00
|
|
|
|
else if (elementName == "tileoffset")
|
|
|
|
|
{
|
|
|
|
|
TMXTilesetInfo* tileset = tmxMapInfo->getTilesets().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
float tileOffsetX = attributeDict["x"].asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
float tileOffsetY = attributeDict["y"].asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2016-07-02 06:51:00 +08:00
|
|
|
|
tileset->_tileOffset = Vec2(tileOffsetX, tileOffsetY);
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "image")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXTilesetInfo* tileset = tmxMapInfo->getTilesets().back();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
|
// build full path
|
2021-12-25 10:04:45 +08:00
|
|
|
|
std::string imagename = attributeDict["source"].asString();
|
2015-11-12 09:49:49 +08:00
|
|
|
|
tileset->_originSourceImage = imagename;
|
2015-11-16 11:08:20 +08:00
|
|
|
|
|
2019-04-22 09:44:19 +08:00
|
|
|
|
if (_TMXFileName.find_last_of('/') != string::npos)
|
2012-08-23 14:20:32 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
|
2013-06-15 14:03:30 +08:00
|
|
|
|
tileset->_sourceImage = dir + imagename;
|
2012-03-26 16:46:23 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "data")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
std::string encoding = attributeDict["encoding"].asString();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
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-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setLayerAttribs(tmxMapInfo->getLayerAttribs() | TMXLayerAttribNone);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Vec2 layerSize = layer->_layerSize;
|
2021-12-26 23:26:34 +08:00
|
|
|
|
int tilesAmount = static_cast<int>(layerSize.width * layerSize.height);
|
2013-06-06 02:29:52 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
uint32_t* tiles = (uint32_t*)malloc(tilesAmount * sizeof(uint32_t));
|
2014-02-19 10:41:12 +08:00
|
|
|
|
// set all value to 0
|
2021-12-25 10:04:45 +08:00
|
|
|
|
memset(tiles, 0, tilesAmount * sizeof(int));
|
2013-06-06 02:29:52 +08:00
|
|
|
|
|
2013-12-20 09:50:53 +08:00
|
|
|
|
layer->_tiles = tiles;
|
2013-06-06 02:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (encoding == "base64")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
int layerAttribs = tmxMapInfo->getLayerAttribs();
|
|
|
|
|
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribBase64);
|
|
|
|
|
tmxMapInfo->setStoringCharacters(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (compression == "gzip")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
layerAttribs = tmxMapInfo->getLayerAttribs();
|
|
|
|
|
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribGzip);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
else if (compression == "zlib")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
layerAttribs = tmxMapInfo->getLayerAttribs();
|
|
|
|
|
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
CCASSERT(compression == "" || compression == "gzip" || compression == "zlib",
|
|
|
|
|
"TMX: unsupported compression method");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2016-04-13 03:51:09 +08:00
|
|
|
|
else if (encoding == "csv")
|
|
|
|
|
{
|
|
|
|
|
int layerAttribs = tmxMapInfo->getLayerAttribs();
|
|
|
|
|
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribCSV);
|
|
|
|
|
tmxMapInfo->setStoringCharacters(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "object")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
|
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-04 17:46:57 +08:00
|
|
|
|
ValueMap dict;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// Parse everything automatically
|
2016-02-23 14:07:04 +08:00
|
|
|
|
const char* keys[] = {"name", "type", "width", "height", "gid", "id"};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2015-03-11 22:12:23 +08:00
|
|
|
|
for (const auto& key : keys)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-03 14:47:35 +08:00
|
|
|
|
Value value = attributeDict[key];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
dict[key] = value;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// But X and Y since they need special treatment
|
|
|
|
|
// X
|
2020-08-18 11:33:18 +08:00
|
|
|
|
int x = attributeDict["x"].asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
// Y
|
2020-08-18 11:33:18 +08:00
|
|
|
|
int y = attributeDict["y"].asInt();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
Vec2 p(x + objectGroup->getPositionOffset().x, _mapSize.height * _tileSize.height - y -
|
|
|
|
|
objectGroup->getPositionOffset().y -
|
|
|
|
|
attributeDict["height"].asInt());
|
|
|
|
|
p = CC_POINT_PIXELS_TO_POINTS(p);
|
2014-02-24 20:14:46 +08:00
|
|
|
|
dict["x"] = Value(p.x);
|
|
|
|
|
dict["y"] = Value(p.y);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
|
|
int width = attributeDict["width"].asInt();
|
2020-08-18 11:33:18 +08:00
|
|
|
|
int height = attributeDict["height"].asInt();
|
2021-10-23 23:27:14 +08:00
|
|
|
|
Vec2 s(width, height);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
s = CC_SIZE_PIXELS_TO_POINTS(s);
|
|
|
|
|
dict["width"] = Value(s.width);
|
2014-02-24 20:14:46 +08:00
|
|
|
|
dict["height"] = Value(s.height);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
dict["rotation"] = attributeDict["rotation"].asDouble();
|
2017-09-20 14:31:15 +08:00
|
|
|
|
|
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
|
|
|
|
|
2015-03-11 21:54:22 +08:00
|
|
|
|
// The parent element is now "object"
|
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyObject);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
2013-02-26 17:42:18 +08:00
|
|
|
|
else if (elementName == "property")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
if (tmxMapInfo->getParentElement() == TMXPropertyNone)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
CCLOG("TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'",
|
|
|
|
|
attributeDict["name"].asString().c_str(), attributeDict["value"].asString().c_str());
|
|
|
|
|
}
|
|
|
|
|
else if (tmxMapInfo->getParentElement() == TMXPropertyMap)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// The parent element is the map
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Value value = attributeDict["value"];
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string key = attributeDict["name"].asString();
|
2016-11-08 11:50:00 +08:00
|
|
|
|
tmxMapInfo->getProperties().emplace(key, value);
|
2013-12-03 14:47:35 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (tmxMapInfo->getParentElement() == TMXPropertyLayer)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// The parent element is the last layer
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
|
2021-12-25 10:04:45 +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
|
2016-11-08 11:50:00 +08:00
|
|
|
|
layer->getProperties().emplace(key, value);
|
2013-12-03 14:47:35 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (tmxMapInfo->getParentElement() == TMXPropertyObjectGroup)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// The parent element is the last object group
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
Value value = attributeDict["value"];
|
|
|
|
|
std::string key = attributeDict["name"].asString();
|
2016-11-08 11:50:00 +08:00
|
|
|
|
objectGroup->getProperties().emplace(key, value);
|
2013-12-03 14:47:35 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (tmxMapInfo->getParentElement() == TMXPropertyObject)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// The parent element is the last object
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string propertyName = attributeDict["name"].asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
dict[propertyName] = attributeDict["value"];
|
2013-12-03 14:47:35 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (tmxMapInfo->getParentElement() == TMXPropertyTile)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-30 11:33:41 +08:00
|
|
|
|
ValueMap& dict = tmxMapInfo->getTileProperties().at(tmxMapInfo->getParentGID()).asValueMap();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string propertyName = attributeDict["name"].asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
dict[propertyName] = attributeDict["value"];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
else if (elementName == "polygon")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// find parent object's dict and add polygon-points to it
|
2013-12-05 10:35:10 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = _objectGroups.back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap();
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// get points value string
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string value = attributeDict["points"].asString();
|
2013-12-03 14:47:35 +08:00
|
|
|
|
if (!value.empty())
|
2013-01-30 06:45:41 +08:00
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
|
ValueVector pointsArray;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
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;
|
2015-03-11 21:54:22 +08:00
|
|
|
|
while (std::getline(pointsStream, pointPair, ' '))
|
2013-01-30 06:45:41 +08:00
|
|
|
|
{
|
|
|
|
|
// parse each point combo into a comma-separated x,y point
|
|
|
|
|
stringstream pointStream(pointPair);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
string xStr, yStr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
|
ValueMap pointDict;
|
2013-01-30 06:45:41 +08:00
|
|
|
|
|
|
|
|
|
// set x
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (std::getline(pointStream, xStr, ','))
|
2013-01-30 06:45:41 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (std::getline(pointStream, yStr, ','))
|
2013-01-30 06:45:41 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-03 14:47:35 +08:00
|
|
|
|
dict["points"] = Value(pointsArray);
|
2013-01-30 06:45:41 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +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-12-05 10:35:10 +08:00
|
|
|
|
TMXObjectGroup* objectGroup = _objectGroups.back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap();
|
|
|
|
|
|
2013-09-12 07:36:53 +08:00
|
|
|
|
// get points value string
|
2020-08-18 11:33:18 +08:00
|
|
|
|
std::string value = attributeDict["points"].asString();
|
2013-12-03 14:47:35 +08:00
|
|
|
|
if (!value.empty())
|
2013-09-12 07:36:53 +08:00
|
|
|
|
{
|
2013-12-04 17:46:57 +08:00
|
|
|
|
ValueVector pointsArray;
|
2013-12-03 14:47:35 +08:00
|
|
|
|
pointsArray.reserve(10);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-09-12 07:36:53 +08:00
|
|
|
|
// parse points string into a space-separated set of points
|
|
|
|
|
stringstream pointsStream(value);
|
|
|
|
|
string pointPair;
|
2015-03-11 21:54:22 +08:00
|
|
|
|
while (std::getline(pointsStream, pointPair, ' '))
|
2013-09-12 07:36:53 +08:00
|
|
|
|
{
|
|
|
|
|
// parse each point combo into a comma-separated x,y point
|
|
|
|
|
stringstream pointStream(pointPair);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
string xStr, yStr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-04 17:46:57 +08:00
|
|
|
|
ValueMap pointDict;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-09-12 07:36:53 +08:00
|
|
|
|
// set x
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (std::getline(pointStream, xStr, ','))
|
2013-09-12 07:36:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-09-12 07:36:53 +08:00
|
|
|
|
// set y
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (std::getline(pointStream, yStr, ','))
|
2013-09-12 07:36:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
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
|
|
|
|
}
|
2021-12-25 10:04:45 +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
|
|
|
|
}
|
2020-08-28 16:33:52 +08:00
|
|
|
|
else if (elementName == "animation")
|
|
|
|
|
{
|
|
|
|
|
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
|
|
|
|
|
info->_animationInfo.insert(tmxMapInfo->getParentGID(), TMXTileAnimInfo::create(tmxMapInfo->getParentGID()));
|
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyAnimation);
|
|
|
|
|
}
|
|
|
|
|
else if (elementName == "frame")
|
|
|
|
|
{
|
|
|
|
|
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
auto animInfo = info->_animationInfo.at(tmxMapInfo->getParentGID());
|
2020-08-28 16:33:52 +08:00
|
|
|
|
// calculate gid of frame
|
2021-12-25 10:04:45 +08:00
|
|
|
|
animInfo->_frames.emplace_back(
|
|
|
|
|
TMXTileAnimFrame(info->_firstGid + attributeDict["tileid"].asInt(), attributeDict["duration"].asFloat()));
|
2020-08-28 16:33:52 +08:00
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXMapInfo* tmxMapInfo = this;
|
2015-03-11 22:39:04 +08:00
|
|
|
|
std::string elementName = name;
|
2012-03-20 15:04:53 +08:00
|
|
|
|
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (elementName == "data")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribBase64)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setStoringCharacters(false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
|
auto currentString = tmxMapInfo->getCurrentString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
unsigned char* buffer;
|
|
|
|
|
auto len =
|
2021-12-26 23:26:34 +08:00
|
|
|
|
base64Decode((unsigned char*)currentString.data(), (unsigned int)currentString.length(), &buffer);
|
2015-03-11 21:54:22 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (tmxMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib))
|
2013-08-13 18:32:50 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
unsigned char* deflated = nullptr;
|
|
|
|
|
Vec2 s = layer->_layerSize;
|
2013-08-13 18:32:50 +08:00
|
|
|
|
// int sizeHint = s.width * s.height * sizeof(uint32_t);
|
2013-12-06 16:32:06 +08:00
|
|
|
|
ssize_t sizeHint = s.width * s.height * sizeof(unsigned int);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-06 16:32:06 +08:00
|
|
|
|
ssize_t CC_UNUSED inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint);
|
2015-07-14 15:28:36 +08:00
|
|
|
|
CCASSERT(inflatedLen == sizeHint, "inflatedLen should be equal to sizeHint!");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-11-12 10:09:47 +08:00
|
|
|
|
free(buffer);
|
2013-11-29 11:36:42 +08:00
|
|
|
|
buffer = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2015-03-11 21:54:22 +08:00
|
|
|
|
if (!deflated)
|
2013-08-13 18:32:50 +08:00
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: TiledMap: inflate data error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2014-02-08 10:11:17 +08:00
|
|
|
|
layer->_tiles = reinterpret_cast<uint32_t*>(deflated);
|
2013-08-13 18:32:50 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2014-02-08 10:11:17 +08:00
|
|
|
|
layer->_tiles = reinterpret_cast<uint32_t*>(buffer);
|
2013-08-13 18:32:50 +08:00
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setCurrentString("");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2016-04-13 03:51:09 +08:00
|
|
|
|
else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribCSV)
|
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
unsigned char* buffer;
|
2016-04-13 03:51:09 +08:00
|
|
|
|
|
|
|
|
|
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
|
|
|
|
|
|
|
|
|
|
tmxMapInfo->setStoringCharacters(false);
|
2021-12-26 23:26:34 +08:00
|
|
|
|
auto currentString = tmxMapInfo->getCurrentString();
|
2016-04-13 03:51:09 +08:00
|
|
|
|
|
|
|
|
|
vector<string> gidTokens;
|
2021-12-26 23:26:34 +08:00
|
|
|
|
std::stringstream filestr;
|
|
|
|
|
filestr << currentString;
|
2016-04-13 03:51:09 +08:00
|
|
|
|
string sRow;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
while (getline(filestr, sRow, '\n'))
|
|
|
|
|
{
|
2016-04-13 03:51:09 +08:00
|
|
|
|
string sGID;
|
|
|
|
|
istringstream rowstr(sRow);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
while (getline(rowstr, sGID, ','))
|
|
|
|
|
{
|
2016-04-13 03:51:09 +08:00
|
|
|
|
gidTokens.push_back(sGID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 32-bits per gid
|
|
|
|
|
buffer = (unsigned char*)malloc(gidTokens.size() * 4);
|
|
|
|
|
if (!buffer)
|
|
|
|
|
{
|
|
|
|
|
CCLOG("cocos2d: TiledMap: CSV buffer not allocated.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t* bufferPtr = reinterpret_cast<uint32_t*>(buffer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
for (const auto& gidToken : gidTokens)
|
|
|
|
|
{
|
2016-10-31 10:42:09 +08:00
|
|
|
|
auto tileGid = (uint32_t)strtoul(gidToken.c_str(), nullptr, 10);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
*bufferPtr = tileGid;
|
2016-04-13 03:51:09 +08:00
|
|
|
|
bufferPtr++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
layer->_tiles = reinterpret_cast<uint32_t*>(buffer);
|
|
|
|
|
|
|
|
|
|
tmxMapInfo->setCurrentString("");
|
|
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
|
else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribNone)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2014-02-19 10:41:12 +08:00
|
|
|
|
_xmlTileIndex = 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
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyNone);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
else if (elementName == "layer")
|
|
|
|
|
{
|
|
|
|
|
// The layer element has ended
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyNone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
|
|
|
|
else if (elementName == "objectgroup")
|
|
|
|
|
{
|
|
|
|
|
// The objectgroup element has ended
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyNone);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
else if (elementName == "object")
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
|
|
|
|
// The object element has ended
|
2013-12-18 17:47:20 +08:00
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyNone);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2013-12-23 10:37:46 +08:00
|
|
|
|
else if (elementName == "tileset")
|
|
|
|
|
{
|
|
|
|
|
_recordFirstGID = true;
|
|
|
|
|
}
|
2020-08-28 16:33:52 +08:00
|
|
|
|
else if (elementName == "animation")
|
|
|
|
|
{
|
|
|
|
|
tmxMapInfo->setParentElement(TMXPropertyNone);
|
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
2010-08-27 18:07:37 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
void TMXMapInfo::textHandler(void* /*ctx*/, const char* ch, size_t len)
|
2012-03-20 15:04:53 +08:00
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXMapInfo* tmxMapInfo = this;
|
2015-03-11 22:39:04 +08:00
|
|
|
|
std::string text(ch, 0, len);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
|
if (tmxMapInfo->isStoringCharacters())
|
2012-04-19 14:35:52 +08:00
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
|
std::string currentString{tmxMapInfo->getCurrentString()};
|
2013-12-18 17:47:20 +08:00
|
|
|
|
currentString += text;
|
2016-02-03 23:12:37 +08:00
|
|
|
|
tmxMapInfo->setCurrentString(currentString);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
}
|
2012-03-20 15:04:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXTileAnimFrame::TMXTileAnimFrame(uint32_t tileID, float duration) : _tileID(tileID), _duration(duration) {}
|
2020-08-28 16:33:52 +08:00
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
|
TMXTileAnimInfo::TMXTileAnimInfo(uint32_t tileID) : _tileID(tileID) {}
|
2020-08-28 16:33:52 +08:00
|
|
|
|
|
|
|
|
|
TMXTileAnimInfo* TMXTileAnimInfo::create(uint32_t tileID)
|
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
|
TMXTileAnimInfo* ret = new TMXTileAnimInfo(tileID);
|
|
|
|
|
ret->autorelease();
|
|
|
|
|
return ret;
|
2020-08-28 16:33:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-20 15:04:53 +08:00
|
|
|
|
NS_CC_END
|