ZMLParticleSystemQuad.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Leonardo Kasperavičius
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2017 Chukong Technologies Inc.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #ifndef __RUZMLPARTICLE_SYSTEM_QUAD_H__
  25. #define __RUZMLPARTICLE_SYSTEM_QUAD_H__
  26. #include "ZMLParticleSystem.h"
  27. #include "RUDefine.h"
  28. NS_RU_BEGIN
  29. struct PARTICLE_VERTEX_INFO{
  30. Vec4 TexturePercentage;
  31. Tex2F bl;
  32. Tex2F br;
  33. Tex2F tl;
  34. Tex2F tr;
  35. };
  36. class ZMLParticleSystemQuad : public ZMLParticleSystem
  37. {
  38. public:
  39. /** Creates a Particle Emitter.
  40. *
  41. * @return An autoreleased ZMLParticleSystemQuad object.
  42. */
  43. static ZMLParticleSystemQuad * create();
  44. /** Creates a Particle Emitter with a number of particles.
  45. *
  46. * @param numberOfParticles A given number of particles.
  47. * @return An autoreleased ZMLParticleSystemQuad object.
  48. */
  49. static ZMLParticleSystemQuad * createWithTotalParticles(int numberOfParticles);
  50. /** Creates an initializes a ZMLParticleSystemQuad from a plist file.
  51. This plist files can be created manually or with Particle Designer.
  52. *
  53. * @param filename Particle plist file name.
  54. * @return An autoreleased ZMLParticleSystemQuad object.
  55. */
  56. static ZMLParticleSystemQuad * create(const std::string& filename);
  57. /** Creates a Particle Emitter with a dictionary.
  58. *
  59. * @param dictionary Particle dictionary.
  60. * @return An autoreleased ZMLParticleSystemQuad object.
  61. */
  62. static ZMLParticleSystemQuad * create(ValueMap &dictionary);
  63. static ZMLParticleSystemQuad *createWithJsonFile(string path);
  64. static ZMLParticleSystemQuad *createWithJsonString(string jsonStr);
  65. /** Sets a new texture with a rect. The rect is in Points.
  66. @since v0.99.4
  67. * @js NA
  68. * @lua NA
  69. *
  70. * @param texture A given texture.
  71. 8 @param rect A given rect, in points.
  72. */
  73. void setTextureWithRect(Texture2D *texture, const Rect& rect, bool isrotaed, Vec2 offset, Vec2 originalSize);
  74. /** Listen the event that renderer was recreated on Android/WP8.
  75. * @js NA
  76. * @lua NA
  77. *
  78. * @param event the event that renderer was recreated on Android/WP8.
  79. */
  80. void listenRendererRecreated(EventCustom* event);
  81. /**
  82. * @js NA
  83. * @lua NA
  84. */
  85. virtual void setTexture(Texture2D* texture) override;
  86. /**
  87. * @js NA
  88. * @lua NA
  89. */
  90. virtual void updateParticleQuads() override;
  91. /**
  92. * @js NA
  93. * @lua NA
  94. */
  95. virtual void postStep() override;
  96. /**
  97. * @js NA
  98. * @lua NA
  99. */
  100. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  101. /**
  102. * @js NA
  103. * @lua NA
  104. */
  105. virtual void setBatchNode(ParticleBatchNode* batchNode) override;
  106. /**
  107. * @js NA
  108. * @lua NA
  109. */
  110. virtual void setTotalParticles(int tp) override;
  111. virtual std::string getDescription() const override;
  112. void SetAlphaTexture(Texture2D* tex);
  113. void UpdateGLProgramState();
  114. CC_CONSTRUCTOR_ACCESS:
  115. /**
  116. * @js ctor
  117. */
  118. ZMLParticleSystemQuad();
  119. /**
  120. * @js NA
  121. * @lua NA
  122. */
  123. virtual ~ZMLParticleSystemQuad();
  124. // Overrides
  125. /**
  126. * @js NA
  127. * @lua NA
  128. */
  129. virtual bool initWithTotalParticles(int numberOfParticles) override;
  130. void setCbOnExit(std::function<void(const std::string&, ZMLParticleSystem*)> cb);
  131. //normalMap
  132. void setLightSprite(ClightSprite*lightSprite){
  133. assert(lightSprite);
  134. if(m_lightSprite==NULL){
  135. m_lightSprite=lightSprite;
  136. m_lightSprite->retain();
  137. }else{
  138. m_lightSprite->release();
  139. m_lightSprite=lightSprite;
  140. m_lightSprite->retain();
  141. }
  142. }
  143. ClightSprite* getLightSprite(){return m_lightSprite;}
  144. void setKBump(float value){_colorPower = value;}
  145. float getKBump()const{return _colorPower;}
  146. protected:
  147. /** initializes the indices for the vertices*/
  148. void initIndices();
  149. /** initializes the texture with a rectangle measured Points */
  150. void initTexCoordsWithRect(const Rect& rect, bool isrotaed, Vec2 offset, Vec2 originalSize);
  151. /** Updates texture coords */
  152. void updateTexCoords();
  153. void setupVBOandVAO();
  154. void setupVBO();
  155. bool allocMemory();
  156. virtual void onExit() override;
  157. V3F_C4B_T2F_Quad *_quads; // quads to be rendered
  158. GLushort *_indices; // indices
  159. GLuint _VAOname;
  160. GLuint _buffersVBO[2]; //0: vertex 1: indices
  161. QuadCommand _quadCommand; // quad command
  162. ClightSprite*m_lightSprite;
  163. Cmaterial m_material;
  164. CustomCommand _passUnifoAndBindTexCommand;
  165. void onPassUnifoAndBindTex(const Mat4 &transform, uint32_t flags);
  166. protected:
  167. // float * _uvleft;
  168. // float * _uvbottom;
  169. // float * _uvright;
  170. // float * _uvtop;
  171. PARTICLE_VERTEX_INFO *_vertexInfo;
  172. public:
  173. void addSpriteFrame(SpriteFrame *spriteFrame);
  174. void addSpriteFrame(const std::string &spriteFrameName);
  175. void autoUpdateTexture();
  176. private:
  177. CC_DISALLOW_COPY_AND_ASSIGN(ZMLParticleSystemQuad);
  178. std::function<void(const std::string&, ZMLParticleSystem*)> _cbOnExit;
  179. std::string _plName;
  180. };
  181. // end of _2d group
  182. /// @}
  183. NS_RU_END
  184. #endif //__RUZMLPARTICLE_SYSTEM_QUAD_H__