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

@ -75,6 +75,7 @@ CCTMXLayerInfo::CCTMXLayerInfo()
{ {
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)
{ {
@ -200,8 +202,10 @@ CCTMXMapInfo::CCTMXMapInfo()
, 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());
} }
@ -350,6 +362,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
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;

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