CCParticleSystem.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. // ideas taken from:
  25. // . The ocean spray in your face [Jeff Lander]
  26. // http://www.double.co.nz/dust/col0798.pdf
  27. // . Building an Advanced Particle System [John van der Burg]
  28. // http://www.gamasutra.com/features/20000623/vanderburg_01.htm
  29. // . LOVE game engine
  30. // http://love2d.org/
  31. //
  32. //
  33. // Radius mode support, from 71 squared
  34. // http://particledesigner.71squared.com/
  35. //
  36. // IMPORTANT: Particle Designer is supported by cocos2d, but
  37. // 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
  38. // cocos2d uses a another approach, but the results are almost identical.
  39. //
  40. #include "2d/CCParticleSystem.h"
  41. #include <string>
  42. #include "2d/CCParticleBatchNode.h"
  43. #include "renderer/CCTextureAtlas.h"
  44. #include "base/base64.h"
  45. #include "base/ZipUtils.h"
  46. #include "base/CCDirector.h"
  47. #include "base/CCProfiling.h"
  48. #include "base/ccUTF8.h"
  49. #include "base/ccUtils.h"
  50. #include "renderer/CCTextureCache.h"
  51. #include "platform/CCFileUtils.h"
  52. using namespace std;
  53. NS_CC_BEGIN
  54. // ideas taken from:
  55. // . The ocean spray in your face [Jeff Lander]
  56. // http://www.double.co.nz/dust/col0798.pdf
  57. // . Building an Advanced Particle System [John van der Burg]
  58. // http://www.gamasutra.com/features/20000623/vanderburg_01.htm
  59. // . LOVE game engine
  60. // http://love2d.org/
  61. //
  62. //
  63. // Radius mode support, from 71 squared
  64. // http://particledesigner.71squared.com/
  65. //
  66. // IMPORTANT: Particle Designer is supported by cocos2d, but
  67. // 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
  68. // cocos2d uses a another approach, but the results are almost identical.
  69. //
  70. inline void normalize_point(float x, float y, particle_point* out)
  71. {
  72. float n = x * x + y * y;
  73. // Already normalized.
  74. if (n == 1.0f)
  75. return;
  76. n = sqrt(n);
  77. // Too close to zero.
  78. if (n < MATH_TOLERANCE)
  79. return;
  80. n = 1.0f / n;
  81. out->x = x * n;
  82. out->y = y * n;
  83. }
  84. /**
  85. A more effect random number getter function, get from ejoy2d.
  86. */
  87. inline static float RANDOM_M11(unsigned int *seed) {
  88. *seed = *seed * 134775813 + 1;
  89. union {
  90. uint32_t d;
  91. float f;
  92. } u;
  93. u.d = (((uint32_t)(*seed) & 0x7fff) << 8) | 0x40000000;
  94. return u.f - 3.0f;
  95. }
  96. ParticleData::ParticleData()
  97. {
  98. memset(this, 0, sizeof(ParticleData));
  99. }
  100. bool ParticleData::init(int count)
  101. {
  102. maxCount = count;
  103. posx= (float*)malloc(count * sizeof(float));
  104. posy= (float*)malloc(count * sizeof(float));
  105. startPosX= (float*)malloc(count * sizeof(float));
  106. startPosY= (float*)malloc(count * sizeof(float));
  107. colorR= (float*)malloc(count * sizeof(float));
  108. colorG= (float*)malloc(count * sizeof(float));
  109. colorB= (float*)malloc(count * sizeof(float));
  110. colorA= (float*)malloc(count * sizeof(float));
  111. deltaColorR= (float*)malloc(count * sizeof(float));
  112. deltaColorG= (float*)malloc(count * sizeof(float));
  113. deltaColorB= (float*)malloc(count * sizeof(float));
  114. deltaColorA= (float*)malloc(count * sizeof(float));
  115. size= (float*)malloc(count * sizeof(float));
  116. deltaSize= (float*)malloc(count * sizeof(float));
  117. rotation= (float*)malloc(count * sizeof(float));
  118. deltaRotation= (float*)malloc(count * sizeof(float));
  119. timeToLive= (float*)malloc(count * sizeof(float));
  120. atlasIndex= (unsigned int*)malloc(count * sizeof(unsigned int));
  121. modeA.dirX= (float*)malloc(count * sizeof(float));
  122. modeA.dirY= (float*)malloc(count * sizeof(float));
  123. modeA.radialAccel= (float*)malloc(count * sizeof(float));
  124. modeA.tangentialAccel= (float*)malloc(count * sizeof(float));
  125. modeB.angle= (float*)malloc(count * sizeof(float));
  126. modeB.degreesPerSecond= (float*)malloc(count * sizeof(float));
  127. modeB.deltaRadius= (float*)malloc(count * sizeof(float));
  128. modeB.radius= (float*)malloc(count * sizeof(float));
  129. return posx && posy && startPosY && startPosX && colorR && colorG && colorB && colorA &&
  130. deltaColorR && deltaColorG && deltaColorB && deltaColorA && size && deltaSize &&
  131. rotation && deltaRotation && timeToLive && atlasIndex && modeA.dirX && modeA.dirY &&
  132. modeA.radialAccel && modeA.tangentialAccel && modeB.angle && modeB.degreesPerSecond &&
  133. modeB.deltaRadius && modeB.radius;
  134. }
  135. void ParticleData::release()
  136. {
  137. CC_SAFE_FREE(posx);
  138. CC_SAFE_FREE(posy);
  139. CC_SAFE_FREE(startPosX);
  140. CC_SAFE_FREE(startPosY);
  141. CC_SAFE_FREE(colorR);
  142. CC_SAFE_FREE(colorG);
  143. CC_SAFE_FREE(colorB);
  144. CC_SAFE_FREE(colorA);
  145. CC_SAFE_FREE(deltaColorR);
  146. CC_SAFE_FREE(deltaColorG);
  147. CC_SAFE_FREE(deltaColorB);
  148. CC_SAFE_FREE(deltaColorA);
  149. CC_SAFE_FREE(size);
  150. CC_SAFE_FREE(deltaSize);
  151. CC_SAFE_FREE(rotation);
  152. CC_SAFE_FREE(deltaRotation);
  153. CC_SAFE_FREE(timeToLive);
  154. CC_SAFE_FREE(atlasIndex);
  155. CC_SAFE_FREE(modeA.dirX);
  156. CC_SAFE_FREE(modeA.dirY);
  157. CC_SAFE_FREE(modeA.radialAccel);
  158. CC_SAFE_FREE(modeA.tangentialAccel);
  159. CC_SAFE_FREE(modeB.angle);
  160. CC_SAFE_FREE(modeB.degreesPerSecond);
  161. CC_SAFE_FREE(modeB.deltaRadius);
  162. CC_SAFE_FREE(modeB.radius);
  163. }
  164. Vector<ParticleSystem*> ParticleSystem::__allInstances;
  165. float ParticleSystem::__totalParticleCountFactor = 1.0f;
  166. ParticleSystem::ParticleSystem()
  167. : _isBlendAdditive(false)
  168. , _isAutoRemoveOnFinish(false)
  169. , _plistFile("")
  170. , _elapsed(0)
  171. , _configName("")
  172. , _emitCounter(0)
  173. , _batchNode(nullptr)
  174. , _atlasIndex(0)
  175. , _transformSystemDirty(false)
  176. , _allocatedParticles(0)
  177. , _isActive(true)
  178. , _particleCount(0)
  179. , _duration(0)
  180. , _life(0)
  181. , _lifeVar(0)
  182. , _angle(0)
  183. , _angleVar(0)
  184. , _emitterMode(Mode::GRAVITY)
  185. , _startSize(0)
  186. , _startSizeVar(0)
  187. , _endSize(0)
  188. , _endSizeVar(0)
  189. , _startSpin(0)
  190. , _startSpinVar(0)
  191. , _endSpin(0)
  192. , _endSpinVar(0)
  193. , _emissionRate(0)
  194. , _totalParticles(0)
  195. , _texture(nullptr)
  196. , _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED)
  197. , _opacityModifyRGB(false)
  198. , _yCoordFlipped(1)
  199. , _positionType(PositionType::FREE)
  200. , _paused(false)
  201. , _sourcePositionCompatible(true) // In the furture this member's default value maybe false or be removed.
  202. {
  203. modeA.gravity.setZero();
  204. modeA.speed = 0;
  205. modeA.speedVar = 0;
  206. modeA.tangentialAccel = 0;
  207. modeA.tangentialAccelVar = 0;
  208. modeA.radialAccel = 0;
  209. modeA.radialAccelVar = 0;
  210. modeA.rotationIsDir = false;
  211. modeB.startRadius = 0;
  212. modeB.startRadiusVar = 0;
  213. modeB.endRadius = 0;
  214. modeB.endRadiusVar = 0;
  215. modeB.rotatePerSecond = 0;
  216. modeB.rotatePerSecondVar = 0;
  217. }
  218. // implementation ParticleSystem
  219. ParticleSystem * ParticleSystem::create(const std::string& plistFile)
  220. {
  221. ParticleSystem *ret = new (std::nothrow) ParticleSystem();
  222. if (ret && ret->initWithFile(plistFile))
  223. {
  224. ret->autorelease();
  225. return ret;
  226. }
  227. CC_SAFE_DELETE(ret);
  228. return ret;
  229. }
  230. ParticleSystem* ParticleSystem::createWithTotalParticles(int numberOfParticles)
  231. {
  232. ParticleSystem *ret = new (std::nothrow) ParticleSystem();
  233. if (ret && ret->initWithTotalParticles(numberOfParticles))
  234. {
  235. ret->autorelease();
  236. return ret;
  237. }
  238. CC_SAFE_DELETE(ret);
  239. return ret;
  240. }
  241. // static
  242. Vector<ParticleSystem*>& ParticleSystem::getAllParticleSystems()
  243. {
  244. return __allInstances;
  245. }
  246. void ParticleSystem::setTotalParticleCountFactor(float factor)
  247. {
  248. __totalParticleCountFactor = factor;
  249. }
  250. bool ParticleSystem::init()
  251. {
  252. return initWithTotalParticles(150);
  253. }
  254. bool ParticleSystem::initWithFile(const std::string& plistFile)
  255. {
  256. bool ret = false;
  257. _plistFile = FileUtils::getInstance()->fullPathForFilename(plistFile);
  258. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(_plistFile);
  259. CCASSERT( !dict.empty(), "Particles: file not found");
  260. // FIXME: compute path from a path, should define a function somewhere to do it
  261. string listFilePath = plistFile;
  262. if (listFilePath.find('/') != string::npos)
  263. {
  264. listFilePath = listFilePath.substr(0, listFilePath.rfind('/') + 1);
  265. ret = this->initWithDictionary(dict, listFilePath);
  266. }
  267. else
  268. {
  269. ret = this->initWithDictionary(dict, "");
  270. }
  271. return ret;
  272. }
  273. bool ParticleSystem::initWithDictionary(ValueMap& dictionary)
  274. {
  275. return initWithDictionary(dictionary, "");
  276. }
  277. bool ParticleSystem::initWithDictionary(ValueMap& dictionary, const std::string& dirname)
  278. {
  279. bool ret = false;
  280. unsigned char *buffer = nullptr;
  281. unsigned char *deflated = nullptr;
  282. Image *image = nullptr;
  283. do
  284. {
  285. int maxParticles = dictionary["maxParticles"].asInt();
  286. // self, not super
  287. if(this->initWithTotalParticles(maxParticles))
  288. {
  289. // Emitter name in particle designer 2.0
  290. _configName = dictionary["configName"].asString();
  291. // angle
  292. _angle = dictionary["angle"].asFloat();
  293. _angleVar = dictionary["angleVariance"].asFloat();
  294. // duration
  295. _duration = dictionary["duration"].asFloat();
  296. // blend function
  297. if (!_configName.empty())
  298. {
  299. _blendFunc.src = utils::toBackendBlendFactor((int)dictionary["blendFuncSource"].asFloat());
  300. }
  301. else
  302. {
  303. _blendFunc.src = utils::toBackendBlendFactor(dictionary["blendFuncSource"].asInt());
  304. }
  305. _blendFunc.dst = utils::toBackendBlendFactor(dictionary["blendFuncDestination"].asInt());
  306. // color
  307. _startColor.r = dictionary["startColorRed"].asFloat();
  308. _startColor.g = dictionary["startColorGreen"].asFloat();
  309. _startColor.b = dictionary["startColorBlue"].asFloat();
  310. _startColor.a = dictionary["startColorAlpha"].asFloat();
  311. _startColorVar.r = dictionary["startColorVarianceRed"].asFloat();
  312. _startColorVar.g = dictionary["startColorVarianceGreen"].asFloat();
  313. _startColorVar.b = dictionary["startColorVarianceBlue"].asFloat();
  314. _startColorVar.a = dictionary["startColorVarianceAlpha"].asFloat();
  315. _endColor.r = dictionary["finishColorRed"].asFloat();
  316. _endColor.g = dictionary["finishColorGreen"].asFloat();
  317. _endColor.b = dictionary["finishColorBlue"].asFloat();
  318. _endColor.a = dictionary["finishColorAlpha"].asFloat();
  319. _endColorVar.r = dictionary["finishColorVarianceRed"].asFloat();
  320. _endColorVar.g = dictionary["finishColorVarianceGreen"].asFloat();
  321. _endColorVar.b = dictionary["finishColorVarianceBlue"].asFloat();
  322. _endColorVar.a = dictionary["finishColorVarianceAlpha"].asFloat();
  323. // particle size
  324. _startSize = dictionary["startParticleSize"].asFloat();
  325. _startSizeVar = dictionary["startParticleSizeVariance"].asFloat();
  326. _endSize = dictionary["finishParticleSize"].asFloat();
  327. _endSizeVar = dictionary["finishParticleSizeVariance"].asFloat();
  328. // position
  329. float x = dictionary["sourcePositionx"].asFloat();
  330. float y = dictionary["sourcePositiony"].asFloat();
  331. if(!_sourcePositionCompatible) {
  332. this->setSourcePosition(Vec2(x, y));
  333. }
  334. else {
  335. this->setPosition(Vec2(x, y));
  336. }
  337. _posVar.x = dictionary["sourcePositionVariancex"].asFloat();
  338. _posVar.y = dictionary["sourcePositionVariancey"].asFloat();
  339. // Spinning
  340. _startSpin = dictionary["rotationStart"].asFloat();
  341. _startSpinVar = dictionary["rotationStartVariance"].asFloat();
  342. _endSpin= dictionary["rotationEnd"].asFloat();
  343. _endSpinVar= dictionary["rotationEndVariance"].asFloat();
  344. _emitterMode = (Mode) dictionary["emitterType"].asInt();
  345. // Mode A: Gravity + tangential accel + radial accel
  346. if (_emitterMode == Mode::GRAVITY)
  347. {
  348. // gravity
  349. modeA.gravity.x = dictionary["gravityx"].asFloat();
  350. modeA.gravity.y = dictionary["gravityy"].asFloat();
  351. // speed
  352. modeA.speed = dictionary["speed"].asFloat();
  353. modeA.speedVar = dictionary["speedVariance"].asFloat();
  354. // radial acceleration
  355. modeA.radialAccel = dictionary["radialAcceleration"].asFloat();
  356. modeA.radialAccelVar = dictionary["radialAccelVariance"].asFloat();
  357. // tangential acceleration
  358. modeA.tangentialAccel = dictionary["tangentialAcceleration"].asFloat();
  359. modeA.tangentialAccelVar = dictionary["tangentialAccelVariance"].asFloat();
  360. // rotation is dir
  361. modeA.rotationIsDir = dictionary["rotationIsDir"].asBool();
  362. }
  363. // or Mode B: radius movement
  364. else if (_emitterMode == Mode::RADIUS)
  365. {
  366. if (!_configName.empty())
  367. {
  368. modeB.startRadius = dictionary["maxRadius"].asInt();
  369. }
  370. else
  371. {
  372. modeB.startRadius = dictionary["maxRadius"].asFloat();
  373. }
  374. modeB.startRadiusVar = dictionary["maxRadiusVariance"].asFloat();
  375. if (!_configName.empty())
  376. {
  377. modeB.endRadius = dictionary["minRadius"].asInt();
  378. }
  379. else
  380. {
  381. modeB.endRadius = dictionary["minRadius"].asFloat();
  382. }
  383. if (dictionary.find("minRadiusVariance") != dictionary.end())
  384. {
  385. modeB.endRadiusVar = dictionary["minRadiusVariance"].asFloat();
  386. }
  387. else
  388. {
  389. modeB.endRadiusVar = 0.0f;
  390. }
  391. if (!_configName.empty())
  392. {
  393. modeB.rotatePerSecond = dictionary["rotatePerSecond"].asInt();
  394. }
  395. else
  396. {
  397. modeB.rotatePerSecond = dictionary["rotatePerSecond"].asFloat();
  398. }
  399. modeB.rotatePerSecondVar = dictionary["rotatePerSecondVariance"].asFloat();
  400. } else {
  401. CCASSERT( false, "Invalid emitterType in config file");
  402. CC_BREAK_IF(true);
  403. }
  404. // life span
  405. _life = dictionary["particleLifespan"].asFloat();
  406. _lifeVar = dictionary["particleLifespanVariance"].asFloat();
  407. // emission Rate
  408. _emissionRate = _totalParticles / _life;
  409. //don't get the internal texture if a batchNode is used
  410. if (!_batchNode)
  411. {
  412. // Set a compatible default for the alpha transfer
  413. _opacityModifyRGB = false;
  414. // texture
  415. // Try to get the texture from the cache
  416. std::string textureName = dictionary["textureFileName"].asString();
  417. size_t rPos = textureName.rfind('/');
  418. if (rPos != string::npos)
  419. {
  420. string textureDir = textureName.substr(0, rPos + 1);
  421. if (!dirname.empty() && textureDir != dirname)
  422. {
  423. textureName = textureName.substr(rPos+1);
  424. textureName = dirname + textureName;
  425. }
  426. }
  427. else if (!dirname.empty() && !textureName.empty())
  428. {
  429. textureName = dirname + textureName;
  430. }
  431. Texture2D *tex = nullptr;
  432. if (!textureName.empty())
  433. {
  434. // set not pop-up message box when load image failed
  435. bool notify = FileUtils::getInstance()->isPopupNotify();
  436. FileUtils::getInstance()->setPopupNotify(false);
  437. tex = Director::getInstance()->getTextureCache()->addImage(textureName);
  438. // reset the value of UIImage notify
  439. FileUtils::getInstance()->setPopupNotify(notify);
  440. }
  441. if (tex)
  442. {
  443. setTexture(tex);
  444. }
  445. else if( dictionary.find("textureImageData") != dictionary.end() )
  446. {
  447. std::string textureData = dictionary.at("textureImageData").asString();
  448. CCASSERT(!textureData.empty(), "textureData can't be empty!");
  449. auto dataLen = textureData.size();
  450. if (dataLen != 0)
  451. {
  452. // if it fails, try to get it from the base64-gzipped data
  453. int decodeLen = base64Decode((unsigned char*)textureData.c_str(), (unsigned int)dataLen, &buffer);
  454. CCASSERT( buffer != nullptr, "CCParticleSystem: error decoding textureImageData");
  455. CC_BREAK_IF(!buffer);
  456. ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
  457. CCASSERT( deflated != nullptr, "CCParticleSystem: error ungzipping textureImageData");
  458. CC_BREAK_IF(!deflated);
  459. // For android, we should retain it in VolatileTexture::addImage which invoked in Director::getInstance()->getTextureCache()->addUIImage()
  460. image = new (std::nothrow) Image();
  461. bool isOK = image->initWithImageData(deflated, deflatedLen);
  462. CCASSERT(isOK, "CCParticleSystem: error init image with Data");
  463. CC_BREAK_IF(!isOK);
  464. setTexture(Director::getInstance()->getTextureCache()->addImage(image, _plistFile + textureName));
  465. image->release();
  466. }
  467. }
  468. _yCoordFlipped = dictionary.find("yCoordFlipped") == dictionary.end() ? 1 : dictionary.at("yCoordFlipped").asInt();
  469. if( !this->_texture)
  470. CCLOGWARN("cocos2d: Warning: ParticleSystemQuad system without a texture");
  471. }
  472. ret = true;
  473. }
  474. } while (0);
  475. free(buffer);
  476. free(deflated);
  477. return ret;
  478. }
  479. bool ParticleSystem::initWithTotalParticles(int numberOfParticles)
  480. {
  481. _totalParticles = numberOfParticles;
  482. _particleData.release();
  483. if( !_particleData.init(_totalParticles) )
  484. {
  485. CCLOG("Particle system: not enough memory");
  486. this->release();
  487. return false;
  488. }
  489. _allocatedParticles = numberOfParticles;
  490. if (_batchNode)
  491. {
  492. for (int i = 0; i < _totalParticles; i++)
  493. {
  494. _particleData.atlasIndex[i] = i;
  495. }
  496. }
  497. // default, active
  498. _isActive = true;
  499. // default blend function
  500. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  501. // default movement type;
  502. _positionType = PositionType::FREE;
  503. // by default be in mode A:
  504. _emitterMode = Mode::GRAVITY;
  505. // default: modulate
  506. // FIXME:: not used
  507. // colorModulate = YES;
  508. _isAutoRemoveOnFinish = false;
  509. // Optimization: compile updateParticle method
  510. //updateParticleSel = @selector(updateQuadWithParticle:newPosition:);
  511. //updateParticleImp = (CC_UPDATE_PARTICLE_IMP) [self methodForSelector:updateParticleSel];
  512. //for batchNode
  513. _transformSystemDirty = false;
  514. return true;
  515. }
  516. ParticleSystem::~ParticleSystem()
  517. {
  518. // Since the scheduler retains the "target (in this case the ParticleSystem)
  519. // it is not needed to call "unscheduleUpdate" here. In fact, it will be called in "cleanup"
  520. //unscheduleUpdate();
  521. _particleData.release();
  522. CC_SAFE_RELEASE(_texture);
  523. }
  524. void ParticleSystem::addParticles(int count)
  525. {
  526. if (_paused)
  527. return;
  528. uint32_t RANDSEED = rand();
  529. int start = _particleCount;
  530. _particleCount += count;
  531. //life
  532. for (int i = start; i < _particleCount ; ++i)
  533. {
  534. float theLife = _life + _lifeVar * RANDOM_M11(&RANDSEED);
  535. _particleData.timeToLive[i] = MAX(0, theLife);
  536. }
  537. //position
  538. for (int i = start; i < _particleCount; ++i)
  539. {
  540. _particleData.posx[i] = _sourcePosition.x + _posVar.x * RANDOM_M11(&RANDSEED);
  541. }
  542. for (int i = start; i < _particleCount; ++i)
  543. {
  544. _particleData.posy[i] = _sourcePosition.y + _posVar.y * RANDOM_M11(&RANDSEED);
  545. }
  546. //color
  547. #define SET_COLOR(c, b, v)\
  548. for (int i = start; i < _particleCount; ++i)\
  549. {\
  550. c[i] = clampf( b + v * RANDOM_M11(&RANDSEED) , 0 , 1 );\
  551. }
  552. SET_COLOR(_particleData.colorR, _startColor.r, _startColorVar.r);
  553. SET_COLOR(_particleData.colorG, _startColor.g, _startColorVar.g);
  554. SET_COLOR(_particleData.colorB, _startColor.b, _startColorVar.b);
  555. SET_COLOR(_particleData.colorA, _startColor.a, _startColorVar.a);
  556. SET_COLOR(_particleData.deltaColorR, _endColor.r, _endColorVar.r);
  557. SET_COLOR(_particleData.deltaColorG, _endColor.g, _endColorVar.g);
  558. SET_COLOR(_particleData.deltaColorB, _endColor.b, _endColorVar.b);
  559. SET_COLOR(_particleData.deltaColorA, _endColor.a, _endColorVar.a);
  560. #define SET_DELTA_COLOR(c, dc)\
  561. for (int i = start; i < _particleCount; ++i)\
  562. {\
  563. dc[i] = (dc[i] - c[i]) / _particleData.timeToLive[i];\
  564. }
  565. SET_DELTA_COLOR(_particleData.colorR, _particleData.deltaColorR);
  566. SET_DELTA_COLOR(_particleData.colorG, _particleData.deltaColorG);
  567. SET_DELTA_COLOR(_particleData.colorB, _particleData.deltaColorB);
  568. SET_DELTA_COLOR(_particleData.colorA, _particleData.deltaColorA);
  569. //size
  570. for (int i = start; i < _particleCount; ++i)
  571. {
  572. _particleData.size[i] = _startSize + _startSizeVar * RANDOM_M11(&RANDSEED);
  573. _particleData.size[i] = MAX(0, _particleData.size[i]);
  574. }
  575. if (_endSize != START_SIZE_EQUAL_TO_END_SIZE)
  576. {
  577. for (int i = start; i < _particleCount; ++i)
  578. {
  579. float endSize = _endSize + _endSizeVar * RANDOM_M11(&RANDSEED);
  580. endSize = MAX(0, endSize);
  581. _particleData.deltaSize[i] = (endSize - _particleData.size[i]) / _particleData.timeToLive[i];
  582. }
  583. }
  584. else
  585. {
  586. for (int i = start; i < _particleCount; ++i)
  587. {
  588. _particleData.deltaSize[i] = 0.0f;
  589. }
  590. }
  591. // rotation
  592. for (int i = start; i < _particleCount; ++i)
  593. {
  594. _particleData.rotation[i] = _startSpin + _startSpinVar * RANDOM_M11(&RANDSEED);
  595. }
  596. for (int i = start; i < _particleCount; ++i)
  597. {
  598. float endA = _endSpin + _endSpinVar * RANDOM_M11(&RANDSEED);
  599. _particleData.deltaRotation[i] = (endA - _particleData.rotation[i]) / _particleData.timeToLive[i];
  600. }
  601. // position
  602. Vec2 pos;
  603. if (_positionType == PositionType::FREE)
  604. {
  605. pos = this->convertToWorldSpace(Vec2::ZERO);
  606. }
  607. else if (_positionType == PositionType::RELATIVE)
  608. {
  609. pos = _position;
  610. }
  611. for (int i = start; i < _particleCount; ++i)
  612. {
  613. _particleData.startPosX[i] = pos.x;
  614. }
  615. for (int i = start; i < _particleCount; ++i)
  616. {
  617. _particleData.startPosY[i] = pos.y;
  618. }
  619. // Mode Gravity: A
  620. if (_emitterMode == Mode::GRAVITY)
  621. {
  622. // radial accel
  623. for (int i = start; i < _particleCount; ++i)
  624. {
  625. _particleData.modeA.radialAccel[i] = modeA.radialAccel + modeA.radialAccelVar * RANDOM_M11(&RANDSEED);
  626. }
  627. // tangential accel
  628. for (int i = start; i < _particleCount; ++i)
  629. {
  630. _particleData.modeA.tangentialAccel[i] = modeA.tangentialAccel + modeA.tangentialAccelVar * RANDOM_M11(&RANDSEED);
  631. }
  632. // rotation is dir
  633. if( modeA.rotationIsDir )
  634. {
  635. for (int i = start; i < _particleCount; ++i)
  636. {
  637. float a = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED) );
  638. Vec2 v(cosf( a ), sinf( a ));
  639. float s = modeA.speed + modeA.speedVar * RANDOM_M11(&RANDSEED);
  640. Vec2 dir = v * s;
  641. _particleData.modeA.dirX[i] = dir.x;//v * s ;
  642. _particleData.modeA.dirY[i] = dir.y;
  643. _particleData.rotation[i] = -CC_RADIANS_TO_DEGREES(dir.getAngle());
  644. }
  645. }
  646. else
  647. {
  648. for (int i = start; i < _particleCount; ++i)
  649. {
  650. float a = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED) );
  651. Vec2 v(cosf( a ), sinf( a ));
  652. float s = modeA.speed + modeA.speedVar * RANDOM_M11(&RANDSEED);
  653. Vec2 dir = v * s;
  654. _particleData.modeA.dirX[i] = dir.x;//v * s ;
  655. _particleData.modeA.dirY[i] = dir.y;
  656. }
  657. }
  658. }
  659. // Mode Radius: B
  660. else
  661. {
  662. //Need to check by Jacky
  663. // Set the default diameter of the particle from the source position
  664. for (int i = start; i < _particleCount; ++i)
  665. {
  666. _particleData.modeB.radius[i] = modeB.startRadius + modeB.startRadiusVar * RANDOM_M11(&RANDSEED);
  667. }
  668. for (int i = start; i < _particleCount; ++i)
  669. {
  670. _particleData.modeB.angle[i] = CC_DEGREES_TO_RADIANS( _angle + _angleVar * RANDOM_M11(&RANDSEED));
  671. }
  672. for (int i = start; i < _particleCount; ++i)
  673. {
  674. _particleData.modeB.degreesPerSecond[i] = CC_DEGREES_TO_RADIANS(modeB.rotatePerSecond + modeB.rotatePerSecondVar * RANDOM_M11(&RANDSEED));
  675. }
  676. if(modeB.endRadius == START_RADIUS_EQUAL_TO_END_RADIUS)
  677. {
  678. for (int i = start; i < _particleCount; ++i)
  679. {
  680. _particleData.modeB.deltaRadius[i] = 0.0f;
  681. }
  682. }
  683. else
  684. {
  685. for (int i = start; i < _particleCount; ++i)
  686. {
  687. float endRadius = modeB.endRadius + modeB.endRadiusVar * RANDOM_M11(&RANDSEED);
  688. _particleData.modeB.deltaRadius[i] = (endRadius - _particleData.modeB.radius[i]) / _particleData.timeToLive[i];
  689. }
  690. }
  691. }
  692. }
  693. void ParticleSystem::onEnter()
  694. {
  695. #if CC_ENABLE_SCRIPT_BINDING
  696. if (_scriptType == kScriptTypeJavascript)
  697. {
  698. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnEnter))
  699. return;
  700. }
  701. #endif
  702. Node::onEnter();
  703. // update after action in run!
  704. this->scheduleUpdateWithPriority(1);
  705. __allInstances.pushBack(this);
  706. }
  707. void ParticleSystem::onExit()
  708. {
  709. #if CC_ENABLE_SCRIPT_BINDING
  710. if (_scriptType == kScriptTypeJavascript)
  711. {
  712. if (ScriptEngineManager::sendNodeEventToJSExtended(this, kNodeOnExit))
  713. return;
  714. }
  715. #endif
  716. this->unscheduleUpdate();
  717. Node::onExit();
  718. auto iter = std::find(std::begin(__allInstances), std::end(__allInstances), this);
  719. if (iter != std::end(__allInstances))
  720. {
  721. __allInstances.erase(iter);
  722. }
  723. }
  724. void ParticleSystem::stopSystem()
  725. {
  726. _isActive = false;
  727. _elapsed = _duration;
  728. _emitCounter = 0;
  729. }
  730. void ParticleSystem::resetSystem()
  731. {
  732. _isActive = true;
  733. _elapsed = 0;
  734. for (int i = 0; i < _particleCount; ++i)
  735. {
  736. _particleData.timeToLive[i] = 0.0f;
  737. }
  738. }
  739. bool ParticleSystem::isFull()
  740. {
  741. return (_particleCount == _totalParticles);
  742. }
  743. // ParticleSystem - MainLoop
  744. void ParticleSystem::update(float dt)
  745. {
  746. CC_PROFILER_START_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
  747. if (_isActive && _emissionRate)
  748. {
  749. float rate = 1.0f / _emissionRate;
  750. int totalParticles = static_cast<int>(_totalParticles * __totalParticleCountFactor);
  751. //issue #1201, prevent bursts of particles, due to too high emitCounter
  752. if (_particleCount < totalParticles)
  753. {
  754. _emitCounter += dt;
  755. if (_emitCounter < 0.f)
  756. _emitCounter = 0.f;
  757. }
  758. int emitCount = MIN(totalParticles - _particleCount, _emitCounter / rate);
  759. addParticles(emitCount);
  760. _emitCounter -= rate * emitCount;
  761. _elapsed += dt;
  762. if (_elapsed < 0.f)
  763. _elapsed = 0.f;
  764. if (_duration != DURATION_INFINITY && _duration < _elapsed)
  765. {
  766. this->stopSystem();
  767. }
  768. }
  769. {
  770. for (int i = 0; i < _particleCount; ++i)
  771. {
  772. _particleData.timeToLive[i] -= dt;
  773. }
  774. for (int i = 0; i < _particleCount; ++i)
  775. {
  776. if (_particleData.timeToLive[i] <= 0.0f)
  777. {
  778. int j = _particleCount - 1;
  779. while (j > 0 && _particleData.timeToLive[j] <= 0)
  780. {
  781. _particleCount--;
  782. j--;
  783. }
  784. _particleData.copyParticle(i, _particleCount - 1);
  785. if (_batchNode)
  786. {
  787. //disable the switched particle
  788. int currentIndex = _particleData.atlasIndex[i];
  789. _batchNode->disableParticle(_atlasIndex + currentIndex);
  790. //switch indexes
  791. _particleData.atlasIndex[_particleCount - 1] = currentIndex;
  792. }
  793. --_particleCount;
  794. if( _particleCount == 0 && _isAutoRemoveOnFinish )
  795. {
  796. this->unscheduleUpdate();
  797. _parent->removeChild(this, true);
  798. return;
  799. }
  800. }
  801. }
  802. if (_emitterMode == Mode::GRAVITY)
  803. {
  804. for (int i = 0 ; i < _particleCount; ++i)
  805. {
  806. particle_point tmp, radial = {0.0f, 0.0f}, tangential;
  807. // radial acceleration
  808. if (_particleData.posx[i] || _particleData.posy[i])
  809. {
  810. normalize_point(_particleData.posx[i], _particleData.posy[i], &radial);
  811. }
  812. tangential = radial;
  813. radial.x *= _particleData.modeA.radialAccel[i];
  814. radial.y *= _particleData.modeA.radialAccel[i];
  815. // tangential acceleration
  816. std::swap(tangential.x, tangential.y);
  817. tangential.x *= - _particleData.modeA.tangentialAccel[i];
  818. tangential.y *= _particleData.modeA.tangentialAccel[i];
  819. // (gravity + radial + tangential) * dt
  820. tmp.x = radial.x + tangential.x + modeA.gravity.x;
  821. tmp.y = radial.y + tangential.y + modeA.gravity.y;
  822. tmp.x *= dt;
  823. tmp.y *= dt;
  824. _particleData.modeA.dirX[i] += tmp.x;
  825. _particleData.modeA.dirY[i] += tmp.y;
  826. // this is cocos2d-x v3.0
  827. // if (_configName.length()>0 && _yCoordFlipped != -1)
  828. // this is cocos2d-x v3.0
  829. tmp.x = _particleData.modeA.dirX[i] * dt * _yCoordFlipped;
  830. tmp.y = _particleData.modeA.dirY[i] * dt * _yCoordFlipped;
  831. _particleData.posx[i] += tmp.x;
  832. _particleData.posy[i] += tmp.y;
  833. }
  834. }
  835. else
  836. {
  837. //Why use so many for-loop separately instead of putting them together?
  838. //When the processor needs to read from or write to a location in memory,
  839. //it first checks whether a copy of that data is in the cache.
  840. //And every property's memory of the particle system is continuous,
  841. //for the purpose of improving cache hit rate, we should process only one property in one for-loop AFAP.
  842. //It was proved to be effective especially for low-end machine.
  843. for (int i = 0; i < _particleCount; ++i)
  844. {
  845. _particleData.modeB.angle[i] += _particleData.modeB.degreesPerSecond[i] * dt;
  846. }
  847. for (int i = 0; i < _particleCount; ++i)
  848. {
  849. _particleData.modeB.radius[i] += _particleData.modeB.deltaRadius[i] * dt;
  850. }
  851. for (int i = 0; i < _particleCount; ++i)
  852. {
  853. _particleData.posx[i] = - cosf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i];
  854. }
  855. for (int i = 0; i < _particleCount; ++i)
  856. {
  857. _particleData.posy[i] = - sinf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i] * _yCoordFlipped;
  858. }
  859. }
  860. //color r,g,b,a
  861. for (int i = 0 ; i < _particleCount; ++i)
  862. {
  863. _particleData.colorR[i] += _particleData.deltaColorR[i] * dt;
  864. }
  865. for (int i = 0 ; i < _particleCount; ++i)
  866. {
  867. _particleData.colorG[i] += _particleData.deltaColorG[i] * dt;
  868. }
  869. for (int i = 0 ; i < _particleCount; ++i)
  870. {
  871. _particleData.colorB[i] += _particleData.deltaColorB[i] * dt;
  872. }
  873. for (int i = 0 ; i < _particleCount; ++i)
  874. {
  875. _particleData.colorA[i] += _particleData.deltaColorA[i] * dt;
  876. }
  877. //size
  878. for (int i = 0 ; i < _particleCount; ++i)
  879. {
  880. _particleData.size[i] += (_particleData.deltaSize[i] * dt);
  881. _particleData.size[i] = MAX(0, _particleData.size[i]);
  882. }
  883. //angle
  884. for (int i = 0 ; i < _particleCount; ++i)
  885. {
  886. _particleData.rotation[i] += _particleData.deltaRotation[i] * dt;
  887. }
  888. updateParticleQuads();
  889. _transformSystemDirty = false;
  890. }
  891. // only update gl buffer when visible
  892. if (_visible && ! _batchNode)
  893. {
  894. postStep();
  895. }
  896. CC_PROFILER_STOP_CATEGORY(kProfilerCategoryParticles , "CCParticleSystem - update");
  897. }
  898. void ParticleSystem::updateWithNoTime()
  899. {
  900. this->update(0.0f);
  901. }
  902. void ParticleSystem::updateParticleQuads()
  903. {
  904. //should be overridden
  905. }
  906. void ParticleSystem::postStep()
  907. {
  908. // should be overridden
  909. }
  910. // ParticleSystem - Texture protocol
  911. void ParticleSystem::setTexture(Texture2D* var)
  912. {
  913. if (_texture != var)
  914. {
  915. CC_SAFE_RETAIN(var);
  916. CC_SAFE_RELEASE(_texture);
  917. _texture = var;
  918. updateBlendFunc();
  919. }
  920. }
  921. void ParticleSystem::updateBlendFunc()
  922. {
  923. CCASSERT(! _batchNode, "Can't change blending functions when the particle is being batched");
  924. if(_texture)
  925. {
  926. bool premultiplied = _texture->hasPremultipliedAlpha();
  927. _opacityModifyRGB = false;
  928. if( _texture && ( _blendFunc.src == CC_BLEND_SRC && _blendFunc.dst == CC_BLEND_DST ) )
  929. {
  930. if( premultiplied )
  931. {
  932. _opacityModifyRGB = true;
  933. }
  934. else
  935. {
  936. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  937. }
  938. }
  939. }
  940. }
  941. Texture2D * ParticleSystem::getTexture() const
  942. {
  943. return _texture;
  944. }
  945. // ParticleSystem - Additive Blending
  946. void ParticleSystem::setBlendAdditive(bool additive)
  947. {
  948. if( additive )
  949. {
  950. _blendFunc = BlendFunc::ADDITIVE;
  951. }
  952. else
  953. {
  954. if( _texture && ! _texture->hasPremultipliedAlpha() )
  955. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  956. else
  957. _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
  958. }
  959. }
  960. bool ParticleSystem::isBlendAdditive() const
  961. {
  962. return( _blendFunc.src == backend::BlendFactor::SRC_ALPHA && _blendFunc.dst == backend::BlendFactor::ONE);
  963. }
  964. // ParticleSystem - Properties of Gravity Mode
  965. void ParticleSystem::setTangentialAccel(float t)
  966. {
  967. CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  968. modeA.tangentialAccel = t;
  969. }
  970. float ParticleSystem::getTangentialAccel() const
  971. {
  972. CCASSERT( _emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  973. return modeA.tangentialAccel;
  974. }
  975. void ParticleSystem::setTangentialAccelVar(float t)
  976. {
  977. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  978. modeA.tangentialAccelVar = t;
  979. }
  980. float ParticleSystem::getTangentialAccelVar() const
  981. {
  982. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  983. return modeA.tangentialAccelVar;
  984. }
  985. void ParticleSystem::setRadialAccel(float t)
  986. {
  987. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  988. modeA.radialAccel = t;
  989. }
  990. float ParticleSystem::getRadialAccel() const
  991. {
  992. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  993. return modeA.radialAccel;
  994. }
  995. void ParticleSystem::setRadialAccelVar(float t)
  996. {
  997. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  998. modeA.radialAccelVar = t;
  999. }
  1000. float ParticleSystem::getRadialAccelVar() const
  1001. {
  1002. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1003. return modeA.radialAccelVar;
  1004. }
  1005. void ParticleSystem::setRotationIsDir(bool t)
  1006. {
  1007. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1008. modeA.rotationIsDir = t;
  1009. }
  1010. bool ParticleSystem::getRotationIsDir() const
  1011. {
  1012. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1013. return modeA.rotationIsDir;
  1014. }
  1015. void ParticleSystem::setGravity(const Vec2& g)
  1016. {
  1017. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1018. modeA.gravity = g;
  1019. }
  1020. const Vec2& ParticleSystem::getGravity()
  1021. {
  1022. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1023. return modeA.gravity;
  1024. }
  1025. void ParticleSystem::setSpeed(float speed)
  1026. {
  1027. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1028. modeA.speed = speed;
  1029. }
  1030. float ParticleSystem::getSpeed() const
  1031. {
  1032. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1033. return modeA.speed;
  1034. }
  1035. void ParticleSystem::setSpeedVar(float speedVar)
  1036. {
  1037. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1038. modeA.speedVar = speedVar;
  1039. }
  1040. float ParticleSystem::getSpeedVar() const
  1041. {
  1042. CCASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
  1043. return modeA.speedVar;
  1044. }
  1045. // ParticleSystem - Properties of Radius Mode
  1046. void ParticleSystem::setStartRadius(float startRadius)
  1047. {
  1048. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1049. modeB.startRadius = startRadius;
  1050. }
  1051. float ParticleSystem::getStartRadius() const
  1052. {
  1053. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1054. return modeB.startRadius;
  1055. }
  1056. void ParticleSystem::setStartRadiusVar(float startRadiusVar)
  1057. {
  1058. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1059. modeB.startRadiusVar = startRadiusVar;
  1060. }
  1061. float ParticleSystem::getStartRadiusVar() const
  1062. {
  1063. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1064. return modeB.startRadiusVar;
  1065. }
  1066. void ParticleSystem::setEndRadius(float endRadius)
  1067. {
  1068. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1069. modeB.endRadius = endRadius;
  1070. }
  1071. float ParticleSystem::getEndRadius() const
  1072. {
  1073. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1074. return modeB.endRadius;
  1075. }
  1076. void ParticleSystem::setEndRadiusVar(float endRadiusVar)
  1077. {
  1078. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1079. modeB.endRadiusVar = endRadiusVar;
  1080. }
  1081. float ParticleSystem::getEndRadiusVar() const
  1082. {
  1083. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1084. return modeB.endRadiusVar;
  1085. }
  1086. void ParticleSystem::setRotatePerSecond(float degrees)
  1087. {
  1088. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1089. modeB.rotatePerSecond = degrees;
  1090. }
  1091. float ParticleSystem::getRotatePerSecond() const
  1092. {
  1093. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1094. return modeB.rotatePerSecond;
  1095. }
  1096. void ParticleSystem::setRotatePerSecondVar(float degrees)
  1097. {
  1098. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1099. modeB.rotatePerSecondVar = degrees;
  1100. }
  1101. float ParticleSystem::getRotatePerSecondVar() const
  1102. {
  1103. CCASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
  1104. return modeB.rotatePerSecondVar;
  1105. }
  1106. bool ParticleSystem::isActive() const
  1107. {
  1108. return _isActive;
  1109. }
  1110. int ParticleSystem::getTotalParticles() const
  1111. {
  1112. return _totalParticles;
  1113. }
  1114. void ParticleSystem::setTotalParticles(int var)
  1115. {
  1116. CCASSERT( var <= _allocatedParticles, "Particle: resizing particle array only supported for quads");
  1117. _totalParticles = var;
  1118. }
  1119. const BlendFunc& ParticleSystem::getBlendFunc() const
  1120. {
  1121. return _blendFunc;
  1122. }
  1123. void ParticleSystem::setBlendFunc(const BlendFunc &blendFunc)
  1124. {
  1125. if( _blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst ) {
  1126. _blendFunc = blendFunc;
  1127. this->updateBlendFunc();
  1128. }
  1129. }
  1130. bool ParticleSystem::isAutoRemoveOnFinish() const
  1131. {
  1132. return _isAutoRemoveOnFinish;
  1133. }
  1134. void ParticleSystem::setAutoRemoveOnFinish(bool var)
  1135. {
  1136. _isAutoRemoveOnFinish = var;
  1137. }
  1138. // ParticleSystem - methods for batchNode rendering
  1139. ParticleBatchNode* ParticleSystem::getBatchNode() const
  1140. {
  1141. return _batchNode;
  1142. }
  1143. void ParticleSystem::setBatchNode(ParticleBatchNode* batchNode)
  1144. {
  1145. if( _batchNode != batchNode ) {
  1146. _batchNode = batchNode; // weak reference
  1147. if( batchNode ) {
  1148. //each particle needs a unique index
  1149. for (int i = 0; i < _totalParticles; i++)
  1150. {
  1151. _particleData.atlasIndex[i] = i;
  1152. }
  1153. }
  1154. }
  1155. }
  1156. //don't use a transform matrix, this is faster
  1157. void ParticleSystem::setScale(float s)
  1158. {
  1159. _transformSystemDirty = true;
  1160. Node::setScale(s);
  1161. }
  1162. void ParticleSystem::setRotation(float newRotation)
  1163. {
  1164. _transformSystemDirty = true;
  1165. Node::setRotation(newRotation);
  1166. }
  1167. void ParticleSystem::setScaleX(float newScaleX)
  1168. {
  1169. _transformSystemDirty = true;
  1170. Node::setScaleX(newScaleX);
  1171. }
  1172. void ParticleSystem::setScaleY(float newScaleY)
  1173. {
  1174. _transformSystemDirty = true;
  1175. Node::setScaleY(newScaleY);
  1176. }
  1177. void ParticleSystem::start()
  1178. {
  1179. resetSystem();
  1180. }
  1181. void ParticleSystem::stop()
  1182. {
  1183. stopSystem();
  1184. }
  1185. bool ParticleSystem::isPaused() const
  1186. {
  1187. return _paused;
  1188. }
  1189. void ParticleSystem::pauseEmissions()
  1190. {
  1191. _paused = true;
  1192. }
  1193. void ParticleSystem::resumeEmissions()
  1194. {
  1195. _paused = false;
  1196. }
  1197. NS_CC_END