From 1c1971255e61dcb373dda61cb946e164bd61fa3a Mon Sep 17 00:00:00 2001 From: Walzer Date: Mon, 6 Sep 2010 12:26:19 +0000 Subject: [PATCH] fixed #129, use NSString to make dictionary --- cocos2dx/include/CCTMXLayer.h | 16 +- cocos2dx/include/CCTMXObjectGroup.h | 30 ++-- cocos2dx/include/CCTMXTiledMap.h | 8 +- cocos2dx/include/CCTMXXMLParser.h | 34 +++-- cocos2dx/include/NSMutableDictionary.h | 1 + cocos2dx/include/NSString.h | 8 + .../tileMap_parallax_nodes/CCTMXLayer.cpp | 46 +++--- .../CCTMXObjectGroup.cpp | 71 +++++---- .../tileMap_parallax_nodes/CCTMXTiledMap.cpp | 47 +++--- .../tileMap_parallax_nodes/CCTMXXMLParser.cpp | 144 ++++++++++-------- test_uphone/tests/TileMapTest/TileMapTest.cpp | 42 ++--- 11 files changed, 231 insertions(+), 216 deletions(-) diff --git a/cocos2dx/include/CCTMXLayer.h b/cocos2dx/include/CCTMXLayer.h index 23e3a70535..85a76a6610 100644 --- a/cocos2dx/include/CCTMXLayer.h +++ b/cocos2dx/include/CCTMXLayer.h @@ -60,12 +60,6 @@ namespace cocos2d { */ class CCX_DLL CCTMXLayer : public CCSpriteSheet { - /** name of the layer */ - protected: - std::string m_sLayerName; - public: - inline const char* getLayerName(){ return m_sLayerName.c_str(); } - inline void setLayerName(const char *layerName){ m_sLayerName = layerName; } /** size of the layer in tiles */ CCX_SYNTHESIZE(CGSize, m_tLayerSize, LayerSize); /** size of the map's tile (could be differnt from the tile's size) */ @@ -77,7 +71,7 @@ namespace cocos2d { /** Layer orientation, which is the same as the map orientation */ CCX_SYNTHESIZE(int, m_nLayerOrientation, LayerOrientation); /** properties from the layer. They can be added using Tiled */ - CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties); + CCX_PROPERTY(StringToStringDictionary*, m_pProperties, Properties); public: CCTMXLayer(); virtual ~CCTMXLayer(); @@ -120,7 +114,7 @@ namespace cocos2d { CGPoint positionAt(CGPoint tileCoordinate); /** return the value for the specific property name */ - const char *propertyNamed(const char *propertyName); + NSString *propertyNamed(const char *propertyName); /** Creates the tiles */ void setupTiles(); @@ -132,6 +126,9 @@ namespace cocos2d { // super method void removeChild(CCNode* child, bool cleanup); void draw(); + + inline const char* getLayerName(){ return m_sLayerName.c_str(); } + inline void setLayerName(const char *layerName){ m_sLayerName = layerName; } private: CGPoint positionForIsoAt(CGPoint pos); CGPoint positionForOrthoAt(CGPoint pos); @@ -152,6 +149,9 @@ namespace cocos2d { unsigned int atlasIndexForExistantZ(unsigned int z); unsigned int atlasIndexForNewZ(int z); protected: + /** name of the layer */ + std::string m_sLayerName; + unsigned char m_cOpacity; // TMX Layer supports opacity unsigned int m_uMinGID; diff --git a/cocos2dx/include/CCTMXObjectGroup.h b/cocos2dx/include/CCTMXObjectGroup.h index a78fd74fd6..39cce3faad 100644 --- a/cocos2dx/include/CCTMXObjectGroup.h +++ b/cocos2dx/include/CCTMXObjectGroup.h @@ -23,48 +23,42 @@ THE SOFTWARE. ****************************************************************************/ #ifndef __CCTMX_OBJECT_GROUP_H__ #define __CCTMX_OBJECT_GROUP_H__ -#include -#include -#include #include "Cocos2dDefine.h" #include "CGGeometry.h" #include "NSString.h" +#include "NSMutableArray.h" +#include "NSMutableDictionary.h" namespace cocos2d { - typedef std::map StringToStringDictionary; - typedef std::pair StringToStringPair; - /** only used in StringToStringDictionary, return "" if not found*/ - CCX_DLL const char * valueForKey(const char* key, StringToStringDictionary *dict); - /** CCTMXObjectGroup represents the TMX object group. @since v0.99.0 */ class CCX_DLL CCTMXObjectGroup : public NSObject { - /** name of the group */ - protected: - std::string m_sGroupName; - public: - inline const char* getGroupName(){ return m_sGroupName.c_str(); } - inline void setGroupName(const char *groupName){ m_sGroupName = groupName; } /** offset position of child objects */ CCX_SYNTHESIZE(CGPoint, m_tPositionOffset, PositionOffset); - /** array of the objects */ - CCX_SYNTHESIZE(std::vector*, m_pObjects, Objects); /** list of properties stored in a dictionary */ - CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties); + CCX_PROPERTY(StringToStringDictionary*, m_pProperties, Properties); + /** array of the objects */ + CCX_PROPERTY(NSArray*, m_pObjects, Objects); public: CCTMXObjectGroup(); virtual ~CCTMXObjectGroup(); + inline const char* getGroupName(){ return m_sGroupName.c_str(); } + inline void setGroupName(const char *groupName){ m_sGroupName = groupName; } + /** return the value for the specific property name */ - const char *propertyNamed(const char* propertyName); + NSString *propertyNamed(const char* propertyName); /** return the dictionary for the specific object name. It will return the 1st object found on the array for the given name. */ StringToStringDictionary *objectNamed(const char *objectName); + protected: + /** name of the group */ + std::string m_sGroupName; }; }// namespace cocos2d diff --git a/cocos2dx/include/CCTMXTiledMap.h b/cocos2dx/include/CCTMXTiledMap.h index f02ca9c098..602b317dba 100644 --- a/cocos2dx/include/CCTMXTiledMap.h +++ b/cocos2dx/include/CCTMXTiledMap.h @@ -108,7 +108,7 @@ namespace cocos2d { /** object groups */ CCX_PROPERTY(NSMutableArray*, m_pObjectGroups, ObjectGroups); /** properties */ - CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties); + CCX_PROPERTY(StringToStringDictionary*, m_pProperties, Properties); public: CCTMXTiledMap(); virtual ~CCTMXTiledMap(); @@ -131,10 +131,10 @@ namespace cocos2d { CCTMXObjectGroup* groupNamed(const char *groupName); /** return the value for the specific property name */ - const char *propertyNamed(const char *propertyName); + NSString *propertyNamed(const char *propertyName); /** return properties dictionary for tile GID */ - StringToStringDictionary *propertiesForGID(int GID); + NSDictionary *propertiesForGID(int GID); private: CCTMXLayer * parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo); @@ -142,7 +142,7 @@ namespace cocos2d { protected: // tile properties - std::map *m_pTileProperties; + NSDictionary *m_pTileProperties; }; diff --git a/cocos2dx/include/CCTMXXMLParser.h b/cocos2dx/include/CCTMXXMLParser.h index 69e25fdfcd..710fa7fae4 100644 --- a/cocos2dx/include/CCTMXXMLParser.h +++ b/cocos2dx/include/CCTMXXMLParser.h @@ -23,8 +23,10 @@ THE SOFTWARE. ****************************************************************************/ #ifndef __CC_TM_XML_PARSER__ #define __CC_TM_XML_PARSER__ -#include "CCTMXObjectGroup.h" #include "NSMutableArray.h" +#include "NSMutableDictionary.h" +#include "CGGeometry.h" +#include "Cocos2dDefine.h" namespace cocos2d { /* @@ -117,14 +119,10 @@ namespace cocos2d { This information is obtained from the TMX file. */ + class CCTMXObjectGroup; class CCX_DLL CCTMXMapInfo : public NSObject { - // tmx filename - protected: - std::string m_sFileName; public: - inline const char* getFileName(){ return m_sFileName.c_str(); } - inline void setFileName(const char *fileName){ m_sFileName = fileName; } // map orientation CCX_SYNTHESIZE(int, m_nOrientation, Orientation); // map width & height @@ -143,16 +141,10 @@ namespace cocos2d { CCX_SYNTHESIZE(unsigned int, m_uParentGID, ParentGID); // layer attribs CCX_SYNTHESIZE(int, m_nLayerAttribs, LayerAttribs); - // current string - protected: - std::string m_sCurrentString; - public: - inline const char* getCurrentString(){ return m_sCurrentString.c_str(); } - inline void setCurrentString(const char *currentString){ m_sCurrentString = currentString; } // is stroing characters? CCX_SYNTHESIZE(bool, m_bStoringCharacters, StoringCharacters); // properties - CCX_SYNTHESIZE(StringToStringDictionary*, m_pProperties, Properties); + CCX_PROPERTY(StringToStringDictionary*, m_pProperties, Properties); public: CCTMXMapInfo(); virtual ~CCTMXMapInfo(); @@ -162,12 +154,22 @@ namespace cocos2d { bool initWithTMXFile(const char *tmxFile); /* initalises parsing of an XML file, either a tmx (Map) file or tsx (Tileset) file */ bool parseXMLFile(const char *xmlFilename); + + NSDictionary * getTileProperties(); + void setTileProperties(NSDictionary * tileProperties); + + inline const char* getCurrentString(){ return m_sCurrentString.c_str(); } + inline void setCurrentString(const char *currentString){ m_sCurrentString = currentString; } + inline const char* getFileName(){ return m_sFileName.c_str(); } + inline void setFileName(const char *fileName){ m_sFileName = fileName; } - inline std::map* getTileProperties(){return m_pTileProperties;} - inline void setTileProperties(std::map * var){m_pTileProperties = var;} protected: + // tmx filename + std::string m_sFileName; + // current string + std::string m_sCurrentString; // tile properties - std::map* m_pTileProperties; + NSDictionary* m_pTileProperties; }; }// namespace cocos2d diff --git a/cocos2dx/include/NSMutableDictionary.h b/cocos2dx/include/NSMutableDictionary.h index add8861177..cb5f836be0 100644 --- a/cocos2dx/include/NSMutableDictionary.h +++ b/cocos2dx/include/NSMutableDictionary.h @@ -221,6 +221,7 @@ public: }; #define NSDictionary NSMutableDictionary +typedef NSDictionary StringToStringDictionary; }//namespace cocos2d diff --git a/cocos2dx/include/NSString.h b/cocos2dx/include/NSString.h index 4d16c0c982..6ac89a3898 100644 --- a/cocos2dx/include/NSString.h +++ b/cocos2dx/include/NSString.h @@ -35,11 +35,19 @@ namespace cocos2d { NSString() :m_sString("") {} + NSString(const char * str) + { + m_sString = str; + } virtual ~NSString(){ m_sString.clear(); } int toInt() { return atoi(m_sString.c_str()); } + unsigned int toUInt() + { + return (unsigned int)atoi(m_sString.c_str()); + } float toFloat() { return (float)atof(m_sString.c_str()); diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp index 518181984c..942b6a11cd 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXLayer.cpp @@ -65,15 +65,7 @@ namespace cocos2d { m_uMinGID = layerInfo->m_uMinGID; m_uMaxGID = layerInfo->m_uMaxGID; m_cOpacity = layerInfo->m_cOpacity; - m_pProperties = new StringToStringDictionary(); - if (layerInfo->m_pProperties && layerInfo->m_pProperties->size()>0) - { - StringToStringDictionary::iterator it; - for (it = layerInfo->m_pProperties->begin(); it != layerInfo->m_pProperties->end(); ++it) - { - m_pProperties->insert(StringToStringPair(it->first, it->second)); - } - } + m_pProperties = StringToStringDictionary::dictionaryWithDictionary(layerInfo->m_pProperties); // tilesetInfo m_pTileSet = tilesetInfo; @@ -112,12 +104,7 @@ namespace cocos2d { { CCX_SAFE_RELEASE(m_pTileSet); CCX_SAFE_RELEASE(m_pReusedTile); - if (m_pProperties) - { - m_pProperties->clear(); - delete m_pProperties; - m_pProperties = NULL; - } + CCX_SAFE_RELEASE(m_pProperties); if( m_pAtlasIndexArray ) { @@ -204,29 +191,32 @@ namespace cocos2d { } // CCTMXLayer - Properties - const char *CCTMXLayer::propertyNamed(const char *propertyName) + NSString *CCTMXLayer::propertyNamed(const char *propertyName) { - return valueForKey(propertyName, m_pProperties); + return m_pProperties->objectForKey(propertyName); } void CCTMXLayer::parseInternalProperties() { // if cc_vertex=automatic, then tiles will be rendered using vertexz - std::string vertexz = propertyNamed("cc_vertexz"); - if( vertexz != "" ) + NSString *vertexz = propertyNamed("cc_vertexz"); + if( vertexz ) { - if( vertexz == "automatic" ) + if( vertexz->m_sString == "automatic" ) { m_bUseAutomaticVertexZ = true; } else { - m_nVertexZvalue = atoi(vertexz.c_str()); + m_nVertexZvalue = vertexz->toInt(); } } - std::string alphaFuncVal = propertyNamed("cc_alpha_func"); - m_fAlphaFuncValue = (float)atof(alphaFuncVal.c_str()); + NSString *alphaFuncVal = propertyNamed("cc_alpha_func"); + if (alphaFuncVal) + { + m_fAlphaFuncValue = alphaFuncVal->toFloat(); + } } // CCTMXLayer - obtaining tiles/gids @@ -633,6 +623,16 @@ namespace cocos2d { } } + StringToStringDictionary * CCTMXLayer::getProperties() + { + return m_pProperties; + } + void CCTMXLayer::setProperties(StringToStringDictionary* var) + { + CCX_SAFE_RETAIN(m_pProperties); + m_pProperties = var; + CCX_SAFE_RELEASE(m_pProperties); + } }// namespace cocos2d diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp index 2e8f588968..214eb4fdc1 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXObjectGroup.cpp @@ -25,63 +25,62 @@ THE SOFTWARE. #include "ccMacros.h" namespace cocos2d { - const char * valueForKey(const char* key, StringToStringDictionary *dict) - { - std::string pKey = key; - if (!dict) - { - return ""; - } - StringToStringDictionary::iterator it = dict->find(pKey); - return it!=dict->end() ? it->second.c_str() : ""; - } - //implementation CCTMXObjectGroup CCTMXObjectGroup::CCTMXObjectGroup() :m_sGroupName("") ,m_tPositionOffset(CGPointZero) { - m_pObjects = new std::vector(); + m_pObjects = new NSArray(); m_pProperties = new StringToStringDictionary(); } CCTMXObjectGroup::~CCTMXObjectGroup() { CCLOGINFO( "cocos2d: deallocing."); - if (m_pProperties) - { - m_pProperties->clear(); - delete m_pProperties; - m_pProperties = NULL; - } - if (m_pObjects) - { - std::vector::iterator it; - for (it = m_pObjects->begin(); it != m_pObjects->end(); ++it) - { - CCX_SAFE_DELETE(*it); - } - m_pObjects->clear(); - delete m_pObjects; - m_pObjects = NULL; - } + CCX_SAFE_RELEASE(m_pObjects); + CCX_SAFE_RELEASE(m_pProperties); } StringToStringDictionary * CCTMXObjectGroup::objectNamed(const char *objectName) { - std::vector::iterator it; - for (it = m_pObjects->begin(); it != m_pObjects->end(); ++it) + if (m_pObjects && m_pObjects->count() > 0) { - if ( *it && strcmp(valueForKey("name", *it), objectName)==0 ) + NSArray::NSMutableArrayIterator it; + for (it = m_pObjects->begin(); it != m_pObjects->end(); ++it) { - return *it; + NSString *name = (*it)->objectForKey("name"); + if (name && name->m_sString == objectName) + { + return *it; + } } } // object not found - return NULL; + return NULL; } - const char *CCTMXObjectGroup::propertyNamed(const char* propertyName) + NSString *CCTMXObjectGroup::propertyNamed(const char* propertyName) { - return valueForKey(propertyName, m_pProperties); + return m_pProperties->objectForKey(propertyName); + } + + StringToStringDictionary * CCTMXObjectGroup::getProperties() + { + return m_pProperties; + } + void CCTMXObjectGroup::setProperties(StringToStringDictionary * properties) + { + CCX_SAFE_RELEASE(m_pProperties); + m_pProperties = properties; + CCX_SAFE_RETAIN(m_pProperties); + } + NSArray *CCTMXObjectGroup::getObjects() + { + return m_pObjects; + } + void CCTMXObjectGroup::setObjects(NSArray * objects) + { + CCX_SAFE_RELEASE(m_pObjects); + m_pObjects = objects; + CCX_SAFE_RETAIN(m_pObjects); } }// namespace cocos2d \ No newline at end of file diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp index 3d5d7dc29a..a60f6a21ff 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXTiledMap.cpp @@ -96,28 +96,9 @@ namespace cocos2d{ } CCTMXTiledMap::~CCTMXTiledMap() { - m_pObjectGroups->release(); - if (m_pProperties) - { - m_pProperties->clear(); - delete m_pProperties; - m_pProperties = NULL; - } - if (m_pTileProperties) - { - std::map::iterator it; - for (it = m_pTileProperties->begin(); it != m_pTileProperties->end(); ++it) - { - if (it->second) - { - it->second->clear(); - delete it->second; - } - } - m_pTileProperties->clear(); - delete m_pTileProperties; - m_pTileProperties = NULL; - } + CCX_SAFE_RELEASE(m_pProperties); + CCX_SAFE_RELEASE(m_pObjectGroups); + CCX_SAFE_RELEASE(m_pTileProperties); } NSMutableArray * CCTMXTiledMap::getObjectGroups() { @@ -129,6 +110,16 @@ namespace cocos2d{ m_pObjectGroups = var; CCX_SAFE_RETAIN(m_pObjectGroups); } + StringToStringDictionary * CCTMXTiledMap::getProperties() + { + return m_pProperties; + } + void CCTMXTiledMap::setProperties(StringToStringDictionary* var) + { + CCX_SAFE_RETAIN(m_pProperties); + m_pProperties = var; + CCX_SAFE_RELEASE(m_pProperties); + } // private CCTMXLayer * CCTMXTiledMap::parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo) { @@ -238,17 +229,15 @@ namespace cocos2d{ { return objectGroupNamed(groupName); } - const char * CCTMXTiledMap::propertyNamed(const char *propertyName) + NSString * CCTMXTiledMap::propertyNamed(const char *propertyName) { - return valueForKey(propertyName, m_pProperties); + return m_pProperties->objectForKey(propertyName); } - StringToStringDictionary * CCTMXTiledMap::propertiesForGID(int GID) + NSDictionary * CCTMXTiledMap::propertiesForGID(int GID) { - std::map::iterator it; - it = m_pTileProperties->find(GID); - return it!=m_pTileProperties->end() ? it->second : NULL; + return m_pTileProperties->objectForKey(GID); } - + }// namespace cocos2d diff --git a/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp b/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp index 64f740c55e..962687cce5 100644 --- a/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp +++ b/cocos2dx/tileMap_parallax_nodes/CCTMXXMLParser.cpp @@ -24,6 +24,7 @@ THE SOFTWARE. #include #include #include +#include #include "CCTMXXMLParser.h" #include "CCTMXTiledMap.h" #include "ccMacros.h" @@ -37,7 +38,15 @@ namespace cocos2d { void tmx_startElement(void *ctx, const xmlChar *name, const xmlChar **atts); void tmx_endElement(void *ctx, const xmlChar *name); void tmx_characters(void *ctx, const xmlChar *ch, int len); - + const char* valueForKey(const char *key, std::map* dict) + { + if (dict) + { + std::map::iterator it = dict->find(key); + return it!=dict->end() ? it->second.c_str() : ""; + } + return ""; + } // implementation CCTMXLayerInfo CCTMXLayerInfo::CCTMXLayerInfo() :m_bOwnTiles(true) @@ -52,12 +61,7 @@ namespace cocos2d { CCTMXLayerInfo::~CCTMXLayerInfo() { CCLOGINFO("cocos2d: deallocing."); - if (m_pProperties) - { - m_pProperties->clear(); - delete m_pProperties; - m_pProperties = NULL; - } + CCX_SAFE_RELEASE(m_pProperties); if( m_bOwnTiles && m_pTiles ) { delete [] m_pTiles; @@ -110,7 +114,7 @@ namespace cocos2d { m_sFileName = CCFileUtils::fullPathFromRelativePath(tmxFile); m_pObjectGroups = new NSMutableArray(); m_pProperties = new StringToStringDictionary(); - m_pTileProperties = new std::map(); + m_pTileProperties = new NSDictionary(); // tmp vars m_sCurrentString = ""; @@ -132,30 +136,11 @@ namespace cocos2d { CCTMXMapInfo::~CCTMXMapInfo() { CCLOGINFO("cocos2d: deallocing."); - m_pTilesets->release(); - m_pLayers->release(); - m_pObjectGroups->release(); - if (m_pProperties) - { - m_pProperties->clear(); - delete m_pProperties; - m_pProperties = NULL; - } - if (m_pTileProperties) - { - std::map::iterator it; - for (it = m_pTileProperties->begin(); it != m_pTileProperties->end(); ++it) - { - if (it->second) - { - it->second->clear(); - delete it->second; - } - } - m_pTileProperties->clear(); - delete m_pTileProperties; - m_pTileProperties = NULL; - } + CCX_SAFE_RELEASE(m_pTilesets); + CCX_SAFE_RELEASE(m_pLayers); + CCX_SAFE_RELEASE(m_pProperties); + CCX_SAFE_RELEASE(m_pTileProperties); + CCX_SAFE_RELEASE(m_pObjectGroups); } NSMutableArray * CCTMXMapInfo::getLayers() { @@ -187,6 +172,27 @@ namespace cocos2d { m_pObjectGroups = var; CCX_SAFE_RETAIN(m_pObjectGroups); } + StringToStringDictionary * CCTMXMapInfo::getProperties() + { + return m_pProperties; + } + void CCTMXMapInfo::setProperties(StringToStringDictionary* var) + { + CCX_SAFE_RELEASE(m_pProperties); + m_pProperties = var; + CCX_SAFE_RETAIN(m_pProperties); + } + NSDictionary * CCTMXMapInfo::getTileProperties() + { + return m_pTileProperties; + } + void CCTMXMapInfo::setTileProperties(NSDictionary * tileProperties) + { + CCX_SAFE_RELEASE(m_pTileProperties); + m_pTileProperties = tileProperties; + CCX_SAFE_RETAIN(m_pTileProperties); + } + bool CCTMXMapInfo::parseXMLFile(const char *xmlFilename) { FILE *fp = NULL; @@ -237,14 +243,14 @@ namespace cocos2d { { CCTMXMapInfo *pTMXMapInfo = (CCTMXMapInfo*)(ctx); std::string elementName = (char*)name; - StringToStringDictionary *attributeDict = new StringToStringDictionary(); + std::map *attributeDict = new std::map(); if(atts && atts[0]) { for(int i = 0; atts[i]; i += 2) { std::string key = (char*)atts[i]; std::string value = (char*)atts[i+1]; - attributeDict->insert(StringToStringPair(key, value)); + attributeDict->insert(pair(key, value)); } } if(elementName == "map") @@ -306,8 +312,8 @@ namespace cocos2d { CCTMXTilesetInfo* info = pTMXMapInfo->getTilesets()->getLastObject(); StringToStringDictionary *dict = new StringToStringDictionary(); pTMXMapInfo->setParentGID(info->m_uFirstGid + atoi(valueForKey("id", attributeDict))); - pTMXMapInfo->getTileProperties()->insert(std::pair(pTMXMapInfo->getParentGID(), dict)); - + pTMXMapInfo->getTileProperties()->setObject(dict, pTMXMapInfo->getParentGID()); + pTMXMapInfo->setParentElement(TMXPropertyTile); } @@ -402,40 +408,45 @@ namespace cocos2d { // Set the name of the object to the value for "name" std::string key = "name"; - std::string value = valueForKey("name", attributeDict); - dict->insert(StringToStringPair(key, value)); + NSString *value = new NSString(valueForKey("name", attributeDict)); + dict->setObject(value, key); + value->release(); // Assign all the attributes as key/name pairs in the properties dictionary key = "type"; - value = valueForKey("type", attributeDict); - dict->insert(StringToStringPair(key, value)); + value = new NSString(valueForKey("type", attributeDict)); + dict->setObject(value, key); + value->release(); int x = atoi(valueForKey("x", attributeDict)) + (int)objectGroup->getPositionOffset().x; key = "x"; - /*value = itoa(x, buffer, 10);*/ sprintf(buffer, "%d", x); - value = buffer; - dict->insert(StringToStringPair(key, value)); + value = new NSString(buffer); + dict->setObject(value, key); + value->release(); int y = atoi(valueForKey("y", attributeDict)) + (int)objectGroup->getPositionOffset().y; // Correct y position. (Tiled uses Flipped, cocos2d uses Standard) y = (int)(pTMXMapInfo->getMapSize().height * pTMXMapInfo->getTileSize().height) - y - atoi(valueForKey("height", attributeDict)); key = "y"; - /*value = itoa(y, buffer, 10);*/ sprintf(buffer, "%d", y); - value = buffer; - dict->insert(StringToStringPair(key, value)); + value = new NSString(buffer); + dict->setObject(value, key); + value->release(); key = "width"; - value = valueForKey("width", attributeDict); - dict->insert(StringToStringPair(key, value)); + value = new NSString(valueForKey("width", attributeDict)); + dict->setObject(value, key); + value->release(); key = "height"; - value = valueForKey("height", attributeDict); - dict->insert(StringToStringPair(key, value)); + value = new NSString(valueForKey("height", attributeDict)); + dict->setObject(value, key); + value->release(); // Add the object to the objectGroup - objectGroup->getObjects()->insert(objectGroup->getObjects()->begin(), dict); + objectGroup->getObjects()->addObject(dict); + dict->release(); // The parent element is now "object" pTMXMapInfo->setParentElement(TMXPropertyObject); @@ -451,48 +462,53 @@ namespace cocos2d { else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap ) { // The parent element is the map - std::string value = valueForKey("value", attributeDict); + NSString *value = new NSString(valueForKey("value", attributeDict)); std::string key = valueForKey("name", attributeDict); - pTMXMapInfo->getProperties()->insert(StringToStringPair(key, value)); + pTMXMapInfo->getProperties()->setObject(value, key); + value->release(); } else if ( pTMXMapInfo->getParentElement() == TMXPropertyLayer ) { // The parent element is the last layer CCTMXLayerInfo *layer = pTMXMapInfo->getLayers()->getLastObject(); - std::string value = valueForKey("value", attributeDict); + NSString *value = new NSString(valueForKey("value", attributeDict)); std::string key = valueForKey("name", attributeDict); // Add the property to the layer - layer->m_pProperties->insert(StringToStringPair(key, value)); + layer->m_pProperties->setObject(value, key); + value->release(); } else if ( pTMXMapInfo->getParentElement() == TMXPropertyObjectGroup ) { // The parent element is the last object group CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject(); + NSString *value = new NSString(valueForKey("value", attributeDict)); std::string key = valueForKey("name", attributeDict); - std::string value = valueForKey("value", attributeDict); - objectGroup->getProperties()->insert(StringToStringPair(key, value)); + objectGroup->getProperties()->setObject(value, key); + value->release(); } else if ( pTMXMapInfo->getParentElement() == TMXPropertyObject ) { // The parent element is the last object CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject(); - StringToStringDictionary *dict = *objectGroup->getObjects()->begin(); + StringToStringDictionary *dict = objectGroup->getObjects()->getLastObject(); std::string propertyName = valueForKey("name", attributeDict); - std::string propertyValue = valueForKey("value", attributeDict); - - dict->insert(StringToStringPair(propertyName, propertyValue)); + NSString *propertyValue = new NSString(valueForKey("value", attributeDict)); + dict->setObject(propertyValue, propertyName); + propertyValue->release(); } else if ( pTMXMapInfo->getParentElement() == TMXPropertyTile ) { StringToStringDictionary *dict; - dict = pTMXMapInfo->getTileProperties()->find(pTMXMapInfo->getParentGID())->second; + dict = pTMXMapInfo->getTileProperties()->objectForKey(pTMXMapInfo->getParentGID()); + std::string propertyName = valueForKey("name", attributeDict); - std::string propertyValue = valueForKey("value", attributeDict); - dict->insert(StringToStringPair(propertyName, propertyValue)); + NSString *propertyValue = new NSString(valueForKey("value", attributeDict)); + dict->setObject(propertyValue, propertyName); + propertyValue->release(); } } if (attributeDict) diff --git a/test_uphone/tests/TileMapTest/TileMapTest.cpp b/test_uphone/tests/TileMapTest/TileMapTest.cpp index 3893c8d73c..3ae338cbd7 100644 --- a/test_uphone/tests/TileMapTest/TileMapTest.cpp +++ b/test_uphone/tests/TileMapTest/TileMapTest.cpp @@ -604,10 +604,10 @@ TMXOrthoObjectsTest::TMXOrthoObjectsTest() ////----UXLOG("----> Iterating over all the group objets"); CCTMXObjectGroup* group = map->objectGroupNamed("Object Group 1"); - std::vector * objects = group->getObjects(); + NSArray * objects = group->getObjects(); StringToStringDictionary* dict; - std::vector::iterator it; + NSArray::NSMutableArrayIterator it; for( it = objects->begin(); it != objects->end(); it++) { dict = (*it);//dynamic_cast(*it); @@ -628,9 +628,9 @@ void TMXOrthoObjectsTest::draw() CCTMXTiledMap* map = (CCTMXTiledMap*) getChildByTag(kTagTileMap); CCTMXObjectGroup* group = map->objectGroupNamed("Object Group 1"); - std::vector * objects = group->getObjects(); + NSArray * objects = group->getObjects(); StringToStringDictionary* dict; - std::vector::iterator it; + NSArray::NSMutableArrayIterator it; for( it = objects->begin(); it != objects->end(); it++) { @@ -638,11 +638,14 @@ void TMXOrthoObjectsTest::draw() if(!dict) break; - - int x = atoi(valueForKey("x", dict)); - int y = atoi(valueForKey("y", dict));//dynamic_cast(dict->objectForKey("y"))->getNumber(); - int width = atoi(valueForKey("width", dict));//dynamic_cast(dict->objectForKey("width"))->getNumber(); - int height = atoi(valueForKey("height", dict));//dynamic_cast(dict->objectForKey("height"))->getNumber(); + std::string key = "x"; + int x = dict->objectForKey(key)->toInt(); + key = "y"; + int y = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("y"))->getNumber(); + key = "width"; + int width = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("width"))->getNumber(); + key = "height"; + int height = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("height"))->getNumber(); glLineWidth(3); @@ -683,11 +686,11 @@ TMXIsoObjectsTest::TMXIsoObjectsTest() CCTMXObjectGroup* group = map->objectGroupNamed("Object Group 1"); //UxMutableArray* objects = group->objects(); - std::vector * objects = group->getObjects(); + NSArray * objects = group->getObjects(); //UxMutableDictionary* dict; StringToStringDictionary* dict; //NSMutableArray::NSMutableArrayIterator it; - std::vector::iterator it; + NSArray::NSMutableArrayIterator it; for( it = objects->begin(); it != objects->end(); it++) { @@ -705,9 +708,9 @@ void TMXIsoObjectsTest::draw() CCTMXTiledMap *map = (CCTMXTiledMap*) getChildByTag(kTagTileMap); CCTMXObjectGroup *group = map->objectGroupNamed("Object Group 1"); - std::vector * objects = group->getObjects(); + NSArray * objects = group->getObjects(); StringToStringDictionary* dict; - std::vector::iterator it; + NSArray::NSMutableArrayIterator it; for( it = objects->begin(); it != objects->end(); it++) { @@ -715,11 +718,14 @@ void TMXIsoObjectsTest::draw() if(!dict) break; - - int x = atoi(valueForKey("x", dict));//dynamic_cast(dict->objectForKey("x"))->getNumber(); - int y = atoi(valueForKey("y", dict));//dynamic_cast(dict->objectForKey("y"))->getNumber(); - int width = atoi(valueForKey("width", dict));//dynamic_cast(dict->objectForKey("width"))->getNumber(); - int height = atoi(valueForKey("height", dict));//dynamic_cast(dict->objectForKey("height"))->getNumber(); + std::string key = "x"; + int x = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("x"))->getNumber(); + key = "y"; + int y = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("y"))->getNumber(); + key = "width"; + int width = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("width"))->getNumber(); + key = "height"; + int height = dict->objectForKey(key)->toInt();//dynamic_cast(dict->objectForKey("height"))->getNumber(); glLineWidth(3);