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
CCTMXLayerInfo::CCTMXLayerInfo()
: m_sName("")
, m_pTiles(NULL)
, m_bOwnTiles(true)
, m_uMinGID(100000)
, m_uMaxGID(0)
, m_tOffset(CCPointZero)
: m_sName("")
, m_pTiles(NULL)
, m_bOwnTiles(true)
, m_uMinGID(100000)
, m_uMaxGID(0)
, m_tOffset(CCPointZero)
{
m_pProperties= new CCDictionary();;
}
CCTMXLayerInfo::~CCTMXLayerInfo()
{
CCLOGINFO("cocos2d: deallocing.");
@ -177,6 +178,7 @@ void CCTMXMapInfo::internalInit(const char* tmxFileName, const char* resourcePat
m_bStoringCharacters = false;
m_nLayerAttribs = TMXLayerAttribNone;
m_nParentElement = TMXPropertyNone;
m_uCurrentFirstGID = 0;
}
bool CCTMXMapInfo::initWithXML(const char* tmxString, const char* resourcePath)
{
@ -191,17 +193,19 @@ bool CCTMXMapInfo::initWithTMXFile(const char *tmxFile)
}
CCTMXMapInfo::CCTMXMapInfo()
:m_tMapSize(CCSizeZero)
,m_tTileSize(CCSizeZero)
,m_pLayers(NULL)
,m_pTilesets(NULL)
,m_pObjectGroups(NULL)
,m_nLayerAttribs(0)
,m_bStoringCharacters(false)
,m_pProperties(NULL)
,m_pTileProperties(NULL)
: m_tMapSize(CCSizeZero)
, m_tTileSize(CCSizeZero)
, m_pLayers(NULL)
, m_pTilesets(NULL)
, m_pObjectGroups(NULL)
, m_nLayerAttribs(0)
, m_bStoringCharacters(false)
, m_pProperties(NULL)
, m_pTileProperties(NULL)
, m_uCurrentFirstGID(0)
{
}
CCTMXMapInfo::~CCTMXMapInfo()
{
CCLOGINFO("cocos2d: deallocing.");
@ -211,40 +215,48 @@ CCTMXMapInfo::~CCTMXMapInfo()
CC_SAFE_RELEASE(m_pTileProperties);
CC_SAFE_RELEASE(m_pObjectGroups);
}
CCArray* CCTMXMapInfo::getLayers()
{
return m_pLayers;
}
void CCTMXMapInfo::setLayers(CCArray* var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pLayers);
m_pLayers = var;
}
CCArray* CCTMXMapInfo::getTilesets()
{
return m_pTilesets;
}
void CCTMXMapInfo::setTilesets(CCArray* var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pTilesets);
m_pTilesets = var;
}
CCArray* CCTMXMapInfo::getObjectGroups()
{
return m_pObjectGroups;
}
void CCTMXMapInfo::setObjectGroups(CCArray* var)
{
CC_SAFE_RETAIN(var);
CC_SAFE_RELEASE(m_pObjectGroups);
m_pObjectGroups = var;
}
CCDictionary * CCTMXMapInfo::getProperties()
{
return m_pProperties;
}
void CCTMXMapInfo::setProperties(CCDictionary* var)
{
CC_SAFE_RETAIN(var);
@ -295,7 +307,7 @@ bool CCTMXMapInfo::parseXMLFile(const char *xmlFilename)
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;
std::string elementName = (char*)name;
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)
{
@ -315,7 +327,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
attributeDict->insert(pair<std::string, std::string>(key, value));
}
}
if(elementName == "map")
if (elementName == "map")
{
std::string version = valueForKey("version", attributeDict);
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());
}
std::string orientationStr = valueForKey("orientation", attributeDict);
if( orientationStr == "orthogonal")
if (orientationStr == "orthogonal")
pTMXMapInfo->setOrientation(CCTMXOrientationOrtho);
else if ( orientationStr == "isometric")
else if (orientationStr == "isometric")
pTMXMapInfo->setOrientation(CCTMXOrientationIso);
else if( orientationStr == "hexagonal")
else if(orientationStr == "hexagonal")
pTMXMapInfo->setOrientation(CCTMXOrientationHex);
else
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"
pTMXMapInfo->setParentElement(TMXPropertyMap);
}
else if(elementName == "tileset")
else if (elementName == "tileset")
{
// If this is an external tileset then start parsing that
std::string externalTilesetFilename = valueForKey("source", attributeDict);
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)
{
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());
m_uCurrentFirstGID = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
pTMXMapInfo->parseXMLFile(externalTilesetFilename.c_str());
}
else
{
CCTMXTilesetInfo *tileset = new CCTMXTilesetInfo();
tileset->m_sName = valueForKey("name", attributeDict);
tileset->m_uFirstGid = (unsigned int)atoi(valueForKey("firstgid", attributeDict));
if (m_uCurrentFirstGID == 0)
{
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_uMargin = (unsigned int)atoi(valueForKey("margin", attributeDict));
CCSize s;
@ -379,7 +402,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
tileset->release();
}
}
else if(elementName == "tile")
else if (elementName == "tile")
{
CCTMXTilesetInfo* info = (CCTMXTilesetInfo*)pTMXMapInfo->getTilesets()->lastObject();
CCDictionary *dict = new CCDictionary();
@ -390,7 +413,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyTile);
}
else if(elementName == "layer")
else if (elementName == "layer")
{
CCTMXLayerInfo *layer = new CCTMXLayerInfo();
layer->m_sName = valueForKey("name", attributeDict);
@ -424,7 +447,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyLayer);
}
else if(elementName == "objectgroup")
else if (elementName == "objectgroup")
{
CCTMXObjectGroup *objectGroup = new CCTMXObjectGroup();
objectGroup->setGroupName(valueForKey("name", attributeDict));
@ -440,7 +463,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
pTMXMapInfo->setParentElement(TMXPropertyObjectGroup);
}
else if(elementName == "image")
else if (elementName == "image")
{
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;
}
}
else if(elementName == "data")
else if (elementName == "data")
{
std::string encoding = valueForKey("encoding", 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" );
}
else if(elementName == "object")
else if (elementName == "object")
{
char buffer[32] = {0};
CCTMXObjectGroup* objectGroup = (CCTMXObjectGroup*)pTMXMapInfo->getObjectGroups()->lastObject();
@ -509,7 +532,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// X
const char* value = valueForKey("x", attributeDict);
if( value )
if (value)
{
int x = atoi(value) + (int)objectGroup->getPositionOffset().x;
sprintf(buffer, "%d", x);
@ -520,7 +543,7 @@ void CCTMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
// Y
value = valueForKey("y", attributeDict);
if( value ) {
if (value) {
int y = atoi(value) + (int)objectGroup->getPositionOffset().y;
// 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);
}
else if(elementName == "property")
else if (elementName == "property")
{
if ( pTMXMapInfo->getParentElement() == TMXPropertyNone )
{

View File

@ -90,15 +90,15 @@ class CC_DLL CCTMXLayerInfo : public CCObject
{
CC_PROPERTY(CCDictionary*, m_pProperties, Properties);
public:
std::string m_sName;
CCSize m_tLayerSize;
std::string m_sName;
CCSize m_tLayerSize;
unsigned int *m_pTiles;
bool m_bVisible;
unsigned char m_cOpacity;
unsigned char m_cOpacity;
bool m_bOwnTiles;
unsigned int m_uMinGID;
unsigned int m_uMaxGID;
CCPoint m_tOffset;
CCPoint m_tOffset;
public:
CCTMXLayerInfo();
virtual ~CCTMXLayerInfo();
@ -117,15 +117,15 @@ This information is obtained from the TMX file.
class CC_DLL CCTMXTilesetInfo : public CCObject
{
public:
std::string m_sName;
std::string m_sName;
unsigned int m_uFirstGid;
CCSize m_tTileSize;
CCSize m_tTileSize;
unsigned int m_uSpacing;
unsigned int m_uMargin;
//! filename containing the tiles (should be spritesheet / texture atlas)
std::string m_sSourceImage;
std::string m_sSourceImage;
//! size in pixels of the image
CCSize m_tImageSize;
CCSize m_tImageSize;
public:
CCTMXTilesetInfo();
virtual ~CCTMXTilesetInfo();
@ -209,6 +209,7 @@ protected:
std::string m_sCurrentString;
//! tile properties
CCDictionary* m_pTileProperties;
unsigned int m_uCurrentFirstGID;
};
// end of tilemap_parallax_nodes group