axmol/core/2d/CCTMXXMLParser.cpp

832 lines
28 KiB
C++
Raw Normal View History

2010-12-28 10:23:14 +08:00
/****************************************************************************
2021-12-25 10:04:45 +08:00
Copyright (c) 2011 Максим Аксенов
Copyright (c) 2009-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2010-12-28 10:23:14 +08:00
2022-08-08 18:02:17 +08:00
https://axys1.github.io/
2010-12-28 10:23:14 +08:00
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.
****************************************************************************/
2014-08-28 17:03:29 +08:00
#include "2d/CCTMXXMLParser.h"
#include <unordered_map>
2013-01-30 06:45:41 +08:00
#include <sstream>
// #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
using namespace std;
NS_AX_BEGIN
2010-12-28 10:23:14 +08:00
// implementation TMXLayerInfo
2021-12-25 10:04:45 +08:00
TMXLayerInfo::TMXLayerInfo() : _name(""), _tiles(nullptr), _ownTiles(true) {}
TMXLayerInfo::~TMXLayerInfo()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing TMXLayerInfo: %p", this);
if (_ownTiles && _tiles)
{
free(_tiles);
_tiles = nullptr;
}
}
ValueMap& TMXLayerInfo::getProperties()
{
return _properties;
}
void TMXLayerInfo::setProperties(ValueMap var)
{
_properties = var;
}
// implementation TMXTilesetInfo
2021-12-25 10:04:45 +08:00
TMXTilesetInfo::TMXTilesetInfo() : _firstGid(0), _tileSize(Vec2::ZERO), _spacing(0), _margin(0), _imageSize(Vec2::ZERO)
{}
TMXTilesetInfo::~TMXTilesetInfo()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing TMXTilesetInfo: %p", this);
}
2014-02-08 10:33:30 +08:00
Rect TMXTilesetInfo::getRectForGID(uint32_t gid)
{
Rect rect;
rect.size = _tileSize;
2014-02-08 10:33:30 +08:00
gid &= kTMXFlippedMask;
gid = gid - _firstGid;
// max_x means the column count in tile map
// in the origin:
// max_x = (int)((_imageSize.width - _margin*2 + _spacing) / (_tileSize.width + _spacing));
// but in editor "Tiled", _margin variable only effect the left side
// for compatible with "Tiled", change the max_x calculation
int max_x = (int)((_imageSize.width - _margin + _spacing) / (_tileSize.width + _spacing));
2021-12-25 10:04:45 +08:00
rect.origin.x = (gid % max_x) * (_tileSize.width + _spacing) + _margin;
rect.origin.y = (gid / max_x) * (_tileSize.height + _spacing) + _margin;
return rect;
}
// implementation TMXMapInfo
2021-12-26 23:26:34 +08:00
TMXMapInfo* TMXMapInfo::create(std::string_view tmxFile)
{
2021-12-25 10:04:45 +08:00
TMXMapInfo* ret = new TMXMapInfo();
if (ret->initWithTMXFile(tmxFile))
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
return nullptr;
}
2021-12-26 23:26:34 +08:00
TMXMapInfo* TMXMapInfo::createWithXML(std::string_view tmxString, std::string_view resourcePath)
{
2021-12-25 10:04:45 +08:00
TMXMapInfo* ret = new TMXMapInfo();
if (ret->initWithXML(tmxString, resourcePath))
{
ret->autorelease();
return ret;
}
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
return nullptr;
}
2021-12-26 23:26:34 +08:00
void TMXMapInfo::internalInit(std::string_view tmxFileName, std::string_view resourcePath)
{
if (!tmxFileName.empty())
{
_TMXFileName = FileUtils::getInstance()->fullPathForFilename(tmxFileName);
}
2021-12-25 10:04:45 +08:00
if (!resourcePath.empty())
{
_resources = resourcePath;
}
2021-12-25 10:04:45 +08:00
_objectGroups.reserve(4);
// tmp vars
2021-12-25 10:04:45 +08:00
_currentString = "";
_storingCharacters = false;
2021-12-25 10:04:45 +08:00
_layerAttribs = TMXLayerAttribNone;
_parentElement = TMXPropertyNone;
_currentFirstGID = -1;
}
2021-12-26 23:26:34 +08:00
bool TMXMapInfo::initWithXML(std::string_view tmxString, std::string_view resourcePath)
{
internalInit("", resourcePath);
return parseXMLString(tmxString);
}
2021-12-26 23:26:34 +08:00
bool TMXMapInfo::initWithTMXFile(std::string_view tmxFile)
{
internalInit(tmxFile, "");
return parseXMLFile(_TMXFileName);
}
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)
{}
TMXMapInfo::~TMXMapInfo()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing TMXMapInfo: %p", this);
}
2021-12-26 23:26:34 +08:00
bool TMXMapInfo::parseXMLString(std::string_view xmlString)
{
size_t len = xmlString.size();
if (len <= 0)
return false;
SAXParser parser;
2021-12-25 10:04:45 +08:00
if (false == parser.init("UTF-8"))
{
return false;
}
parser.setDelegator(this);
2021-12-26 23:26:34 +08:00
return parser.parse(xmlString.data(), len);
}
2021-12-26 23:26:34 +08:00
bool TMXMapInfo::parseXMLFile(std::string_view xmlFilename)
{
SAXParser parser;
2021-12-25 10:04:45 +08:00
if (false == parser.init("UTF-8"))
{
return false;
}
2021-12-25 10:04:45 +08:00
parser.setDelegator(this);
return parser.parse(FileUtils::getInstance()->fullPathForFilename(xmlFilename));
}
2010-08-27 18:07:37 +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;
ValueMap attributeDict;
if (atts && atts[0])
{
for (int i = 0; atts[i]; i += 2)
{
2021-12-25 10:04:45 +08:00
std::string key = atts[i];
std::string value = atts[i + 1];
attributeDict.emplace(key, Value(value));
}
}
if (elementName == "map")
{
2020-08-18 11:33:18 +08:00
std::string version = attributeDict["version"].asString();
2022-07-16 10:43:05 +08:00
AXLOG("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")
{
tmxMapInfo->setOrientation(TMXOrientationOrtho);
}
2021-12-25 10:04:45 +08:00
else if (orientationStr == "isometric")
{
tmxMapInfo->setOrientation(TMXOrientationIso);
}
2021-12-25 10:04:45 +08:00
else if (orientationStr == "hexagonal")
{
tmxMapInfo->setOrientation(TMXOrientationHex);
}
2021-12-25 10:04:45 +08:00
else if (orientationStr == "staggered")
{
tmxMapInfo->setOrientation(TMXOrientationStaggered);
}
2021-12-25 10:04:45 +08:00
else
{
2022-07-16 10:43:05 +08:00
AXLOG("cocos2d: TMXFomat: Unsupported orientation: %d", tmxMapInfo->getOrientation());
}
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")
{
tmxMapInfo->setStaggerAxis(TMXStaggerAxis_X);
}
2021-12-25 10:04:45 +08:00
else if (staggerAxisStr == "y")
{
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")
{
tmxMapInfo->setStaggerIndex(TMXStaggerIndex_Odd);
}
2021-12-25 10:04:45 +08:00
else if (staggerIndex == "even")
{
tmxMapInfo->setStaggerIndex(TMXStaggerIndex_Even);
}
2021-12-26 23:26:34 +08:00
auto hexSideLength = attributeDict["hexsidelength"].asInt();
tmxMapInfo->setHexSideLength(hexSideLength);
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();
tmxMapInfo->setMapSize(s);
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();
tmxMapInfo->setTileSize(s);
// The parent element is now "map"
tmxMapInfo->setParentElement(TMXPropertyMap);
2021-12-25 10:04:45 +08:00
}
else if (elementName == "tileset")
{
// If this is an external tileset then start parsing that
2020-08-18 11:33:18 +08:00
std::string externalTilesetFilename = attributeDict["source"].asString();
if (externalTilesetFilename != "")
{
_externalTilesetFilename = externalTilesetFilename;
// Tileset file will be relative to the map file. So we need to convert it to an absolute path
if (_TMXFileName.find_last_of('/') != string::npos)
{
2021-12-25 10:04:45 +08:00
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
externalTilesetFilename = dir + externalTilesetFilename;
}
2021-12-25 10:04:45 +08:00
else
{
externalTilesetFilename = _resources + "/" + externalTilesetFilename;
}
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();
if (_currentFirstGID < 0)
{
_currentFirstGID = 0;
}
_recordFirstGID = false;
2021-12-25 10:04:45 +08:00
tmxMapInfo->parseXMLFile(externalTilesetFilename);
}
else
{
2021-12-25 10:04:45 +08:00
TMXTilesetInfo* tileset = new TMXTilesetInfo();
tileset->_name = attributeDict["name"].asString();
if (_recordFirstGID)
{
// 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
if (tileset->_firstGid < 0)
{
tileset->_firstGid = 0;
}
}
else
{
tileset->_firstGid = _currentFirstGID;
2021-12-25 10:04:45 +08:00
_currentFirstGID = 0;
}
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();
tileset->_tileSize = s;
tmxMapInfo->getTilesets().pushBack(tileset);
tileset->release();
}
}
else if (elementName == "tile")
{
if (tmxMapInfo->getParentElement() == TMXPropertyLayer)
2013-06-06 02:29:52 +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
if (_xmlTileIndex < tilesAmount)
2013-07-04 19:28:54 +08:00
{
layer->_tiles[_xmlTileIndex++] = gid;
}
2013-06-06 02:29:52 +08:00
}
else
{
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
2020-08-18 11:33:18 +08:00
tmxMapInfo->setParentGID(info->_firstGid + attributeDict["id"].asInt());
tmxMapInfo->getTileProperties()[tmxMapInfo->getParentGID()] = Value(ValueMap());
tmxMapInfo->setParentElement(TMXPropertyTile);
2013-06-06 02:29:52 +08:00
}
}
else if (elementName == "layer")
{
2021-12-25 10:04:45 +08:00
TMXLayerInfo* layer = new TMXLayerInfo();
layer->_name = attributeDict["name"].asString();
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();
layer->_layerSize = s;
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();
Value& opacityValue = attributeDict["opacity"];
2021-12-25 10:04:45 +08:00
layer->_opacity = opacityValue.isNull() ? 255 : (unsigned char)(255.0f * opacityValue.asFloat());
2020-08-18 11:33:18 +08:00
float x = attributeDict["x"].asFloat();
float y = attributeDict["y"].asFloat();
layer->_offset.set(x, y);
tmxMapInfo->getLayers().pushBack(layer);
layer->release();
// The parent element is now "layer"
tmxMapInfo->setParentElement(TMXPropertyLayer);
2021-12-25 10:04:45 +08:00
}
else if (elementName == "objectgroup")
{
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());
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
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;
objectGroup->setPositionOffset(positionOffset);
tmxMapInfo->getObjectGroups().pushBack(objectGroup);
objectGroup->release();
// The parent element is now "objectgroup"
tmxMapInfo->setParentElement(TMXPropertyObjectGroup);
}
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
tileset->_tileOffset = Vec2(tileOffsetX, tileOffsetY);
}
else if (elementName == "image")
{
TMXTilesetInfo* tileset = tmxMapInfo->getTilesets().back();
// build full path
2021-12-25 10:04:45 +08:00
std::string imagename = attributeDict["source"].asString();
tileset->_originSourceImage = imagename;
if (_TMXFileName.find_last_of('/') != string::npos)
{
2021-12-25 10:04:45 +08:00
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
tileset->_sourceImage = dir + imagename;
}
2021-12-25 10:04:45 +08:00
else
{
tileset->_sourceImage = _resources + (_resources.size() ? "/" : "") + imagename;
}
2021-12-25 10:04:45 +08:00
}
else if (elementName == "data")
{
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();
2013-06-06 02:29:52 +08:00
if (encoding == "")
{
tmxMapInfo->setLayerAttribs(tmxMapInfo->getLayerAttribs() | TMXLayerAttribNone);
2021-12-25 10:04:45 +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));
// 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")
{
int layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribBase64);
tmxMapInfo->setStoringCharacters(true);
if (compression == "gzip")
{
layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribGzip);
2021-12-25 10:04:45 +08:00
}
else if (compression == "zlib")
{
layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib);
}
2022-07-16 10:43:05 +08:00
AXASSERT(compression == "" || compression == "gzip" || compression == "zlib",
2021-12-25 10:04:45 +08:00
"TMX: unsupported compression method");
}
2016-04-13 03:51:09 +08:00
else if (encoding == "csv")
{
int layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribCSV);
tmxMapInfo->setStoringCharacters(true);
}
}
else if (elementName == "object")
{
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
// The value for "type" was blank or not a valid class name
// Create an instance of TMXObjectInfo to store the object and its properties
ValueMap dict;
// Parse everything automatically
const char* keys[] = {"name", "type", "width", "height", "gid", "id"};
2021-12-25 10:04:45 +08:00
for (const auto& key : keys)
{
Value value = attributeDict[key];
2021-12-25 10:04:45 +08:00
dict[key] = value;
}
// But X and Y since they need special treatment
// X
2020-08-18 11:33:18 +08:00
int x = attributeDict["x"].asInt();
// 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());
2022-07-15 19:17:01 +08:00
p = AX_POINT_PIXELS_TO_POINTS(p);
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);
2022-07-15 19:17:01 +08:00
s = AX_SIZE_PIXELS_TO_POINTS(s);
2021-12-25 10:04:45 +08:00
dict["width"] = Value(s.width);
dict["height"] = Value(s.height);
2020-08-18 11:33:18 +08:00
dict["rotation"] = attributeDict["rotation"].asDouble();
// Add the object to the objectGroup
objectGroup->getObjects().emplace_back(Value(dict));
// The parent element is now "object"
tmxMapInfo->setParentElement(TMXPropertyObject);
2021-12-25 10:04:45 +08:00
}
else if (elementName == "property")
{
2021-12-25 10:04:45 +08:00
if (tmxMapInfo->getParentElement() == TMXPropertyNone)
{
2022-07-16 10:43:05 +08:00
AXLOG("TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'",
2021-12-25 10:04:45 +08:00
attributeDict["name"].asString().c_str(), attributeDict["value"].asString().c_str());
}
else if (tmxMapInfo->getParentElement() == TMXPropertyMap)
{
// 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();
tmxMapInfo->getProperties().emplace(key, value);
}
2021-12-25 10:04:45 +08:00
else if (tmxMapInfo->getParentElement() == TMXPropertyLayer)
{
// The parent element is the last layer
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
2021-12-25 10:04:45 +08:00
Value value = attributeDict["value"];
std::string key = attributeDict["name"].asString();
// Add the property to the layer
layer->getProperties().emplace(key, value);
}
2021-12-25 10:04:45 +08:00
else if (tmxMapInfo->getParentElement() == TMXPropertyObjectGroup)
{
// The parent element is the last object group
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
2021-12-25 10:04:45 +08:00
Value value = attributeDict["value"];
std::string key = attributeDict["name"].asString();
objectGroup->getProperties().emplace(key, value);
}
2021-12-25 10:04:45 +08:00
else if (tmxMapInfo->getParentElement() == TMXPropertyObject)
{
// The parent element is the last object
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
2021-12-25 10:04:45 +08:00
ValueMap& dict = objectGroup->getObjects().rbegin()->asValueMap();
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"];
}
2021-12-25 10:04:45 +08:00
else if (tmxMapInfo->getParentElement() == TMXPropertyTile)
{
ValueMap& dict = tmxMapInfo->getTileProperties().at(tmxMapInfo->getParentGID()).asValueMap();
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"];
}
}
2021-12-25 10:04:45 +08:00
else if (elementName == "polygon")
{
// find parent object's dict and add polygon-points to it
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();
if (!value.empty())
2013-01-30 06:45:41 +08:00
{
ValueVector pointsArray;
pointsArray.reserve(10);
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, ' '))
2013-01-30 06:45:41 +08:00
{
// parse each point combo into a comma-separated x,y point
stringstream pointStream(pointPair);
string xStr, yStr;
2021-12-25 10:04:45 +08:00
ValueMap pointDict;
2013-01-30 06:45:41 +08:00
// set x
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;
pointDict["x"] = Value(x);
2013-01-30 06:45:41 +08:00
}
// set y
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;
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
pointsArray.emplace_back(Value(pointDict));
2013-01-30 06:45:41 +08:00
}
2021-12-25 10:04:45 +08:00
dict["points"] = Value(pointsArray);
2013-01-30 06:45:41 +08:00
}
2021-12-25 10:04:45 +08:00
}
else if (elementName == "polyline")
{
// find parent object's dict and add polyline-points to it
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();
if (!value.empty())
2013-09-12 07:36:53 +08:00
{
ValueVector pointsArray;
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;
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);
string xStr, yStr;
2021-12-25 10:04:45 +08:00
ValueMap pointDict;
2021-12-25 10:04:45 +08:00
2013-09-12 07:36:53 +08:00
// set x
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;
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
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;
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
pointsArray.emplace_back(Value(pointDict));
2013-09-12 07:36:53 +08:00
}
2021-12-25 10:04:45 +08:00
dict["polylinePoints"] = Value(pointsArray);
2013-09-12 07:36:53 +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());
// 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()));
}
}
2010-12-28 10:23:14 +08:00
2021-12-25 10:04:45 +08:00
void TMXMapInfo::endElement(void* /*ctx*/, const char* name)
{
2021-12-25 10:04:45 +08:00
TMXMapInfo* tmxMapInfo = this;
2015-03-11 22:39:04 +08:00
std::string elementName = name;
if (elementName == "data")
{
if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribBase64)
{
tmxMapInfo->setStoringCharacters(false);
2021-12-25 10:04:45 +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);
if (!buffer)
{
2022-07-16 10:43:05 +08:00
AXLOG("cocos2d: TiledMap: decode data error");
return;
}
2021-12-25 10:04:45 +08:00
if (tmxMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib))
{
2021-12-25 10:04:45 +08:00
unsigned char* deflated = nullptr;
Vec2 s = layer->_layerSize;
// 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
2022-07-15 19:17:01 +08:00
ssize_t AX_UNUSED inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint);
2022-07-16 10:43:05 +08:00
AXASSERT(inflatedLen == sizeHint, "inflatedLen should be equal to sizeHint!");
2021-12-25 10:04:45 +08:00
free(buffer);
buffer = nullptr;
2021-12-25 10:04:45 +08:00
if (!deflated)
{
2022-07-16 10:43:05 +08:00
AXLOG("cocos2d: TiledMap: inflate data error");
return;
}
2021-12-25 10:04:45 +08:00
layer->_tiles = reinterpret_cast<uint32_t*>(deflated);
}
else
{
layer->_tiles = reinterpret_cast<uint32_t*>(buffer);
}
2021-12-25 10:04:45 +08:00
tmxMapInfo->setCurrentString("");
}
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, ','))
{
gidTokens.emplace_back(sGID);
2016-04-13 03:51:09 +08:00
}
}
// 32-bits per gid
buffer = (unsigned char*)malloc(gidTokens.size() * 4);
if (!buffer)
{
2022-07-16 10:43:05 +08:00
AXLOG("cocos2d: TiledMap: CSV buffer not allocated.");
2016-04-13 03:51:09 +08:00
return;
}
uint32_t* bufferPtr = reinterpret_cast<uint32_t*>(buffer);
2021-12-25 10:04:45 +08:00
for (const auto& gidToken : gidTokens)
{
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("");
}
else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribNone)
{
_xmlTileIndex = 0;
}
}
else if (elementName == "map")
{
// The map element has ended
tmxMapInfo->setParentElement(TMXPropertyNone);
2021-12-25 10:04:45 +08:00
}
else if (elementName == "layer")
{
// The layer element has ended
tmxMapInfo->setParentElement(TMXPropertyNone);
}
else if (elementName == "objectgroup")
{
// The objectgroup element has ended
tmxMapInfo->setParentElement(TMXPropertyNone);
2021-12-25 10:04:45 +08:00
}
else if (elementName == "object")
{
// The object element has ended
tmxMapInfo->setParentElement(TMXPropertyNone);
}
else if (elementName == "tileset")
{
_recordFirstGID = true;
}
else if (elementName == "animation")
{
tmxMapInfo->setParentElement(TMXPropertyNone);
}
}
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)
{
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);
if (tmxMapInfo->isStoringCharacters())
{
2021-12-26 23:26:34 +08:00
std::string currentString{tmxMapInfo->getCurrentString()};
currentString += text;
tmxMapInfo->setCurrentString(currentString);
}
}
2021-12-25 10:04:45 +08:00
TMXTileAnimFrame::TMXTileAnimFrame(uint32_t tileID, float duration) : _tileID(tileID), _duration(duration) {}
2021-12-25 10:04:45 +08:00
TMXTileAnimInfo::TMXTileAnimInfo(uint32_t tileID) : _tileID(tileID) {}
TMXTileAnimInfo* TMXTileAnimInfo::create(uint32_t tileID)
{
2021-12-08 00:11:53 +08:00
TMXTileAnimInfo* ret = new TMXTileAnimInfo(tileID);
ret->autorelease();
return ret;
}
NS_AX_END