2012-04-19 14:35:52 +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) 2009-2010 Ricardo Quesada
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCTMXTiledMap.h"
|
|
|
|
#include "CCTMXXMLParser.h"
|
|
|
|
#include "CCTMXLayer.h"
|
2013-10-14 14:01:00 +08:00
|
|
|
#include "CCSprite.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation TMXTiledMap
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXTiledMap *ret = new TMXTiledMap();
|
|
|
|
if (ret->initWithTMXFile(tmxFile))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-07 09:05:13 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXTiledMap *ret = new TMXTiledMap();
|
|
|
|
if (ret->initWithXML(tmxString, resourcePath))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-11-07 09:05:13 +08:00
|
|
|
CC_SAFE_DELETE(ret);
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CCASSERT(tmxFile.size()>0, "TMXTiledMap: tmx file should not be empty");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-12 14:47:36 +08:00
|
|
|
setContentSize(Size::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! mapInfo)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename.");
|
2012-04-19 14:35:52 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
setContentSize(Size::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 18:26:26 +08:00
|
|
|
TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename.");
|
2012-04-19 14:35:52 +08:00
|
|
|
buildWithMapInfo(mapInfo);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTiledMap::TMXTiledMap()
|
2013-07-12 14:47:36 +08:00
|
|
|
:_mapSize(Size::ZERO)
|
|
|
|
,_tileSize(Size::ZERO)
|
2013-06-15 14:03:30 +08:00
|
|
|
,_objectGroups(NULL)
|
|
|
|
,_properties(NULL)
|
|
|
|
,_tileProperties(NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTiledMap::~TMXTiledMap()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
CC_SAFE_RELEASE(_objectGroups);
|
|
|
|
CC_SAFE_RELEASE(_tileProperties);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
|
|
|
|
TMXLayer *layer = TMXLayer::create(tileset, layerInfo, mapInfo);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tell the layerinfo to release the ownership of the tiles map.
|
2013-06-15 14:03:30 +08:00
|
|
|
layerInfo->_ownTiles = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
layer->setupTiles();
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Size size = layerInfo->_layerSize;
|
|
|
|
Array* tilesets = mapInfo->getTilesets();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (tilesets && tilesets->count()>0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXTilesetInfo* tileset = NULL;
|
|
|
|
Object* pObj = NULL;
|
2012-06-05 10:51:53 +08:00
|
|
|
CCARRAY_FOREACH_REVERSE(tilesets, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
tileset = static_cast<TMXTilesetInfo*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (tileset)
|
|
|
|
{
|
|
|
|
for( unsigned int y=0; y < size.height; y++ )
|
|
|
|
{
|
|
|
|
for( unsigned int x=0; x < size.width; x++ )
|
|
|
|
{
|
|
|
|
unsigned int pos = (unsigned int)(x + size.width * y);
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int gid = layerInfo->_tiles[ pos ];
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// gid are stored in little endian.
|
|
|
|
// if host is big endian, then swap
|
|
|
|
//if( o == CFByteOrderBigEndian )
|
|
|
|
// gid = CFSwapInt32( gid );
|
|
|
|
/* We support little endian.*/
|
|
|
|
|
|
|
|
// XXX: gid == 0 --> empty tile
|
|
|
|
if( gid != 0 )
|
|
|
|
{
|
|
|
|
// Optimization: quick return
|
|
|
|
// if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later
|
2013-06-20 14:13:12 +08:00
|
|
|
if( (gid & kFlippedMask) >= tileset->_firstGid )
|
2012-04-19 14:35:52 +08:00
|
|
|
return tileset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all the tiles are 0, return empty tileset
|
2013-06-15 14:03:30 +08:00
|
|
|
CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_mapSize = mapInfo->getMapSize();
|
|
|
|
_tileSize = mapInfo->getTileSize();
|
|
|
|
_mapOrientation = mapInfo->getOrientation();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_objectGroups);
|
|
|
|
_objectGroups = mapInfo->getObjectGroups();
|
|
|
|
CC_SAFE_RETAIN(_objectGroups);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_properties);
|
|
|
|
_properties = mapInfo->getProperties();
|
|
|
|
CC_SAFE_RETAIN(_properties);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_tileProperties);
|
|
|
|
_tileProperties = mapInfo->getTileProperties();
|
|
|
|
CC_SAFE_RETAIN(_tileProperties);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int idx=0;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* layers = mapInfo->getLayers();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (layers && layers->count()>0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayerInfo* layerInfo = NULL;
|
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(layers, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
layerInfo = static_cast<TMXLayerInfo*>(pObj);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (layerInfo && layerInfo->_visible)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer *child = parseLayer(layerInfo, mapInfo);
|
|
|
|
addChild((Node*)child, idx, idx);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update content size with the max size
|
2013-06-20 14:13:12 +08:00
|
|
|
const Size& childSize = child->getContentSize();
|
|
|
|
Size currentSize = this->getContentSize();
|
2013-11-07 09:05:13 +08:00
|
|
|
currentSize.width = std::max( currentSize.width, childSize.width );
|
|
|
|
currentSize.height = std::max( currentSize.height, childSize.height );
|
2012-04-19 14:35:52 +08:00
|
|
|
this->setContentSize(currentSize);
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// public
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CCASSERT(layerName.size() > 0, "Invalid layer name!");
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXLayer* layer = dynamic_cast<TMXLayer*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
if(layer)
|
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
if(layerName.compare( layer->getLayerName()) == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// layer not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-11-07 09:05:13 +08:00
|
|
|
CCASSERT(groupName.size() > 0, "Invalid group name!");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_objectGroups && _objectGroups->count()>0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
TMXObjectGroup* objectGroup = NULL;
|
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_objectGroups, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
objectGroup = static_cast<TMXObjectGroup*>(pObj);
|
2013-11-07 09:05:13 +08:00
|
|
|
if (objectGroup && objectGroup->getGroupName() == groupName)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return objectGroup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// objectGroup not found
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-11-07 09:05:13 +08:00
|
|
|
String* TMXTiledMap::getProperty(const std::string& propertyName) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-23 22:07:33 +08:00
|
|
|
return static_cast<String*>(_properties->objectForKey(propertyName));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-23 22:07:33 +08:00
|
|
|
Dictionary* TMXTiledMap::getPropertiesForGID(int GID) const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-23 22:07:33 +08:00
|
|
|
return static_cast<Dictionary*>(_tileProperties->objectForKey(GID));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|