axmol/cocos/2d/CCParticleBatchNode.cpp

521 lines
16 KiB
C++
Raw Normal View History

/*
* Copyright (C) 2009 Matt Oswald
* Copyright (c) 2009-2010 Ricardo Quesada
* Copyright (c) 2010-2012 cocos2d-x.org
* Copyright (c) 2011 Zynga Inc.
* Copyright (c) 2011 Marco Tillemans
* 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.
*
*/
2014-05-10 09:39:25 +08:00
#include "2d/CCParticleBatchNode.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/CCTextureAtlas.h"
2014-05-10 09:39:25 +08:00
#include "2d/CCGrid.h"
#include "2d/CCParticleSystem.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 "platform/CCFileUtils.h"
2014-05-10 09:39:25 +08:00
#include "base/CCProfiling.h"
2014-04-30 08:37:36 +08:00
#include "base/ccConfig.h"
#include "base/ccMacros.h"
#include "base/base64.h"
#include "base/ZipUtils.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-05-10 09:39:25 +08:00
#include "renderer/CCGLProgramState.h"
#include "renderer/CCGLProgram.h"
#include "renderer/ccGLStateCache.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCQuadCommand.h"
#include "renderer/CCRenderer.h"
2014-05-10 09:39:25 +08:00
NS_CC_BEGIN
ParticleBatchNode::ParticleBatchNode()
: _textureAtlas(nullptr)
{
}
ParticleBatchNode::~ParticleBatchNode()
{
CC_SAFE_RELEASE(_textureAtlas);
}
/*
* creation with Texture2D
*/
ParticleBatchNode* ParticleBatchNode::createWithTexture(Texture2D *tex, int capacity/* = kParticleDefaultCapacity*/)
{
ParticleBatchNode * p = new ParticleBatchNode();
if( p && p->initWithTexture(tex, capacity))
{
p->autorelease();
return p;
}
CC_SAFE_DELETE(p);
return nullptr;
}
/*
* creation with File Image
*/
ParticleBatchNode* ParticleBatchNode::create(const std::string& imageFile, int capacity/* = kParticleDefaultCapacity*/)
{
ParticleBatchNode * p = new ParticleBatchNode();
if( p && p->initWithFile(imageFile, capacity))
{
p->autorelease();
return p;
}
CC_SAFE_DELETE(p);
return nullptr;
}
/*
* init with Texture2D
*/
bool ParticleBatchNode::initWithTexture(Texture2D *tex, int capacity)
{
_textureAtlas = new TextureAtlas();
_textureAtlas->initWithTexture(tex, capacity);
_children.reserve(capacity);
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
2013-11-30 01:09:38 +08:00
return true;
}
/*
* init with FileImage
*/
bool ParticleBatchNode::initWithFile(const std::string& fileImage, int capacity)
{
Texture2D *tex = Director::getInstance()->getTextureCache()->addImage(fileImage);
return initWithTexture(tex, capacity);
}
// ParticleBatchNode - composition
// override visit.
// Don't call visit on it's children
void ParticleBatchNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
// CAREFUL:
// This visit is almost identical to Node#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)
{
return;
}
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);
2013-11-30 01:09:38 +08:00
director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
// override addChild:
void ParticleBatchNode::addChild(Node * aChild, int zOrder, int tag)
{
CCASSERT( aChild != nullptr, "Argument must be non-nullptr");
CCASSERT( dynamic_cast<ParticleSystem*>(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children");
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
CCASSERT( child->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCParticleSystem is not using the same texture id");
2014-06-25 11:27:48 +08:00
addChildByTagOrName(child, zOrder, tag, "", true);
}
void ParticleBatchNode::addChild(Node * aChild, int zOrder, const std::string &name)
{
CCASSERT( aChild != nullptr, "Argument must be non-nullptr");
CCASSERT( dynamic_cast<ParticleSystem*>(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children");
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
CCASSERT( child->getTexture()->getName() == _textureAtlas->getTexture()->getName(), "CCParticleSystem is not using the same texture id");
addChildByTagOrName(child, zOrder, 0, name, false);
}
void ParticleBatchNode::addChildByTagOrName(ParticleSystem* child, int zOrder, int tag, const std::string &name, bool setTag)
{
// If this is the 1st children, then copy blending function
if (_children.empty())
{
setBlendFunc(child->getBlendFunc());
}
2014-06-25 11:27:48 +08:00
2013-11-30 01:09:38 +08:00
CCASSERT( _blendFunc.src == child->getBlendFunc().src && _blendFunc.dst == child->getBlendFunc().dst, "Can't add a ParticleSystem that uses a different blending function");
2014-06-25 11:27:48 +08:00
//no lazy sorting, so don't call super addChild, call helper instead
2014-06-25 11:27:48 +08:00
int pos = 0;
if (setTag)
pos = addChildHelper(child, zOrder, tag, "", true);
else
pos = addChildHelper(child, zOrder, 0, name, false);
//get new atlasIndex
int atlasIndex = 0;
2014-06-25 11:27:48 +08:00
2013-11-30 01:09:38 +08:00
if (pos != 0)
{
ParticleSystem* p = static_cast<ParticleSystem*>(_children.at(pos-1));
atlasIndex = p->getAtlasIndex() + p->getTotalParticles();
}
else
{
atlasIndex = 0;
}
2014-06-25 11:27:48 +08:00
insertChild(child, atlasIndex);
2014-06-25 11:27:48 +08:00
// update quad info
child->setBatchNode(this);
}
// don't use lazy sorting, reordering the particle systems quads afterwards would be too complex
// XXX research whether lazy sorting + freeing current quads and calloc a new block with size of capacity would be faster
// XXX or possibly using vertexZ for reordering, that would be fastest
// this helper is almost equivalent to Node's addChild, but doesn't make use of the lazy sorting
2014-06-25 11:27:48 +08:00
int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag, const std::string &name, bool setTag)
{
CCASSERT( child != nullptr, "Argument must be non-nil");
CCASSERT( child->getParent() == nullptr, "child already added. It can't be added again");
_children.reserve(4);
//don't use a lazy insert
2013-12-05 17:19:01 +08:00
auto pos = searchNewPositionInChildrenForZ(z);
_children.insert(pos, child);
2014-06-25 11:27:48 +08:00
if (setTag)
child->setTag(aTag);
else
child->setName(name);
child->_setLocalZOrder(z);
child->setParent(this);
2013-11-30 01:09:38 +08:00
if( _running )
{
child->onEnter();
child->onEnterTransitionDidFinish();
}
return pos;
}
// Reorder will be done in this function, no "lazy" reorder to particles
void ParticleBatchNode::reorderChild(Node * aChild, int zOrder)
{
CCASSERT( aChild != nullptr, "Child must be non-nullptr");
CCASSERT( dynamic_cast<ParticleSystem*>(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children");
CCASSERT( _children.contains(aChild), "Child doesn't belong to batch" );
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
if( zOrder == child->getLocalZOrder() )
{
return;
}
// no reordering if only 1 child
if (!_children.empty())
{
2013-12-05 17:19:01 +08:00
int newIndex = 0, oldIndex = 0;
getCurrentIndex(&oldIndex, &newIndex, child, zOrder);
if( oldIndex != newIndex )
{
// reorder _children->array
child->retain();
_children.erase(oldIndex);
_children.insert(newIndex, child);
child->release();
// save old altasIndex
int oldAtlasIndex = child->getAtlasIndex();
// update atlas index
updateAllAtlasIndexes();
// Find new AtlasIndex
int newAtlasIndex = 0;
for( int i=0;i < _children.size();i++)
{
ParticleSystem* node = static_cast<ParticleSystem*>(_children.at(i));
if( node == child )
{
newAtlasIndex = child->getAtlasIndex();
break;
}
}
// reorder textureAtlas quads
_textureAtlas->moveQuadsFromIndex(oldAtlasIndex, child->getTotalParticles(), newAtlasIndex);
child->updateWithNoTime();
}
}
child->_setLocalZOrder(zOrder);
}
2013-12-05 17:19:01 +08:00
void ParticleBatchNode::getCurrentIndex(int* oldIndex, int* newIndex, Node* child, int z)
{
bool foundCurrentIdx = false;
bool foundNewIdx = false;
int minusOne = 0;
2013-12-05 17:19:01 +08:00
auto count = _children.size();
2013-12-05 17:19:01 +08:00
for( int i=0; i < count; i++ )
{
Node* pNode = _children.at(i);
// new index
if( pNode->getLocalZOrder() > z && ! foundNewIdx )
{
*newIndex = i;
foundNewIdx = true;
if( foundCurrentIdx && foundNewIdx )
{
break;
}
}
// current index
2013-11-30 01:09:38 +08:00
if( child == pNode )
{
*oldIndex = i;
foundCurrentIdx = true;
if( ! foundNewIdx )
{
minusOne = -1;
}
if( foundCurrentIdx && foundNewIdx )
{
break;
}
}
}
if( ! foundNewIdx )
{
2013-12-05 17:19:01 +08:00
*newIndex = static_cast<int>(count);
}
*newIndex += minusOne;
}
2013-12-05 17:19:01 +08:00
int ParticleBatchNode::searchNewPositionInChildrenForZ(int z)
{
2013-12-05 17:19:01 +08:00
auto count = _children.size();
2013-12-05 17:19:01 +08:00
for( int i=0; i < count; i++ )
{
Node *child = _children.at(i);
if (child->getLocalZOrder() > z)
{
return i;
}
}
2013-12-05 17:19:01 +08:00
return static_cast<int>(count);
}
// override removeChild:
void ParticleBatchNode::removeChild(Node* aChild, bool cleanup)
{
// explicit nil handling
if (aChild == nullptr)
return;
2013-11-30 01:09:38 +08:00
CCASSERT( dynamic_cast<ParticleSystem*>(aChild) != nullptr, "CCParticleBatchNode only supports QuadParticleSystems as children");
CCASSERT(_children.contains(aChild), "CCParticleBatchNode doesn't contain the sprite. Can't remove it");
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
// remove child helper
_textureAtlas->removeQuadsAtIndex(child->getAtlasIndex(), child->getTotalParticles());
// after memmove of data, empty the quads at the end of array
_textureAtlas->fillWithEmptyQuadsFromIndex(_textureAtlas->getTotalQuads(), child->getTotalParticles());
// particle could be reused for self rendering
child->setBatchNode(nullptr);
Node::removeChild(child, cleanup);
updateAllAtlasIndexes();
}
2013-12-05 17:19:01 +08:00
void ParticleBatchNode::removeChildAtIndex(int index, bool doCleanup)
{
removeChild(_children.at(index), doCleanup);
}
void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
{
for(const auto &child : _children)
static_cast<ParticleSystem*>(child)->setBatchNode(nullptr);
Node::removeAllChildrenWithCleanup(doCleanup);
_textureAtlas->removeAllQuads();
}
void ParticleBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
2013-03-22 14:53:58 +08:00
CC_PROFILER_START("CCParticleBatchNode - draw");
if( _textureAtlas->getTotalQuads() == 0 )
{
return;
}
_batchCommand.init(
_globalZOrder,
2014-05-09 06:43:12 +08:00
getGLProgram(),
_blendFunc,
_textureAtlas,
_modelViewTransform);
renderer->addCommand(&_batchCommand);
CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
2013-12-12 12:07:20 +08:00
void ParticleBatchNode::increaseAtlasCapacityTo(ssize_t quantity)
{
CCLOG("cocos2d: ParticleBatchNode: resizing TextureAtlas capacity from [%lu] to [%lu].",
(long)_textureAtlas->getCapacity(),
(long)quantity);
if( ! _textureAtlas->resizeCapacity(quantity) ) {
// serious problems
CCLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
CCASSERT(false,"XXX: ParticleBatchNode #increaseAtlasCapacity SHALL handle this assert");
}
}
//sets a 0'd quad into the quads array
2013-12-05 17:19:01 +08:00
void ParticleBatchNode::disableParticle(int particleIndex)
{
V3F_C4B_T2F_Quad* quad = &((_textureAtlas->getQuads())[particleIndex]);
quad->br.vertices.x = quad->br.vertices.y = quad->tr.vertices.x = quad->tr.vertices.y = quad->tl.vertices.x = quad->tl.vertices.y = quad->bl.vertices.x = quad->bl.vertices.y = 0.0f;
}
// ParticleBatchNode - add / remove / reorder helper methods
// add child helper
void ParticleBatchNode::insertChild(ParticleSystem* system, int index)
{
system->setAtlasIndex(index);
if(_textureAtlas->getTotalQuads() + system->getTotalParticles() > _textureAtlas->getCapacity())
{
increaseAtlasCapacityTo(_textureAtlas->getTotalQuads() + system->getTotalParticles());
// after a realloc empty quads of textureAtlas can be filled with gibberish (realloc doesn't perform calloc), insert empty quads to prevent it
_textureAtlas->fillWithEmptyQuadsFromIndex(_textureAtlas->getCapacity() - system->getTotalParticles(), system->getTotalParticles());
}
// make room for quads, not necessary for last child
if (system->getAtlasIndex() + system->getTotalParticles() != _textureAtlas->getTotalQuads())
2012-04-04 21:58:04 +08:00
{
_textureAtlas->moveQuadsFromIndex(index, index+system->getTotalParticles());
2012-04-04 21:58:04 +08:00
}
// increase totalParticles here for new particles, update method of particle-system will fill the quads
_textureAtlas->increaseTotalQuadsWith(system->getTotalParticles());
updateAllAtlasIndexes();
}
//rebuild atlas indexes
void ParticleBatchNode::updateAllAtlasIndexes()
{
2013-12-05 17:19:01 +08:00
int index = 0;
for(const auto &child : _children) {
ParticleSystem* partiSys = static_cast<ParticleSystem*>(child);
partiSys->setAtlasIndex(index);
index += partiSys->getTotalParticles();
}
}
// ParticleBatchNode - CocosNodeTexture protocol
2013-12-05 17:19:01 +08:00
void ParticleBatchNode::updateBlendFunc()
{
if( ! _textureAtlas->getTexture()->hasPremultipliedAlpha())
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
void ParticleBatchNode::setTexture(Texture2D* texture)
{
_textureAtlas->setTexture(texture);
// If the new texture has No premultiplied alpha, AND the blendFunc hasn't been changed, then update it
if( texture && ! texture->hasPremultipliedAlpha() && ( _blendFunc.src == CC_BLEND_SRC && _blendFunc.dst == CC_BLEND_DST ) )
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
}
2013-12-05 17:19:01 +08:00
Texture2D* ParticleBatchNode::getTexture() const
{
return _textureAtlas->getTexture();
}
void ParticleBatchNode::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
}
// returns the blending function used for the texture
2013-12-05 17:19:01 +08:00
const BlendFunc& ParticleBatchNode::getBlendFunc() const
{
return _blendFunc;
}
NS_CC_END