ParticleSystem.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "2d/core/ParticleSystem.h"
  23. //------------------------------------------------------------------------------
  24. ParticleSystem* ParticleSystem::Instance = NULL;
  25. //------------------------------------------------------------------------------
  26. void ParticleSystem::Init( void )
  27. {
  28. // Create the particle system.
  29. Instance = new ParticleSystem();
  30. }
  31. //------------------------------------------------------------------------------
  32. void ParticleSystem::destroy( void )
  33. {
  34. // Delete the particle system.
  35. delete Instance;
  36. Instance = NULL;
  37. }
  38. //------------------------------------------------------------------------------
  39. ParticleSystem::ParticleSystem() :
  40. mParticlePoolBlockSize(512)
  41. {
  42. // Reset the free particle head.
  43. mpFreeParticleNodes = NULL;
  44. // Reset the active particle count.
  45. mActiveParticleCount = 0;
  46. }
  47. //------------------------------------------------------------------------------
  48. ParticleSystem::~ParticleSystem()
  49. {
  50. // Destroy all the particle pool blocks.
  51. for ( U32 n = 0; n < (U32)mParticlePool.size(); n++ )
  52. delete [] mParticlePool[n];
  53. // Clear the particle pool.
  54. mParticlePool.clear();
  55. // Reset the free particle head.
  56. mpFreeParticleNodes = NULL;
  57. }
  58. //------------------------------------------------------------------------------
  59. ParticleSystem::ParticleNode* ParticleSystem::createParticle( void )
  60. {
  61. // Have we got any free particle nodes?
  62. if ( mpFreeParticleNodes == NULL )
  63. {
  64. // No, so generate a new free pool block.
  65. ParticleNode* pFreePoolBlock = new ParticleNode[mParticlePoolBlockSize];
  66. // Store new free pool block.
  67. mParticlePool.push_back( pFreePoolBlock );
  68. // Initialise Free Pool Block.
  69. for ( U32 n = 0; n < (mParticlePoolBlockSize-1); n++ )
  70. {
  71. pFreePoolBlock[n].mPreviousNode = NULL;
  72. pFreePoolBlock[n].mNextNode = pFreePoolBlock+n+1;
  73. }
  74. // Insert Last Node Preceding any existing free nodes.
  75. pFreePoolBlock[mParticlePoolBlockSize-1].mPreviousNode = NULL;
  76. pFreePoolBlock[mParticlePoolBlockSize-1].mNextNode = mpFreeParticleNodes;
  77. // Set Free References.
  78. mpFreeParticleNodes = pFreePoolBlock;
  79. }
  80. // Fetch a free node,
  81. ParticleNode* pFreeParticleNode = mpFreeParticleNodes;
  82. // Set the new free node reference.
  83. mpFreeParticleNodes = mpFreeParticleNodes->mNextNode;
  84. // Reset any previous or next node references.
  85. pFreeParticleNode->mNextNode = NULL;
  86. pFreeParticleNode->mPreviousNode = NULL;
  87. // Increase the active particle count.
  88. mActiveParticleCount++;
  89. return pFreeParticleNode;
  90. }
  91. //------------------------------------------------------------------------------
  92. void ParticleSystem::freeParticle( ParticleNode* pParticleNode )
  93. {
  94. // Reset the particle.
  95. pParticleNode->resetState();
  96. // Remove the previous node reference.
  97. pParticleNode->mPreviousNode = NULL;
  98. // Insert the node into the free pool.
  99. pParticleNode->mNextNode = mpFreeParticleNodes;
  100. mpFreeParticleNodes = pParticleNode;
  101. // Decrease the active particle count.
  102. mActiveParticleCount--;
  103. }