afxParticlePool.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. addField("poolType", TYPEID< afxParticlePoolData::PoolType >(), Offset(pool_type, afxParticlePoolData),
  60. "...");
  61. addField("baseColor", TypeColorF, Offset(base_color, afxParticlePoolData),
  62. "...");
  63. addField("blendWeight", TypeF32, Offset(blend_weight, afxParticlePoolData),
  64. "...");
  65. Parent::initPersistFields();
  66. };
  67. void afxParticlePoolData::packData(BitStream* stream)
  68. {
  69. Parent::packData(stream);
  70. stream->write(pool_type);
  71. stream->write(base_color);
  72. stream->write(blend_weight);
  73. };
  74. void afxParticlePoolData::unpackData(BitStream* stream)
  75. {
  76. Parent::unpackData(stream);
  77. stream->read(&pool_type);
  78. stream->read(&base_color);
  79. stream->read(&blend_weight);
  80. };
  81. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  82. IMPLEMENT_CO_NETOBJECT_V1(afxParticlePool);
  83. ConsoleDocClass( afxParticlePool,
  84. "@brief A ParticlePool object as defined by an afxParticlePoolData datablock.\n\n"
  85. "@ingroup afxUtil\n"
  86. "@ingroup AFX\n"
  87. );
  88. afxParticlePool::afxParticlePool()
  89. {
  90. mDataBlock = 0;
  91. key_block = 0;
  92. key_index = 0;
  93. choreographer = 0;
  94. sort_priority = S8_MAX;
  95. mNetFlags.set(IsGhost);
  96. mTypeMask |= StaticObjectType;
  97. mCurBuffSize = mCurBuffSize2 = 0;
  98. };
  99. afxParticlePool::~afxParticlePool()
  100. {
  101. for (S32 i = 0; i < emitters.size(); i++)
  102. if (emitters[i])
  103. emitters[i]->clearPool();
  104. if (choreographer)
  105. choreographer->unregisterParticlePool(this);
  106. if (mDataBlock && mDataBlock->isTempClone())
  107. {
  108. delete mDataBlock;
  109. mDataBlock = 0;
  110. }
  111. }
  112. bool afxParticlePool::onNewDataBlock(GameBaseData* dptr, bool reload)
  113. {
  114. mDataBlock = dynamic_cast<afxParticlePoolData*>(dptr);
  115. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  116. return false;
  117. return true;
  118. }
  119. bool afxParticlePool::onAdd()
  120. {
  121. if (!Parent::onAdd())
  122. return false;
  123. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  124. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  125. resetWorldBox();
  126. addToScene();
  127. return true;
  128. };
  129. void afxParticlePool::onRemove()
  130. {
  131. removeFromScene();
  132. Parent::onRemove();
  133. };
  134. void afxParticlePool::addParticleEmitter(ParticleEmitter* emitter)
  135. {
  136. emitters.push_back(emitter);
  137. }
  138. void afxParticlePool::removeParticleEmitter(ParticleEmitter* emitter)
  139. {
  140. for (U32 i=0; i < emitters.size(); i++)
  141. if (emitters[i] == emitter)
  142. {
  143. emitters.erase(i);
  144. break;
  145. }
  146. if (emitters.empty())
  147. {
  148. if (choreographer)
  149. {
  150. choreographer->unregisterParticlePool(this);
  151. choreographer = 0;
  152. }
  153. Sim::postEvent(this, new ObjectDeleteEvent, Sim::getCurrentTime() + 500);
  154. }
  155. }
  156. void afxParticlePool::updatePoolBBox(ParticleEmitter* emitter)
  157. {
  158. if (emitter->mObjBox.minExtents.x < mObjBox.minExtents.x)
  159. mObjBox.minExtents.x = emitter->mObjBox.minExtents.x;
  160. if (emitter->mObjBox.minExtents.y < mObjBox.minExtents.y)
  161. mObjBox.minExtents.y = emitter->mObjBox.minExtents.y;
  162. if (emitter->mObjBox.minExtents.z < mObjBox.minExtents.z)
  163. mObjBox.minExtents.z = emitter->mObjBox.minExtents.z;
  164. if (emitter->mObjBox.maxExtents.x > mObjBox.maxExtents.x)
  165. mObjBox.maxExtents.x = emitter->mObjBox.maxExtents.x;
  166. if (emitter->mObjBox.maxExtents.y > mObjBox.maxExtents.y)
  167. mObjBox.maxExtents.y = emitter->mObjBox.maxExtents.y;
  168. if (emitter->mObjBox.maxExtents.z > mObjBox.maxExtents.z)
  169. mObjBox.maxExtents.z = emitter->mObjBox.maxExtents.z;
  170. resetWorldBox();
  171. }
  172. void afxParticlePool::setSortPriority(S8 priority)
  173. {
  174. if (priority < sort_priority)
  175. sort_priority = (priority == 0) ? 1 : priority;
  176. }
  177. int QSORT_CALLBACK afxParticlePool::cmpSortParticlePool(const void* p1, const void* p2)
  178. {
  179. const SortParticlePool* sp1 = (const SortParticlePool*)p1;
  180. const SortParticlePool* sp2 = (const SortParticlePool*)p2;
  181. if (sp2->k > sp1->k)
  182. return 1;
  183. else if (sp2->k == sp1->k)
  184. return 0;
  185. else
  186. return -1;
  187. }
  188. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//