issue #1686:synchronize CCTMXXMLParser.cpp

This commit is contained in:
minggo 2013-02-26 17:42:18 +08:00
parent 98249b4754
commit 9d07778bb0
2 changed files with 64 additions and 40 deletions

View File

@ -66,15 +66,16 @@ static const char* valueForKey(const char *key, std::map<std::string, std::strin
} }
// implementation CCTMXLayerInfo // implementation CCTMXLayerInfo
CCTMXLayerInfo::CCTMXLayerInfo() CCTMXLayerInfo::CCTMXLayerInfo()
: m_sName("") : m_sName("")
, m_pTiles(NULL) , m_pTiles(NULL)
, m_bOwnTiles(true) , m_bOwnTiles(true)
, m_uMinGID(100000) , m_uMinGID(100000)
, m_uMaxGID(0) , m_uMaxGID(0)
, m_tOffset(CCPointZero) , m_tOffset(CCPointZero)
{ {
m_pProperties= new CCDictionary();; m_pProperties= new CCDictionary();;
} }
CCTMXLayerInfo::~CCTMXLayerInfo() CCTMXLayerInfo::~CCTMXLayerInfo()
{ {
CCLOGINFO("cocos2d: deallocing."); CCLOGINFO("cocos2d: deallocing.");
@ -177,6 +178,7 @@ void CCTMXMapInfo::internalInit(const char* tmxFileName, const char* resourcePat
m_bStoringCharacters = false; m_bStoringCharacters = false;
m_nLayerAttribs = TMXLayerAttribNone; m_nLayerAttribs = TMXLayerAttribNone;
m_nParentElement = TMXPropertyNone; m_nParentElement = TMXPropertyNone;
m_uCurrentFirstGID = 0;
} }
bool CCTMXMapInfo::initWithXML(const char* tmxString, const char* resourcePath) bool CCTMXMapInfo::initWithXML(const char* tmxString, const char* resourcePath)
{ {
@ -191,17 +193,19 @@ bool CCTMXMapInfo::initWithTMXFile(const char *tmxFile)
} }
CCTMXMapInfo::CCTMXMapInfo() CCTMXMapInfo::CCTMXMapInfo()
:m_tMapSize(CCSizeZero) : m_tMapSize(CCSizeZero)
,m_tTileSize(CCSizeZero) , m_tTileSize(CCSizeZero)
,m_pLayers(NULL) , m_pLayers(NULL)
,m_pTilesets(NULL) , m_pTilesets(NULL)
,m_pObjectGroups(NULL) , m_pObjectGroups(NULL)
,m_nLayerAttribs(0) , m_nLayerAttribs(0)
,m_bStoringCharacters(false) , m_bStoringCharacters(false)
,m_pProperties(NULL) , m_pProperties(NULL)
,m_pTileProperties(NULL) , m_pTileProperties(NULL)
, m_uCurrentFirstGID(0)
{ {
} }
CCTMXMapInfo::~CCTMXMapInfo() CCTMXMapInfo::~CCTMXMapInfo()
{ {
CCLOGINFO("cocos2d: deallocing."); CCLOGINFO("cocos2d: deallocing.");
@ -211,40 +215,48 @@ CCTMXMapInfo::~CCTMXMapInfo()
CC_SAFE_RELEASE(m_pTileProperties); CC_SAFE_RELEASE(m_pTileProperties);
CC_SAFE_RELEASE(m_pObjectGroups); CC_SAFE_RELEASE(m_pObjectGroups);
} }
CCArray* CCTMXMapInfo::getLayers() CCArray* CCTMXMapInfo::getLayers()
{ {
return m_pLayers; return m_pLayers;
} }
void CCTMXMapInfo::setLayers(CCArray* var) void CCTMXMapInfo::setLayers(CCArray* var)
{ {
CC_SAFE_RETAIN(var); CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pLayers); CC_SAFE_RELEASE(m_pLayers);
m_pLayers = var; m_pLayers = var;
} }
CCArray* CCTMXMapInfo::getTilesets() CCArray* CCTMXMapInfo::getTilesets()
{ {
return m_pTilesets; return m_pTilesets;
} }
void CCTMXMapInfo::setTilesets(CCArray* var) void CCTMXMapInfo::setTilesets(CCArray* var)
{ {
CC_SAFE_RETAIN(var); CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pTilesets); CC_SAFE_RELEASE(m_pTilesets);
m_pTilesets = var; m_pTilesets = var;
} }
CCArray* CCTMXMapInfo::getObjectGroups() CCArray* CCTMXMapInfo::getObjectGroups()
{ {
return m_pObjectGroups; return m_pObjectGroups;
} }
void CCTMXMapInfo::setObjectGroups(CCArray* var) void CCTMXMapInfo::setObjectGroups(CCArray* var)
{ {
CC_SAFE_RETAIN(var); CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pObjectGroups); CC_SAFE_RELEASE(m_pObjectGroups);
m_pObjectGroups = var; m_pObjectGroups = var;
} }
CCDictionary * CCTMXMapInfo::getProperties() CCDictionary * CCTMXMapInfo::getProperties()
{ {
return m_pProperties; return m_pProperties;
} }
void CCTMXMapInfo::setProperties(CCDictionary* var) void CCTMXMapInfo::setProperties(CCDictionary* var)
{ {
CC_SAFE_RETAIN(var); CC_SAFE_RETAIN(var);
@ -295,7 +307,7 @@ bool CCTMXMapInfo::parseXMLFile(const char *xmlFilename)
parser.setDelegator(this); parser.setDelegator(this);
return parser.parse(xmlFilename); return parser.parse(CCFileUtils::sharedFileUtils()->fullPathForFilename(xmlFilename).c_str());
} }
@ -306,7 +318,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
CCTMXMapInfo *pTMXMapInfo = this; CCTMXMapInfo *pTMXMapInfo = this;
std::string elementName = (char*)name; std::string elementName = (char*)name;
std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>(); std::map<std::string, std::string> *attributeDict = new std::map<std::string, std::string>();
if(atts && atts[0]) if (atts && atts[0])
{ {
for(int i = 0; atts[i]; i += 2) for(int i = 0; atts[i]; i += 2)
{ {
@ -315,7 +327,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
attributeDict->insert(pair<std::string, std::string>(key, value)); attributeDict->insert(pair<std::string, std::string>(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")
@ -323,11 +335,11 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %@", version.c_str()); CCLOG("cocos2d: TMXFormat: Unsupported TMX version: %@", version.c_str());
} }
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")
pTMXMapInfo->setOrientation(CCTMXOrientationIso); pTMXMapInfo->setOrientation(CCTMXOrientationIso);
else if( orientationStr == "hexagonal") else if(orientationStr == "hexagonal")
pTMXMapInfo->setOrientation(CCTMXOrientationHex); pTMXMapInfo->setOrientation(CCTMXOrientationHex);
else else
CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation()); CCLOG("cocos2d: TMXFomat: Unsupported orientation: %d", pTMXMapInfo->getOrientation());
@ -344,12 +356,13 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// The parent element is now "map" // The parent element is now "map"
pTMXMapInfo->setParentElement(TMXPropertyMap); pTMXMapInfo->setParentElement(TMXPropertyMap);
} }
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 != "")
{ {
// Tileset file will be relative to the map file. So we need to convert it to an absolute path
if (m_sTMXFileName.find_last_of("/") != string::npos) if (m_sTMXFileName.find_last_of("/") != string::npos)
{ {
string dir = m_sTMXFileName.substr(0, m_sTMXFileName.find_last_of("/") + 1); string dir = m_sTMXFileName.substr(0, m_sTMXFileName.find_last_of("/") + 1);
@ -361,13 +374,23 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
} }
externalTilesetFilename = CCFileUtils::sharedFileUtils()->fullPathForFilename(externalTilesetFilename.c_str()); externalTilesetFilename = CCFileUtils::sharedFileUtils()->fullPathForFilename(externalTilesetFilename.c_str());
m_uCurrentFirstGID = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
pTMXMapInfo->parseXMLFile(externalTilesetFilename.c_str()); pTMXMapInfo->parseXMLFile(externalTilesetFilename.c_str());
} }
else else
{ {
CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo(); CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo();
tileset->m_sName = valueForKey("name", attributeDict); tileset->m_sName = valueForKey("name", attributeDict);
if (m_uCurrentFirstGID == 0)
{
tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict)); tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
}
else
{
tileset->m_uFirstGid = m_uCurrentFirstGID;
m_uCurrentFirstGID = 0;
}
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));
CCSize s; CCSize s;
@ -379,7 +402,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
tileset->release(); tileset->release();
} }
} }
else if(elementName == "tile") else if (elementName == "tile")
{ {
CCTMXTilesetInfo* info = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject(); CCTMXTilesetInfo* info = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
CCDictionary *dict = new CCDictionary(); CCDictionary *dict = new CCDictionary();
@ -390,7 +413,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyTile); pTMXMapInfo->setParentElement(TMXPropertyTile);
} }
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);
@ -424,7 +447,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyLayer); pTMXMapInfo->setParentElement(TMXPropertyLayer);
} }
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));
@ -440,7 +463,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyObjectGroup); pTMXMapInfo->setParentElement(TMXPropertyObjectGroup);
} }
else if(elementName == "image") else if (elementName == "image")
{ {
CCTMXTilesetInfo* tileset = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject(); CCTMXTilesetInfo* tileset = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
@ -457,7 +480,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
tileset->m_sSourceImage = m_sResources + (m_sResources.size() ? "/" : "") + imagename; tileset->m_sSourceImage = m_sResources + (m_sResources.size() ? "/" : "") + imagename;
} }
} }
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);
@ -483,7 +506,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
CCAssert( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported" ); CCAssert( pTMXMapInfo->getLayerAttribs() != TMXLayerAttribNone, "TMX tile map: Only base64 and/or gzip/zlib maps are supported" );
} }
else if(elementName == "object") else if (elementName == "object")
{ {
char buffer[32] = {0}; char buffer[32] = {0};
CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject(); CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
@ -509,7 +532,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// X // X
const char* value = valueForKey("x", attributeDict); const char* value = valueForKey("x", attributeDict);
if( value ) if (value)
{ {
int x = atoi(value) + (int)objectGroup->getPositionOffset().x; int x = atoi(value) + (int)objectGroup->getPositionOffset().x;
sprintf(buffer, "%d", x); sprintf(buffer, "%d", x);
@ -520,7 +543,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// Y // Y
value = valueForKey("y", attributeDict); value = valueForKey("y", attributeDict);
if( value ) { if (value) {
int y = atoi(value) + (int)objectGroup->getPositionOffset().y; int y = atoi(value) + (int)objectGroup->getPositionOffset().y;
// Correct y position. (Tiled uses Flipped, cocos2d uses Standard) // Correct y position. (Tiled uses Flipped, cocos2d uses Standard)
@ -539,7 +562,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyObject); pTMXMapInfo->setParentElement(TMXPropertyObject);
} }
else if(elementName == "property") else if (elementName == "property")
{ {
if ( pTMXMapInfo->getParentElement() == TMXPropertyNone ) if ( pTMXMapInfo->getParentElement() == TMXPropertyNone )
{ {

View File

@ -209,6 +209,7 @@ protected:
std::string m_sCurrentString; std::string m_sCurrentString;
//! tile properties //! tile properties
CCDictionary* m_pTileProperties; CCDictionary* m_pTileProperties;
unsigned int m_uCurrentFirstGID;
}; };
// end of tilemap_parallax_nodes group // end of tilemap_parallax_nodes group