afxParticlePool.cpp 6.4 KB

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