fixed #71, complete CCTMXTiledMap

This commit is contained in:
Walzer 2010-08-31 08:48:42 +00:00
parent 19dcbc2f31
commit a0ec92f6c4
6 changed files with 393 additions and 31 deletions

View File

@ -460,6 +460,10 @@
RelativePath=".\include\CCTMXObjectGroup.h"
>
</File>
<File
RelativePath=".\include\CCTMXTiledMap.h"
>
</File>
<File
RelativePath=".\include\CCTMXXMLParser.h"
>
@ -784,6 +788,10 @@
RelativePath=".\tileMap_parallax_nodes\CCTMXObjectGroup.cpp"
>
</File>
<File
RelativePath=".\tileMap_parallax_nodes\CCTMXTiledMap.cpp"
>
</File>
<File
RelativePath=".\tileMap_parallax_nodes\CCTMXXMLParser.cpp"
>

View File

@ -0,0 +1,152 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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.
****************************************************************************/
#ifndef __CCTMX_TILE_MAP_H__
#define __CCTMX_TILE_MAP_H__
#include "CCNode.h"
#include "CCTMXObjectGroup.h"
namespace cocos2d {
class CCTMXObjectGroup;
class CCTMXLayer;
class CCTMXLayerInfo;
class CCTMXTilesetInfo;
class CCTMXMapInfo;
/** Possible oritentations of the TMX map */
enum
{
/** Orthogonal orientation */
CCTMXOrientationOrtho,
/** Hexagonal orientation */
CCTMXOrientationHex,
/** Isometric orientation */
CCTMXOrientationIso,
};
/** CCTMXTiledMap knows how to parse and render a TMX map.
It adds support for the TMX tiled map format used by http://www.mapeditor.org
It supports isometric, hexagonal and orthogonal tiles.
It also supports object groups, objects, and properties.
Features:
- Each tile will be treated as an CCSprite
- Each tile can be rotated / moved / scaled / tinted / "opacitied"
- Tiles can be added/removed in runtime
- The z-order of the tiles can be modified in runtime.
- Each tile has an anchorPoint of (0,0)
- The anchorPoint of the TMXTileMap is (0,0)
- The TMX layers will be added as a child
- The TMX layers will be aliased by default
- The tileset image will be loaded using the TextureMgr
- Each tile will have a unique tag
- Each tile will have a unique z value. top-left: z=1, bottom-right: z=max z
- Each object group will be treated as an NSMutableArray
- Objects can be created using your own classes or a generic object class which will contain all the properties in a dictionary
- Properties can be assigned to the Map, Layer, Object Group, and Object
Limitations:
- It only supports one tileset per layer.
- Embeded images are not supported
- It only supports the XML format (the JSON format is not supported)
Technical description:
Each layer is created using an TMXLayer (subclass of CCSpriteSheet). If you have 5 layers, then 5 TMXLayer will be created,
unless the layer visibility is off. In that case, the layer won't be created at all.
You can obtain the layers (TMXLayer objects) at runtime by:
- [map getChildByTag: tag_number]; // 0=1st layer, 1=2nd layer, 2=3rd layer, etc...
- [map layerNamed: name_of_the_layer];
Each object group is created using a TMXObjectGroup which is a subclass of NSMutableArray.
You can obtain the object groups at runtime by:
- [map objectGroupNamed: name_of_the_object_group];
Each object is a TMXObject.
Each property is stored as a key-value pair in an NSMutableDictionary.
You can obtain the properties at runtime by:
[map propertyNamed: name_of_the_property];
[layer propertyNamed: name_of_the_property];
[objectGroup propertyNamed: name_of_the_property];
[object propertyNamed: name_of_the_property];
@since v0.8.1
*/
class CCX_DLL CCTMXTiledMap : public CCNode
{
/** the map's size property measured in tiles */
CCX_SYNTHESIZE(CGSize, m_tMapSize, MapSize);
/** the tiles's size property measured in pixels */
CCX_SYNTHESIZE(CGSize, m_tTileSize, TileSize);
/** map orientation */
CCX_SYNTHESIZE(int, m_nMapOrientation, MapOrientation);
/** object groups */
CCX_SYNTHESIZE(NSMutableArray<CCTMXObjectGroup*>*, m_pObjectGroups, ObjectGroups);
/** properties */
CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties);
public:
CCTMXTiledMap();
virtual ~CCTMXTiledMap();
/** creates a TMX Tiled Map with a TMX file.*/
static CCTMXTiledMap * tiledMapWithTMXFile(const char *tmxFile);
/** initializes a TMX Tiled Map with a TMX file */
bool initWithTMXFile(const char *tmxFile);
/** return the TMXLayer for the specific layer */
CCTMXLayer* layerNamed(const char *layerName);
/** return the TMXObjectGroup for the secific group */
CCTMXObjectGroup* objectGroupNamed(const char *groupName);
/** return the TMXObjectGroup for the secific group
@deprecated Use map#objectGroupNamed instead
*/
CCTMXObjectGroup* groupNamed(const char *groupName);
/** return the value for the specific property name */
const char *propertyNamed(const char *propertyName);
/** return properties dictionary for tile GID */
StringToStringDictionary *propertiesForGID(int GID);
private:
CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
CCTMXTilesetInfo * tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
protected:
// tile properties
std::map<int, StringToStringDictionary*> *m_pTileProperties;
};
}// namespace cocos2d
#endif //__CCTMX_TILE_MAP_H__

View File

@ -143,6 +143,8 @@ namespace cocos2d {
CCX_SYNTHESIZE(std::string, m_sCurrentString, CurrentString);
// is stroing characters?
CCX_SYNTHESIZE(bool, m_bStoringCharacters, StoringCharacters);
// properties
CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties);
public:
CCTMXMapInfo(){}
virtual ~CCTMXMapInfo();
@ -153,13 +155,9 @@ namespace cocos2d {
/* initalises parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file */
bool parseXMLFile(const char *xmlFilename);
inline StringToStringDictionary* getProperties(){return m_pProperties;}
inline void setProperties(StringToStringDictionary * var){m_pProperties = var;}
inline std::map<int, StringToStringDictionary*>* getTileProperties(){return m_pTileProperties;}
inline void setTileProperties(std::map<int, StringToStringDictionary*> * var){m_pTileProperties = var;}
protected:
// properties
StringToStringDictionary* m_pProperties;
// tile properties
std::map<int, StringToStringDictionary*>* m_pTileProperties;
};

View File

@ -21,11 +21,10 @@ 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 "CCLayer.h"
#include "CCTMXLayer.h"
#include "CCTMXXMLParser.h"
#include "CCTMXTiledMap.h"
#include "CCSprite.h"
#include "CCSpriteSheet.h"
#include "CCTextureCache.h"
#include "CGPointExtension.h"
@ -510,18 +509,6 @@ namespace cocos2d {
}
}
/** Possible oritentations of the TMX map */
enum
{
/** Orthogonal orientation */
CCTMXOrientationOrtho,
/** Hexagonal orientation */
CCTMXOrientationHex,
/** Isometric orientation */
CCTMXOrientationIso,
};/// @todo to be deleted
//CCTMXLayer - obtaining positions, offset
CGPoint CCTMXLayer::calculateLayerOffset(CGPoint pos)
{

View File

@ -0,0 +1,229 @@
/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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"
#include "CCSprite.h"
#include "CGPointExtension.h"
namespace cocos2d{
// implementation CCTMXTiledMap
CCTMXTiledMap * CCTMXTiledMap::tiledMapWithTMXFile(const char *tmxFile)
{
CCTMXTiledMap *pRet = new CCTMXTiledMap();
if (pRet->initWithTMXFile(tmxFile))
{
pRet->autorelease();
return pRet;
}
return NULL;
}
bool CCTMXTiledMap::initWithTMXFile(const char *tmxFile)
{
NSAssert(tmxFile != NULL && strlen(tmxFile)>0, "TMXTiledMap: tmx file should not bi nil");
setContentSize(CGSizeZero);
CCTMXMapInfo *mapInfo = CCTMXMapInfo::formatWithTMXFile(tmxFile);
NSAssert( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename.");
m_tMapSize = mapInfo->getMapSize();
m_tTileSize = mapInfo->getTileSize();
m_nMapOrientation = mapInfo->getOrientation();
m_pObjectGroups = mapInfo->getObjectGroups();
m_pObjectGroups->retain();
m_pProperties = mapInfo->getProperties();
m_pTileProperties = mapInfo->getTileProperties();
int idx = 0;
if (mapInfo->getLayers() && mapInfo->getLayers()->count()>0)
{
CCTMXLayerInfo *layerInfo = NULL;
NSMutableArray<CCTMXLayerInfo*>::NSMutableArrayIterator rit;
for (rit = mapInfo->getLayers()->begin(); rit != mapInfo->getLayers()->end(); ++rit)
{
layerInfo = *rit;
if (!layerInfo && layerInfo->m_bVisible)
{
CCTMXLayer *child = parseLayer(layerInfo, mapInfo);
addChild((CCNode*)child, idx, idx);
// update content size with the max size
CGSize childSize = child->getContentSize();
CGSize currentSize = this->getContentSize();
currentSize.width = MAX( currentSize.width, childSize.width );
currentSize.height = MAX( currentSize.height, childSize.height );
this->setContentSize(currentSize);
idx++;
}
}
}
return true;
}
CCTMXTiledMap::CCTMXTiledMap()
{
}
CCTMXTiledMap::~CCTMXTiledMap()
{
m_pObjectGroups->release();
if (m_pProperties)
{
m_pProperties->clear();
delete m_pProperties;
}
if (m_pTileProperties)
{
m_pTileProperties->clear();
delete m_pTileProperties;
}
}
// private
CCTMXLayer * CCTMXTiledMap::parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo)
{
CCTMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
CCTMXLayer *layer = CCTMXLayer::layerWithTilesetInfo(tileset, layerInfo, mapInfo);
// tell the layerinfo to release the ownership of the tiles map.
layerInfo->m_bOwnTiles = false;
layer->setupTiles();
return layer;
}
CCTMXTilesetInfo * CCTMXTiledMap::tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo)
{
CCTMXTilesetInfo *tileset = NULL;
//CFByteOrder o = CFByteOrderGetCurrent();
CGSize size = layerInfo->m_tLayerSize;
if (mapInfo->getTilesets() && mapInfo->getTilesets()->count()>0)
{
CCTMXTilesetInfo *tileset = NULL;
NSMutableArray<CCTMXTilesetInfo*>::NSMutableArrayRevIterator it;
for (it = mapInfo->getTilesets()->rbegin(); it != mapInfo->getTilesets()->rend(); ++it)
{
tileset = *it;
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);
unsigned int gid = layerInfo->m_pTiles[ pos ];
// 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 assert will be thrown later
if( gid >= tileset->m_uFirstGid )
return tileset;
}
}
}
}
}
}
// If all the tiles are 0, return empty tileset
CCLOG("cocos2d: Warning: TMX Layer '%@' has no tiles", layerInfo.name);
return tileset;
}
// public
CCTMXLayer * CCTMXTiledMap::layerNamed(const char *layerName)
{
std::string sLayerName = layerName;
if (m_pChildren && m_pChildren->count()>0)
{
CCTMXLayer *layer;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
{
layer = (CCTMXLayer*)(*it);
if (layer && layer->getLayerName() == sLayerName)
{
return layer;
}
}
}
// layer not found
return NULL;
}
CCTMXObjectGroup * CCTMXTiledMap::objectGroupNamed(const char *groupName)
{
std::string sGroupName = groupName;
if (m_pChildren && m_pChildren->count()>0)
{
CCTMXObjectGroup *objectGroup;
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
{
objectGroup = (CCTMXObjectGroup*)(*it);
if (objectGroup && objectGroup->getGroupName() == sGroupName)
{
return objectGroup;
}
}
}
// objectGroup not found
return NULL;
}
// XXX deprecated
CCTMXObjectGroup * CCTMXTiledMap::groupNamed(const char *groupName)
{
return objectGroupNamed(groupName);
}
const char * CCTMXTiledMap::propertyNamed(const char *propertyName)
{
return valueForKey(propertyName, m_pProperties);
}
StringToStringDictionary * CCTMXTiledMap::propertiesForGID(int GID)
{
std::map<int, StringToStringDictionary*>::iterator it;
it = m_pTileProperties->find(GID);
return it!=m_pTileProperties->end() ? it->second : NULL;
}
}// namespace cocos2d

View File

@ -25,6 +25,7 @@ THE SOFTWARE.
#include <libxml/tree.h>
#include <libxml/xmlmemory.h>
#include "CCTMXXMLParser.h"
#include "CCTMXTiledMap.h"
#include "ccMacros.h"
#include "CCXFileUtils.h"
#include "CGPointExtension.h"
@ -164,19 +165,6 @@ namespace cocos2d {
return true;
}
/** Possible oritentations of the TMX map */
enum
{
/** Orthogonal orientation */
CCTMXOrientationOrtho,
/** Hexagonal orientation */
CCTMXOrientationHex,
/** Isometric orientation */
CCTMXOrientationIso,
};/// @todo to be deleted
// the XML parser calls here with all the elements
void tmx_startElement(void *ctx, const xmlChar *name, const xmlChar **atts)