Merge pull request #1930 from NatWeiss/tmxpolygons

Added TMX polygon parsing.
This commit is contained in:
minggo 2013-02-02 19:17:44 -08:00
commit 7f594f00b8
1 changed files with 49 additions and 3 deletions

View File

@ -26,6 +26,7 @@ THE SOFTWARE.
****************************************************************************/
#include <map>
#include <sstream>
#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")
{