2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-09-18 11:55:42 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/SpriteBatchNode.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Types.h"
|
|
|
|
#include "2d/Sprite.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/Profiling.h"
|
|
|
|
#include "base/UTF8.h"
|
|
|
|
#include "renderer/TextureCache.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "renderer/QuadCommand.h"
|
|
|
|
#include "renderer/Shaders.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
#include "renderer/backend/Device.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* creation with Texture2D
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::createWithTexture(Texture2D* tex, ssize_t capacity /* = DEFAULT_CAPACITY*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteBatchNode* batchNode = new SpriteBatchNode();
|
|
|
|
if (batchNode->initWithTexture(tex, capacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
batchNode->autorelease();
|
|
|
|
return batchNode;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
delete batchNode;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* creation with File Image
|
|
|
|
*/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::create(std::string_view fileImage, ssize_t capacity /* = DEFAULT_CAPACITY*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteBatchNode* batchNode = new SpriteBatchNode();
|
|
|
|
if (batchNode->initWithFile(fileImage, capacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
batchNode->autorelease();
|
|
|
|
return batchNode;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
delete batchNode;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* init with Texture2D
|
|
|
|
*/
|
|
|
|
bool SpriteBatchNode::initWithTexture(Texture2D* tex, ssize_t capacity /* = DEFAULT_CAPACITY*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (tex == nullptr)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(capacity >= 0, "Capacity must be >= 0");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!tex->hasPremultipliedAlpha())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
|
|
|
}
|
2021-12-08 00:11:53 +08:00
|
|
|
_textureAtlas = new TextureAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (capacity <= 0)
|
|
|
|
{
|
|
|
|
capacity = DEFAULT_CAPACITY;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_textureAtlas->initWithTexture(tex, capacity);
|
|
|
|
|
|
|
|
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, tex);
|
|
|
|
|
|
|
|
updateBlendFunc();
|
|
|
|
|
|
|
|
_children.reserve(capacity);
|
|
|
|
|
|
|
|
_descendants.reserve(capacity);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::setUniformLocation()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_programState, "programState should not be nullptr");
|
2019-11-23 20:27:39 +08:00
|
|
|
_mvpMatrixLocaiton = _programState->getUniformLocation("u_MVPMatrix");
|
2022-07-05 00:39:20 +08:00
|
|
|
_textureLocation = _programState->getUniformLocation("u_tex0");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2022-10-20 20:19:56 +08:00
|
|
|
void SpriteBatchNode::setVertexLayout()
|
|
|
|
{
|
|
|
|
AXASSERT(_programState, "programState should not be nullptr");
|
2023-08-31 22:08:50 +08:00
|
|
|
_programState->validateSharedVertexLayout(VertexLayoutHelper::setupSprite);
|
2022-10-20 20:19:56 +08:00
|
|
|
}
|
|
|
|
|
2023-08-31 22:08:50 +08:00
|
|
|
bool SpriteBatchNode::setProgramState(backend::ProgramState* programState, bool needsRetain)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(programState, "programState should not be nullptr");
|
2023-08-31 22:08:50 +08:00
|
|
|
if (Node::setProgramState(programState, needsRetain))
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
|
2020-09-09 13:03:31 +08:00
|
|
|
pipelineDescriptor.programState = _programState;
|
|
|
|
|
2022-10-20 20:19:56 +08:00
|
|
|
setVertexLayout();
|
2020-09-09 15:29:56 +08:00
|
|
|
updateProgramStateTexture(_textureAtlas->getTexture());
|
2020-09-09 13:03:31 +08:00
|
|
|
setUniformLocation();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SpriteBatchNode::init()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture = new Texture2D();
|
2019-11-23 20:27:39 +08:00
|
|
|
texture->autorelease();
|
|
|
|
return this->initWithTexture(texture, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-25 10:04:45 +08:00
|
|
|
* init with FileImage
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool SpriteBatchNode::initWithFile(std::string_view fileImage, ssize_t capacity /* = DEFAULT_CAPACITY*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* texture2D = _director->getTextureCache()->addImage(fileImage);
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithTexture(texture2D, capacity);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteBatchNode::SpriteBatchNode() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
SpriteBatchNode::~SpriteBatchNode()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_textureAtlas);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// override visit
|
|
|
|
// don't call visit on it's children
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PROFILER_START_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
//
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_visible)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sortAllChildren();
|
|
|
|
|
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
|
|
|
|
|
|
|
if (isVisitableByVisitingCamera())
|
|
|
|
{
|
|
|
|
// IMPORTANT:
|
|
|
|
// 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->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
draw(renderer, _modelViewTransform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PROFILER_STOP_CATEGORY(kProfilerCategoryBatchSprite, "CCSpriteBatchNode - visit");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::addChild(Node* child, int zOrder, int tag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child != nullptr, "child should not be null");
|
|
|
|
AXASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = static_cast<Sprite*>(child);
|
2019-11-23 20:27:39 +08:00
|
|
|
// check Sprite is using the same texture id
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(sprite->getTexture()->getBackendTexture() == _textureAtlas->getTexture()->getBackendTexture(),
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCSprite is not using the same texture id");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Node::addChild(child, zOrder, tag);
|
|
|
|
|
|
|
|
appendChild(sprite);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void SpriteBatchNode::addChild(Node* child, int zOrder, std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child != nullptr, "child should not be null");
|
|
|
|
AXASSERT(dynamic_cast<Sprite*>(child) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = static_cast<Sprite*>(child);
|
2019-11-23 20:27:39 +08:00
|
|
|
// check Sprite is using the same texture id
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(sprite->getTexture() == _textureAtlas->getTexture(), "CCSprite is not using the same texture id");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Node::addChild(child, zOrder, name);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
appendChild(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
// override reorderChild
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::reorderChild(Node* child, int zOrder)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child != nullptr, "the child should not be null");
|
|
|
|
AXASSERT(_children.contains(child), "Child doesn't belong to Sprite");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (zOrder == child->getLocalZOrder())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// set the z-order and sort later
|
2019-11-23 20:27:39 +08:00
|
|
|
Node::reorderChild(child, zOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// override remove child
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::removeChild(Node* child, bool cleanup)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* sprite = static_cast<Sprite*>(child);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// explicit null handling
|
|
|
|
if (sprite == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_children.contains(sprite), "sprite batch node should contain the child");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// cleanup before removing
|
|
|
|
removeSpriteFromAtlas(sprite);
|
|
|
|
|
|
|
|
Node::removeChild(sprite, cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::removeChildAtIndex(ssize_t index, bool doCleanup)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index >= 0 && index < _children.size(), "Invalid index");
|
2019-11-23 20:27:39 +08:00
|
|
|
removeChild(_children.at(index), doCleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
|
|
|
|
{
|
|
|
|
// Invalidate atlas index. issue #569
|
|
|
|
// useSelfRender should be performed on all descendants. issue #1216
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& sprite : _descendants)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setBatchNode(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node::removeAllChildrenWithCleanup(doCleanup);
|
|
|
|
|
|
|
|
_descendants.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_textureAtlas)
|
|
|
|
{
|
|
|
|
_textureAtlas->removeAllQuads();
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// override sortAllChildren
|
2019-11-23 20:27:39 +08:00
|
|
|
void SpriteBatchNode::sortAllChildren()
|
|
|
|
{
|
|
|
|
if (_reorderChildDirty)
|
|
|
|
{
|
|
|
|
sortNodes(_children);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// sorted now check all children
|
2019-11-23 20:27:39 +08:00
|
|
|
if (!_children.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// first sort all children recursively based on zOrder
|
|
|
|
for (const auto& child : _children)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
child->sortAllChildren();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t index = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// 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)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(child);
|
|
|
|
updateAtlasIndex(sp, &index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_reorderChildDirty = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::updateAtlasIndex(Sprite* sprite, ssize_t* curIndex)
|
|
|
|
{
|
|
|
|
auto& array = sprite->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto count = array.size();
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t oldIndex = 0;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (count == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
oldIndex = sprite->getAtlasIndex();
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(*curIndex));
|
2021-12-25 10:04:45 +08:00
|
|
|
if (oldIndex != *curIndex)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool needNewIndex = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (array.at(0)->getLocalZOrder() >= 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// all children are in front of the parent
|
2019-11-23 20:27:39 +08:00
|
|
|
oldIndex = sprite->getAtlasIndex();
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(*curIndex));
|
2019-11-23 20:27:39 +08:00
|
|
|
if (oldIndex != *curIndex)
|
|
|
|
{
|
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
|
|
|
|
needNewIndex = false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : array)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(child);
|
|
|
|
if (needNewIndex && sp->getLocalZOrder() >= 0)
|
|
|
|
{
|
|
|
|
oldIndex = sprite->getAtlasIndex();
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(*curIndex));
|
2021-12-25 10:04:45 +08:00
|
|
|
if (oldIndex != *curIndex)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
this->swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
needNewIndex = false;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
updateAtlasIndex(sp, curIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needNewIndex)
|
2021-12-25 10:04:45 +08:00
|
|
|
{ // all children have a zOrder < 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
oldIndex = sprite->getAtlasIndex();
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(*curIndex));
|
2021-12-25 10:04:45 +08:00
|
|
|
if (oldIndex != *curIndex)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
swap(oldIndex, *curIndex);
|
|
|
|
}
|
|
|
|
(*curIndex)++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::swap(ssize_t oldIndex, ssize_t newIndex)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(
|
2021-12-25 10:04:45 +08:00
|
|
|
oldIndex >= 0 && oldIndex < (int)_descendants.size() && newIndex >= 0 && newIndex < (int)_descendants.size(),
|
|
|
|
"Invalid index");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
V3F_C4B_T2F_Quad* quads = _textureAtlas->getQuads();
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(quads[oldIndex], quads[newIndex]);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// update the index of other swapped item
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto oldIt = std::next(_descendants.begin(), oldIndex);
|
|
|
|
auto newIt = std::next(_descendants.begin(), newIndex);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-11-20 08:58:34 +08:00
|
|
|
(*newIt)->setAtlasIndex(static_cast<unsigned int>(oldIndex));
|
2021-12-25 10:04:45 +08:00
|
|
|
// (*oldIt)->setAtlasIndex(newIndex);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::swap(*oldIt, *newIt);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::reorderBatch(bool reorder)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_reorderChildDirty = reorder;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Optimization: Fast Dispatch
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_textureAtlas->getTotalQuads() == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : _children)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
child->updateTransform();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
const auto& matrixProjection = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto programState = _quadCommand.getPipelineDescriptor().programState;
|
2019-11-23 20:27:39 +08:00
|
|
|
programState->setUniform(_mvpMatrixLocaiton, matrixProjection.m, sizeof(matrixProjection.m));
|
2021-12-25 10:04:45 +08:00
|
|
|
_quadCommand.init(_globalZOrder, _textureAtlas->getTexture(), _blendFunc, _textureAtlas->getQuads(),
|
|
|
|
_textureAtlas->getTotalQuads(), transform, flags);
|
2019-11-23 20:27:39 +08:00
|
|
|
renderer->addCommand(&_quadCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
ssize_t quantity = (_textureAtlas->getCapacity() + 1) * 4 / 3;
|
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOG("axmol: SpriteBatchNode: resizing TextureAtlas capacity from [%d] to [%d].",
|
2021-12-25 10:04:45 +08:00
|
|
|
static_cast<int>(_textureAtlas->getCapacity()), static_cast<int>(quantity));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->resizeCapacity(quantity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// serious problems
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOGWARN("axmol: WARNING: Not enough memory to resize the atlas");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Not enough memory to resize the atlas");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::reserveCapacity(ssize_t newCapacity)
|
|
|
|
{
|
|
|
|
if (newCapacity <= _textureAtlas->getCapacity())
|
|
|
|
return;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->resizeCapacity(newCapacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// serious problems
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOGWARN("axmol: WARNING: Not enough memory to resize the atlas");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Not enough memory to resize the atlas");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t SpriteBatchNode::rebuildIndexInOrder(Sprite* parent, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index >= 0 && index < _children.size(), "Invalid index");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
auto& children = parent->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
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))
|
|
|
|
{
|
2022-11-20 08:58:34 +08:00
|
|
|
parent->setAtlasIndex(static_cast<unsigned int>(index));
|
2019-11-23 20:27:39 +08:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : children)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite* sp = static_cast<Sprite*>(child);
|
|
|
|
if (sp && (sp->getLocalZOrder() >= 0))
|
|
|
|
{
|
|
|
|
index = rebuildIndexInOrder(sp, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t SpriteBatchNode::highestAtlasIndexInChild(Sprite* sprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto& children = sprite->getChildren();
|
|
|
|
|
|
|
|
if (children.empty())
|
|
|
|
{
|
|
|
|
return sprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return highestAtlasIndexInChild(static_cast<Sprite*>(children.back()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t SpriteBatchNode::lowestAtlasIndexInChild(Sprite* sprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto& children = sprite->getChildren();
|
|
|
|
|
2020-08-18 14:29:09 +08:00
|
|
|
if (children.empty())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return sprite->getAtlasIndex();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return lowestAtlasIndexInChild(static_cast<Sprite*>(children.at(0)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t SpriteBatchNode::atlasIndexForChild(Sprite* sprite, int nZ)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& siblings = sprite->getParent()->getChildren();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto childIndex = siblings.getIndex(sprite);
|
|
|
|
|
|
|
|
// ignore parent Z if parent is spriteSheet
|
|
|
|
bool ignoreParent = (SpriteBatchNode*)(sprite->getParent()) == this;
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* prev = nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
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)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* p = static_cast<Sprite*>(sprite->getParent());
|
2019-11-23 20:27:39 +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 ((prev->getLocalZOrder() < 0 && nZ < 0) || (prev->getLocalZOrder() >= 0 && nZ >= 0))
|
|
|
|
{
|
|
|
|
return highestAtlasIndexInChild(prev) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else (previous < 0 and sprite >= 0 )
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* p = static_cast<Sprite*>(sprite->getParent());
|
2019-11-23 20:27:39 +08:00
|
|
|
return p->getAtlasIndex() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not happen. Error calculating Z on SpriteSheet
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(0, "should not run here");
|
2019-11-23 20:27:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// addChild helper, faster than insertChild
|
|
|
|
void SpriteBatchNode::appendChild(Sprite* sprite)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_reorderChildDirty = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setBatchNode(this);
|
|
|
|
sprite->setDirty(true);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_textureAtlas->getTotalQuads() == _textureAtlas->getCapacity())
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
|
2022-08-08 13:18:33 +08:00
|
|
|
_descendants.emplace_back(sprite);
|
2021-12-25 10:04:45 +08:00
|
|
|
int index = static_cast<int>(_descendants.size() - 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
sprite->setAtlasIndex(index);
|
|
|
|
|
2023-07-09 19:00:12 +08:00
|
|
|
auto&& quad = sprite->getQuad();
|
|
|
|
_textureAtlas->insertQuad(quad, index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// add children recursively
|
|
|
|
auto& children = sprite->getChildren();
|
2021-09-18 11:55:42 +08:00
|
|
|
|
2022-10-18 19:13:40 +08:00
|
|
|
// axmol Github issue #502
|
2021-09-18 11:55:42 +08:00
|
|
|
for (auto iter = children.begin(); iter != children.end();)
|
|
|
|
{
|
|
|
|
auto child = *iter;
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_SPRITE_DEBUG_DRAW
|
|
|
|
// when using AX_SPRITE_DEBUG_DRAW, a DrawNode is appended to sprites. remove it since only Sprites can be used
|
2019-11-23 20:27:39 +08:00
|
|
|
// as children in SpriteBatchNode
|
|
|
|
// Github issue #14730
|
2021-09-18 11:55:42 +08:00
|
|
|
if (dynamic_cast<DrawNode*>(child))
|
|
|
|
{
|
2021-09-18 14:06:52 +08:00
|
|
|
sprite->resetChild(child, true);
|
2021-09-18 11:55:42 +08:00
|
|
|
iter = children.erase(iter);
|
|
|
|
continue;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
#endif
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(dynamic_cast<Sprite*>(child) != nullptr,
|
2021-09-18 11:55:42 +08:00
|
|
|
"You can only add Sprites (or subclass of Sprite) to SpriteBatchNode");
|
2019-11-23 20:27:39 +08:00
|
|
|
appendChild(static_cast<Sprite*>(child));
|
2021-09-18 11:55:42 +08:00
|
|
|
++iter;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::removeSpriteFromAtlas(Sprite* sprite)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// remove from TextureAtlas
|
|
|
|
_textureAtlas->removeQuadAtIndex(sprite->getAtlasIndex());
|
|
|
|
|
|
|
|
// Cleanup sprite. It might be reused (issue #569)
|
|
|
|
sprite->setBatchNode(nullptr);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto it = std::find(_descendants.begin(), _descendants.end(), sprite);
|
|
|
|
if (it != _descendants.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto next = std::next(it);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Sprite* spr = nullptr;
|
|
|
|
for (auto nextEnd = _descendants.end(); next != nextEnd; ++next)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
spr = *next;
|
2021-12-25 10:04:45 +08:00
|
|
|
spr->setAtlasIndex(spr->getAtlasIndex() - 1);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_descendants.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove children recursively
|
|
|
|
auto& children = sprite->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& obj : children)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Sprite* child = static_cast<Sprite*>(obj);
|
|
|
|
if (child)
|
|
|
|
{
|
|
|
|
removeSpriteFromAtlas(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteBatchNode::updateBlendFunc()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->getTexture()->hasPremultipliedAlpha())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
|
|
|
setOpacityModifyRGB(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
setOpacityModifyRGB(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CocosNodeTexture protocol
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::setBlendFunc(const BlendFunc& blendFunc)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& SpriteBatchNode::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* SpriteBatchNode::getTexture() const
|
|
|
|
{
|
|
|
|
return _textureAtlas->getTexture();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::setTexture(Texture2D* texture)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_textureAtlas->setTexture(texture);
|
|
|
|
setProgramStateWithRegistry(backend::ProgramType::POSITION_TEXTURE_COLOR, texture);
|
|
|
|
updateBlendFunc();
|
|
|
|
}
|
|
|
|
|
|
|
|
// SpriteSheet Extension
|
2021-12-25 10:04:45 +08:00
|
|
|
// implementation SpriteSheet (TMXTiledMapExtension)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::insertQuadFromSprite(Sprite* sprite, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(sprite != nullptr, "Argument must be non-nullptr");
|
|
|
|
AXASSERT(dynamic_cast<Sprite*>(sprite), "CCSpriteBatchNode only supports Sprites as children");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// make needed room
|
2021-12-25 10:04:45 +08:00
|
|
|
while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
this->increaseAtlasCapacity();
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// update the quad directly. Don't add the sprite to the scene graph
|
|
|
|
//
|
|
|
|
sprite->setBatchNode(this);
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(index));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-07-09 19:00:12 +08:00
|
|
|
auto&& quad = sprite->getQuad();
|
|
|
|
_textureAtlas->insertQuad(quad, index);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// FIXME:: updateTransform will update the textureAtlas too, using updateQuad.
|
|
|
|
// FIXME:: so, it should be AFTER the insertQuad
|
|
|
|
sprite->setDirty(true);
|
|
|
|
sprite->updateTransform();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void SpriteBatchNode::updateQuadFromSprite(Sprite* sprite, ssize_t index)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(sprite != nullptr, "Argument must be non-nil");
|
|
|
|
AXASSERT(dynamic_cast<Sprite*>(sprite) != nullptr, "CCSpriteBatchNode only supports Sprites as children");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// make needed room
|
|
|
|
while (index >= _textureAtlas->getCapacity() || _textureAtlas->getCapacity() == _textureAtlas->getTotalQuads())
|
|
|
|
{
|
|
|
|
this->increaseAtlasCapacity();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
//
|
|
|
|
// update the quad directly. Don't add the sprite to the scene graph
|
|
|
|
//
|
|
|
|
sprite->setBatchNode(this);
|
2022-11-20 08:58:34 +08:00
|
|
|
sprite->setAtlasIndex(static_cast<unsigned int>(index));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setDirty(true);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// UpdateTransform updates the textureAtlas quad
|
|
|
|
sprite->updateTransform();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
SpriteBatchNode* SpriteBatchNode::addSpriteWithoutQuad(Sprite* child, int z, int aTag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child != nullptr, "Argument must be non-nullptr");
|
|
|
|
AXASSERT(dynamic_cast<Sprite*>(child), "CCSpriteBatchNode only supports Sprites as children");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// quad index is Z
|
|
|
|
child->setAtlasIndex(z);
|
|
|
|
|
|
|
|
// FIXME:: optimize with a binary search
|
|
|
|
auto it = _descendants.begin();
|
|
|
|
for (auto itEnd = _descendants.end(); it != itEnd; ++it)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((*it)->getAtlasIndex() >= z)
|
2019-11-23 20:27:39 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|