2012-03-14 14:55:17 +08:00
/*
* Copyright ( C ) 2009 Matt Oswald
* Copyright ( c ) 2009 - 2010 Ricardo Quesada
2014-01-07 11:25:07 +08:00
* Copyright ( c ) 2010 - 2012 cocos2d - x . org
* Copyright ( c ) 2011 Zynga Inc .
* Copyright ( c ) 2011 Marco Tillemans
* Copyright ( c ) 2013 - 2014 Chukong Technologies Inc .
2012-03-14 14:55:17 +08:00
*
* 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 .
*
*/
# ifndef __CCPARTICLEBATCHNODE_H__
# define __CCPARTICLEBATCHNODE_H__
2014-04-27 01:11:22 +08:00
# include "2d/CCNode.h"
2014-05-17 05:36:00 +08:00
# include "base/CCProtocols.h"
2014-04-30 08:37:36 +08:00
# include "renderer/CCBatchCommand.h"
2012-03-14 14:55:17 +08:00
NS_CC_BEGIN
2013-06-20 14:13:12 +08:00
class Texture2D ;
class TextureAtlas ;
class ParticleSystem ;
2012-03-14 14:55:17 +08:00
2012-06-20 18:09:11 +08:00
/**
* @ addtogroup particle_nodes
* @ {
*/
2013-06-20 14:13:12 +08:00
# define kParticleDefaultCapacity 500
2012-06-14 15:13:16 +08:00
2013-06-20 14:13:12 +08:00
/** ParticleBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call
2012-03-14 14:55:17 +08:00
* ( often known as " batch draw " ) .
*
2013-06-20 14:13:12 +08:00
* A ParticleBatchNode can reference one and only one texture ( one image file , one texture atlas ) .
* Only the ParticleSystems that are contained in that texture can be added to the SpriteBatchNode .
* All ParticleSystems added to a SpriteBatchNode are drawn in one OpenGL ES draw call .
* If the ParticleSystems are not added to a ParticleBatchNode then an OpenGL ES draw call will be needed for each one , which is less efficient .
2012-03-14 14:55:17 +08:00
*
*
* Limitations :
2013-06-20 14:13:12 +08:00
* - At the moment only ParticleSystemQuad is supported
2012-03-14 14:55:17 +08:00
* - All systems need to be drawn with the same parameters , blend function , aliasing , texture
*
* Most efficient usage
* - Initialize the ParticleBatchNode with the texture and enough capacity for all the particle systems
* - Initialize all particle systems and add them as child to the batch node
* @ since v1 .1
*/
2013-06-20 14:13:12 +08:00
class CC_DLL ParticleBatchNode : public Node , public TextureProtocol
2012-03-14 14:55:17 +08:00
{
public :
2013-06-20 14:13:12 +08:00
/** initializes the particle system with Texture2D, a capacity of particles, which particle system to use */
2013-11-15 09:19:16 +08:00
static ParticleBatchNode * createWithTexture ( Texture2D * tex , int capacity = kParticleDefaultCapacity ) ;
2012-04-19 14:35:52 +08:00
2013-06-20 14:13:12 +08:00
/** initializes the particle system with the name of a file on disk (for a list of supported formats look at the Texture2D class), a capacity of particles */
2013-11-15 09:19:16 +08:00
static ParticleBatchNode * create ( const std : : string & fileImage , int capacity = kParticleDefaultCapacity ) ;
2014-01-03 18:15:56 +08:00
2013-06-20 14:13:12 +08:00
/** Inserts a child into the ParticleBatchNode */
2013-07-26 07:27:24 +08:00
void insertChild ( ParticleSystem * system , int index ) ;
2012-04-19 14:35:52 +08:00
2013-12-05 17:19:01 +08:00
void removeChildAtIndex ( int index , bool doCleanup ) ;
2012-04-19 14:35:52 +08:00
void removeAllChildrenWithCleanup ( bool doCleanup ) ;
2013-07-18 07:56:19 +08:00
2012-04-19 14:35:52 +08:00
/** disables a particle by inserting a 0'd quad into the texture atlas */
2013-12-05 17:19:01 +08:00
void disableParticle ( int particleIndex ) ;
2012-04-19 14:35:52 +08:00
2013-07-23 18:26:26 +08:00
/** Gets the texture atlas used for drawing the quads */
inline TextureAtlas * getTextureAtlas ( ) const { return _textureAtlas ; } ;
/** Sets the texture atlas used for drawing the quads */
inline void setTextureAtlas ( TextureAtlas * atlas ) { _textureAtlas = atlas ; } ;
2013-07-18 07:56:19 +08:00
// Overrides
2014-05-31 07:42:05 +08:00
virtual void visit ( Renderer * renderer , const Mat4 & parentTransform , uint32_t parentFlags ) override ;
2013-12-23 16:58:26 +08:00
using Node : : addChild ;
2013-07-18 07:56:19 +08:00
virtual void addChild ( Node * child , int zOrder , int tag ) override ;
virtual void removeChild ( Node * child , bool cleanup ) override ;
virtual void reorderChild ( Node * child , int zOrder ) override ;
2014-05-31 07:42:05 +08:00
virtual void draw ( Renderer * renderer , const Mat4 & transform , uint32_t flags ) override ;
2013-07-23 18:26:26 +08:00
virtual Texture2D * getTexture ( void ) const override ;
2013-07-18 07:56:19 +08:00
virtual void setTexture ( Texture2D * texture ) override ;
2013-09-13 11:41:20 +08:00
/**
* @ code
* When this function bound into js or lua , the parameter will be changed
* In js : var setBlendFunc ( var src , var dst )
* @ endcode
* @ lua NA
*/
2013-07-18 07:56:19 +08:00
virtual void setBlendFunc ( const BlendFunc & blendFunc ) override ;
2013-09-13 11:41:20 +08:00
/**
* @ js NA
* @ lua NA
*/
2013-07-18 07:56:19 +08:00
virtual const BlendFunc & getBlendFunc ( void ) const override ;
2014-01-03 18:15:56 +08:00
2014-03-21 13:44:29 +08:00
CC_CONSTRUCTOR_ACCESS :
2014-01-03 18:15:56 +08:00
/**
* @ js ctor
*/
ParticleBatchNode ( ) ;
2014-03-21 13:44:29 +08:00
/**
* @ js NA
* @ lua NA
*/
virtual ~ ParticleBatchNode ( ) ;
2014-01-03 18:15:56 +08:00
/** initializes the particle system with Texture2D, a capacity of particles */
bool initWithTexture ( Texture2D * tex , int capacity ) ;
/** initializes the particle system with the name of a file on disk (for a list of supported formats look at the Texture2D class), a capacity of particles */
bool initWithFile ( const std : : string & fileImage , int capacity ) ;
2014-03-21 13:44:29 +08:00
2012-03-14 14:55:17 +08:00
private :
2012-04-19 14:35:52 +08:00
void updateAllAtlasIndexes ( ) ;
2013-12-12 12:07:20 +08:00
void increaseAtlasCapacityTo ( ssize_t quantity ) ;
2013-12-05 17:19:01 +08:00
int searchNewPositionInChildrenForZ ( int z ) ;
void getCurrentIndex ( int * oldIndex , int * newIndex , Node * child , int z ) ;
int addChildHelper ( ParticleSystem * child , int z , int aTag ) ;
2012-04-19 14:35:52 +08:00
void updateBlendFunc ( void ) ;
/** the texture atlas used for drawing the quads */
2013-07-23 18:26:26 +08:00
TextureAtlas * _textureAtlas ;
2013-07-18 07:56:19 +08:00
2012-04-19 14:35:52 +08:00
/** the blend function used for drawing the quads */
2013-07-05 16:49:22 +08:00
BlendFunc _blendFunc ;
2013-12-26 16:36:03 +08:00
// quad command
2014-01-16 08:07:38 +08:00
BatchCommand _batchCommand ;
2012-03-14 14:55:17 +08:00
} ;
2012-06-20 18:09:11 +08:00
// end of particle_nodes group
/// @}
2012-03-14 14:55:17 +08:00
NS_CC_END
# endif /* __CCPARTICLEBATCHNODE_H__ */