issue #2790: Vector<T> replaces Array* finished.

This commit is contained in:
James Chen 2013-11-29 11:36:42 +08:00
parent 0d581a15d8
commit cb215bc931
11 changed files with 129 additions and 163 deletions

View File

@ -2129,7 +2129,7 @@ Animate* Animate::reverse() const
if (oldArray.count() > 0) if (oldArray.count() > 0)
{ {
for (auto iter = oldArray.rcbegin(); iter != oldArray.rcend(); ++iter) for (auto iter = oldArray.crbegin(); iter != oldArray.crend(); ++iter)
{ {
AnimationFrame* animFrame = *iter; AnimationFrame* animFrame = *iter;
if (!animFrame) if (!animFrame)

View File

@ -916,19 +916,14 @@ void LayerGradient::setCompressedInterpolation(bool compress)
LayerMultiplex::LayerMultiplex() LayerMultiplex::LayerMultiplex()
: _enabledLayer(0) : _enabledLayer(0)
, _layers(NULL)
{ {
} }
LayerMultiplex::~LayerMultiplex() LayerMultiplex::~LayerMultiplex()
{ {
if (_layers) _layers.forEach([](Layer* layer){
{ layer->cleanup();
for (auto& item : *_layers) });
{
static_cast<Layer*>(item)->cleanup();
}
_layers->release();
}
} }
LayerMultiplex * LayerMultiplex::create(Layer * layer, ...) LayerMultiplex * LayerMultiplex::create(Layer * layer, ...)
@ -967,7 +962,7 @@ LayerMultiplex* LayerMultiplex::create()
return pRet; return pRet;
} }
LayerMultiplex* LayerMultiplex::createWithArray(Array* arrayOfLayers) LayerMultiplex* LayerMultiplex::createWithArray(const Vector<Layer*>& arrayOfLayers)
{ {
LayerMultiplex* pRet = new LayerMultiplex(); LayerMultiplex* pRet = new LayerMultiplex();
if (pRet && pRet->initWithArray(arrayOfLayers)) if (pRet && pRet->initWithArray(arrayOfLayers))
@ -983,17 +978,13 @@ LayerMultiplex* LayerMultiplex::createWithArray(Array* arrayOfLayers)
void LayerMultiplex::addLayer(Layer* layer) void LayerMultiplex::addLayer(Layer* layer)
{ {
CCASSERT(_layers, ""); _layers.addObject(layer);
_layers->addObject(layer);
} }
bool LayerMultiplex::init() bool LayerMultiplex::init()
{ {
if (Layer::init()) if (Layer::init())
{ {
_layers = Array::create();
_layers->retain();
_enabledLayer = 0; _enabledLayer = 0;
return true; return true;
} }
@ -1004,34 +995,32 @@ bool LayerMultiplex::initWithLayers(Layer *layer, va_list params)
{ {
if (Layer::init()) if (Layer::init())
{ {
_layers = Array::createWithCapacity(5); _layers.setCapacity(5);
_layers->retain(); _layers.addObject(layer);
_layers->addObject(layer);
Layer *l = va_arg(params,Layer*); Layer *l = va_arg(params,Layer*);
while( l ) { while( l ) {
_layers->addObject(l); _layers.addObject(l);
l = va_arg(params,Layer*); l = va_arg(params,Layer*);
} }
_enabledLayer = 0; _enabledLayer = 0;
this->addChild((Node*)_layers->getObjectAtIndex(_enabledLayer)); this->addChild(_layers[_enabledLayer]);
return true; return true;
} }
return false; return false;
} }
bool LayerMultiplex::initWithArray(Array* arrayOfLayers) bool LayerMultiplex::initWithArray(const Vector<Layer*>& arrayOfLayers)
{ {
if (Layer::init()) if (Layer::init())
{ {
_layers = Array::createWithCapacity(arrayOfLayers->count()); _layers.setCapacity(arrayOfLayers.count());
_layers->addObjectsFromArray(arrayOfLayers); _layers.addObjectsFromArray(arrayOfLayers);
_layers->retain();
_enabledLayer = 0; _enabledLayer = 0;
this->addChild((Node*)_layers->getObjectAtIndex(_enabledLayer)); this->addChild(_layers[_enabledLayer]);
return true; return true;
} }
return false; return false;
@ -1039,27 +1028,26 @@ bool LayerMultiplex::initWithArray(Array* arrayOfLayers)
void LayerMultiplex::switchTo(int n) void LayerMultiplex::switchTo(int n)
{ {
CCASSERT( n < _layers->count(), "Invalid index in MultiplexLayer switchTo message" ); CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" );
this->removeChild((Node*)_layers->getObjectAtIndex(_enabledLayer), true); this->removeChild(_layers[_enabledLayer], true);
_enabledLayer = n; _enabledLayer = n;
this->addChild((Node*)_layers->getObjectAtIndex(n)); this->addChild(_layers[n]);
} }
void LayerMultiplex::switchToAndReleaseMe(int n) void LayerMultiplex::switchToAndReleaseMe(int n)
{ {
CCASSERT( n < _layers->count(), "Invalid index in MultiplexLayer switchTo message" ); CCASSERT( n < _layers.count(), "Invalid index in MultiplexLayer switchTo message" );
this->removeChild((Node*)_layers->getObjectAtIndex(_enabledLayer), true); this->removeChild(_layers[_enabledLayer], true);
//[layers replaceObjectAtIndex:enabledLayer withObject:[NSNull null]]; _layers.replaceObjectAtIndex(_enabledLayer, nullptr);
_layers->replaceObjectAtIndex(_enabledLayer, NULL);
_enabledLayer = n; _enabledLayer = n;
this->addChild((Node*)_layers->getObjectAtIndex(n)); this->addChild(_layers[n]);
} }
NS_CC_END NS_CC_END

View File

@ -412,7 +412,7 @@ public:
@since v2.1 @since v2.1
* @js NA * @js NA
*/ */
static LayerMultiplex* createWithArray(Array* arrayOfLayers); static LayerMultiplex* createWithArray(const Vector<Layer*>& arrayOfLayers);
/** creates a LayerMultiplex with one or more layers using a variable argument list. /** creates a LayerMultiplex with one or more layers using a variable argument list.
* @code * @code
@ -430,27 +430,7 @@ public:
* @lua NA * @lua NA
*/ */
static LayerMultiplex * createWithLayer(Layer* layer); static LayerMultiplex * createWithLayer(Layer* layer);
/**
* @js ctor
*/
LayerMultiplex();
/**
* @js NA
* @lua NA
*/
virtual ~LayerMultiplex();
virtual bool init();
/** initializes a MultiplexLayer with one or more layers using a variable argument list.
* @js NA
* @lua NA
*/
bool initWithLayers(Layer* layer, va_list params);
/** initializes a MultiplexLayer with an array of layers
@since v2.1
*/
bool initWithArray(Array* arrayOfLayers);
void addLayer(Layer* layer); void addLayer(Layer* layer);
@ -464,8 +444,34 @@ public:
void switchToAndReleaseMe(int n); void switchToAndReleaseMe(int n);
protected: protected:
/**
* @js ctor
*/
LayerMultiplex();
/**
* @js NA
* @lua NA
*/
virtual ~LayerMultiplex();
virtual bool init();
/** initializes a MultiplexLayer with one or more layers using a variable argument list.
* @js NA
* @lua NA
*/
bool initWithLayers(Layer* layer, va_list params);
/** initializes a MultiplexLayer with an array of layers
@since v2.1
*/
bool initWithArray(const Vector<Layer*>& arrayOfLayers);
unsigned int _enabledLayer; unsigned int _enabledLayer;
Array* _layers; Vector<Layer*> _layers;
private:
CC_DISALLOW_COPY_AND_ASSIGN(LayerMultiplex);
}; };

View File

@ -564,7 +564,7 @@ MenuItem* Menu::itemForTouch(Touch *touch)
if (!_children.empty()) if (!_children.empty())
{ {
for (auto iter = _children.rcbegin(); iter != _children.rcend(); ++iter) for (auto iter = _children.crbegin(); iter != _children.crend(); ++iter)
{ {
MenuItem* child = dynamic_cast<MenuItem*>(*iter); MenuItem* child = dynamic_cast<MenuItem*>(*iter);
if (child && child->isVisible() && child->isEnabled()) if (child && child->isVisible() && child->isEnabled())

View File

@ -42,7 +42,7 @@ TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
return ret; return ret;
} }
CC_SAFE_DELETE(ret); CC_SAFE_DELETE(ret);
return NULL; return nullptr;
} }
TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath) TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
@ -54,7 +54,7 @@ TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std:
return ret; return ret;
} }
CC_SAFE_DELETE(ret); CC_SAFE_DELETE(ret);
return NULL; return nullptr;
} }
bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile) bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
@ -69,7 +69,7 @@ bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
{ {
return false; return false;
} }
CCASSERT( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename."); CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
buildWithMapInfo(mapInfo); buildWithMapInfo(mapInfo);
return true; return true;
@ -81,7 +81,7 @@ bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& r
TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath); TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
CCASSERT( mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found. Please check the filename."); CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
buildWithMapInfo(mapInfo); buildWithMapInfo(mapInfo);
return true; return true;
@ -90,15 +90,13 @@ bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& r
TMXTiledMap::TMXTiledMap() TMXTiledMap::TMXTiledMap()
:_mapSize(Size::ZERO) :_mapSize(Size::ZERO)
,_tileSize(Size::ZERO) ,_tileSize(Size::ZERO)
,_objectGroups(NULL) ,_properties(nullptr)
,_properties(NULL) ,_tileProperties(nullptr)
,_tileProperties(NULL)
{ {
} }
TMXTiledMap::~TMXTiledMap() TMXTiledMap::~TMXTiledMap()
{ {
CC_SAFE_RELEASE(_properties); CC_SAFE_RELEASE(_properties);
CC_SAFE_RELEASE(_objectGroups);
CC_SAFE_RELEASE(_tileProperties); CC_SAFE_RELEASE(_tileProperties);
} }
@ -118,14 +116,13 @@ TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo) TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
{ {
Size size = layerInfo->_layerSize; Size size = layerInfo->_layerSize;
Array* tilesets = mapInfo->getTilesets(); auto& tilesets = mapInfo->getTilesets();
if (tilesets && tilesets->count()>0) if (tilesets.count()>0)
{ {
TMXTilesetInfo* tileset = NULL; TMXTilesetInfo* tileset = nullptr;
Object* pObj = NULL; for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
CCARRAY_FOREACH_REVERSE(tilesets, pObj)
{ {
tileset = static_cast<TMXTilesetInfo*>(pObj); tileset = *iter;
if (tileset) if (tileset)
{ {
for( unsigned int y=0; y < size.height; y++ ) for( unsigned int y=0; y < size.height; y++ )
@ -157,7 +154,7 @@ TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInf
// If all the tiles are 0, return empty tileset // If all the tiles are 0, return empty tileset
CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str()); CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
return NULL; return nullptr;
} }
void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo) void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
@ -166,9 +163,7 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
_tileSize = mapInfo->getTileSize(); _tileSize = mapInfo->getTileSize();
_mapOrientation = mapInfo->getOrientation(); _mapOrientation = mapInfo->getOrientation();
CC_SAFE_RELEASE(_objectGroups);
_objectGroups = mapInfo->getObjectGroups(); _objectGroups = mapInfo->getObjectGroups();
CC_SAFE_RETAIN(_objectGroups);
CC_SAFE_RELEASE(_properties); CC_SAFE_RELEASE(_properties);
_properties = mapInfo->getProperties(); _properties = mapInfo->getProperties();
@ -180,30 +175,22 @@ void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
int idx=0; int idx=0;
Array* layers = mapInfo->getLayers(); mapInfo->getLayers().forEach([&idx, this, &mapInfo](TMXLayerInfo* layerInfo){
if (layers && layers->count()>0) if (layerInfo && layerInfo->_visible)
{
TMXLayerInfo* layerInfo = NULL;
Object* pObj = NULL;
CCARRAY_FOREACH(layers, pObj)
{ {
layerInfo = static_cast<TMXLayerInfo*>(pObj); TMXLayer *child = parseLayer(layerInfo, mapInfo);
if (layerInfo && layerInfo->_visible) addChild((Node*)child, idx, idx);
{
TMXLayer *child = parseLayer(layerInfo, mapInfo); // update content size with the max size
addChild((Node*)child, idx, idx); const Size& childSize = child->getContentSize();
Size currentSize = this->getContentSize();
// update content size with the max size currentSize.width = std::max( currentSize.width, childSize.width );
const Size& childSize = child->getContentSize(); currentSize.height = std::max( currentSize.height, childSize.height );
Size currentSize = this->getContentSize(); this->setContentSize(currentSize);
currentSize.width = std::max( currentSize.width, childSize.width );
currentSize.height = std::max( currentSize.height, childSize.height ); idx++;
this->setContentSize(currentSize);
idx++;
}
} }
} });
} }
// public // public
@ -231,13 +218,12 @@ TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
{ {
CCASSERT(groupName.size() > 0, "Invalid group name!"); CCASSERT(groupName.size() > 0, "Invalid group name!");
if (_objectGroups && _objectGroups->count()>0) if (_objectGroups.count()>0)
{ {
TMXObjectGroup* objectGroup = NULL; TMXObjectGroup* objectGroup = nullptr;
Object* pObj = NULL; for (auto iter = _objectGroups.cbegin(); iter != _objectGroups.cend(); ++iter)
CCARRAY_FOREACH(_objectGroups, pObj)
{ {
objectGroup = static_cast<TMXObjectGroup*>(pObj); objectGroup = *iter;
if (objectGroup && objectGroup->getGroupName() == groupName) if (objectGroup && objectGroup->getGroupName() == groupName)
{ {
return objectGroup; return objectGroup;
@ -246,7 +232,7 @@ TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
} }
// objectGroup not found // objectGroup not found
return NULL; return nullptr;
} }
String* TMXTiledMap::getProperty(const std::string& propertyName) const String* TMXTiledMap::getProperty(const std::string& propertyName) const

View File

@ -156,10 +156,9 @@ public:
inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; }; inline void setMapOrientation(int mapOrientation) { _mapOrientation = mapOrientation; };
/** object groups */ /** object groups */
inline Array* getObjectGroups() const { return _objectGroups; }; inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
inline void setObjectGroups(Array* groups) { inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
CC_SAFE_RETAIN(groups); inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
CC_SAFE_RELEASE(_objectGroups);
_objectGroups = groups; _objectGroups = groups;
}; };
@ -199,7 +198,7 @@ protected:
/** map orientation */ /** map orientation */
int _mapOrientation; int _mapOrientation;
/** object groups */ /** object groups */
Array* _objectGroups; Vector<TMXObjectGroup*> _objectGroups;
/** properties */ /** properties */
Dictionary* _properties; Dictionary* _properties;

View File

@ -50,7 +50,7 @@ static const char* valueForKey(const char *key, std::unordered_map<std::string,
// implementation TMXLayerInfo // implementation TMXLayerInfo
TMXLayerInfo::TMXLayerInfo() TMXLayerInfo::TMXLayerInfo()
: _name("") : _name("")
, _tiles(NULL) , _tiles(nullptr)
, _ownTiles(true) , _ownTiles(true)
, _minGID(100000) , _minGID(100000)
, _maxGID(0) , _maxGID(0)
@ -67,7 +67,7 @@ TMXLayerInfo::~TMXLayerInfo()
if( _ownTiles && _tiles ) if( _ownTiles && _tiles )
{ {
free(_tiles); free(_tiles);
_tiles = NULL; _tiles = nullptr;
} }
} }
@ -121,7 +121,7 @@ TMXMapInfo * TMXMapInfo::create(const std::string& tmxFile)
return pRet; return pRet;
} }
CC_SAFE_DELETE(pRet); CC_SAFE_DELETE(pRet);
return NULL; return nullptr;
} }
TMXMapInfo * TMXMapInfo::createWithXML(const std::string& tmxString, const std::string& resourcePath) TMXMapInfo * TMXMapInfo::createWithXML(const std::string& tmxString, const std::string& resourcePath)
@ -133,17 +133,11 @@ TMXMapInfo * TMXMapInfo::createWithXML(const std::string& tmxString, const std::
return pRet; return pRet;
} }
CC_SAFE_DELETE(pRet); CC_SAFE_DELETE(pRet);
return NULL; return nullptr;
} }
void TMXMapInfo::internalInit(const std::string& tmxFileName, const std::string& resourcePath) void TMXMapInfo::internalInit(const std::string& tmxFileName, const std::string& resourcePath)
{ {
_tilesets = Array::create();
_tilesets->retain();
_layers = Array::create();
_layers->retain();
if (tmxFileName.size() > 0) if (tmxFileName.size() > 0)
{ {
_TMXFileName = FileUtils::getInstance()->fullPathForFilename(tmxFileName); _TMXFileName = FileUtils::getInstance()->fullPathForFilename(tmxFileName);
@ -154,8 +148,7 @@ void TMXMapInfo::internalInit(const std::string& tmxFileName, const std::string&
_resources = resourcePath; _resources = resourcePath;
} }
_objectGroups = Array::createWithCapacity(4); _objectGroups.setCapacity(4);
_objectGroups->retain();
_properties = new Dictionary(); _properties = new Dictionary();
_properties->init(); _properties->init();
@ -184,13 +177,10 @@ bool TMXMapInfo::initWithTMXFile(const std::string& tmxFile)
TMXMapInfo::TMXMapInfo() TMXMapInfo::TMXMapInfo()
: _mapSize(Size::ZERO) : _mapSize(Size::ZERO)
, _tileSize(Size::ZERO) , _tileSize(Size::ZERO)
, _layers(NULL)
, _tilesets(NULL)
, _objectGroups(NULL)
, _layerAttribs(0) , _layerAttribs(0)
, _storingCharacters(false) , _storingCharacters(false)
, _properties(NULL) , _properties(nullptr)
, _tileProperties(NULL) , _tileProperties(nullptr)
, _currentFirstGID(0) , _currentFirstGID(0)
{ {
} }
@ -198,16 +188,13 @@ TMXMapInfo::TMXMapInfo()
TMXMapInfo::~TMXMapInfo() TMXMapInfo::~TMXMapInfo()
{ {
CCLOGINFO("deallocing TMXMapInfo: %p", this); CCLOGINFO("deallocing TMXMapInfo: %p", this);
CC_SAFE_RELEASE(_tilesets);
CC_SAFE_RELEASE(_layers);
CC_SAFE_RELEASE(_properties); CC_SAFE_RELEASE(_properties);
CC_SAFE_RELEASE(_tileProperties); CC_SAFE_RELEASE(_tileProperties);
CC_SAFE_RELEASE(_objectGroups);
} }
bool TMXMapInfo::parseXMLString(const std::string& xmlString) bool TMXMapInfo::parseXMLString(const std::string& xmlString)
{ {
int len = xmlString.size(); size_t len = xmlString.size();
if (len <= 0) if (len <= 0)
return false; return false;
@ -325,7 +312,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
s.height = (float)atof(valueForKey("tileheight", attributeDict)); s.height = (float)atof(valueForKey("tileheight", attributeDict));
tileset->_tileSize = s; tileset->_tileSize = s;
pTMXMapInfo->getTilesets()->addObject(tileset); pTMXMapInfo->getTilesets().addObject(tileset);
tileset->release(); tileset->release();
} }
} }
@ -333,7 +320,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
{ {
if (pTMXMapInfo->getParentElement() == TMXPropertyLayer) if (pTMXMapInfo->getParentElement() == TMXPropertyLayer)
{ {
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->getLastObject(); TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
Size layerSize = layer->_layerSize; Size layerSize = layer->_layerSize;
unsigned int gid = (unsigned int)atoi(valueForKey("gid", attributeDict)); unsigned int gid = (unsigned int)atoi(valueForKey("gid", attributeDict));
int tilesAmount = layerSize.width*layerSize.height; int tilesAmount = layerSize.width*layerSize.height;
@ -367,7 +354,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
} }
else else
{ {
TMXTilesetInfo* info = (TMXTilesetInfo*)pTMXMapInfo->getTilesets()->getLastObject(); TMXTilesetInfo* info = pTMXMapInfo->getTilesets().getLastObject();
Dictionary *dict = new Dictionary(); Dictionary *dict = new Dictionary();
dict->init(); dict->init();
pTMXMapInfo->setParentGID(info->_firstGid + atoi(valueForKey("id", attributeDict))); pTMXMapInfo->setParentGID(info->_firstGid + atoi(valueForKey("id", attributeDict)));
@ -404,7 +391,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
float y = (float)atof(valueForKey("y", attributeDict)); float y = (float)atof(valueForKey("y", attributeDict));
layer->_offset = Point(x,y); layer->_offset = Point(x,y);
pTMXMapInfo->getLayers()->addObject(layer); pTMXMapInfo->getLayers().addObject(layer);
layer->release(); layer->release();
// The parent element is now "layer" // The parent element is now "layer"
@ -420,7 +407,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
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);
objectGroup->release(); objectGroup->release();
// The parent element is now "objectgroup" // The parent element is now "objectgroup"
@ -429,7 +416,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
} }
else if (elementName == "image") else if (elementName == "image")
{ {
TMXTilesetInfo* tileset = (TMXTilesetInfo*)pTMXMapInfo->getTilesets()->getLastObject(); TMXTilesetInfo* tileset = pTMXMapInfo->getTilesets().getLastObject();
// build full path // build full path
std::string imagename = valueForKey("source", attributeDict); std::string imagename = valueForKey("source", attributeDict);
@ -453,7 +440,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
{ {
pTMXMapInfo->setLayerAttribs(pTMXMapInfo->getLayerAttribs() | TMXLayerAttribNone); pTMXMapInfo->setLayerAttribs(pTMXMapInfo->getLayerAttribs() | TMXLayerAttribNone);
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->getLastObject(); TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
Size layerSize = layer->_layerSize; Size layerSize = layer->_layerSize;
int tilesAmount = layerSize.width*layerSize.height; int tilesAmount = layerSize.width*layerSize.height;
@ -499,7 +486,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if (elementName == "object") else if (elementName == "object")
{ {
char buffer[32] = {0}; char buffer[32] = {0};
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->getLastObject(); TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
// The value for "type" was blank or not a valid class name // The value for "type" was blank or not a valid class name
// Create an instance of TMXObjectInfo to store the object and its properties // Create an instance of TMXObjectInfo to store the object and its properties
@ -572,7 +559,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if ( pTMXMapInfo->getParentElement() == TMXPropertyLayer ) else if ( pTMXMapInfo->getParentElement() == TMXPropertyLayer )
{ {
// The parent element is the last layer // The parent element is the last layer
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->getLastObject(); TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
String *value = new String(valueForKey("value", attributeDict)); String *value = new String(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
@ -583,7 +570,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if ( pTMXMapInfo->getParentElement() == TMXPropertyObjectGroup ) else if ( pTMXMapInfo->getParentElement() == TMXPropertyObjectGroup )
{ {
// The parent element is the last object group // The parent element is the last object group
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->getLastObject(); TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
String *value = new String(valueForKey("value", attributeDict)); String *value = new String(valueForKey("value", attributeDict));
const char* key = valueForKey("name", attributeDict); const char* key = valueForKey("name", attributeDict);
objectGroup->getProperties()->setObject(value, key); objectGroup->getProperties()->setObject(value, key);
@ -593,7 +580,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if ( pTMXMapInfo->getParentElement() == TMXPropertyObject ) else if ( pTMXMapInfo->getParentElement() == TMXPropertyObject )
{ {
// The parent element is the last object // The parent element is the last object
TMXObjectGroup* objectGroup = (TMXObjectGroup*)pTMXMapInfo->getObjectGroups()->getLastObject(); TMXObjectGroup* objectGroup = pTMXMapInfo->getObjectGroups().getLastObject();
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject(); Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
const char* propertyName = valueForKey("name", attributeDict); const char* propertyName = valueForKey("name", attributeDict);
@ -614,7 +601,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if (elementName == "polygon") else if (elementName == "polygon")
{ {
// find parent object's dict and add polygon-points to it // find parent object's dict and add polygon-points to it
TMXObjectGroup* objectGroup = (TMXObjectGroup*)_objectGroups->getLastObject(); TMXObjectGroup* objectGroup = _objectGroups.getLastObject();
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject(); Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
// get points value string // get points value string
@ -667,7 +654,7 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
else if (elementName == "polyline") else if (elementName == "polyline")
{ {
// find parent object's dict and add polyline-points to it // find parent object's dict and add polyline-points to it
TMXObjectGroup* objectGroup = (TMXObjectGroup*)_objectGroups->getLastObject(); TMXObjectGroup* objectGroup = _objectGroups.getLastObject();
Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject(); Dictionary* dict = (Dictionary*)objectGroup->getObjects()->getLastObject();
// get points value string // get points value string
@ -739,7 +726,7 @@ void TMXMapInfo::endElement(void *ctx, const char *name)
{ {
pTMXMapInfo->setStoringCharacters(false); pTMXMapInfo->setStoringCharacters(false);
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->getLastObject(); TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
std::string currentString = pTMXMapInfo->getCurrentString(); std::string currentString = pTMXMapInfo->getCurrentString();
unsigned char *buffer; unsigned char *buffer;
@ -763,7 +750,7 @@ void TMXMapInfo::endElement(void *ctx, const char *name)
inflatedLen = (size_t)&inflatedLen; // XXX: to avoid warnings in compiler inflatedLen = (size_t)&inflatedLen; // XXX: to avoid warnings in compiler
free(buffer); free(buffer);
buffer = NULL; buffer = nullptr;
if( ! deflated ) if( ! deflated )
{ {
@ -782,7 +769,7 @@ void TMXMapInfo::endElement(void *ctx, const char *name)
} }
else if (pTMXMapInfo->getLayerAttribs() & TMXLayerAttribNone) else if (pTMXMapInfo->getLayerAttribs() & TMXLayerAttribNone)
{ {
TMXLayerInfo* layer = (TMXLayerInfo*)pTMXMapInfo->getLayers()->getLastObject(); TMXLayerInfo* layer = pTMXMapInfo->getLayers().getLastObject();
Size layerSize = layer->_layerSize; Size layerSize = layer->_layerSize;
int tilesAmount = layerSize.width * layerSize.height; int tilesAmount = layerSize.width * layerSize.height;

View File

@ -32,12 +32,15 @@ THE SOFTWARE.
#include "CCDictionary.h" #include "CCDictionary.h"
#include "CCGeometry.h" #include "CCGeometry.h"
#include "platform/CCSAXParser.h" #include "platform/CCSAXParser.h"
#include "CCVector.h"
#include <string> #include <string>
NS_CC_BEGIN NS_CC_BEGIN
class TMXLayerInfo;
class TMXObjectGroup; class TMXObjectGroup;
class TMXTilesetInfo;
/** @file /** @file
* Internal TMX parser * Internal TMX parser
@ -213,26 +216,23 @@ public:
inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; }; inline void setTileSize(const Size& tileSize) { _tileSize = tileSize; };
/// Layers /// Layers
inline Array* getLayers() const { return _layers; }; inline const Vector<TMXLayerInfo*>& getLayers() const { return _layers; };
inline void setLayers(Array* layers) { inline Vector<TMXLayerInfo*>& getLayers() { return _layers; };
CC_SAFE_RETAIN(layers); inline void setLayers(const Vector<TMXLayerInfo*>& layers) {
CC_SAFE_RELEASE(_layers);
_layers = layers; _layers = layers;
}; };
/// tilesets /// tilesets
inline Array* getTilesets() const { return _tilesets; }; inline const Vector<TMXTilesetInfo*>& getTilesets() const { return _tilesets; };
inline void setTilesets(Array* tilesets) { inline Vector<TMXTilesetInfo*>& getTilesets() { return _tilesets; };
CC_SAFE_RETAIN(tilesets); inline void setTilesets(const Vector<TMXTilesetInfo*>& tilesets) {
CC_SAFE_RELEASE(_tilesets);
_tilesets = tilesets; _tilesets = tilesets;
}; };
/// ObjectGroups /// ObjectGroups
inline Array* getObjectGroups() const { return _objectGroups; }; inline const Vector<TMXObjectGroup*>& getObjectGroups() const { return _objectGroups; };
inline void setObjectGroups(Array* groups) { inline Vector<TMXObjectGroup*>& getObjectGroups() { return _objectGroups; };
CC_SAFE_RETAIN(groups); inline void setObjectGroups(const Vector<TMXObjectGroup*>& groups) {
CC_SAFE_RELEASE(_objectGroups);
_objectGroups = groups; _objectGroups = groups;
}; };
@ -293,11 +293,11 @@ protected:
/// tiles width & height /// tiles width & height
Size _tileSize; Size _tileSize;
/// Layers /// Layers
Array* _layers; Vector<TMXLayerInfo*> _layers;
/// tilesets /// tilesets
Array* _tilesets; Vector<TMXTilesetInfo*> _tilesets;
/// ObjectGroups /// ObjectGroups
Array* _objectGroups; Vector<TMXObjectGroup*> _objectGroups;
/// parent element /// parent element
int _parentElement; int _parentElement;
/// parent GID /// parent GID

View File

@ -102,7 +102,7 @@ bool SAXParser::init(const char *pszEncoding)
return true; return true;
} }
bool SAXParser::parse(const char* pXMLData, unsigned int uDataLength) bool SAXParser::parse(const char* pXMLData, size_t uDataLength)
{ {
tinyxml2::XMLDocument tinyDoc; tinyxml2::XMLDocument tinyDoc;
tinyDoc.Parse(pXMLData, uDataLength); tinyDoc.Parse(pXMLData, uDataLength);

View File

@ -81,7 +81,7 @@ public:
* @js NA * @js NA
* @lua NA * @lua NA
*/ */
bool parse(const char* pXMLData, unsigned int uDataLength); bool parse(const char* pXMLData, size_t uDataLength);
/** /**
* @js NA * @js NA
* @lua NA * @lua NA

View File

@ -346,8 +346,8 @@ public:
reverse_iterator rend() { return _data.rend(); } reverse_iterator rend() { return _data.rend(); }
const_reverse_iterator rend() const { return _data.rend(); } const_reverse_iterator rend() const { return _data.rend(); }
const_reverse_iterator rcbegin() const { return _data.crbegin(); } const_reverse_iterator crbegin() const { return _data.crbegin(); }
const_reverse_iterator rcend() const { return _data.crend(); } const_reverse_iterator crend() const { return _data.crend(); }
protected: protected:
std::vector<T> _data; std::vector<T> _data;