2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2009-2010 Ricardo Quesada
|
2012-04-19 14:35:52 +08:00
|
|
|
Copyright (c) 2009 Matt Oswald
|
|
|
|
Copyright (c) 2011 Zynga Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCSpriteBatchNode.h"
|
|
|
|
#include "ccConfig.h"
|
|
|
|
#include "CCSprite.h"
|
|
|
|
#include "effects/CCGrid.h"
|
2012-11-16 14:23:14 +08:00
|
|
|
#include "draw_nodes/CCDrawingPrimitives.h"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "textures/CCTextureCache.h"
|
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "shaders/CCShaderCache.h"
|
|
|
|
#include "shaders/CCGLProgram.h"
|
|
|
|
#include "shaders/ccGLStateCache.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "support/TransformUtils.h"
|
|
|
|
#include "support/CCProfiling.h"
|
|
|
|
// external
|
|
|
|
#include "kazmath/GL/matrix.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* creation with Texture2D
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, unsigned int capacity/* = kDefaultSpriteBatchCapacity*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode *batchNode = new SpriteBatchNode();
|
2012-04-19 14:35:52 +08:00
|
|
|
batchNode->initWithTexture(tex, capacity);
|
|
|
|
batchNode->autorelease();
|
|
|
|
|
|
|
|
return batchNode;
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
/*
|
|
|
|
* creation with File Image
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::create(const char *fileImage, unsigned int capacity/* = kDefaultSpriteBatchCapacity*/)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode *batchNode = new SpriteBatchNode();
|
2012-06-14 15:13:16 +08:00
|
|
|
batchNode->initWithFile(fileImage, capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
batchNode->autorelease();
|
|
|
|
|
|
|
|
return batchNode;
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
/*
|
2013-06-20 14:13:12 +08:00
|
|
|
* init with Texture2D
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteBatchNode::initWithTexture(Texture2D *tex, unsigned int capacity)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = CC_BLEND_SRC;
|
|
|
|
_blendFunc.dst = CC_BLEND_DST;
|
2013-06-20 14:13:12 +08:00
|
|
|
_textureAtlas = new TextureAtlas();
|
2012-06-12 14:33:53 +08:00
|
|
|
|
|
|
|
if (0 == capacity)
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
capacity = kDefaultSpriteBatchCapacity;
|
2012-06-12 14:33:53 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->initWithTexture(tex, capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
updateBlendFunc();
|
|
|
|
|
|
|
|
// no lazy alloc in this node
|
2013-06-20 14:13:12 +08:00
|
|
|
_children = new Array();
|
2013-06-15 14:03:30 +08:00
|
|
|
_children->initWithCapacity(capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
_descendants = new Array();
|
2013-06-15 14:03:30 +08:00
|
|
|
_descendants->initWithCapacity(capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kShader_PositionTextureColor));
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteBatchNode::init()
|
2012-05-31 06:31:08 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D * texture = new Texture2D();
|
2012-05-31 06:31:08 +08:00
|
|
|
texture->autorelease();
|
|
|
|
return this->initWithTexture(texture, 0);
|
|
|
|
}
|
|
|
|
|
2012-03-16 13:42:53 +08:00
|
|
|
/*
|
|
|
|
* init with FileImage
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
bool SpriteBatchNode::initWithFile(const char* fileImage, unsigned int capacity)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D *pTexture2D = TextureCache::sharedTextureCache()->addImage(fileImage);
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithTexture(pTexture2D, capacity);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::SpriteBatchNode()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _textureAtlas(NULL)
|
|
|
|
, _descendants(NULL)
|
2013-04-12 06:02:09 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode::~SpriteBatchNode()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_RELEASE(_textureAtlas);
|
|
|
|
CC_SAFE_RELEASE(_descendants);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// override visit
|
|
|
|
// don't call visit on it's children
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::visit(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROFILER_START_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// CAREFUL:
|
|
|
|
// This visit is almost identical to CocosNode#visit
|
|
|
|
// with the exception that it doesn't call visit on it's children
|
|
|
|
//
|
2013-06-20 14:13:12 +08:00
|
|
|
// The alternative is to have a void Sprite#visit, but
|
2012-09-17 15:02:24 +08:00
|
|
|
// although this is less maintainable, is faster
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _visible)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPushMatrix();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->beforeDraw();
|
2012-04-19 14:35:52 +08:00
|
|
|
transformAncestors();
|
|
|
|
}
|
|
|
|
|
|
|
|
sortAllChildren();
|
|
|
|
transform();
|
|
|
|
|
|
|
|
draw();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_grid && _grid->isActive())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grid->afterDraw(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kmGLPopMatrix();
|
|
|
|
setOrderOfArrival(0);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
CC_PROFILER_STOP_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::addChild(Node *child, int zOrder, int tag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert(child != NULL, "child should not be null");
|
2013-06-20 14:13:12 +08:00
|
|
|
CCAssert(dynamic_cast<Sprite*>(child) != NULL, "CCSpriteBatchNode only supports Sprites as children");
|
|
|
|
Sprite *pSprite = (Sprite*)(child);
|
|
|
|
// check Sprite is using the same texture id
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(pSprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::addChild(child, zOrder, tag);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
appendChild(pSprite);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::addChild(Node *child)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::addChild(child);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::addChild(Node *child, int zOrder)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::addChild(child, zOrder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// override reorderChild
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::reorderChild(Node *child, int zOrder)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert(child != NULL, "the child should not be null");
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(_children->containsObject(child), "Child doesn't belong to Sprite");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (zOrder == child->getZOrder())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//set the z-order and sort later
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::reorderChild(child, zOrder);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// override remove child
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::removeChild(Node *child, bool cleanup)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *pSprite = (Sprite*)(child);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// explicit null handling
|
|
|
|
if (pSprite == NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
CCAssert(_children->containsObject(pSprite), "sprite batch node should contain the child");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// cleanup before removing
|
|
|
|
removeSpriteFromAtlas(pSprite);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::removeChild(pSprite, cleanup);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::removeChildAtIndex(unsigned int uIndex, bool bDoCleanup)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
removeChild((Sprite*)(_children->objectAtIndex(uIndex)), bDoCleanup);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::removeAllChildrenWithCleanup(bool bCleanup)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// Invalidate atlas index. issue #569
|
|
|
|
// useSelfRender should be performed on all descendants. issue #1216
|
2013-06-20 14:13:12 +08:00
|
|
|
arrayMakeObjectsPerformSelectorWithObject(_descendants, setBatchNode, NULL, Sprite*);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::removeAllChildrenWithCleanup(bCleanup);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_descendants->removeAllObjects();
|
|
|
|
_textureAtlas->removeAllQuads();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//override sortAllChildren
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::sortAllChildren()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_reorderChildDirty)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int i = 0,j = 0,length = _children->data->num;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node ** x = (Node**)_children->data->arr;
|
|
|
|
Node *tempItem = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//insertion sort
|
|
|
|
for(i=1; i<length; i++)
|
|
|
|
{
|
|
|
|
tempItem = x[i];
|
|
|
|
j = i-1;
|
|
|
|
|
|
|
|
//continue moving element downwards while zOrder is smaller or when zOrder is the same but orderOfArrival is smaller
|
|
|
|
while(j>=0 && ( tempItem->getZOrder() < x[j]->getZOrder() || ( tempItem->getZOrder() == x[j]->getZOrder() && tempItem->getOrderOfArrival() < x[j]->getOrderOfArrival() ) ) )
|
|
|
|
{
|
|
|
|
x[j+1] = x[j];
|
|
|
|
j--;
|
|
|
|
}
|
|
|
|
|
|
|
|
x[j+1] = tempItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
//sorted now check all children
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//first sort all children recursively based on zOrder
|
2013-06-20 14:13:12 +08:00
|
|
|
arrayMakeObjectsPerformSelector(_children, sortAllChildren, Sprite*);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int index=0;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
//fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact)
|
2012-09-17 15:02:24 +08:00
|
|
|
// and at the same time reorder descendants and the quads to the right index
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = (Sprite*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
updateAtlasIndex(pChild, &index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_reorderChildDirty=false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, int* curIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
unsigned int count = 0;
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pArray = sprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pArray != NULL)
|
|
|
|
{
|
|
|
|
count = pArray->count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int oldIndex = 0;
|
|
|
|
|
|
|
|
if( count == 0 )
|
|
|
|
{
|
|
|
|
oldIndex = sprite->getAtlasIndex();
|
|
|
|
sprite->setAtlasIndex(*curIndex);
|
|
|
|
sprite->setOrderOfArrival(0);
|
|
|
|
if (oldIndex != *curIndex){
|
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool needNewIndex=true;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (((Sprite*) (pArray->data->arr[0]))->getZOrder() >= 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
//all children are in front of the parent
|
|
|
|
oldIndex = sprite->getAtlasIndex();
|
|
|
|
sprite->setAtlasIndex(*curIndex);
|
|
|
|
sprite->setOrderOfArrival(0);
|
|
|
|
if (oldIndex != *curIndex)
|
|
|
|
{
|
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
|
|
|
|
needNewIndex = false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pArray,pObj)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* child = (Sprite*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (needNewIndex && child->getZOrder() >= 0)
|
|
|
|
{
|
|
|
|
oldIndex = sprite->getAtlasIndex();
|
|
|
|
sprite->setAtlasIndex(*curIndex);
|
|
|
|
sprite->setOrderOfArrival(0);
|
|
|
|
if (oldIndex != *curIndex) {
|
|
|
|
this->swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
needNewIndex = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
updateAtlasIndex(child, curIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needNewIndex)
|
|
|
|
{//all children have a zOrder < 0)
|
|
|
|
oldIndex=sprite->getAtlasIndex();
|
|
|
|
sprite->setAtlasIndex(*curIndex);
|
|
|
|
sprite->setOrderOfArrival(0);
|
|
|
|
if (oldIndex!=*curIndex) {
|
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::swap(int oldIndex, int newIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object** x = _descendants->data->arr;
|
2013-06-15 14:03:30 +08:00
|
|
|
ccV3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* tempItem = x[oldIndex];
|
2012-04-19 14:35:52 +08:00
|
|
|
ccV3F_C4B_T2F_Quad tempItemQuad=quads[oldIndex];
|
|
|
|
|
|
|
|
//update the index of other swapped item
|
2013-06-20 14:13:12 +08:00
|
|
|
((Sprite*) x[newIndex])->setAtlasIndex(oldIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
x[oldIndex]=x[newIndex];
|
|
|
|
quads[oldIndex]=quads[newIndex];
|
|
|
|
x[newIndex]=tempItem;
|
|
|
|
quads[newIndex]=tempItemQuad;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::reorderBatch(bool reorder)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_reorderChildDirty=reorder;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::draw(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CC_PROFILER_START("CCSpriteBatchNode - draw");
|
|
|
|
|
|
|
|
// Optimization: Fast Dispatch
|
2013-06-15 14:03:30 +08:00
|
|
|
if( _textureAtlas->getTotalQuads() == 0 )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_NODE_DRAW_SETUP();
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
arrayMakeObjectsPerformSelector(_children, updateTransform, Sprite*);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccGLBlendFunc( _blendFunc.src, _blendFunc.dst );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->drawQuads();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_STOP("CCSpriteBatchNode - draw");
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::increaseAtlasCapacity(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// if we're going beyond the current TextureAtlas's capacity,
|
|
|
|
// all the previously initialized sprites will need to redo their texture coords
|
|
|
|
// this is likely computationally expensive
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: SpriteBatchNode: resizing TextureAtlas capacity from [%lu] to [%lu].",
|
2013-06-15 14:03:30 +08:00
|
|
|
(long)_textureAtlas->getCapacity(),
|
2012-04-19 14:35:52 +08:00
|
|
|
(long)quantity);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _textureAtlas->resizeCapacity(quantity))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// serious problems
|
2012-06-08 14:11:48 +08:00
|
|
|
CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
|
2012-09-17 15:02:24 +08:00
|
|
|
CCAssert(false, "Not enough memory to resize the atlas");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int SpriteBatchNode::rebuildIndexInOrder(Sprite *pobParent, unsigned int uIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pChildren = pobParent->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pChildren && pChildren->count() > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
CCARRAY_FOREACH(pChildren, pObject)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = (Sprite*) pObject;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pChild && (pChild->getZOrder() < 0))
|
|
|
|
{
|
|
|
|
uIndex = rebuildIndexInOrder(pChild, uIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore self (batch node)
|
|
|
|
if (! pobParent->isEqual(this))
|
|
|
|
{
|
|
|
|
pobParent->setAtlasIndex(uIndex);
|
|
|
|
uIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pChildren && pChildren->count() > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
CCARRAY_FOREACH(pChildren, pObject)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = (Sprite*) pObject;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pChild && (pChild->getZOrder() >= 0))
|
|
|
|
{
|
|
|
|
uIndex = rebuildIndexInOrder(pChild, uIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uIndex;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int SpriteBatchNode::highestAtlasIndexInChild(Sprite *pSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pChildren = pSprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! pChildren || pChildren->count() == 0)
|
|
|
|
{
|
|
|
|
return pSprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return highestAtlasIndexInChild((Sprite*)(pChildren->lastObject()));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int SpriteBatchNode::lowestAtlasIndexInChild(Sprite *pSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pChildren = pSprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (! pChildren || pChildren->count() == 0)
|
|
|
|
{
|
|
|
|
return pSprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return lowestAtlasIndexInChild((Sprite*)(pChildren->objectAtIndex(0)));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
unsigned int SpriteBatchNode::atlasIndexForChild(Sprite *pobSprite, int nZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pBrothers = pobSprite->getParent()->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int uChildIndex = pBrothers->indexOfObject(pobSprite);
|
|
|
|
|
|
|
|
// ignore parent Z if parent is spriteSheet
|
2013-06-20 14:13:12 +08:00
|
|
|
bool bIgnoreParent = (SpriteBatchNode*)(pobSprite->getParent()) == this;
|
|
|
|
Sprite *pPrevious = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (uChildIndex > 0 &&
|
|
|
|
uChildIndex < UINT_MAX)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pPrevious = (Sprite*)(pBrothers->objectAtIndex(uChildIndex - 1));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// first child of the sprite sheet
|
|
|
|
if (bIgnoreParent)
|
|
|
|
{
|
|
|
|
if (uChildIndex == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return highestAtlasIndexInChild(pPrevious) + 1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// parent is a Sprite, so, it must be taken into account
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// first child of an Sprite ?
|
2012-04-19 14:35:52 +08:00
|
|
|
if (uChildIndex == 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *p = (Sprite*)(pobSprite->getParent());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// less than parent and brothers
|
|
|
|
if (nZ < 0)
|
|
|
|
{
|
|
|
|
return p->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return p->getAtlasIndex() + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// previous & sprite belong to the same branch
|
|
|
|
if ((pPrevious->getZOrder() < 0 && nZ < 0) || (pPrevious->getZOrder() >= 0 && nZ >= 0))
|
|
|
|
{
|
|
|
|
return highestAtlasIndexInChild(pPrevious) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else (previous < 0 and sprite >= 0 )
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *p = (Sprite*)(pobSprite->getParent());
|
2012-04-19 14:35:52 +08:00
|
|
|
return p->getAtlasIndex() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not happen. Error calculating Z on SpriteSheet
|
|
|
|
CCAssert(0, "should not run here");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add child helper
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::insertChild(Sprite *pSprite, unsigned int uIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pSprite->setBatchNode(this);
|
|
|
|
pSprite->setAtlasIndex(uIndex);
|
|
|
|
pSprite->setDirty(true);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
ccV3F_C4B_T2F_Quad quad = pSprite->getQuad();
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->insertQuad(&quad, uIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArray *descendantsData = _descendants->data;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
ccArrayInsertObjectAtIndex(descendantsData, pSprite, uIndex);
|
|
|
|
|
|
|
|
// update indices
|
|
|
|
unsigned int i = uIndex+1;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
for(; i<descendantsData->num; i++){
|
2013-06-20 14:13:12 +08:00
|
|
|
pChild = (Sprite*)descendantsData->arr[i];
|
2012-04-19 14:35:52 +08:00
|
|
|
pChild->setAtlasIndex(pChild->getAtlasIndex() + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add children recursively
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pSprite->getChildren(), pObj)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pChild = (Sprite*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
unsigned int idx = atlasIndexForChild(pChild, pChild->getZOrder());
|
|
|
|
insertChild(pChild, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// addChild helper, faster than insertChild
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::appendChild(Sprite* sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_reorderChildDirty=true;
|
2012-04-19 14:35:52 +08:00
|
|
|
sprite->setBatchNode(this);
|
|
|
|
sprite->setDirty(true);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity()) {
|
2012-04-19 14:35:52 +08:00
|
|
|
increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
ccArray *descendantsData = _descendants->data;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
ccArrayAppendObjectWithResize(descendantsData, sprite);
|
|
|
|
|
|
|
|
unsigned int index=descendantsData->num-1;
|
|
|
|
|
|
|
|
sprite->setAtlasIndex(index);
|
|
|
|
|
|
|
|
ccV3F_C4B_T2F_Quad quad = sprite->getQuad();
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->insertQuad(&quad, index);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// add children recursively
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(sprite->getChildren(), pObj)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* child = (Sprite*)pObj;
|
2012-04-19 14:35:52 +08:00
|
|
|
appendChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::removeSpriteFromAtlas(Sprite *pobSprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// remove from TextureAtlas
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->removeQuadAtIndex(pobSprite->getAtlasIndex());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Cleanup sprite. It might be reused (issue #569)
|
|
|
|
pobSprite->setBatchNode(NULL);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uIndex = _descendants->indexOfObject(pobSprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (uIndex != UINT_MAX)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_descendants->removeObjectAtIndex(uIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update all sprites beyond this one
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int count = _descendants->count();
|
2012-10-30 18:20:47 +08:00
|
|
|
|
|
|
|
for(; uIndex < count; ++uIndex)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* s = (Sprite*)(_descendants->objectAtIndex(uIndex));
|
2012-04-19 14:35:52 +08:00
|
|
|
s->setAtlasIndex( s->getAtlasIndex() - 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove children recursively
|
2013-06-20 14:13:12 +08:00
|
|
|
Array *pChildren = pobSprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pChildren && pChildren->count() > 0)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2012-03-16 13:42:53 +08:00
|
|
|
CCARRAY_FOREACH(pChildren, pObject)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = (Sprite*) pObject;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pChild)
|
|
|
|
{
|
|
|
|
removeSpriteFromAtlas(pChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::updateBlendFunc(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _textureAtlas->getTexture()->hasPremultipliedAlpha())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc.src = GL_SRC_ALPHA;
|
|
|
|
_blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CocosNodeTexture protocol
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::setBlendFunc(ccBlendFunc blendFunc)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ccBlendFunc SpriteBatchNode::getBlendFunc(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Texture2D* SpriteBatchNode::getTexture(void)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _textureAtlas->getTexture();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::setTexture(Texture2D *texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->setTexture(texture);
|
2012-04-19 14:35:52 +08:00
|
|
|
updateBlendFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// SpriteSheet Extension
|
|
|
|
//implementation SpriteSheet (TMXTiledMapExtension)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::insertQuadFromSprite(Sprite *sprite, unsigned int index)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert( sprite != NULL, "Argument must be non-NULL");
|
2013-06-20 14:13:12 +08:00
|
|
|
CCAssert( dynamic_cast<Sprite*>(sprite), "CCSpriteBatchNode only supports Sprites as children");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
// make needed room
|
2013-06-15 14:03:30 +08:00
|
|
|
while(index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// update the quad directly. Don't add the sprite to the scene graph
|
|
|
|
//
|
|
|
|
sprite->setBatchNode(this);
|
|
|
|
sprite->setAtlasIndex(index);
|
|
|
|
|
|
|
|
ccV3F_C4B_T2F_Quad quad = sprite->getQuad();
|
2013-06-15 14:03:30 +08:00
|
|
|
_textureAtlas->insertQuad(&quad, index);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
// XXX: updateTransform will update the textureAtlas too, using updateQuad.
|
2012-04-19 14:35:52 +08:00
|
|
|
// XXX: so, it should be AFTER the insertQuad
|
|
|
|
sprite->setDirty(true);
|
|
|
|
sprite->updateTransform();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void SpriteBatchNode::updateQuadFromSprite(Sprite *sprite, unsigned int index)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
CCAssert(sprite != NULL, "Argument must be non-nil");
|
2013-06-20 14:13:12 +08:00
|
|
|
CCAssert(dynamic_cast<Sprite*>(sprite) != NULL, "CCSpriteBatchNode only supports Sprites as children");
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
// make needed room
|
2013-06-15 14:03:30 +08:00
|
|
|
while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
this->increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// update the quad directly. Don't add the sprite to the scene graph
|
|
|
|
//
|
|
|
|
sprite->setBatchNode(this);
|
|
|
|
sprite->setAtlasIndex(index);
|
|
|
|
|
|
|
|
sprite->setDirty(true);
|
|
|
|
|
|
|
|
// UpdateTransform updates the textureAtlas quad
|
|
|
|
sprite->updateTransform();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
SpriteBatchNode * SpriteBatchNode::addSpriteWithoutQuad(Sprite*child, unsigned int z, int aTag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
CCAssert( child != NULL, "Argument must be non-NULL");
|
2013-06-20 14:13:12 +08:00
|
|
|
CCAssert( dynamic_cast<Sprite*>(child), "CCSpriteBatchNode only supports Sprites as children");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// quad index is Z
|
|
|
|
child->setAtlasIndex(z);
|
|
|
|
|
|
|
|
// XXX: optimize with a binary search
|
|
|
|
int i=0;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_descendants, pObject)
|
2012-03-23 17:31:28 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite* pChild = (Sprite*) pObject;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pChild && (pChild->getAtlasIndex() >= z))
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_descendants->insertObject(child, i);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::addChild(child, z, aTag);
|
2012-04-19 14:35:52 +08:00
|
|
|
//#issue 1262 don't use lazy sorting, tiles are added as quads not as sprites, so sprites need to be added in order
|
|
|
|
reorderBatch(false);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|