CCParticleSystem.cpp 43 KB

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