afxParticlePool.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "T3D/fx/particleEmitter.h"
  26. #include "afx/afxChoreographer.h"
  27. #include "afx/util/afxParticlePool.h"
  28. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  29. IMPLEMENT_CO_DATABLOCK_V1(afxParticlePoolData);
  30. ConsoleDocClass( afxParticlePoolData,
  31. "@brief A ParticlePool datablock.\n\n"
  32. "@ingroup afxUtil\n"
  33. "@ingroup AFX\n"
  34. );
  35. Vector<afxParticlePool::SortParticlePool> afxParticlePool::orderedVector;
  36. afxParticlePoolData::afxParticlePoolData()
  37. {
  38. pool_type = POOL_NORMAL;
  39. base_color.set(0.0f, 0.0f, 0.0f, 1.0f);
  40. blend_weight = 1.0f;
  41. }
  42. afxParticlePoolData::afxParticlePoolData(const afxParticlePoolData& other, bool temp_clone) : GameBaseData(other, temp_clone)
  43. {
  44. pool_type = other.pool_type;
  45. base_color = other.base_color;
  46. blend_weight = other.blend_weight;
  47. }
  48. ImplementEnumType( afxParticlePool_PoolType, "Possible particle pool types.\n" "@ingroup afxParticlePool\n\n" )
  49. { afxParticlePoolData::POOL_NORMAL, "normal", "..." },
  50. { afxParticlePoolData::POOL_TWOPASS, "two-pass", "..." },
  51. // Alias...
  52. { afxParticlePoolData::POOL_TWOPASS, "twopass", "..." },
  53. EndImplementEnumType;
  54. afxParticlePoolData::~afxParticlePoolData()
  55. {
  56. }
  57. void afxParticlePoolData::initPersistFields()
  58. {
  59. docsURL;
  60. addField("poolType", TYPEID< afxParticlePoolData::PoolType >(), Offset(pool_type, afxParticlePoolData),
  61. "...");
  62. addField("baseColor", TypeColorF, Offset(base_color, afxParticlePoolData),
  63. "...");
  64. addField("blendWeight", TypeF32, Offset(blend_weight, afxParticlePoolData),
  65. "...");
  66. Parent::initPersistFields();
  67. };
  68. void afxParticlePoolData::packData(BitStream* stream)
  69. {
  70. Parent::packData(stream);
  71. stream->write(pool_type);
  72. stream->write(base_color);
  73. stream->write(blend_weight);
  74. };
  75. void afxParticlePoolData::unpackData(BitStream* stream)
  76. {
  77. Parent::unpackData(stream);
  78. stream->read(&pool_type);
  79. stream->read(&base_color);
  80. stream->read(&blend_weight);
  81. };
  82. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  83. IMPLEMENT_CO_NETOBJECT_V1(afxParticlePool);
  84. ConsoleDocClass( afxParticlePool,
  85. "@brief A ParticlePool object as defined by an afxParticlePoolData datablock.\n\n"
  86. "@ingroup afxUtil\n"
  87. "@ingroup AFX\n"
  88. );
  89. afxParticlePool::afxParticlePool()
  90. {
  91. mDataBlock = 0;
  92. key_block = 0;
  93. key_index = 0;
  94. choreographer = 0;
  95. sort_priority = S8_MAX;
  96. mNetFlags.set(IsGhost);
  97. mTypeMask |= StaticObjectType;
  98. mCurBuffSize = mCurBuffSize2 = 0;
  99. };
  100. afxParticlePool::~afxParticlePool()
  101. {
  102. for (S32 i = 0; i < emitters.size(); i++)
  103. if (emitters[i])
  104. emitters[i]->clearPool();
  105. if (choreographer)
  106. choreographer->unregisterParticlePool(this);
  107. if (mDataBlock && mDataBlock->isTempClone())
  108. {
  109. delete mDataBlock;
  110. mDataBlock = 0;
  111. }
  112. }
  113. bool afxParticlePool::onNewDataBlock(GameBaseData* dptr, bool reload)
  114. {
  115. mDataBlock = dynamic_cast<afxParticlePoolData*>(dptr);
  116. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  117. return false;
  118. return true;
  119. }
  120. bool afxParticlePool::onAdd()
  121. {
  122. if (!Parent::onAdd())
  123. return false;
  124. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  125. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  126. resetWorldBox();
  127. addToScene();
  128. return true;
  129. };
  130. void afxParticlePool::onRemove()
  131. {
  132. removeFromScene();
  133. Parent::onRemove();
  134. };
  135. void afxParticlePool::addParticleEmitter(ParticleEmitter* emitter)
  136. {
  137. emitters.push_back(emitter);
  138. }
  139. void afxParticlePool::removeParticleEmitter(ParticleEmitter* emitter)
  140. {
  141. for (U32 i=0; i < emitters.size(); i++)
  142. if (emitters[i] == emitter)
  143. {
  144. emitters.erase(i);
  145. break;
  146. }
  147. if (emitters.empty())
  148. {
  149. if (choreographer)
  150. {
  151. choreographer->unregisterParticlePool(this);
  152. choreographer = 0;
  153. }
  154. Sim::postEvent(this, new ObjectDeleteEvent, Sim::getCurrentTime() + 500);
  155. }
  156. }
  157. void afxParticlePool::updatePoolBBox(ParticleEmitter* emitter)
  158. {
  159. if (emitter->mObjBox.minExtents.x < mObjBox.minExtents.x)
  160. mObjBox.minExtents.x = emitter->mObjBox.minExtents.x;
  161. if (emitter->mObjBox.minExtents.y < mObjBox.minExtents.y)
  162. mObjBox.minExtents.y = emitter->mObjBox.minExtents.y;
  163. if (emitter->mObjBox.minExtents.z < mObjBox.minExtents.z)
  164. mObjBox.minExtents.z = emitter->mObjBox.minExtents.z;
  165. if (emitter->mObjBox.maxExtents.x > mObjBox.maxExtents.x)
  166. mObjBox.maxExtents.x = emitter->mObjBox.maxExtents.x;
  167. if (emitter->mObjBox.maxExtents.y > mObjBox.maxExtents.y)
  168. mObjBox.maxExtents.y = emitter->mObjBox.maxExtents.y;
  169. if (emitter->mObjBox.maxExtents.z > mObjBox.maxExtents.z)
  170. mObjBox.maxExtents.z = emitter->mObjBox.maxExtents.z;
  171. resetWorldBox();
  172. }
  173. void afxParticlePool::setSortPriority(S8 priority)
  174. {
  175. if (priority < sort_priority)
  176. sort_priority = (priority == 0) ? 1 : priority;
  177. }
  178. int QSORT_CALLBACK afxParticlePool::cmpSortParticlePool(const void* p1, const void* p2)
  179. {
  180. const SortParticlePool* sp1 = (const SortParticlePool*)p1;
  181. const SortParticlePool* sp2 = (const SortParticlePool*)p2;
  182. if (sp2->k > sp1->k)
  183. return 1;
  184. else if (sp2->k == sp1->k)
  185. return 0;
  186. else
  187. return -1;
  188. }
  189. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//