issue #114, CCTMXTiledMap debug

This commit is contained in:
Walzer 2010-09-02 10:16:02 +00:00
parent 863fc8584a
commit 082b90ea06
3 changed files with 90 additions and 47 deletions

View File

@ -54,6 +54,11 @@ namespace cocos2d {
} }
if (m_pObjects) if (m_pObjects)
{ {
std::vector<StringToStringDictionary*>::iterator it;
for (it = m_pObjects->begin(); it != m_pObjects->end(); ++it)
{
CCX_SAFE_DELETE(*it);
}
m_pObjects->clear(); m_pObjects->clear();
delete m_pObjects; delete m_pObjects;
} }

View File

@ -96,6 +96,7 @@ namespace cocos2d{
} }
CCTMXTiledMap::~CCTMXTiledMap() CCTMXTiledMap::~CCTMXTiledMap()
{ {
m_pObjectGroups->removeAllObjects();
m_pObjectGroups->release(); m_pObjectGroups->release();
if (m_pProperties) if (m_pProperties)
{ {
@ -104,6 +105,15 @@ namespace cocos2d{
} }
if (m_pTileProperties) if (m_pTileProperties)
{ {
std::map<int, StringToStringDictionary*>::iterator it;
for (it = m_pTileProperties->begin(); it != m_pTileProperties->end(); ++it)
{
if (it->second)
{
it->second->clear();
delete it->second;
}
}
m_pTileProperties->clear(); m_pTileProperties->clear();
delete m_pTileProperties; delete m_pTileProperties;
} }

View File

@ -131,9 +131,32 @@ namespace cocos2d {
CCTMXMapInfo::~CCTMXMapInfo() CCTMXMapInfo::~CCTMXMapInfo()
{ {
CCLOGINFO("cocos2d: deallocing."); CCLOGINFO("cocos2d: deallocing.");
m_pTilesets->removeAllObjects();
m_pTilesets->release(); m_pTilesets->release();
m_pLayers->removeAllObjects();
m_pLayers->release(); m_pLayers->release();
m_pObjectGroups->removeAllObjects();
m_pObjectGroups->release(); m_pObjectGroups->release();
if (m_pProperties)
{
m_pProperties->clear();
delete m_pProperties;
}
if (m_pTileProperties)
{
std::map<int, StringToStringDictionary*>::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;
}
CCX_SAFE_DELETE(m_pProperties); CCX_SAFE_DELETE(m_pProperties);
CCX_SAFE_DELETE(m_pTileProperties); CCX_SAFE_DELETE(m_pTileProperties);
} }
@ -188,24 +211,24 @@ namespace cocos2d {
{ {
CCTMXMapInfo *pTMXMapInfo = (CCTMXMapInfo*)(ctx); CCTMXMapInfo *pTMXMapInfo = (CCTMXMapInfo*)(ctx);
std::string elementName = (char*)name; std::string elementName = (char*)name;
StringToStringDictionary attributeDict; StringToStringDictionary *attributeDict = new StringToStringDictionary();
if(atts && atts[0]) if(atts && atts[0])
{ {
for(int i = 0; atts[i]; i += 2) for(int i = 0; atts[i]; i += 2)
{ {
std::string key = (char*)atts[i]; std::string key = (char*)atts[i];
std::string value = (char*)atts[i+1]; std::string value = (char*)atts[i+1];
attributeDict.insert(StringToStringPair(key, value)); attributeDict->insert(StringToStringPair(key, value));
} }
} }
if(elementName == "map") if(elementName == "map")
{ {
std::string version = valueForKey("version", &attributeDict); std::string version = valueForKey("version", attributeDict);
if ( version != "1.0") if ( version != "1.0")
{ {
CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %@", version); CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %@", version);
} }
std::string orientationStr = valueForKey("orientation", &attributeDict); std::string orientationStr = valueForKey("orientation", attributeDict);
if( orientationStr == "orthogonal") if( orientationStr == "orthogonal")
pTMXMapInfo->setOrientation(CCTMXOrientationOrtho); pTMXMapInfo->setOrientation(CCTMXOrientationOrtho);
else if ( orientationStr == "isometric") else if ( orientationStr == "isometric")
@ -216,12 +239,12 @@ namespace cocos2d {
CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation()); CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation());
CGSize s; CGSize s;
s.width = (float)atof(valueForKey("width", &attributeDict)); s.width = (float)atof(valueForKey("width", attributeDict));
s.height = (float)atof(valueForKey("height", &attributeDict)); s.height = (float)atof(valueForKey("height", attributeDict));
pTMXMapInfo->setMapSize(s); pTMXMapInfo->setMapSize(s);
s.width = (float)atof(valueForKey("tilewidth", &attributeDict)); s.width = (float)atof(valueForKey("tilewidth", attributeDict));
s.height = (float)atof(valueForKey("tileheight", &attributeDict)); s.height = (float)atof(valueForKey("tileheight", attributeDict));
pTMXMapInfo->setTileSize(s); pTMXMapInfo->setTileSize(s);
// The parent element is now "map" // The parent element is now "map"
@ -230,7 +253,7 @@ namespace cocos2d {
else if(elementName == "tileset") else if(elementName == "tileset")
{ {
// If this is an external tileset then start parsing that // If this is an external tileset then start parsing that
std::string externalTilesetFilename = valueForKey("source", &attributeDict); std::string externalTilesetFilename = valueForKey("source", attributeDict);
if (externalTilesetFilename != "") if (externalTilesetFilename != "")
{ {
externalTilesetFilename = CCFileUtils::fullPathFromRelativePath(externalTilesetFilename.c_str()); externalTilesetFilename = CCFileUtils::fullPathFromRelativePath(externalTilesetFilename.c_str());
@ -239,13 +262,13 @@ namespace cocos2d {
else else
{ {
CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo(); CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo();
tileset->m_sName = valueForKey("name", &attributeDict); tileset->m_sName = valueForKey("name", attributeDict);
tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", &attributeDict)); tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
tileset->m_uSpacing = (unsigned int)atoi(valueForKey("spacing", &attributeDict)); tileset->m_uSpacing = (unsigned int)atoi(valueForKey("spacing", attributeDict));
tileset->m_uMargin = (unsigned int)atoi(valueForKey("margin", &attributeDict)); tileset->m_uMargin = (unsigned int)atoi(valueForKey("margin", attributeDict));
CGSize s; CGSize s;
s.width = (float)atof(valueForKey("tilewidth", &attributeDict)); s.width = (float)atof(valueForKey("tilewidth", attributeDict));
s.height = (float)atof(valueForKey("tileheight", &attributeDict)); s.height = (float)atof(valueForKey("tileheight", attributeDict));
tileset->m_tTileSize = s; tileset->m_tTileSize = s;
pTMXMapInfo->getTilesets()->addObject(tileset); pTMXMapInfo->getTilesets()->addObject(tileset);
@ -256,7 +279,7 @@ namespace cocos2d {
{ {
CCTMXTilesetInfo* info = pTMXMapInfo->getTilesets()->getLastObject(); CCTMXTilesetInfo* info = pTMXMapInfo->getTilesets()->getLastObject();
StringToStringDictionary *dict = new StringToStringDictionary(); StringToStringDictionary *dict = new StringToStringDictionary();
pTMXMapInfo->setParentGID(info->m_uFirstGid + atoi(valueForKey("id", &attributeDict))); pTMXMapInfo->setParentGID(info->m_uFirstGid + atoi(valueForKey("id", attributeDict)));
pTMXMapInfo->getTileProperties()->insert(std::pair<int, StringToStringDictionary*>(pTMXMapInfo->getParentGID(), dict)); pTMXMapInfo->getTileProperties()->insert(std::pair<int, StringToStringDictionary*>(pTMXMapInfo->getParentGID(), dict));
pTMXMapInfo->setParentElement(TMXPropertyTile); pTMXMapInfo->setParentElement(TMXPropertyTile);
@ -265,17 +288,17 @@ namespace cocos2d {
else if(elementName == "layer") else if(elementName == "layer")
{ {
CCTMXLayerInfo *layer = new CCTMXLayerInfo(); CCTMXLayerInfo *layer = new CCTMXLayerInfo();
layer->m_sName = valueForKey("name", &attributeDict); layer->m_sName = valueForKey("name", attributeDict);
CGSize s; CGSize s;
s.width = (float)atof(valueForKey("width", &attributeDict)); s.width = (float)atof(valueForKey("width", attributeDict));
s.height = (float)atof(valueForKey("height", &attributeDict)); s.height = (float)atof(valueForKey("height", attributeDict));
layer->m_tLayerSize = s; layer->m_tLayerSize = s;
std::string visible = valueForKey("visible", &attributeDict); std::string visible = valueForKey("visible", attributeDict);
layer->m_bVisible = !(visible == "0"); layer->m_bVisible = !(visible == "0");
std::string opacity = valueForKey("opacity", &attributeDict); std::string opacity = valueForKey("opacity", attributeDict);
if( opacity != "" ) if( opacity != "" )
{ {
layer->m_cOpacity = (unsigned char)(255 * atof(opacity.c_str())); layer->m_cOpacity = (unsigned char)(255 * atof(opacity.c_str()));
@ -285,8 +308,8 @@ namespace cocos2d {
layer->m_cOpacity = 255; layer->m_cOpacity = 255;
} }
float x = (float)atof(valueForKey("x", &attributeDict)); float x = (float)atof(valueForKey("x", attributeDict));
float y = (float)atof(valueForKey("y", &attributeDict)); float y = (float)atof(valueForKey("y", attributeDict));
layer->m_tOffset = ccp(x,y); layer->m_tOffset = ccp(x,y);
pTMXMapInfo->getLayers()->addObject(layer); pTMXMapInfo->getLayers()->addObject(layer);
@ -299,10 +322,10 @@ namespace cocos2d {
else if(elementName == "objectgroup") else if(elementName == "objectgroup")
{ {
CCTMXObjectGroup *objectGroup = new CCTMXObjectGroup(); CCTMXObjectGroup *objectGroup = new CCTMXObjectGroup();
objectGroup->setGroupName(valueForKey("name", &attributeDict)); objectGroup->setGroupName(valueForKey("name", attributeDict));
CGPoint positionOffset; CGPoint positionOffset;
positionOffset.x = (float)atof(valueForKey("x", &attributeDict)) * pTMXMapInfo->getTileSize().width; positionOffset.x = (float)atof(valueForKey("x", attributeDict)) * pTMXMapInfo->getTileSize().width;
positionOffset.y = (float)atof(valueForKey("y", &attributeDict)) * pTMXMapInfo->getTileSize().height; positionOffset.y = (float)atof(valueForKey("y", attributeDict)) * pTMXMapInfo->getTileSize().height;
objectGroup->setPositionOffset(positionOffset); objectGroup->setPositionOffset(positionOffset);
pTMXMapInfo->getObjectGroups()->addObject(objectGroup); pTMXMapInfo->getObjectGroups()->addObject(objectGroup);
@ -317,14 +340,14 @@ namespace cocos2d {
CCTMXTilesetInfo *tileset = pTMXMapInfo->getTilesets()->getLastObject(); CCTMXTilesetInfo *tileset = pTMXMapInfo->getTilesets()->getLastObject();
// build full path // build full path
std::string imagename = valueForKey("source", &attributeDict); std::string imagename = valueForKey("source", attributeDict);
tileset->m_sSourceImage = CCFileUtils::fullPathFromRelativePath(imagename.c_str()); tileset->m_sSourceImage = CCFileUtils::fullPathFromRelativePath(imagename.c_str());
} }
else if(elementName == "data") else if(elementName == "data")
{ {
std::string encoding = valueForKey("encoding", &attributeDict); std::string encoding = valueForKey("encoding", attributeDict);
std::string compression = valueForKey("compression", &attributeDict); std::string compression = valueForKey("compression", attributeDict);
if( encoding == "base64" ) if( encoding == "base64" )
{ {
@ -353,32 +376,32 @@ namespace cocos2d {
// Set the name of the object to the value for "name" // Set the name of the object to the value for "name"
std::string key = "name"; std::string key = "name";
std::string value = valueForKey("name", &attributeDict); std::string value = valueForKey("name", attributeDict);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
// Assign all the attributes as key/name pairs in the properties dictionary // Assign all the attributes as key/name pairs in the properties dictionary
key = "type"; key = "type";
value = valueForKey("type", &attributeDict); value = valueForKey("type", attributeDict);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
int x = atoi(valueForKey("x", &attributeDict)) + (int)objectGroup->getPositionOffset().x; int x = atoi(valueForKey("x", attributeDict)) + (int)objectGroup->getPositionOffset().x;
key = "x"; key = "x";
value = itoa(x, buffer, 10); value = itoa(x, buffer, 10);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
int y = atoi(valueForKey("y", &attributeDict)) + (int)objectGroup->getPositionOffset().y; int y = atoi(valueForKey("y", attributeDict)) + (int)objectGroup->getPositionOffset().y;
// Correct y position. (Tiled uses Flipped, cocos2d uses Standard) // Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
y = (int)(pTMXMapInfo->getMapSize().height * pTMXMapInfo->getTileSize().height) - y - atoi(valueForKey("height", &attributeDict)); y = (int)(pTMXMapInfo->getMapSize().height * pTMXMapInfo->getTileSize().height) - y - atoi(valueForKey("height", attributeDict));
key = "y"; key = "y";
value = itoa(y, buffer, 10); value = itoa(y, buffer, 10);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
key = "width"; key = "width";
value = valueForKey("width", &attributeDict); value = valueForKey("width", attributeDict);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
key = "height"; key = "height";
value = valueForKey("height", &attributeDict); value = valueForKey("height", attributeDict);
dict->insert(StringToStringPair(key, value)); dict->insert(StringToStringPair(key, value));
// Add the object to the objectGroup // Add the object to the objectGroup
@ -393,13 +416,13 @@ namespace cocos2d {
if ( pTMXMapInfo->getParentElement() == TMXPropertyNone ) if ( pTMXMapInfo->getParentElement() == TMXPropertyNone )
{ {
CCLOG( "TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'", CCLOG( "TMX tile map: Parent element is unsupported. Cannot add property named '%s' with value '%s'",
valueForKey("name", &attributeDict), valueForKey("value",&attributeDict) ); valueForKey("name", attributeDict), valueForKey("value",attributeDict) );
} }
else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap ) else if ( pTMXMapInfo->getParentElement() == TMXPropertyMap )
{ {
// The parent element is the map // The parent element is the map
std::string value = valueForKey("value", &attributeDict); std::string value = valueForKey("value", attributeDict);
std::string key = valueForKey("name", &attributeDict); std::string key = valueForKey("name", attributeDict);
pTMXMapInfo->getProperties()->insert(StringToStringPair(key, value)); pTMXMapInfo->getProperties()->insert(StringToStringPair(key, value));
} }
@ -407,8 +430,8 @@ namespace cocos2d {
{ {
// The parent element is the last layer // The parent element is the last layer
CCTMXLayerInfo *layer = pTMXMapInfo->getLayers()->getLastObject(); CCTMXLayerInfo *layer = pTMXMapInfo->getLayers()->getLastObject();
std::string value = valueForKey("value", &attributeDict); std::string value = valueForKey("value", attributeDict);
std::string key = valueForKey("name", &attributeDict); std::string key = valueForKey("name", attributeDict);
// Add the property to the layer // Add the property to the layer
layer->m_pProperties->insert(StringToStringPair(key, value)); layer->m_pProperties->insert(StringToStringPair(key, value));
@ -417,8 +440,8 @@ namespace cocos2d {
{ {
// The parent element is the last object group // The parent element is the last object group
CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject(); CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject();
std::string key = valueForKey("name", &attributeDict); std::string key = valueForKey("name", attributeDict);
std::string value = valueForKey("value", &attributeDict); std::string value = valueForKey("value", attributeDict);
objectGroup->getProperties()->insert(StringToStringPair(key, value)); objectGroup->getProperties()->insert(StringToStringPair(key, value));
} }
@ -428,8 +451,8 @@ namespace cocos2d {
CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject(); CCTMXObjectGroup *objectGroup = pTMXMapInfo->getObjectGroups()->getLastObject();
StringToStringDictionary *dict = *objectGroup->getObjects()->begin(); StringToStringDictionary *dict = *objectGroup->getObjects()->begin();
std::string propertyName = valueForKey("name", &attributeDict); std::string propertyName = valueForKey("name", attributeDict);
std::string propertyValue = valueForKey("value", &attributeDict); std::string propertyValue = valueForKey("value", attributeDict);
dict->insert(StringToStringPair(propertyName, propertyValue)); dict->insert(StringToStringPair(propertyName, propertyValue));
} }
@ -437,11 +460,16 @@ namespace cocos2d {
{ {
StringToStringDictionary *dict; StringToStringDictionary *dict;
dict = pTMXMapInfo->getTileProperties()->find(pTMXMapInfo->getParentGID())->second; dict = pTMXMapInfo->getTileProperties()->find(pTMXMapInfo->getParentGID())->second;
std::string propertyName = valueForKey("name", &attributeDict); std::string propertyName = valueForKey("name", attributeDict);
std::string propertyValue = valueForKey("value", &attributeDict); std::string propertyValue = valueForKey("value", attributeDict);
dict->insert(StringToStringPair(propertyName, propertyValue)); dict->insert(StringToStringPair(propertyName, propertyValue));
} }
} }
if (attributeDict)
{
attributeDict->clear();
delete attributeDict;
}
} }
void tmx_endElement(void *ctx, const xmlChar *name) void tmx_endElement(void *ctx, const xmlChar *name)