2012-03-14 10:55:17 +04:00
/*
2012-06-14 11:13:16 +04:00
* Copyright ( c ) 2010 - 2012 cocos2d - x . org
2012-03-14 10:55:17 +04:00
* Copyright ( C ) 2009 Matt Oswald
* Copyright ( c ) 2009 - 2010 Ricardo Quesada
* Copyright ( c ) 2011 Zynga Inc .
* Copyright ( c ) 2011 Marco Tillemans
*
* 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 .
*
*/
# include "CCParticleBatchNode.h"
2013-10-14 10:01:00 +04:00
# include "CCTextureCache.h"
# include "CCTextureAtlas.h"
2012-03-14 10:55:17 +04:00
# include "ccConfig.h"
# include "ccMacros.h"
2013-10-14 10:01:00 +04:00
# include "CCGrid.h"
2012-03-14 10:55:17 +04:00
# include "CCParticleSystem.h"
2013-10-14 10:01:00 +04:00
# include "CCShaderCache.h"
# include "CCGLProgram.h"
# include "ccGLStateCache.h"
# include "base64.h"
# include "ZipUtils.h"
2012-06-19 12:20:46 +04:00
# include "platform/CCFileUtils.h"
2012-03-14 10:55:17 +04:00
# include "kazmath/GL/matrix.h"
2013-10-14 10:01:00 +04:00
# include "CCProfiling.h"
2012-03-29 07:25:08 +04:00
2012-03-14 10:55:17 +04:00
NS_CC_BEGIN
2013-06-20 10:13:12 +04:00
ParticleBatchNode : : ParticleBatchNode ( )
2013-06-15 10:03:30 +04:00
: _textureAtlas ( NULL )
2012-03-14 10:55:17 +04:00
{
}
2013-06-20 10:13:12 +04:00
ParticleBatchNode : : ~ ParticleBatchNode ( )
2012-03-14 10:55:17 +04:00
{
2013-06-15 10:03:30 +04:00
CC_SAFE_RELEASE ( _textureAtlas ) ;
2012-03-14 10:55:17 +04:00
}
/*
2013-06-20 10:13:12 +04:00
* creation with Texture2D
2012-03-14 10:55:17 +04:00
*/
2013-11-15 05:19:16 +04:00
ParticleBatchNode * ParticleBatchNode : : createWithTexture ( Texture2D * tex , int capacity /* = kParticleDefaultCapacity*/ )
2012-03-14 10:55:17 +04:00
{
2013-06-20 10:13:12 +04:00
ParticleBatchNode * p = new ParticleBatchNode ( ) ;
2012-04-19 10:35:52 +04:00
if ( p & & p - > initWithTexture ( tex , capacity ) )
{
p - > autorelease ( ) ;
return p ;
}
CC_SAFE_DELETE ( p ) ;
return NULL ;
2012-03-14 10:55:17 +04:00
}
/*
* creation with File Image
*/
2013-11-15 05:19:16 +04:00
ParticleBatchNode * ParticleBatchNode : : create ( const std : : string & imageFile , int capacity /* = kParticleDefaultCapacity*/ )
2012-03-14 10:55:17 +04:00
{
2013-06-20 10:13:12 +04:00
ParticleBatchNode * p = new ParticleBatchNode ( ) ;
2012-06-14 11:13:16 +04:00
if ( p & & p - > initWithFile ( imageFile , capacity ) )
2012-04-19 10:35:52 +04:00
{
p - > autorelease ( ) ;
return p ;
}
CC_SAFE_DELETE ( p ) ;
return NULL ;
2012-03-14 10:55:17 +04:00
}
/*
2013-06-20 10:13:12 +04:00
* init with Texture2D
2012-03-14 10:55:17 +04:00
*/
2013-11-15 05:19:16 +04:00
bool ParticleBatchNode : : initWithTexture ( Texture2D * tex , int capacity )
2012-03-14 10:55:17 +04:00
{
2013-06-20 10:13:12 +04:00
_textureAtlas = new TextureAtlas ( ) ;
2013-06-15 10:03:30 +04:00
_textureAtlas - > initWithTexture ( tex , capacity ) ;
2012-03-14 10:55:17 +04:00
2013-12-05 06:35:10 +04:00
_children . reserve ( capacity ) ;
2013-11-28 12:02:03 +04:00
2013-07-26 04:47:42 +04:00
_blendFunc = BlendFunc : : ALPHA_PREMULTIPLIED ;
2012-03-14 10:55:17 +04:00
2013-09-16 16:38:03 +04:00
setShaderProgram ( ShaderCache : : getInstance ( ) - > getProgram ( GLProgram : : SHADER_NAME_POSITION_TEXTURE_COLOR ) ) ;
2013-11-29 21:09:38 +04:00
2012-04-19 10:35:52 +04:00
return true ;
2012-03-14 10:55:17 +04:00
}
/*
* init with FileImage
*/
2013-11-15 05:19:16 +04:00
bool ParticleBatchNode : : initWithFile ( const std : : string & fileImage , int capacity )
2012-03-14 10:55:17 +04:00
{
2013-11-07 15:11:09 +04:00
Texture2D * tex = Director : : getInstance ( ) - > getTextureCache ( ) - > addImage ( fileImage ) ;
2012-04-19 10:35:52 +04:00
return initWithTexture ( tex , capacity ) ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
// ParticleBatchNode - composition
2012-03-14 10:55:17 +04:00
// override visit.
// Don't call visit on it's children
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : visit ( )
2012-03-14 10:55:17 +04:00
{
2012-04-19 10:35:52 +04:00
// CAREFUL:
2013-06-20 10:13:12 +04:00
// This visit is almost identical to Node#visit
2012-04-19 10:35:52 +04:00
// with the exception that it doesn't call visit on it's children
//
2013-06-20 10:13:12 +04:00
// The alternative is to have a void Sprite#visit, but
2012-09-17 11:02:24 +04:00
// although this is less maintainable, is faster
2012-04-19 10:35:52 +04:00
//
2013-06-15 10:03:30 +04:00
if ( ! _visible )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
return ;
2012-03-29 07:25:08 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
kmGLPushMatrix ( ) ;
2012-03-14 10:55:17 +04:00
2013-06-15 10:03:30 +04:00
if ( _grid & & _grid - > isActive ( ) )
2012-03-29 07:25:08 +04:00
{
2013-06-15 10:03:30 +04:00
_grid - > beforeDraw ( ) ;
2012-04-19 10:35:52 +04:00
transformAncestors ( ) ;
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
transform ( ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
draw ( ) ;
2013-11-29 21:09:38 +04:00
2013-06-15 10:03:30 +04:00
if ( _grid & & _grid - > isActive ( ) )
2012-03-29 07:25:08 +04:00
{
2013-06-15 10:03:30 +04:00
_grid - > afterDraw ( this ) ;
2012-03-29 07:25:08 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
kmGLPopMatrix ( ) ;
2012-03-14 10:55:17 +04:00
}
// override addChild:
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : addChild ( Node * child )
2012-03-22 14:29:07 +04:00
{
2013-06-20 10:13:12 +04:00
Node : : addChild ( child ) ;
2012-03-22 14:29:07 +04:00
}
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : addChild ( Node * child , int zOrder )
2012-03-22 14:29:07 +04:00
{
2013-06-20 10:13:12 +04:00
Node : : addChild ( child , zOrder ) ;
2012-03-22 14:29:07 +04:00
}
2013-07-26 03:27:24 +04:00
void ParticleBatchNode : : addChild ( Node * aChild , int zOrder , int tag )
2012-03-14 10:55:17 +04:00
{
2013-07-26 03:27:24 +04:00
CCASSERT ( aChild ! = NULL , " Argument must be non-NULL " ) ;
CCASSERT ( dynamic_cast < ParticleSystem * > ( aChild ) ! = NULL , " 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 " ) ;
2012-04-19 10:35:52 +04:00
// If this is the 1st children, then copy blending function
2013-11-28 14:23:06 +04:00
if ( _children . empty ( ) )
2012-03-27 09:48:14 +04:00
{
2013-07-26 03:27:24 +04:00
setBlendFunc ( child - > getBlendFunc ( ) ) ;
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
2013-11-29 21:09:38 +04:00
CCASSERT ( _blendFunc . src = = child - > getBlendFunc ( ) . src & & _blendFunc . dst = = child - > getBlendFunc ( ) . dst , " Can't add a ParticleSystem that uses a different blending function " ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
//no lazy sorting, so don't call super addChild, call helper instead
2013-11-29 12:33:15 +04:00
long pos = addChildHelper ( child , zOrder , tag ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
//get new atlasIndex
2013-07-20 09:01:27 +04:00
int atlasIndex = 0 ;
2012-03-14 10:55:17 +04:00
2013-11-29 21:09:38 +04:00
if ( pos ! = 0 )
2012-03-27 09:48:14 +04:00
{
2013-12-05 06:35:10 +04:00
ParticleSystem * p = static_cast < ParticleSystem * > ( _children . at ( pos - 1 ) ) ;
2012-04-19 10:35:52 +04:00
atlasIndex = p - > getAtlasIndex ( ) + p - > getTotalParticles ( ) ;
}
else
2012-03-27 09:48:14 +04:00
{
2012-04-19 10:35:52 +04:00
atlasIndex = 0 ;
}
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
insertChild ( child , atlasIndex ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// update quad info
2013-07-26 03:27:24 +04:00
child - > setBatchNode ( this ) ;
2012-03-14 10:55:17 +04:00
}
// 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
2013-06-20 10:13:12 +04:00
// this helper is almost equivalent to Node's addChild, but doesn't make use of the lazy sorting
2013-11-28 12:02:03 +04:00
long ParticleBatchNode : : addChildHelper ( ParticleSystem * child , int z , int aTag )
2012-03-14 10:55:17 +04:00
{
2013-07-20 09:01:27 +04:00
CCASSERT ( child ! = NULL , " Argument must be non-nil " ) ;
CCASSERT ( child - > getParent ( ) = = NULL , " child already added. It can't be added again " ) ;
2012-03-14 10:55:17 +04:00
2013-12-05 06:35:10 +04:00
_children . reserve ( 4 ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
//don't use a lazy insert
2013-11-28 12:02:03 +04:00
long pos = searchNewPositionInChildrenForZ ( z ) ;
2012-03-14 10:55:17 +04:00
2013-12-05 06:35:10 +04:00
_children . insert ( pos , child ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
child - > setTag ( aTag ) ;
child - > _setZOrder ( z ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
child - > setParent ( this ) ;
2012-03-14 10:55:17 +04:00
2013-11-29 21:09:38 +04:00
if ( _running )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
child - > onEnter ( ) ;
child - > onEnterTransitionDidFinish ( ) ;
}
return pos ;
2012-03-14 10:55:17 +04:00
}
// Reorder will be done in this function, no "lazy" reorder to particles
2013-07-26 03:27:24 +04:00
void ParticleBatchNode : : reorderChild ( Node * aChild , int zOrder )
2012-03-14 10:55:17 +04:00
{
2013-07-26 03:27:24 +04:00
CCASSERT ( aChild ! = NULL , " Child must be non-NULL " ) ;
CCASSERT ( dynamic_cast < ParticleSystem * > ( aChild ) ! = NULL , " CCParticleBatchNode only supports QuadParticleSystems as children " ) ;
2013-12-05 06:35:10 +04:00
CCASSERT ( _children . contains ( aChild ) , " Child doesn't belong to batch " ) ;
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
ParticleSystem * child = static_cast < ParticleSystem * > ( aChild ) ;
2012-03-29 07:25:08 +04:00
2013-11-29 21:09:38 +04:00
if ( zOrder = = child - > getZOrder ( ) )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
return ;
}
2012-03-29 07:25:08 +04:00
2012-04-19 10:35:52 +04:00
// no reordering if only 1 child
2013-11-28 14:23:06 +04:00
if ( ! _children . empty ( ) )
2012-04-19 10:35:52 +04:00
{
2013-11-28 12:02:03 +04:00
long newIndex = 0 , oldIndex = 0 ;
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
getCurrentIndex ( & oldIndex , & newIndex , child , zOrder ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
if ( oldIndex ! = newIndex )
2012-03-29 07:25:08 +04:00
{
2012-03-14 10:55:17 +04:00
2013-06-15 10:03:30 +04:00
// reorder _children->array
2013-07-26 03:27:24 +04:00
child - > retain ( ) ;
2013-12-05 06:35:10 +04:00
_children . remove ( oldIndex ) ;
_children . insert ( newIndex , child ) ;
2013-07-26 03:27:24 +04:00
child - > release ( ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// save old altasIndex
2013-07-26 03:27:24 +04:00
int oldAtlasIndex = child - > getAtlasIndex ( ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// update atlas index
updateAllAtlasIndexes ( ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// Find new AtlasIndex
2013-07-20 09:01:27 +04:00
int newAtlasIndex = 0 ;
2013-12-05 06:35:10 +04:00
for ( int i = 0 ; i < _children . size ( ) ; i + + )
2012-03-29 07:25:08 +04:00
{
2013-12-05 06:35:10 +04:00
ParticleSystem * node = static_cast < ParticleSystem * > ( _children . at ( i ) ) ;
2013-11-28 12:02:03 +04:00
if ( node = = child )
2012-03-29 07:25:08 +04:00
{
2013-07-26 03:27:24 +04:00
newAtlasIndex = child - > getAtlasIndex ( ) ;
2012-04-19 10:35:52 +04:00
break ;
}
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// reorder textureAtlas quads
2013-07-26 03:27:24 +04:00
_textureAtlas - > moveQuadsFromIndex ( oldAtlasIndex , child - > getTotalParticles ( ) , newAtlasIndex ) ;
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
child - > updateWithNoTime ( ) ;
2012-04-19 10:35:52 +04:00
}
}
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
child - > _setZOrder ( zOrder ) ;
2012-03-14 10:55:17 +04:00
}
2013-11-28 12:02:03 +04:00
void ParticleBatchNode : : getCurrentIndex ( long * oldIndex , long * newIndex , Node * child , int z )
2012-03-14 10:55:17 +04:00
{
2012-04-19 10:35:52 +04:00
bool foundCurrentIdx = false ;
bool foundNewIdx = false ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
int minusOne = 0 ;
2013-12-05 06:35:10 +04:00
long count = _children . size ( ) ;
2012-03-14 10:55:17 +04:00
2013-11-28 12:02:03 +04:00
for ( long i = 0 ; i < count ; i + + )
2012-03-29 07:25:08 +04:00
{
2013-12-05 06:35:10 +04:00
Node * pNode = _children . at ( i ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// new index
2013-11-29 21:09:38 +04:00
if ( pNode - > getZOrder ( ) > z & & ! foundNewIdx )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
* newIndex = i ;
foundNewIdx = true ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
if ( foundCurrentIdx & & foundNewIdx )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
break ;
2012-03-29 07:25:08 +04:00
}
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// current index
2013-11-29 21:09:38 +04:00
if ( child = = pNode )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
* oldIndex = i ;
foundCurrentIdx = true ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
if ( ! foundNewIdx )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
minusOne = - 1 ;
2012-03-29 07:25:08 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
if ( foundCurrentIdx & & foundNewIdx )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
break ;
2012-03-29 07:25:08 +04:00
}
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
if ( ! foundNewIdx )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
* newIndex = count ;
2012-03-29 07:25:08 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
* newIndex + = minusOne ;
2012-03-14 10:55:17 +04:00
}
2013-11-28 12:02:03 +04:00
long ParticleBatchNode : : searchNewPositionInChildrenForZ ( int z )
2012-03-14 10:55:17 +04:00
{
2013-12-05 06:35:10 +04:00
long count = _children . size ( ) ;
2012-03-14 10:55:17 +04:00
2013-11-28 12:02:03 +04:00
for ( long i = 0 ; i < count ; i + + )
2012-03-29 07:25:08 +04:00
{
2013-12-05 06:35:10 +04:00
Node * child = _children . at ( i ) ;
2012-04-19 10:35:52 +04:00
if ( child - > getZOrder ( ) > z )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
return i ;
2012-03-29 07:25:08 +04:00
}
2012-04-19 10:35:52 +04:00
}
return count ;
2012-03-14 10:55:17 +04:00
}
// override removeChild:
2013-07-26 03:27:24 +04:00
void ParticleBatchNode : : removeChild ( Node * aChild , bool cleanup )
2012-03-14 10:55:17 +04:00
{
2012-04-19 10:35:52 +04:00
// explicit nil handling
2013-07-26 03:27:24 +04:00
if ( aChild = = NULL )
2012-04-19 10:35:52 +04:00
return ;
2013-11-29 21:09:38 +04:00
2013-07-26 03:27:24 +04:00
CCASSERT ( dynamic_cast < ParticleSystem * > ( aChild ) ! = NULL , " CCParticleBatchNode only supports QuadParticleSystems as children " ) ;
2013-12-05 06:35:10 +04:00
CCASSERT ( _children . contains ( aChild ) , " CCParticleBatchNode doesn't contain the sprite. Can't remove it " ) ;
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
ParticleSystem * child = static_cast < ParticleSystem * > ( aChild ) ;
Node : : removeChild ( child , cleanup ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// remove child helper
2013-07-26 03:27:24 +04:00
_textureAtlas - > removeQuadsAtIndex ( child - > getAtlasIndex ( ) , child - > getTotalParticles ( ) ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// after memmove of data, empty the quads at the end of array
2013-07-26 03:27:24 +04:00
_textureAtlas - > fillWithEmptyQuadsFromIndex ( _textureAtlas - > getTotalQuads ( ) , child - > getTotalParticles ( ) ) ;
2012-03-14 10:55:17 +04:00
2012-09-17 11:02:24 +04:00
// particle could be reused for self rendering
2013-07-26 03:27:24 +04:00
child - > setBatchNode ( NULL ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
updateAllAtlasIndexes ( ) ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : removeChildAtIndex ( unsigned int index , bool doCleanup )
2012-03-14 10:55:17 +04:00
{
2013-12-05 06:35:10 +04:00
removeChild ( _children . at ( index ) , doCleanup ) ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : removeAllChildrenWithCleanup ( bool doCleanup )
2012-03-14 10:55:17 +04:00
{
2013-11-28 14:23:06 +04:00
_children . forEach ( [ ] ( Node * child ) {
2013-11-28 12:02:03 +04:00
static_cast < ParticleSystem * > ( child ) - > setBatchNode ( nullptr ) ;
} ) ;
2012-03-14 10:55:17 +04:00
2013-06-20 10:13:12 +04:00
Node : : removeAllChildrenWithCleanup ( doCleanup ) ;
2012-03-14 10:55:17 +04:00
2013-06-15 10:03:30 +04:00
_textureAtlas - > removeAllQuads ( ) ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : draw ( void )
2012-03-14 10:55:17 +04:00
{
2013-03-22 10:53:58 +04:00
CC_PROFILER_START ( " CCParticleBatchNode - draw " ) ;
2012-03-14 10:55:17 +04:00
2013-06-15 10:03:30 +04:00
if ( _textureAtlas - > getTotalQuads ( ) = = 0 )
2012-03-29 07:25:08 +04:00
{
2012-04-19 10:35:52 +04:00
return ;
2012-03-29 07:25:08 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
CC_NODE_DRAW_SETUP ( ) ;
2012-03-14 10:55:17 +04:00
2013-07-26 05:42:53 +04:00
GL : : blendFunc ( _blendFunc . src , _blendFunc . dst ) ;
2012-03-14 10:55:17 +04:00
2013-06-15 10:03:30 +04:00
_textureAtlas - > drawQuads ( ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
CC_PROFILER_STOP ( " CCParticleBatchNode - draw " ) ;
2012-03-14 10:55:17 +04:00
}
2013-11-28 12:02:03 +04:00
void ParticleBatchNode : : increaseAtlasCapacityTo ( long quantity )
2012-03-14 10:55:17 +04:00
{
2013-06-20 10:13:12 +04:00
CCLOG ( " cocos2d: ParticleBatchNode: resizing TextureAtlas capacity from [%lu] to [%lu]. " ,
2013-06-15 10:03:30 +04:00
( long ) _textureAtlas - > getCapacity ( ) ,
2012-04-19 10:35:52 +04:00
( long ) quantity ) ;
2013-06-15 10:03:30 +04:00
if ( ! _textureAtlas - > resizeCapacity ( quantity ) ) {
2012-04-19 10:35:52 +04:00
// serious problems
2012-06-08 10:11:48 +04:00
CCLOGWARN ( " cocos2d: WARNING: Not enough memory to resize the atlas " ) ;
2013-07-20 09:01:27 +04:00
CCASSERT ( false , " XXX: ParticleBatchNode #increaseAtlasCapacity SHALL handle this assert " ) ;
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
}
//sets a 0'd quad into the quads array
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : disableParticle ( unsigned int particleIndex )
2012-03-14 10:55:17 +04:00
{
2013-07-05 12:49:22 +04:00
V3F_C4B_T2F_Quad * quad = & ( ( _textureAtlas - > getQuads ( ) ) [ particleIndex ] ) ;
2012-04-19 10:35:52 +04:00
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 ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
// ParticleBatchNode - add / remove / reorder helper methods
2012-03-14 10:55:17 +04:00
// add child helper
2013-07-26 03:27:24 +04:00
void ParticleBatchNode : : insertChild ( ParticleSystem * system , int index )
2012-03-14 10:55:17 +04:00
{
2013-07-26 03:27:24 +04:00
system - > setAtlasIndex ( index ) ;
2012-03-14 10:55:17 +04:00
2013-07-26 03:27:24 +04:00
if ( _textureAtlas - > getTotalQuads ( ) + system - > getTotalParticles ( ) > _textureAtlas - > getCapacity ( ) )
2012-04-19 10:35:52 +04:00
{
2013-07-26 03:27:24 +04:00
increaseAtlasCapacityTo ( _textureAtlas - > getTotalQuads ( ) + system - > getTotalParticles ( ) ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// after a realloc empty quads of textureAtlas can be filled with gibberish (realloc doesn't perform calloc), insert empty quads to prevent it
2013-07-26 03:27:24 +04:00
_textureAtlas - > fillWithEmptyQuadsFromIndex ( _textureAtlas - > getCapacity ( ) - system - > getTotalParticles ( ) , system - > getTotalParticles ( ) ) ;
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
// make room for quads, not necessary for last child
2013-07-26 03:27:24 +04:00
if ( system - > getAtlasIndex ( ) + system - > getTotalParticles ( ) ! = _textureAtlas - > getTotalQuads ( ) )
2012-04-04 17:58:04 +04:00
{
2013-07-26 03:27:24 +04:00
_textureAtlas - > moveQuadsFromIndex ( index , index + system - > getTotalParticles ( ) ) ;
2012-04-04 17:58:04 +04:00
}
2012-03-14 10:55:17 +04:00
2012-09-17 11:02:24 +04:00
// increase totalParticles here for new particles, update method of particle-system will fill the quads
2013-07-26 03:27:24 +04:00
_textureAtlas - > increaseTotalQuadsWith ( system - > getTotalParticles ( ) ) ;
2012-03-14 10:55:17 +04:00
2012-04-19 10:35:52 +04:00
updateAllAtlasIndexes ( ) ;
2012-03-14 10:55:17 +04:00
}
//rebuild atlas indexes
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : updateAllAtlasIndexes ( )
2012-03-14 10:55:17 +04:00
{
2012-04-19 10:35:52 +04:00
unsigned int index = 0 ;
2013-11-28 12:02:03 +04:00
2013-11-28 14:23:06 +04:00
_children . forEach ( [ & index ] ( Node * child ) {
2013-11-28 12:02:03 +04:00
ParticleSystem * partiSys = static_cast < ParticleSystem * > ( child ) ;
partiSys - > setAtlasIndex ( index ) ;
index + = partiSys - > getTotalParticles ( ) ;
} ) ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
// ParticleBatchNode - CocosNodeTexture protocol
2012-03-14 10:55:17 +04:00
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : updateBlendFunc ( void )
2012-03-14 10:55:17 +04:00
{
2013-07-26 00:36:19 +04:00
if ( ! _textureAtlas - > getTexture ( ) - > hasPremultipliedAlpha ( ) )
_blendFunc = BlendFunc : : ALPHA_NON_PREMULTIPLIED ;
2012-03-14 10:55:17 +04:00
}
2013-06-20 10:13:12 +04:00
void ParticleBatchNode : : setTexture ( Texture2D * texture )
2012-03-14 10:55:17 +04:00
{
2013-06-15 10:03:30 +04:00
_textureAtlas - > setTexture ( texture ) ;
2012-04-19 10:35:52 +04:00
// If the new texture has No premultiplied alpha, AND the blendFunc hasn't been changed, then update it
2013-06-15 10:03:30 +04:00
if ( texture & & ! texture - > hasPremultipliedAlpha ( ) & & ( _blendFunc . src = = CC_BLEND_SRC & & _blendFunc . dst = = CC_BLEND_DST ) )
2012-04-19 10:35:52 +04:00
{
2013-07-26 00:36:19 +04:00
_blendFunc = BlendFunc : : ALPHA_NON_PREMULTIPLIED ;
2012-04-19 10:35:52 +04:00
}
2012-03-14 10:55:17 +04:00
}
2013-07-23 14:26:26 +04:00
Texture2D * ParticleBatchNode : : getTexture ( void ) const
2012-03-14 10:55:17 +04:00
{
2013-06-15 10:03:30 +04:00
return _textureAtlas - > getTexture ( ) ;
2012-03-14 10:55:17 +04:00
}
2013-07-05 12:49:22 +04:00
void ParticleBatchNode : : setBlendFunc ( const BlendFunc & blendFunc )
2012-04-19 10:35:52 +04:00
{
2013-06-15 10:03:30 +04:00
_blendFunc = blendFunc ;
2012-04-19 10:35:52 +04:00
}
// returns the blending function used for the texture
2013-07-05 12:49:22 +04:00
const BlendFunc & ParticleBatchNode : : getBlendFunc ( void ) const
2012-03-14 10:55:17 +04:00
{
2013-06-15 10:03:30 +04:00
return _blendFunc ;
2012-03-14 10:55:17 +04:00
}
NS_CC_END