ParticleSystem.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// Interpolated Tick Position.
  62. Vector2 mPreTickPosition;
  63. Vector2 mPostTickPosition;
  64. Vector2 mRenderTickPosition;
  65. ParticleNode() { constructInPlace<ImageFrameProviderCore>(&mFrameProvider); resetState(); }
  66. virtual void resetState( void )
  67. {
  68. mFrameProvider.resetState();
  69. }
  70. };
  71. private:
  72. const U32 mParticlePoolBlockSize;
  73. Vector<ParticleNode*> mParticlePool;
  74. ParticleNode* mpFreeParticleNodes;
  75. U32 mActiveParticleCount;
  76. public:
  77. static void Init( void );
  78. static void destroy( void );
  79. static ParticleSystem* Instance;
  80. ParticleSystem();
  81. ~ParticleSystem();
  82. ParticleNode* createParticle( void );
  83. void freeParticle( ParticleNode* pParticleNode );
  84. inline U32 getActiveParticleCount( void ) const { return mActiveParticleCount; };
  85. inline U32 getAllocatedParticleCount( void ) const { return (U32)mParticlePool.size() * mParticlePoolBlockSize; }
  86. };
  87. #endif // _PARTICLE_SYSTEM_H_