ParticleSystem.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PARTICLE_SYSTEM_H_
  23. #define _PARTICLE_SYSTEM_H_
  24. #ifndef _IMAGE_FRAME_PROVIDER_H
  25. #include "2d/core/ImageFrameProvider.h"
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. class ParticleSystem
  29. {
  30. public:
  31. /// Particle node.
  32. struct ParticleNode : public IFactoryObjectReset
  33. {
  34. /// Particle Node Linkages.
  35. ParticleNode* mPreviousNode;
  36. ParticleNode* mNextNode;
  37. /// Suppress Movement.
  38. bool mSuppressMovement;
  39. /// Particle Components.
  40. F32 mParticleLifetime;
  41. F32 mParticleAge;
  42. Vector2 mPosition;
  43. Vector2 mVelocity;
  44. F32 mOrientationAngle;
  45. Vector2 mRenderOOBB[4];
  46. b2Transform mTransform;
  47. ImageFrameProviderCore mFrameProvider;
  48. /// Render Properties.
  49. Vector2 mRenderSize;
  50. F32 mRenderSpeed;
  51. F32 mRenderSpin;
  52. F32 mRenderFixedForce;
  53. F32 mRenderRandomMotion;
  54. /// Base Properties.
  55. Vector2 mSize;
  56. F32 mSpeed;
  57. F32 mSpin;
  58. F32 mFixedForce;
  59. F32 mRandomMotion;
  60. ColorF mColor;
  61. /// Physics particles
  62. bool mPhysicsParticles;
  63. b2ParticleGroupDef mParticleGroup;
  64. b2ParticleSystem* mParticleSystem;
  65. /// Interpolated Tick Position.
  66. Vector2 mPreTickPosition;
  67. Vector2 mPostTickPosition;
  68. Vector2 mRenderTickPosition;
  69. ParticleNode() { constructInPlace<ImageFrameProviderCore>(&mFrameProvider); resetState(); }
  70. virtual void resetState( void )
  71. {
  72. mFrameProvider.resetState();
  73. }
  74. };
  75. private:
  76. const U32 mParticlePoolBlockSize;
  77. Vector<ParticleNode*> mParticlePool;
  78. ParticleNode* mpFreeParticleNodes;
  79. U32 mActiveParticleCount;
  80. public:
  81. static void Init( void );
  82. static void destroy( void );
  83. static ParticleSystem* Instance;
  84. ParticleSystem();
  85. ~ParticleSystem();
  86. ParticleNode* createParticle( void );
  87. void freeParticle( ParticleNode* pParticleNode );
  88. inline U32 getActiveParticleCount( void ) const { return mActiveParticleCount; };
  89. inline U32 getAllocatedParticleCount( void ) const { return (U32)mParticlePool.size() * mParticlePoolBlockSize; }
  90. };
  91. #endif // _PARTICLE_SYSTEM_H_