2014-06-06 16:15:46 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-06-06 16:15:46 +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.
|
|
|
|
****************************************************************************/
|
2014-08-28 17:03:29 +08:00
|
|
|
#include "2d/CCFastTMXTiledMap.h"
|
|
|
|
#include "2d/CCFastTMXLayer.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
2014-07-17 10:46:20 +08:00
|
|
|
namespace experimental {
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
// implementation FastTMXTiledMap
|
2014-06-06 16:15:46 +08:00
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
|
2014-06-06 16:15:46 +08:00
|
|
|
if (ret->initWithTMXFile(tmxFile))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
|
2014-06-06 16:15:46 +08:00
|
|
|
if (ret->initWithXML(tmxString, resourcePath))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT(tmxFile.size()>0, "FastTMXTiledMap: tmx file should not be empty");
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
setContentSize(Size::ZERO);
|
|
|
|
|
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
|
|
|
|
|
|
|
|
if (! mapInfo)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
|
2014-06-06 16:15:46 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
setContentSize(Size::ZERO);
|
|
|
|
|
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
|
|
|
|
|
2014-06-19 15:57:13 +08:00
|
|
|
CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
|
2014-06-06 16:15:46 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXTiledMap::TMXTiledMap()
|
2014-06-06 16:15:46 +08:00
|
|
|
:_mapSize(Size::ZERO)
|
|
|
|
,_tileSize(Size::ZERO)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXTiledMap::~TMXTiledMap()
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
|
2015-05-22 11:14:34 +08:00
|
|
|
if (tileset == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer *layer = TMXLayer::create(tileset, layerInfo, mapInfo);
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
// tell the layerinfo to release the ownership of the tiles map.
|
|
|
|
layerInfo->_ownTiles = false;
|
|
|
|
layer->setupTiles();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
Size size = layerInfo->_layerSize;
|
|
|
|
auto& tilesets = mapInfo->getTilesets();
|
2014-06-27 18:28:59 +08:00
|
|
|
|
2014-06-30 11:08:57 +08:00
|
|
|
for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-30 11:08:57 +08:00
|
|
|
TMXTilesetInfo* tilesetInfo = *iter;
|
|
|
|
if (tilesetInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
for( int y=0; y < size.height; y++ )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
for( int x=0; x < size.width; x++ )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
int pos = static_cast<int>(x + size.width * y);
|
|
|
|
int gid = layerInfo->_tiles[ pos ];
|
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: gid == 0 --> empty tile
|
2014-06-27 18:28:59 +08:00
|
|
|
if( gid != 0 )
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-27 18:28:59 +08:00
|
|
|
// Optimization: quick return
|
|
|
|
// if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later
|
2014-06-30 11:08:57 +08:00
|
|
|
if( (gid & kTMXFlippedMask) >= tilesetInfo->_firstGid )
|
|
|
|
return tilesetInfo;
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
2014-06-27 18:28:59 +08:00
|
|
|
}
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the tiles are 0, return empty tileset
|
|
|
|
CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
_mapSize = mapInfo->getMapSize();
|
|
|
|
_tileSize = mapInfo->getTileSize();
|
|
|
|
_mapOrientation = mapInfo->getOrientation();
|
|
|
|
|
|
|
|
_objectGroups = mapInfo->getObjectGroups();
|
|
|
|
|
|
|
|
_properties = mapInfo->getProperties();
|
|
|
|
|
|
|
|
_tileProperties = mapInfo->getTileProperties();
|
|
|
|
|
|
|
|
int idx=0;
|
|
|
|
|
|
|
|
auto& layers = mapInfo->getLayers();
|
|
|
|
for(const auto &layerInfo : layers) {
|
|
|
|
if (layerInfo->_visible)
|
|
|
|
{
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer *child = parseLayer(layerInfo, mapInfo);
|
2015-05-22 11:14:34 +08:00
|
|
|
if (child == nullptr) {
|
|
|
|
idx++;
|
|
|
|
continue;
|
|
|
|
}
|
2014-06-06 16:15:46 +08:00
|
|
|
addChild(child, idx, idx);
|
|
|
|
|
|
|
|
// update content size with the max size
|
|
|
|
const Size& childSize = child->getContentSize();
|
|
|
|
Size currentSize = this->getContentSize();
|
|
|
|
currentSize.width = std::max( currentSize.width, childSize.width );
|
|
|
|
currentSize.height = std::max( currentSize.height, childSize.height );
|
|
|
|
this->setContentSize(currentSize);
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
CCASSERT(layerName.size() > 0, "Invalid layer name!");
|
|
|
|
|
|
|
|
for (auto& child : _children)
|
|
|
|
{
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXLayer* layer = dynamic_cast<TMXLayer*>(child);
|
2014-06-06 16:15:46 +08:00
|
|
|
if(layer)
|
|
|
|
{
|
|
|
|
if(layerName.compare( layer->getLayerName()) == 0)
|
|
|
|
{
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// layer not found
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
CCASSERT(groupName.size() > 0, "Invalid group name!");
|
|
|
|
|
|
|
|
if (_objectGroups.size()>0)
|
|
|
|
{
|
|
|
|
TMXObjectGroup* objectGroup = nullptr;
|
|
|
|
for (auto iter = _objectGroups.cbegin(); iter != _objectGroups.cend(); ++iter)
|
|
|
|
{
|
|
|
|
objectGroup = *iter;
|
|
|
|
if (objectGroup && objectGroup->getGroupName() == groupName)
|
|
|
|
{
|
|
|
|
return objectGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectGroup not found
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
Value TMXTiledMap::getProperty(const std::string& propertyName) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
if (_properties.find(propertyName) != _properties.end())
|
|
|
|
return _properties.at(propertyName);
|
|
|
|
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
Value TMXTiledMap::getPropertiesForGID(int GID) const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
|
|
|
if (_tileProperties.find(GID) != _tileProperties.end())
|
|
|
|
return _tileProperties.at(GID);
|
|
|
|
|
|
|
|
return Value();
|
|
|
|
}
|
|
|
|
|
2014-07-17 14:09:53 +08:00
|
|
|
std::string TMXTiledMap::getDescription() const
|
2014-06-06 16:15:46 +08:00
|
|
|
{
|
2014-06-19 15:57:13 +08:00
|
|
|
return StringUtils::format("<FastTMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
|
2014-06-06 16:15:46 +08:00
|
|
|
}
|
|
|
|
|
2014-07-17 10:46:20 +08:00
|
|
|
} //end of namespace experimental
|
2014-06-06 16:15:46 +08:00
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|