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.
|
|
|
|
****************************************************************************/
|
2013-08-26 01:50:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCSpriteBatchNode.h"
|
2013-08-26 01:50:29 +08:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#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 "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-07-20 13:01:27 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, 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-07-20 13:01:27 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::create(const char *fileImage, 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-07-20 13:01:27 +08:00
|
|
|
bool SpriteBatchNode::initWithTexture(Texture2D *tex, int capacity)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(capacity>=0, "Capacity must be >= 0");
|
|
|
|
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
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-07-25 17:48:22 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
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-07-20 13:01:27 +08:00
|
|
|
bool SpriteBatchNode::initWithFile(const char* fileImage, int capacity)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
Texture2D *texture2D = TextureCache::getInstance()->addImage(fileImage);
|
|
|
|
return initWithTexture(texture2D, capacity);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(child != NULL, "child should not be null");
|
|
|
|
CCASSERT(dynamic_cast<Sprite*>(child) != NULL, "CCSpriteBatchNode only supports Sprites as children");
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *pSprite = (Sprite*)(child);
|
|
|
|
// check Sprite is using the same texture id
|
2013-07-20 13:01:27 +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
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(child != NULL, "the child should not be null");
|
|
|
|
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-07-20 13:01:27 +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-08-22 10:45:47 +08:00
|
|
|
removeChild((Sprite*)(_children->getObjectAtIndex(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
|
|
|
}
|
|
|
|
|
2013-08-25 01:56:07 +08:00
|
|
|
#if CC_USE_ARRAY_VECTOR
|
|
|
|
static bool objectComparisonLess(const RCPtr<Object>& pp1, const RCPtr<Object>& pp2)
|
|
|
|
{
|
|
|
|
Object *p1 = static_cast<Object*>(pp1);
|
|
|
|
Object *p2 = static_cast<Object*>(pp2);
|
|
|
|
Node *n1 = static_cast<Node*>(p1);
|
|
|
|
Node *n2 = static_cast<Node*>(p2);
|
|
|
|
|
|
|
|
return( n1->getZOrder() < n2->getZOrder() ||
|
|
|
|
( n1->getZOrder() == n2->getZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static bool objectComparisonLess(Object* p1, Object* p2)
|
|
|
|
{
|
|
|
|
Node *n1 = static_cast<Node*>(p1);
|
|
|
|
Node *n2 = static_cast<Node*>(p2);
|
|
|
|
|
|
|
|
return( n1->getZOrder() < n2->getZOrder() ||
|
|
|
|
( n1->getZOrder() == n2->getZOrder() && n1->getOrderOfArrival() < n2->getOrderOfArrival() )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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-08-25 01:56:07 +08:00
|
|
|
#if 0
|
2013-08-20 08:09:28 +08:00
|
|
|
int i = 0,j = 0,length = _children->count();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
// insertion sort
|
2012-04-19 14:35:52 +08:00
|
|
|
for(i=1; i<length; i++)
|
|
|
|
{
|
|
|
|
j = i-1;
|
2013-08-20 08:27:31 +08:00
|
|
|
auto tempI = static_cast<Node*>( _children->getObjectAtIndex(i) );
|
|
|
|
auto tempJ = static_cast<Node*>( _children->getObjectAtIndex(j) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
//continue moving element downwards while zOrder is smaller or when zOrder is the same but mutatedIndex is smaller
|
|
|
|
while(j>=0 && ( tempI->getZOrder() < tempJ->getZOrder() ||
|
|
|
|
( tempI->getZOrder() == tempJ->getZOrder() &&
|
|
|
|
tempI->getOrderOfArrival() < tempJ->getOrderOfArrival() ) ) )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-20 08:09:28 +08:00
|
|
|
_children->fastSetObject( tempJ, j+1 );
|
|
|
|
j = j-1;
|
2013-08-20 08:27:31 +08:00
|
|
|
if(j>=0)
|
|
|
|
tempJ = static_cast<Node*>( _children->getObjectAtIndex(j) );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-08-20 08:09:28 +08:00
|
|
|
_children->fastSetObject(tempI, j+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-08-25 01:56:07 +08:00
|
|
|
#else
|
|
|
|
std::sort(std::begin(*_children), std::end(*_children), objectComparisonLess);
|
|
|
|
#endif
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//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-07-26 07:27:24 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObj);
|
|
|
|
updateAtlasIndex(child, &index);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-26 08:47:42 +08:00
|
|
|
Array* array = sprite->getChildren();
|
|
|
|
if (array != NULL)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
count = array->count();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-20 08:09:28 +08:00
|
|
|
if (static_cast<Sprite*>(array->getObjectAtIndex(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;
|
2013-07-26 08:47:42 +08:00
|
|
|
CCARRAY_FOREACH(array,pObj)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Sprite* child = static_cast<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-07-05 16:49:22 +08:00
|
|
|
V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//update the index of other swapped item
|
2013-08-20 08:09:28 +08:00
|
|
|
static_cast<Sprite*>( _descendants->getObjectAtIndex(newIndex) )->setAtlasIndex(oldIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-20 08:09:28 +08:00
|
|
|
std::swap( quads[oldIndex], quads[newIndex] );
|
|
|
|
_descendants->swap( oldIndex, newIndex );
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
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-07-26 09:42:53 +08:00
|
|
|
GL::blendFunc( _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");
|
2013-07-20 13:01:27 +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-07-26 07:27:24 +08:00
|
|
|
Array *children = pobParent->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
if (children && children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-07-26 07:27:24 +08:00
|
|
|
CCARRAY_FOREACH(children, pObject)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObject);
|
|
|
|
if (child && (child->getZOrder() < 0))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
uIndex = rebuildIndexInOrder(child, uIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore self (batch node)
|
|
|
|
if (! pobParent->isEqual(this))
|
|
|
|
{
|
|
|
|
pobParent->setAtlasIndex(uIndex);
|
|
|
|
uIndex++;
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
if (children && children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-07-26 07:27:24 +08:00
|
|
|
CCARRAY_FOREACH(children, pObject)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObject);
|
|
|
|
if (child && (child->getZOrder() >= 0))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
uIndex = rebuildIndexInOrder(child, uIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-26 07:27:24 +08:00
|
|
|
Array *children = pSprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
if (! children || children->count() == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return pSprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-22 10:45:47 +08:00
|
|
|
return highestAtlasIndexInChild((Sprite*)(children->getLastObject()));
|
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-07-26 07:27:24 +08:00
|
|
|
Array *children = pSprite->getChildren();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
if (! children || children->count() == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return pSprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-22 10:45:47 +08:00
|
|
|
return lowestAtlasIndexInChild((Sprite*)(children->getObjectAtIndex(0)));
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
unsigned int SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
Array *pBrothers = sprite->getParent()->getChildren();
|
2013-08-22 10:45:47 +08:00
|
|
|
unsigned int uChildIndex = pBrothers->getIndexOfObject(sprite);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// ignore parent Z if parent is spriteSheet
|
2013-07-26 06:53:24 +08:00
|
|
|
bool bIgnoreParent = (SpriteBatchNode*)(sprite->getParent()) == this;
|
2013-06-20 14:13:12 +08:00
|
|
|
Sprite *pPrevious = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
if (uChildIndex > 0 &&
|
|
|
|
uChildIndex < UINT_MAX)
|
|
|
|
{
|
2013-08-22 10:45:47 +08:00
|
|
|
pPrevious = (Sprite*)(pBrothers->getObjectAtIndex(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-07-26 06:53:24 +08:00
|
|
|
Sprite *p = (Sprite*)(sprite->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-07-26 06:53:24 +08:00
|
|
|
Sprite *p = (Sprite*)(sprite->getParent());
|
2012-04-19 14:35:52 +08:00
|
|
|
return p->getAtlasIndex() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not happen. Error calculating Z on SpriteSheet
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(0, "should not run here");
|
2012-04-19 14:35:52 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_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-08-20 08:09:28 +08:00
|
|
|
_descendants->insertObject(pSprite, uIndex);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// update indices
|
|
|
|
unsigned int i = uIndex+1;
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
Sprite* child = nullptr;
|
2013-08-20 08:09:28 +08:00
|
|
|
for(; i<_descendants->count(); i++){
|
|
|
|
child = static_cast<Sprite*>(_descendants->getObjectAtIndex(i));
|
2013-07-26 07:27:24 +08:00
|
|
|
child->setAtlasIndex(child->getAtlasIndex() + 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// add children recursively
|
2013-07-09 14:29:51 +08:00
|
|
|
Object* pObj = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pSprite->getChildren(), pObj)
|
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
child = static_cast<Sprite*>(pObj);
|
|
|
|
unsigned int idx = atlasIndexForChild(child, child->getZOrder());
|
|
|
|
insertChild(child, idx);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-20 08:09:28 +08:00
|
|
|
_descendants->addObject(sprite);
|
|
|
|
unsigned int index=_descendants->count()-1;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
sprite->setAtlasIndex(index);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_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-07-09 14:29:51 +08:00
|
|
|
Object* pObj = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(sprite->getChildren(), pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
appendChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// remove from TextureAtlas
|
2013-07-26 06:53:24 +08:00
|
|
|
_textureAtlas->removeQuadAtIndex(sprite->getAtlasIndex());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// Cleanup sprite. It might be reused (issue #569)
|
2013-07-26 06:53:24 +08:00
|
|
|
sprite->setBatchNode(NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-22 10:45:47 +08:00
|
|
|
unsigned int uIndex = _descendants->getIndexOfObject(sprite);
|
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-08-22 10:45:47 +08:00
|
|
|
Sprite* s = (Sprite*)(_descendants->getObjectAtIndex(uIndex));
|
2012-04-19 14:35:52 +08:00
|
|
|
s->setAtlasIndex( s->getAtlasIndex() - 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove children recursively
|
2013-07-26 07:27:24 +08:00
|
|
|
Array *children = sprite->getChildren();
|
|
|
|
if (children && children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-07-26 07:27:24 +08:00
|
|
|
CCARRAY_FOREACH(children, pObject)
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObject);
|
|
|
|
if (child)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
removeSpriteFromAtlas(child);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CocosNodeTexture protocol
|
2013-07-05 16:49:22 +08:00
|
|
|
void SpriteBatchNode::setBlendFunc(const BlendFunc &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-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& SpriteBatchNode::getBlendFunc(void) const
|
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-07-23 18:26:26 +08:00
|
|
|
Texture2D* SpriteBatchNode::getTexture(void) const
|
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-07-22 14:28:19 +08:00
|
|
|
void SpriteBatchNode::insertQuadFromSprite(Sprite *sprite, int index)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( sprite != NULL, "Argument must be non-NULL");
|
|
|
|
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);
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
V3F_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-07-22 14:28:19 +08:00
|
|
|
void SpriteBatchNode::updateQuadFromSprite(Sprite *sprite, int index)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(sprite != NULL, "Argument must be non-nil");
|
|
|
|
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-07-22 14:28:19 +08:00
|
|
|
SpriteBatchNode * SpriteBatchNode::addSpriteWithoutQuad(Sprite*child, int z, int aTag)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( child != NULL, "Argument must be non-NULL");
|
|
|
|
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-07-26 07:27:24 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(pObject);
|
|
|
|
if (child && (child->getAtlasIndex() >= z))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
++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
|