2012-03-14 14:55:17 +08:00
/*
2012-06-14 15:13:16 +08:00
* Copyright ( c ) 2010 - 2012 cocos2d - x . org
2012-03-14 14:55:17 +08: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 .
*
*/
# ifndef __CCPARTICLEBATCHNODE_H__
# define __CCPARTICLEBATCHNODE_H__
2012-06-19 13:50:11 +08:00
# include "base_nodes/CCNode.h"
2012-03-14 14:55:17 +08:00
# include "CCProtocols.h"
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 */
static ParticleBatchNode * createWithTexture ( Texture2D * tex , unsigned 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 */
static ParticleBatchNode * create ( const char * fileImage , unsigned int capacity = kParticleDefaultCapacity ) ;
2012-04-19 14:35:52 +08:00
2013-07-18 07:56:19 +08:00
ParticleBatchNode ( ) ;
virtual ~ ParticleBatchNode ( ) ;
2013-06-20 14:13:12 +08:00
/** initializes the particle system with Texture2D, a capacity of particles */
bool initWithTexture ( Texture2D * tex , unsigned int capacity ) ;
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 */
2012-04-19 14:35:52 +08:00
bool initWithFile ( const char * fileImage , unsigned int capacity ) ;
2013-06-20 14:13:12 +08:00
/** Inserts a child into the ParticleBatchNode */
void insertChild ( ParticleSystem * pSystem , unsigned int index ) ;
2012-04-19 14:35:52 +08:00
void removeChildAtIndex ( unsigned int index , bool doCleanup ) ;
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 */
void disableParticle ( unsigned int particleIndex ) ;
2013-07-18 07:56:19 +08:00
// Overrides
2012-04-19 14:35:52 +08:00
void visit ( ) ;
2013-07-18 07:56:19 +08:00
virtual void addChild ( Node * child ) override ;
virtual void addChild ( Node * child , int zOrder ) override ;
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 ;
virtual void draw ( void ) override ;
virtual Texture2D * getTexture ( void ) override ;
virtual void setTexture ( Texture2D * texture ) override ;
virtual void setBlendFunc ( const BlendFunc & blendFunc ) override ;
virtual const BlendFunc & getBlendFunc ( void ) const override ;
2012-03-14 14:55:17 +08:00
private :
2012-04-19 14:35:52 +08:00
void updateAllAtlasIndexes ( ) ;
void increaseAtlasCapacityTo ( unsigned int quantity ) ;
unsigned int searchNewPositionInChildrenForZ ( int z ) ;
2013-06-20 14:13:12 +08:00
void getCurrentIndex ( unsigned int * oldIndex , unsigned int * newIndex , Node * child , int z ) ;
unsigned 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-06-20 14:13:12 +08:00
CC_SYNTHESIZE ( TextureAtlas * , _textureAtlas , TextureAtlas ) ;
2013-07-18 07:56:19 +08:00
2012-03-14 14:55:17 +08:00
private :
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 ;
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__ */