axmol/cocos/2d/CCSpriteBatchNode.cpp

702 lines
20 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2009-2010 Ricardo Quesada
Copyright (c) 2009 Matt Oswald
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies 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
#include "2d/CCSpriteBatchNode.h"
#include "2d/CCSprite.h"
2014-05-10 09:39:25 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "renderer/CCTextureCache.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCRenderer.h"
#include "renderer/CCQuadCommand.h"
2014-04-09 22:30:49 +08:00
#include "deprecated/CCString.h" // For StringUtils::format
NS_CC_BEGIN
/*
* creation with Texture2D
*/
2013-12-12 12:07:20 +08:00
SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
batchNode->initWithTexture(tex, capacity);
batchNode->autorelease();
return batchNode;
}
/*
* creation with File Image
*/
SpriteBatchNode* SpriteBatchNode::create(const std::string& fileImage, ssize_t capacity/* = DEFAULT_CAPACITY*/)
{
SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
batchNode->initWithFile(fileImage, capacity);
batchNode->autorelease();
return batchNode;
}
/*
* init with Texture2D
*/
2013-12-12 12:07:20 +08:00
bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity)
{
CCASSERT(capacity>=0, "Capacity must be >= 0");
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
if(!tex->hasPremultipliedAlpha())
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
_textureAtlas = new (std::nothrow) TextureAtlas();
if (capacity == 0)
{
capacity = DEFAULT_CAPACITY;
}
_textureAtlas->initWithTexture(tex, capacity);
updateBlendFunc();
_children.reserve(capacity);
_descendants.reserve(capacity);
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
return true;
}
bool SpriteBatchNode::init()
{
Texture2D * texture = new (std::nothrow) Texture2D();
texture->autorelease();
return this->initWithTexture(texture, 0);
}
/*
* init with FileImage
*/
bool SpriteBatchNode::initWithFile(const std::string& fileImage, ssize_t capacity)
{
Texture2D *texture2D = Director::getInstance()->getTextureCache()->addImage(fileImage);
return initWithTexture(texture2D, capacity);
}
SpriteBatchNode::SpriteBatchNode()
: _textureAtlas(nullptr)
{
}
SpriteBatchNode::~SpriteBatchNode()
{
CC_SAFE_RELEASE(_textureAtlas);
}
// override visit
// don't call visit on it's children
void SpriteBatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
CC_PROFILER_START_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
// CAREFUL:
// This visit is almost identical to CocosNode#visit
// with the exception that it doesn't call visit on it's children
//
// The alternative is to have a void Sprite#visit, but
// although this is less maintainable, is faster
//
if (! _visible || !isVisitableByVisitingCamera())
{
return;
}
sortAllChildren();
uint32_t flags = processParentFlags(parentTransform, parentFlags);
// IMPORTANT:
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
// To ease the migration to v3.0, we still support the Mat4 stack,
// but it is deprecated and your code should not rely on it
Director* director = Director::getInstance();
director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
draw(renderer, _modelViewTransform, flags);
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
// FIX ME: Why need to set _orderOfArrival to 0??
// Please refer to https://github.com/cocos2d/cocos2d-x/pull/6920
// setOrderOfArrival(0);
CC_PROFILER_STOP_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
}
void SpriteBatchNode::addChild(Node *child, int zOrder, int tag)
{
CCASSERT(child != nullptr, "child should not be null");
CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
Sprite *sprite = static_cast<Sprite*>(child);
// check Sprite is using the same texture id
CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
Node::addChild(child, zOrder, tag);
appendChild(sprite);
}
2014-06-25 11:27:48 +08:00
void SpriteBatchNode::addChild(Node * child, int zOrder, const std::string &name)
{
CCASSERT(child != nullptr, "child should not be null");
CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
Sprite *sprite = static_cast<Sprite*>(child);
// check Sprite is using the same texture id
CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
Node::addChild(child, zOrder, name);
appendChild(sprite);
}
// override reorderChild
void SpriteBatchNode::reorderChild(Node *child, int zOrder)
{
CCASSERT(child != nullptr, "the child should not be null");
CCASSERT(_children.contains(child), "Child doesn't belong to Sprite");
if (zOrder == child->getLocalZOrder())
{
return;
}
//set the z-order and sort later
Node::reorderChild(child, zOrder);
}
// override remove child
void SpriteBatchNode::removeChild(Node *child, bool cleanup)
{
Sprite *sprite = static_cast<Sprite*>(child);
// explicit null handling
if (sprite == nullptr)
{
return;
}
CCASSERT(_children.contains(sprite), "sprite batch node should contain the child");
// cleanup before removing
removeSpriteFromAtlas(sprite);
Node::removeChild(sprite, cleanup);
}
2013-12-12 12:07:20 +08:00
void SpriteBatchNode::removeChildAtIndex(ssize_t index, bool doCleanup)
{
CCASSERT(index>=0 && index < _children.size(), "Invalid index");
removeChild(_children.at(index), doCleanup);
}
void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
{
// Invalidate atlas index. issue #569
// useSelfRender should be performed on all descendants. issue #1216
for(const auto &sprite: _descendants) {
sprite->setBatchNode(nullptr);
}
Node::removeAllChildrenWithCleanup(doCleanup);
_descendants.clear();
_textureAtlas->removeAllQuads();
}
//override sortAllChildren
void SpriteBatchNode::sortAllChildren()
{
if (_reorderChildDirty)
{
std::sort(std::begin(_children), std::end(_children), nodeComparisonLess);
//sorted now check all children
if (!_children.empty())
{
//first sort all children recursively based on zOrder
for(const auto &child: _children) {
child->sortAllChildren();
}
2013-12-12 12:07:20 +08:00
ssize_t index=0;
//fast dispatch, give every child a new atlasIndex based on their relative zOrder (keep parent -> child relations intact)
// and at the same time reorder descendants and the quads to the right index
for(const auto &child: _children) {
Sprite* sp = static_cast<Sprite*>(child);
updateAtlasIndex(sp, &index);
}
}
_reorderChildDirty=false;
}
}
2013-12-12 12:07:20 +08:00
void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, ssize_t* curIndex)
{
auto& array = sprite->getChildren();
2013-12-05 17:19:01 +08:00
auto count = array.size();
2013-12-12 12:07:20 +08:00
ssize_t 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;
if (array.at(0)->getLocalZOrder() >= 0)
{
//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;
}
for(const auto &child: array) {
Sprite* sp = static_cast<Sprite*>(child);
if (needNewIndex && sp->getLocalZOrder() >= 0)
{
oldIndex = sprite->getAtlasIndex();
sprite->setAtlasIndex(*curIndex);
sprite->setOrderOfArrival(0);
if (oldIndex != *curIndex) {
this->swap(oldIndex, *curIndex);
}
(*curIndex)++;
needNewIndex = false;
}
updateAtlasIndex(sp, 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-12-12 12:07:20 +08:00
void SpriteBatchNode::swap(ssize_t oldIndex, ssize_t newIndex)
{
2013-09-08 11:26:38 +08:00
CCASSERT(oldIndex>=0 && oldIndex < (int)_descendants.size() && newIndex >=0 && newIndex < (int)_descendants.size(), "Invalid index");
V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
std::swap( quads[oldIndex], quads[newIndex] );
//update the index of other swapped item
auto oldIt = std::next( _descendants.begin(), oldIndex );
auto newIt = std::next( _descendants.begin(), newIndex );
(*newIt)->setAtlasIndex(oldIndex);
// (*oldIt)->setAtlasIndex(newIndex);
std::swap( *oldIt, *newIt );
}
void SpriteBatchNode::reorderBatch(bool reorder)
{
_reorderChildDirty=reorder;
}
void SpriteBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
// Optimization: Fast Dispatch
if( _textureAtlas->getTotalQuads() == 0 )
{
return;
}
2015-01-07 17:08:04 +08:00
for (const auto &child : _children)
{
#if CC_USE_PHYSICS
auto physicsBody = child->getPhysicsBody();
if (physicsBody)
{
child->updateTransformFromPhysics(transform, flags);
}
#endif
child->updateTransform();
2015-01-07 17:08:04 +08:00
}
2015-01-16 06:00:49 +08:00
_batchCommand.init(_globalZOrder, getGLProgram(), _blendFunc, _textureAtlas, transform, flags);
renderer->addCommand(&_batchCommand);
}
void SpriteBatchNode::increaseAtlasCapacity()
{
// 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-12-12 12:07:20 +08:00
ssize_t quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3;
CCLOG("cocos2d: SpriteBatchNode: resizing TextureAtlas capacity from [%d] to [%d].",
static_cast<int>(_textureAtlas->getCapacity()),
static_cast<int>(quantity));
if (! _textureAtlas->resizeCapacity(quantity))
{
// serious problems
CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
CCASSERT(false, "Not enough memory to resize the atlas");
}
}
2013-12-12 12:07:20 +08:00
ssize_t SpriteBatchNode::rebuildIndexInOrder(Sprite *parent, ssize_t index)
{
CCASSERT(index>=0 && index < _children.size(), "Invalid index");
auto& children = parent->getChildren();
for(const auto &child: children) {
Sprite* sp = static_cast<Sprite*>(child);
if (sp && (sp->getLocalZOrder() < 0))
{
index = rebuildIndexInOrder(sp, index);
}
}
// ignore self (batch node)
if (parent != static_cast<Ref*>(this))
{
parent->setAtlasIndex(index);
index++;
}
for(const auto &child: children) {
Sprite* sp = static_cast<Sprite*>(child);
if (sp && (sp->getLocalZOrder() >= 0))
{
index = rebuildIndexInOrder(sp, index);
}
}
return index;
}
2013-12-12 12:07:20 +08:00
ssize_t SpriteBatchNode::highestAtlasIndexInChild(Sprite *sprite)
{
auto& children = sprite->getChildren();
if (children.size() == 0)
{
return sprite->getAtlasIndex();
}
else
{
return highestAtlasIndexInChild( static_cast<Sprite*>(children.back()));
}
}
2013-12-12 12:07:20 +08:00
ssize_t SpriteBatchNode::lowestAtlasIndexInChild(Sprite *sprite)
{
auto& children = sprite->getChildren();
if (children.size() == 0)
{
return sprite->getAtlasIndex();
}
else
{
return lowestAtlasIndexInChild(static_cast<Sprite*>(children.at(0)));
}
}
2013-12-12 12:07:20 +08:00
ssize_t SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
{
auto& siblings = sprite->getParent()->getChildren();
2013-12-05 17:19:01 +08:00
auto childIndex = siblings.getIndex(sprite);
// ignore parent Z if parent is spriteSheet
bool ignoreParent = (SpriteBatchNode*)(sprite->getParent()) == this;
Sprite *prev = nullptr;
if (childIndex > 0 && childIndex != -1)
{
prev = static_cast<Sprite*>(siblings.at(childIndex - 1));
}
// first child of the sprite sheet
if (ignoreParent)
{
if (childIndex == 0)
{
return 0;
}
return highestAtlasIndexInChild(prev) + 1;
}
// parent is a Sprite, so, it must be taken into account
// first child of an Sprite ?
if (childIndex == 0)
{
Sprite *p = static_cast<Sprite*>(sprite->getParent());
// 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 ((prev->getLocalZOrder() < 0 && nZ < 0) || (prev->getLocalZOrder() >= 0 && nZ >= 0))
{
return highestAtlasIndexInChild(prev) + 1;
}
// else (previous < 0 and sprite >= 0 )
Sprite *p = static_cast<Sprite*>(sprite->getParent());
return p->getAtlasIndex() + 1;
}
// Should not happen. Error calculating Z on SpriteSheet
CCASSERT(0, "should not run here");
return 0;
}
// addChild helper, faster than insertChild
void SpriteBatchNode::appendChild(Sprite* sprite)
{
_reorderChildDirty=true;
sprite->setBatchNode(this);
sprite->setDirty(true);
if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity()) {
increaseAtlasCapacity();
}
_descendants.push_back(sprite);
2013-12-06 16:32:06 +08:00
int index = static_cast<int>(_descendants.size()-1);
sprite->setAtlasIndex(index);
V3F_C4B_T2F_Quad quad = sprite->getQuad();
_textureAtlas->insertQuad(&quad, index);
// add children recursively
auto& children = sprite->getChildren();
for(const auto &child: children) {
appendChild(static_cast<Sprite*>(child));
}
}
void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite)
{
// remove from TextureAtlas
_textureAtlas->removeQuadAtIndex(sprite->getAtlasIndex());
// Cleanup sprite. It might be reused (issue #569)
sprite->setBatchNode(nullptr);
auto it = std::find(_descendants.begin(), _descendants.end(), sprite );
if( it != _descendants.end() )
{
auto next = std::next(it);
Sprite *spr = nullptr;
for(; next != _descendants.end(); ++next) {
spr = *next;
2013-11-11 12:49:38 +08:00
spr->setAtlasIndex( spr->getAtlasIndex() - 1 );
}
_descendants.erase(it);
}
// remove children recursively
auto& children = sprite->getChildren();
for(const auto &obj: children) {
Sprite* child = static_cast<Sprite*>(obj);
if (child)
{
removeSpriteFromAtlas(child);
}
}
}
void SpriteBatchNode::updateBlendFunc()
{
if (! _textureAtlas->getTexture()->hasPremultipliedAlpha())
2014-10-09 10:54:35 +08:00
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
2014-10-09 10:54:35 +08:00
setOpacityModifyRGB(false);
}
else
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setOpacityModifyRGB(true);
}
}
// CocosNodeTexture protocol
void SpriteBatchNode::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
}
const BlendFunc& SpriteBatchNode::getBlendFunc() const
{
return _blendFunc;
}
Texture2D* SpriteBatchNode::getTexture() const
{
return _textureAtlas->getTexture();
}
void SpriteBatchNode::setTexture(Texture2D *texture)
{
_textureAtlas->setTexture(texture);
updateBlendFunc();
}
// SpriteSheet Extension
//implementation SpriteSheet (TMXTiledMapExtension)
2013-12-12 12:07:20 +08:00
void SpriteBatchNode::insertQuadFromSprite(Sprite *sprite, ssize_t index)
{
CCASSERT( sprite != nullptr, "Argument must be non-nullptr");
CCASSERT( dynamic_cast<Sprite*>(sprite), "CCSpriteBatchNode only supports Sprites as children");
2012-11-14 18:05:15 +08:00
// make needed room
while(index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
{
this->increaseAtlasCapacity();
}
//
// update the quad directly. Don't add the sprite to the scene graph
//
sprite->setBatchNode(this);
sprite->setAtlasIndex(index);
V3F_C4B_T2F_Quad quad = sprite->getQuad();
_textureAtlas->insertQuad(&quad, index);
// FIXME:: updateTransform will update the textureAtlas too, using updateQuad.
// FIXME:: so, it should be AFTER the insertQuad
sprite->setDirty(true);
sprite->updateTransform();
}
2013-12-12 12:07:20 +08:00
void SpriteBatchNode::updateQuadFromSprite(Sprite *sprite, ssize_t index)
2012-11-14 18:05:15 +08:00
{
CCASSERT(sprite != nullptr, "Argument must be non-nil");
CCASSERT(dynamic_cast<Sprite*>(sprite) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
2012-11-14 18:05:15 +08:00
// make needed room
while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
2012-11-14 18:05:15 +08:00
{
this->increaseAtlasCapacity();
2012-11-14 18:05:15 +08:00
}
//
// update the quad directly. Don't add the sprite to the scene graph
//
sprite->setBatchNode(this);
2012-11-14 18:05:15 +08:00
sprite->setAtlasIndex(index);
sprite->setDirty(true);
// UpdateTransform updates the textureAtlas quad
sprite->updateTransform();
2012-11-14 18:05:15 +08:00
}
SpriteBatchNode * SpriteBatchNode::addSpriteWithoutQuad(Sprite*child, int z, int aTag)
{
CCASSERT( child != nullptr, "Argument must be non-nullptr");
CCASSERT( dynamic_cast<Sprite*>(child), "CCSpriteBatchNode only supports Sprites as children");
// quad index is Z
child->setAtlasIndex(z);
// FIXME:: optimize with a binary search
auto it = _descendants.begin();
for (; it != _descendants.end(); ++it)
{
if((*it)->getAtlasIndex() >= z)
break;
}
_descendants.insert(it, child);
// IMPORTANT: Call super, and not self. Avoid adding it to the texture atlas array
Node::addChild(child, z, aTag);
//#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;
}
std::string SpriteBatchNode::getDescription() const
{
return StringUtils::format("<SpriteBatchNode | tag = %d>", _tag);
}
NS_CC_END