123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- #include "2d/CCSpriteBatchNode.h"
- #include "2d/CCSprite.h"
- #include "base/CCDirector.h"
- #include "base/CCProfiling.h"
- #include "base/ccUTF8.h"
- #include "renderer/CCTextureCache.h"
- #include "renderer/CCRenderer.h"
- #include "renderer/CCQuadCommand.h"
- NS_CC_BEGIN
- SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, ssize_t capacity)
- {
- SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
- if(batchNode && batchNode->initWithTexture(tex, capacity))
- {
- batchNode->autorelease();
- return batchNode;
- }
-
- delete batchNode;
- return nullptr;
- }
- SpriteBatchNode* SpriteBatchNode::create(const std::string& fileImage, ssize_t capacity)
- {
- SpriteBatchNode *batchNode = new (std::nothrow) SpriteBatchNode();
- if(batchNode && batchNode->initWithFile(fileImage, capacity))
- {
- batchNode->autorelease();
- return batchNode;
- }
-
- delete batchNode;
- return nullptr;
- }
- bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity)
- {
- if(tex == nullptr)
- {
- return false;
- }
-
- 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, tex));
- return true;
- }
- bool SpriteBatchNode::init()
- {
- Texture2D * texture = new (std::nothrow) Texture2D();
- texture->autorelease();
- return this->initWithTexture(texture, 0);
- }
- 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);
- }
- void SpriteBatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
- {
- CC_PROFILER_START_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
-
-
-
-
-
-
-
- if (! _visible)
- {
- return;
- }
- sortAllChildren();
- uint32_t flags = processParentFlags(parentTransform, parentFlags);
- if (isVisitableByVisitingCamera())
- {
-
-
-
- _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);
-
-
-
-
- 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);
-
- CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
- Node::addChild(child, zOrder, tag);
- appendChild(sprite);
- }
- 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);
-
- CCASSERT(sprite->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCSprite is not using the same texture id");
-
- Node::addChild(child, zOrder, name);
-
- appendChild(sprite);
- }
- 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;
- }
-
- Node::reorderChild(child, zOrder);
- }
- void SpriteBatchNode::removeChild(Node *child, bool cleanup)
- {
- Sprite *sprite = static_cast<Sprite*>(child);
-
- if (sprite == nullptr)
- {
- return;
- }
- CCASSERT(_children.contains(sprite), "sprite batch node should contain the child");
-
- removeSpriteFromAtlas(sprite);
- Node::removeChild(sprite, cleanup);
- }
- 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)
- {
-
-
- for(const auto &sprite: _descendants) {
- sprite->setBatchNode(nullptr);
- }
- Node::removeAllChildrenWithCleanup(doCleanup);
- _descendants.clear();
- if (_textureAtlas) {_textureAtlas->removeAllQuads();}
- }
- void SpriteBatchNode::sortAllChildren()
- {
- if (_reorderChildDirty)
- {
- sortNodes(_children);
-
- if (!_children.empty())
- {
-
- for(const auto &child: _children) {
- child->sortAllChildren();
- }
- ssize_t index=0;
-
-
- for(const auto &child: _children) {
- Sprite* sp = static_cast<Sprite*>(child);
- updateAtlasIndex(sp, &index);
- }
- }
- _reorderChildDirty=false;
- }
- }
- void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, ssize_t* curIndex)
- {
- auto& array = sprite->getChildren();
- auto count = array.size();
-
- ssize_t oldIndex = 0;
- if( count == 0 )
- {
- oldIndex = sprite->getAtlasIndex();
- sprite->setAtlasIndex(*curIndex);
- if (oldIndex != *curIndex){
- swap(oldIndex, *curIndex);
- }
- (*curIndex)++;
- }
- else
- {
- bool needNewIndex=true;
- if (array.at(0)->getLocalZOrder() >= 0)
- {
-
- oldIndex = sprite->getAtlasIndex();
- sprite->setAtlasIndex(*curIndex);
- 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);
- if (oldIndex != *curIndex) {
- this->swap(oldIndex, *curIndex);
- }
- (*curIndex)++;
- needNewIndex = false;
- }
-
- updateAtlasIndex(sp, curIndex);
- }
- if (needNewIndex)
- {
- oldIndex = sprite->getAtlasIndex();
- sprite->setAtlasIndex(*curIndex);
- if (oldIndex != *curIndex) {
- swap(oldIndex, *curIndex);
- }
- (*curIndex)++;
- }
- }
- }
- void SpriteBatchNode::swap(ssize_t oldIndex, ssize_t newIndex)
- {
- 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] );
-
- auto oldIt = std::next( _descendants.begin(), oldIndex );
- auto newIt = std::next( _descendants.begin(), newIndex );
- (*newIt)->setAtlasIndex(oldIndex);
- std::swap( *oldIt, *newIt );
- }
- void SpriteBatchNode::reorderBatch(bool reorder)
- {
- _reorderChildDirty=reorder;
- }
- void SpriteBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
- {
-
- if( _textureAtlas->getTotalQuads() == 0 )
- {
- return;
- }
- for (const auto &child : _children)
- {
- child->updateTransform();
- }
- _batchCommand.init(_globalZOrder, getGLProgram(), _blendFunc, _textureAtlas, transform, flags);
- renderer->addCommand(&_batchCommand);
- }
- void SpriteBatchNode::increaseAtlasCapacity()
- {
-
-
-
- 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))
- {
-
- CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
- CCASSERT(false, "Not enough memory to resize the atlas");
- }
- }
- void SpriteBatchNode::reserveCapacity(ssize_t newCapacity)
- {
- if (newCapacity <= _textureAtlas->getCapacity())
- return;
- if (! _textureAtlas->resizeCapacity(newCapacity))
- {
-
- CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
- CCASSERT(false, "Not enough memory to resize the atlas");
- }
- }
- 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);
- }
- }
-
- 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;
- }
- ssize_t SpriteBatchNode::highestAtlasIndexInChild(Sprite *sprite)
- {
- auto& children = sprite->getChildren();
- if (children.empty())
- {
- return sprite->getAtlasIndex();
- }
- else
- {
- return highestAtlasIndexInChild( static_cast<Sprite*>(children.back()));
- }
- }
- 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)));
- }
- }
- ssize_t SpriteBatchNode::atlasIndexForChild(Sprite *sprite, int nZ)
- {
- auto& siblings = sprite->getParent()->getChildren();
- auto childIndex = siblings.getIndex(sprite);
-
- bool ignoreParent = (SpriteBatchNode*)(sprite->getParent()) == this;
- Sprite *prev = nullptr;
- if (childIndex > 0 && childIndex != -1)
- {
- prev = static_cast<Sprite*>(siblings.at(childIndex - 1));
- }
-
- if (ignoreParent)
- {
- if (childIndex == 0)
- {
- return 0;
- }
- return highestAtlasIndexInChild(prev) + 1;
- }
-
-
- if (childIndex == 0)
- {
- Sprite *p = static_cast<Sprite*>(sprite->getParent());
-
- if (nZ < 0)
- {
- return p->getAtlasIndex();
- }
- else
- {
- return p->getAtlasIndex() + 1;
- }
- }
- else
- {
-
- if ((prev->getLocalZOrder() < 0 && nZ < 0) || (prev->getLocalZOrder() >= 0 && nZ >= 0))
- {
- return highestAtlasIndexInChild(prev) + 1;
- }
-
- Sprite *p = static_cast<Sprite*>(sprite->getParent());
- return p->getAtlasIndex() + 1;
- }
-
- CCASSERT(0, "should not run here");
- return 0;
- }
- void SpriteBatchNode::appendChild(Sprite* sprite)
- {
- _reorderChildDirty=true;
- sprite->setBatchNode(this);
- sprite->setDirty(true);
- if(_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity()) {
- increaseAtlasCapacity();
- }
- _descendants.push_back(sprite);
- int index = static_cast<int>(_descendants.size()-1);
- sprite->setAtlasIndex(index);
- V3F_C4B_T2F_Quad quad = sprite->getQuad();
- _textureAtlas->insertQuad(&quad, index);
-
- auto& children = sprite->getChildren();
- for(const auto &child: children) {
- #if CC_SPRITE_DEBUG_DRAW
-
-
-
- if (dynamic_cast<DrawNode*>(child)) {
-
- sprite->Node::removeChild(child, true);
- }
- else
- #else
- CCASSERT(dynamic_cast<Sprite*>(child) != nullptr, "You can only add Sprites (or subclass of Sprite) to SpriteBatchNode");
- #endif
- appendChild(static_cast<Sprite*>(child));
- }
- }
- void SpriteBatchNode::removeSpriteFromAtlas(Sprite *sprite)
- {
-
- _textureAtlas->removeQuadAtIndex(sprite->getAtlasIndex());
-
- 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(auto nextEnd = _descendants.end(); next != nextEnd; ++next) {
- spr = *next;
- spr->setAtlasIndex( spr->getAtlasIndex() - 1 );
- }
- _descendants.erase(it);
- }
-
- 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())
- {
- _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
- setOpacityModifyRGB(false);
- }
- else
- {
- _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
- setOpacityModifyRGB(true);
- }
- }
- 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();
- }
- 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");
-
- while(index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
- {
- this->increaseAtlasCapacity();
- }
-
-
-
- sprite->setBatchNode(this);
- sprite->setAtlasIndex(index);
- V3F_C4B_T2F_Quad quad = sprite->getQuad();
- _textureAtlas->insertQuad(&quad, index);
-
-
- sprite->setDirty(true);
- sprite->updateTransform();
- }
- void SpriteBatchNode::updateQuadFromSprite(Sprite *sprite, ssize_t index)
- {
- CCASSERT(sprite != nullptr, "Argument must be non-nil");
- CCASSERT(dynamic_cast<Sprite*>(sprite) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
-
-
- while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
- {
- this->increaseAtlasCapacity();
- }
-
-
-
-
- sprite->setBatchNode(this);
- sprite->setAtlasIndex(index);
-
- sprite->setDirty(true);
-
-
- sprite->updateTransform();
- }
- 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");
-
- child->setAtlasIndex(z);
-
- auto it = _descendants.begin();
- for (auto itEnd = _descendants.end(); it != itEnd; ++it)
- {
- if((*it)->getAtlasIndex() >= z)
- break;
- }
- _descendants.insert(it, child);
-
- Node::addChild(child, z, aTag);
-
- reorderBatch(false);
- return this;
- }
- std::string SpriteBatchNode::getDescription() const
- {
- return StringUtils::format("<SpriteBatchNode | tag = %d>", _tag);
- }
- NS_CC_END
|