From 9b98e4548f71d4af2041e3d0764eac56d7a19b31 Mon Sep 17 00:00:00 2001 From: Nat Weiss Date: Tue, 29 Jan 2013 14:45:41 -0800 Subject: [PATCH] Added TMX polygon parsing. --- .../tilemap_parallax_nodes/CCTMXXMLParser.cpp | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/cocos2dx/tilemap_parallax_nodes/CCTMXXMLParser.cpp b/cocos2dx/tilemap_parallax_nodes/CCTMXXMLParser.cpp index ac220d9b07..383a1e74a8 100644 --- a/cocos2dx/tilemap_parallax_nodes/CCTMXXMLParser.cpp +++ b/cocos2dx/tilemap_parallax_nodes/CCTMXXMLParser.cpp @@ -26,6 +26,7 @@ THE SOFTWARE. ****************************************************************************/ #include +#include #include "CCTMXXMLParser.h" #include "CCTMXTiledMap.h" #include "ccMacros.h" @@ -599,10 +600,55 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts) else if (elementName == "polygon") { // find parent object's dict and add polygon-points to it - // CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject(); - // CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject(); - // TODO: dict->setObject(attributeDict objectForKey:@"points"] forKey:@"polygonPoints"]; + CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)m_pObjectGroups->lastObject(); + CCDictionary* dict = (CCDictionary*)objectGroup->getObjects()->lastObject(); + // get points value string + const char* value = valueForKey("points", attributeDict); + if(value) + { + CCArray* pPointsArray = new CCArray; + + // parse points string into a space-separated set of points + stringstream pointsStream(value); + string pointPair; + while(std::getline(pointsStream, pointPair, ' ')) + { + // parse each point combo into a comma-separated x,y point + stringstream pointStream(pointPair); + string xStr,yStr; + char buffer[32] = {0}; + + CCDictionary* pPointDict = new CCDictionary; + + // set x + if(std::getline(pointStream, xStr, ',')) + { + int x = atoi(xStr.c_str()) + (int)objectGroup->getPositionOffset().x; + sprintf(buffer, "%d", x); + CCString* pStr = new CCString(buffer); + pStr->autorelease(); + pPointDict->setObject(pStr, "x"); + } + + // set y + if(std::getline(pointStream, yStr, ',')) + { + int y = atoi(yStr.c_str()) + (int)objectGroup->getPositionOffset().y; + sprintf(buffer, "%d", y); + CCString* pStr = new CCString(buffer); + pStr->autorelease(); + pPointDict->setObject(pStr, "y"); + } + + // add to points array + pPointsArray->addObject(pPointDict); + pPointDict->release(); + } + + dict->setObject(pPointsArray, "points"); + pPointsArray->release(); + } } else if (elementName == "polyline") {