PuzzleSDK
ParticleTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
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
25#include "ParticleTest.h"
26#include "../testResource.h"
27#include "editor-support/cocostudio/CocosStudioExtension.h"
28
30
31enum {
33};
34
35//------------------------------------------------------------------
36//
37// DemoFirework
38//
39//------------------------------------------------------------------
41{
43
44 _emitter = ParticleFireworks::create();
45 _emitter->retain();
46 _background->addChild(_emitter, 10);
47
48 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
49
51}
52
53std::string DemoFirework::subtitle() const
54{
55 return "ParticleFireworks";
56}
57
58
59//------------------------------------------------------------------
60//
61// DemoFire
62//
63//------------------------------------------------------------------
65{
67
68 _emitter = ParticleFire::create();
69 _emitter->retain();
70 _background->addChild(_emitter, 10);
71
72 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );//.pvr");
73 auto p = _emitter->getPosition();
74 _emitter->setPosition( Vec2(p.x, 100.0f) );
75
77}
78
79std::string DemoFire::subtitle() const
80{
81 return "ParticleFire";
82}
83
84//------------------------------------------------------------------
85//
86// DemoSun
87//
88//------------------------------------------------------------------
90{
92
93 _emitter = ParticleSun::create();
94 _emitter->retain();
95 _background->addChild(_emitter, 10);
96
97 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
98
100}
101
102std::string DemoSun::subtitle() const
103{
104 return "ParticleSun";
105}
106
107//------------------------------------------------------------------
108//
109// DemoPause
110//
111//------------------------------------------------------------------
113{
115
116 _emitter = ParticleSmoke::create();
117 _emitter->retain();
118 _background->addChild(_emitter, 10);
119
120 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
121
123 schedule(CC_SCHEDULE_SELECTOR(DemoPause::pauseEmitter), 2.0f);
124
125
126}
128{
129 if (_emitter->isPaused())
130 {
131 _emitter->resumeEmissions();
132 }
133 else
134 {
135 _emitter->pauseEmissions();
136 }
137}
138
139std::string DemoPause::subtitle() const
140{
141 return "Pause Particle";
142}
143
144//------------------------------------------------------------------
145//
146// DemoGalaxy
147//
148//------------------------------------------------------------------
150{
152
153 _emitter = ParticleGalaxy::create();
154 _emitter->retain();
155 _background->addChild(_emitter, 10);
156
157 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
158
160}
161
162std::string DemoGalaxy::subtitle() const
163{
164 return "ParticleGalaxy";
165}
166
167//------------------------------------------------------------------
168//
169// DemoFlower
170//
171//------------------------------------------------------------------
173{
175
176 _emitter = ParticleFlower::create();
177 _emitter->retain();
178 _background->addChild(_emitter, 10);
179 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
180
182}
183
184std::string DemoFlower::subtitle() const
185{
186 return "ParticleFlower";
187}
188
189//------------------------------------------------------------------
190//
191// DemoBigFlower
192//
193//------------------------------------------------------------------
195{
197
198 _emitter = ParticleSystemQuad::createWithTotalParticles(50);
199 _emitter->retain();
200
201 _background->addChild(_emitter, 10);
203 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
204
205 _emitter->setDuration(-1);
206
207 // gravity
208 _emitter->setGravity(Vec2::ZERO);
209
210 // angle
211 _emitter->setAngle(90);
212 _emitter->setAngleVar(360);
213
214 // speed of particles
215 _emitter->setSpeed(160);
216 _emitter->setSpeedVar(20);
217
218 // radial
219 _emitter->setRadialAccel(-120);
220 _emitter->setRadialAccelVar(0);
221
222 // tangential
223 _emitter->setTangentialAccel(30);
224 _emitter->setTangentialAccelVar(0);
225
226 // emitter position
227 _emitter->setPosition( Vec2(160,240) );
228 _emitter->setPosVar(Vec2::ZERO);
229
230 // life of particles
231 _emitter->setLife(4);
232 _emitter->setLifeVar(1);
233
234 // spin of particles
235 _emitter->setStartSpin(0);
236 _emitter->setStartSizeVar(0);
237 _emitter->setEndSpin(0);
238 _emitter->setEndSpinVar(0);
239
240 // color of particles
241 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
242 _emitter->setStartColor(startColor);
243
244 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
245 _emitter->setStartColorVar(startColorVar);
246
247 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
248 _emitter->setEndColor(endColor);
249
250 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
251 _emitter->setEndColorVar(endColorVar);
252
253 // size, in pixels
254 _emitter->setStartSize(80.0f);
255 _emitter->setStartSizeVar(40.0f);
256 _emitter->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
257
258 // emits per second
259 _emitter->setEmissionRate(_emitter->getTotalParticles()/_emitter->getLife());
260
261 // additive
262 _emitter->setBlendAdditive(true);
263
265}
266
267std::string DemoBigFlower::subtitle() const
268{
269 return "ParticleBigFlower";
270}
271
272//------------------------------------------------------------------
273//
274// DemoRotFlower
275//
276//------------------------------------------------------------------
278{
280
281 _emitter = ParticleSystemQuad::createWithTotalParticles(300);
282 _emitter->retain();
283
284 _background->addChild(_emitter, 10);
286 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars2) );
287
288 // duration
289 _emitter->setDuration(-1);
290
291 // gravity
292 _emitter->setGravity(Vec2::ZERO);
293
294 // angle
295 _emitter->setAngle(90);
296 _emitter->setAngleVar(360);
297
298 // speed of particles
299 _emitter->setSpeed(160);
300 _emitter->setSpeedVar(20);
301
302 // radial
303 _emitter->setRadialAccel(-120);
304 _emitter->setRadialAccelVar(0);
305
306 // tangential
307 _emitter->setTangentialAccel(30);
308 _emitter->setTangentialAccelVar(0);
309
310 // emitter position
311 _emitter->setPosition( Vec2(160,240) );
312 _emitter->setPosVar(Vec2::ZERO);
313
314 // life of particles
315 _emitter->setLife(3);
316 _emitter->setLifeVar(1);
317
318 // spin of particles
319 _emitter->setStartSpin(0);
320 _emitter->setStartSpinVar(0);
321 _emitter->setEndSpin(0);
322 _emitter->setEndSpinVar(2000);
323
324 // color of particles
325 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
326 _emitter->setStartColor(startColor);
327
328 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
329 _emitter->setStartColorVar(startColorVar);
330
331 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
332 _emitter->setEndColor(endColor);
333
334 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
335 _emitter->setEndColorVar(endColorVar);
336
337 // size, in pixels
338 _emitter->setStartSize(30.0f);
339 _emitter->setStartSizeVar(00.0f);
340 _emitter->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
341
342 // emits per second
343 _emitter->setEmissionRate(_emitter->getTotalParticles()/_emitter->getLife());
344
345 // additive
346 _emitter->setBlendAdditive(false);
347
349}
350
351std::string DemoRotFlower::subtitle() const
352{
353 return "ParticleRotFlower";
354}
355
356//------------------------------------------------------------------
357//
358// DemoMeteor
359//
360//------------------------------------------------------------------
362{
364
365 _emitter = ParticleMeteor::create();
366 _emitter->retain();
367 _background->addChild(_emitter, 10);
368
369 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
370
372}
373
374std::string DemoMeteor::subtitle() const
375{
376 return "ParticleMeteor";
377}
378
379//------------------------------------------------------------------
380//
381// DemoSpiral
382//
383//------------------------------------------------------------------
385{
387
388 _emitter = ParticleSpiral::create();
389 _emitter->retain();
390 _background->addChild(_emitter, 10);
391
392 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
393
395}
396
397std::string DemoSpiral::subtitle() const
398{
399 return "ParticleSpiral";
400}
401
402//------------------------------------------------------------------
403//
404// DemoExplosion
405//
406//------------------------------------------------------------------
408{
410
411 _emitter = ParticleExplosion::create();
412 _emitter->retain();
413 _background->addChild(_emitter, 10);
414
415 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
416
417 _emitter->setAutoRemoveOnFinish(true);
418
420}
421
422std::string DemoExplosion::subtitle() const
423{
424 return "ParticleExplosion";
425}
426
427//------------------------------------------------------------------
428//
429// DemoSmoke
430//
431//------------------------------------------------------------------
433{
435
436 _emitter = ParticleSmoke::create();
437 _emitter->retain();
438 _background->addChild(_emitter, 10);
439 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
440
441 auto p = _emitter->getPosition();
442 _emitter->setPosition( Vec2( p.x, 100.0f) );
443
445}
446
447std::string DemoSmoke::subtitle() const
448{
449 return "ParticleSmoke";
450}
451
452//------------------------------------------------------------------
453//
454// DemoSnow
455//
456//------------------------------------------------------------------
458{
460
461 _emitter = ParticleSnow::create();
462 _emitter->retain();
463 _background->addChild(_emitter, 10);
464
465 auto p = _emitter->getPosition();
466 _emitter->setPosition( Vec2( p.x, p.y-110) );
467 _emitter->setLife(3);
468 _emitter->setLifeVar(1);
469
470 // gravity
471 _emitter->setGravity(Vec2(0.0f,-10.0f));
472
473 // speed of particles
474 _emitter->setSpeed(130);
475 _emitter->setSpeedVar(30);
476
477
478 Color4F startColor = _emitter->getStartColor();
479 startColor.r = 0.9f;
480 startColor.g = 0.9f;
481 startColor.b = 0.9f;
482 _emitter->setStartColor(startColor);
483
484 Color4F startColorVar = _emitter->getStartColorVar();
485 startColorVar.b = 0.1f;
486 _emitter->setStartColorVar(startColorVar);
487
488 _emitter->setEmissionRate(_emitter->getTotalParticles()/_emitter->getLife());
489
490 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_snow) );
491
493}
494
495std::string DemoSnow::subtitle() const
496{
497 return "ParticleSnow";
498}
499
500//------------------------------------------------------------------
501//
502// DemoRain
503//
504//------------------------------------------------------------------
506{
508
509 _emitter = ParticleRain::create();
510 _emitter->retain();
511 _background->addChild(_emitter, 10);
512
513 auto p = _emitter->getPosition();
514 _emitter->setPosition( Vec2( p.x, p.y-100) );
515 _emitter->setLife(4);
516
517 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
518
520}
521
522std::string DemoRain::subtitle() const
523{
524 return "ParticleRain";
525}
526
527//------------------------------------------------------------------
528//
529// DemoModernArt
530//
531//------------------------------------------------------------------
533{
535
536//FIXME: If use ParticleSystemPoint, bada 1.0 device will crash.
537// Crash place: ParticleSystemPoint.cpp Line 149, function: glDrawArrays(GL_POINTS, 0, _particleIdx);
538 _emitter = ParticleSystemQuad::createWithTotalParticles(1000);
539 _emitter->retain();
540
541 _background->addChild(_emitter, 10);
543
544 auto s = Director::getInstance()->getWinSize();
545
546 // duration
547 _emitter->setDuration(-1);
548
549 // gravity
550 _emitter->setGravity(Vec2(0.0f,0.0f));
551
552 // angle
553 _emitter->setAngle(0);
554 _emitter->setAngleVar(360);
555
556 // radial
557 _emitter->setRadialAccel(70);
558 _emitter->setRadialAccelVar(10);
559
560 // tangential
561 _emitter->setTangentialAccel(80);
562 _emitter->setTangentialAccelVar(0);
563
564 // speed of particles
565 _emitter->setSpeed(50);
566 _emitter->setSpeedVar(10);
567
568 // emitter position
569 _emitter->setPosition( Vec2( s.width/2, s.height/2) );
570 _emitter->setPosVar(Vec2::ZERO);
571
572 // life of particles
573 _emitter->setLife(2.0f);
574 _emitter->setLifeVar(0.3f);
575
576 // emits per frame
577 _emitter->setEmissionRate(_emitter->getTotalParticles()/_emitter->getLife());
578
579 // color of particles
580 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
581 _emitter->setStartColor(startColor);
582
583 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
584 _emitter->setStartColorVar(startColorVar);
585
586 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
587 _emitter->setEndColor(endColor);
588
589 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
590 _emitter->setEndColorVar(endColorVar);
591
592 // size, in pixels
593 _emitter->setStartSize(1.0f);
594 _emitter->setStartSizeVar(1.0f);
595 _emitter->setEndSize(32.0f);
596 _emitter->setEndSizeVar(8.0f);
597
598 // texture
599 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
600
601 // additive
602 _emitter->setBlendAdditive(false);
603
605}
606
607std::string DemoModernArt::subtitle() const
608{
609 return "Varying size";
610}
611
612//------------------------------------------------------------------
613//
614// DemoRing
615//
616//------------------------------------------------------------------
618{
620
621 _emitter = ParticleFlower::create();
622 _emitter->retain();
623
624 _background->addChild(_emitter, 10);
625
626 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
627 _emitter->setLifeVar(0);
628 _emitter->setLife(10);
629 _emitter->setSpeed(100);
630 _emitter->setSpeedVar(0);
631 _emitter->setEmissionRate(10000);
632
634}
635
636std::string DemoRing::subtitle() const
637{
638 return "Ring Demo";
639}
640
641//------------------------------------------------------------------
642//
643// ParallaxParticle
644//
645//------------------------------------------------------------------
647{
649
650 _background->getParent()->removeChild(_background, true);
651 _background = nullptr;
652
653 auto p = ParallaxNode::create();
654 addChild(p, 5);
655
656 auto p1 = Sprite::create(s_back3);
657 auto p2 = Sprite::create(s_back3);
658
659 p->addChild( p1, 1, Vec2(0.5f,1.0f), Vec2(0.0f,250.0f) );
660 p->addChild(p2, 2, Vec2(1.5f,1.0f), Vec2(0.0f,50.0f) );
661
662 _emitter = ParticleFlower::create();
663 _emitter->retain();
664 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
665
666 p1->addChild(_emitter, 10);
667 _emitter->setPosition( Vec2(250,200) );
668
669 auto par = ParticleSun::create();
670 p2->addChild(par, 10);
671 par->setTexture( Director::getInstance()->getTextureCache()->addImage(s_fire) );
672
673 auto move = MoveBy::create(4, Vec2(300.0f,0.0f));
674 auto move_back = move->reverse();
675 auto seq = Sequence::create( move, move_back, nullptr);
676 p->runAction(RepeatForever::create(seq));
677}
678
679std::string ParallaxParticle::subtitle() const
680{
681 return "Parallax + Particles";
682}
683
684//------------------------------------------------------------------
685//
686// RadiusMode1
687//
688//------------------------------------------------------------------
690{
692
693 _color->setColor(Color3B::BLACK);
694 removeChild(_background, true);
695 _background = nullptr;
696
697 _emitter = ParticleSystemQuad::createWithTotalParticles(200);
698 _emitter->retain();
699 addChild(_emitter, 10);
700 _emitter->setTexture(Director::getInstance()->getTextureCache()->addImage("Images/stars-grayscale.png"));
701
702 // duration
703 _emitter->setDuration(ParticleSystem::DURATION_INFINITY);
704
705 // radius mode
706 _emitter->setEmitterMode(ParticleSystem::Mode::RADIUS);
707
708 // radius mode: start and end radius in pixels
709 _emitter->setStartRadius(0);
710 _emitter->setStartRadiusVar(0);
711 _emitter->setEndRadius(160);
712 _emitter->setEndRadiusVar(0);
713
714 // radius mode: degrees per second
715 _emitter->setRotatePerSecond(180);
716 _emitter->setRotatePerSecondVar(0);
717
718
719 // angle
720 _emitter->setAngle(90);
721 _emitter->setAngleVar(0);
722
723 // emitter position
724 auto size = Director::getInstance()->getWinSize();
725 _emitter->setPosition(Vec2(size.width/2, size.height/2));
726 _emitter->setPosVar(Vec2::ZERO);
727
728 // life of particles
729 _emitter->setLife(5);
730 _emitter->setLifeVar(0);
731
732 // spin of particles
733 _emitter->setStartSpin(0);
734 _emitter->setStartSpinVar(0);
735 _emitter->setEndSpin(0);
736 _emitter->setEndSpinVar(0);
737
738 // color of particles
739 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
740 _emitter->setStartColor(startColor);
741
742 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
743 _emitter->setStartColorVar(startColorVar);
744
745 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
746 _emitter->setEndColor(endColor);
747
748 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
749 _emitter->setEndColorVar(endColorVar);
750
751 // size, in pixels
752 _emitter->setStartSize(32);
753 _emitter->setStartSizeVar(0);
754 _emitter->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
755
756 // emits per second
757 _emitter->setEmissionRate(_emitter->getTotalParticles() / _emitter->getLife());
758
759 // additive
760 _emitter->setBlendAdditive(false);
761}
762
763std::string RadiusMode1::subtitle() const
764{
765 return "Radius Mode: Spiral";
766}
767
768//------------------------------------------------------------------
769//
770// RadiusMode2
771//
772//------------------------------------------------------------------
774{
776
777 _color->setColor(Color3B::BLACK);
778 removeChild(_background, true);
779 _background = nullptr;
780
781 _emitter = ParticleSystemQuad::createWithTotalParticles(200);
782 _emitter->retain();
783 addChild(_emitter, 10);
784 _emitter->setTexture(Director::getInstance()->getTextureCache()->addImage("Images/stars-grayscale.png"));
785
786 // duration
787 _emitter->setDuration(ParticleSystem::DURATION_INFINITY);
788
789 // radius mode
790 _emitter->setEmitterMode(ParticleSystem::Mode::RADIUS);
791
792 // radius mode: start and end radius in pixels
793 _emitter->setStartRadius(100);
794 _emitter->setStartRadiusVar(0);
795 _emitter->setEndRadius(ParticleSystem::START_RADIUS_EQUAL_TO_END_RADIUS);
796 _emitter->setEndRadiusVar(0);
797
798 // radius mode: degrees per second
799 _emitter->setRotatePerSecond(45);
800 _emitter->setRotatePerSecondVar(0);
801
802
803 // angle
804 _emitter->setAngle(90);
805 _emitter->setAngleVar(0);
806
807 // emitter position
808 auto size = Director::getInstance()->getWinSize();
809 _emitter->setPosition(Vec2(size.width/2, size.height/2));
810 _emitter->setPosVar(Vec2::ZERO);
811
812 // life of particles
813 _emitter->setLife(4);
814 _emitter->setLifeVar(0);
815
816 // spin of particles
817 _emitter->setStartSpin(0);
818 _emitter->setStartSpinVar(0);
819 _emitter->setEndSpin(0);
820 _emitter->setEndSpinVar(0);
821
822 // color of particles
823 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
824 _emitter->setStartColor(startColor);
825
826 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
827 _emitter->setStartColorVar(startColorVar);
828
829 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
830 _emitter->setEndColor(endColor);
831
832 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
833 _emitter->setEndColorVar(endColorVar);
834
835 // size, in pixels
836 _emitter->setStartSize(32);
837 _emitter->setStartSizeVar(0);
838 _emitter->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
839
840 // emits per second
841 _emitter->setEmissionRate(_emitter->getTotalParticles() / _emitter->getLife());
842
843 // additive
844 _emitter->setBlendAdditive(false);
845}
846
847std::string RadiusMode2::subtitle() const
848{
849 return "Radius Mode: Semi Circle";
850}
851
852//------------------------------------------------------------------
853//
854// Issue704
855//
856//------------------------------------------------------------------
858{
860
861 _color->setColor(Color3B::BLACK);
862 removeChild(_background, true);
863 _background = nullptr;
864
865 _emitter = ParticleSystemQuad::createWithTotalParticles(100);
866 _emitter->retain();
867 addChild(_emitter, 10);
868 _emitter->setTexture(Director::getInstance()->getTextureCache()->addImage("Images/fire.png"));
869
870 // duration
871 _emitter->setDuration(ParticleSystem::DURATION_INFINITY);
872
873 // radius mode
874 _emitter->setEmitterMode(ParticleSystem::Mode::RADIUS);
875
876 // radius mode: start and end radius in pixels
877 _emitter->setStartRadius(50);
878 _emitter->setStartRadiusVar(0);
879 _emitter->setEndRadius(ParticleSystem::START_RADIUS_EQUAL_TO_END_RADIUS);
880 _emitter->setEndRadiusVar(0);
881
882 // radius mode: degrees per second
883 _emitter->setRotatePerSecond(0);
884 _emitter->setRotatePerSecondVar(0);
885
886
887 // angle
888 _emitter->setAngle(90);
889 _emitter->setAngleVar(0);
890
891 // emitter position
892 auto size = Director::getInstance()->getWinSize();
893 _emitter->setPosition(Vec2(size.width/2, size.height/2));
894 _emitter->setPosVar(Vec2::ZERO);
895
896 // life of particles
897 _emitter->setLife(5);
898 _emitter->setLifeVar(0);
899
900 // spin of particles
901 _emitter->setStartSpin(0);
902 _emitter->setStartSpinVar(0);
903 _emitter->setEndSpin(0);
904 _emitter->setEndSpinVar(0);
905
906 // color of particles
907 Color4F startColor(0.5f, 0.5f, 0.5f, 1.0f);
908 _emitter->setStartColor(startColor);
909
910 Color4F startColorVar(0.5f, 0.5f, 0.5f, 1.0f);
911 _emitter->setStartColorVar(startColorVar);
912
913 Color4F endColor(0.1f, 0.1f, 0.1f, 0.2f);
914 _emitter->setEndColor(endColor);
915
916 Color4F endColorVar(0.1f, 0.1f, 0.1f, 0.2f);
917 _emitter->setEndColorVar(endColorVar);
918
919 // size, in pixels
920 _emitter->setStartSize(16);
921 _emitter->setStartSizeVar(0);
922 _emitter->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
923
924 // emits per second
925 _emitter->setEmissionRate(_emitter->getTotalParticles() / _emitter->getLife());
926
927 // additive
928 _emitter->setBlendAdditive(false);
929
930 auto rot = RotateBy::create(16, 360);
931 _emitter->runAction(RepeatForever::create(rot));
932}
933
934std::string Issue704::title() const
935{
936 return "Issue 704. Free + Rot";
937}
938
939std::string Issue704::subtitle() const
940{
941 return "Emitted particles should not rotate";
942}
943
944//------------------------------------------------------------------
945//
946// Issue870
947//
948//------------------------------------------------------------------
950{
952
953 _color->setColor(Color3B::BLACK);
954 removeChild(_background, true);
955 _background = nullptr;
956
957 _emitter = ParticleSystemQuad::create("Particles/SpinningPeas.plist");
958 _emitter->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("Images/particles.png"), Rect(0,0,32,32));
959 addChild(_emitter, 10);
960 _emitter->retain();
961
962 _index = 0;
963 schedule(CC_SCHEDULE_SELECTOR(Issue870::updateQuads), 2.0f);
964}
965
967{
968 _index = (_index + 1) % 4;
969 auto rect = Rect(_index * 32, 0, 32, 32);
970 auto system = (ParticleSystemQuad*)_emitter;
971 system->setTextureWithRect(_emitter->getTexture(), rect);
972}
973
974std::string Issue870::title() const
975{
976 return "Issue 870. SubRect";
977}
978
979std::string Issue870::subtitle() const
980{
981 return "Every 2 seconds the particle should change";
982}
983
984//------------------------------------------------------------------
985//
986// DemoParticleFromFile
987//
988//------------------------------------------------------------------
990{
992
993 _color->setColor(Color3B::BLACK);
994 removeChild(_background, true);
995 _background = nullptr;
996
997 std::string filename = "Particles/" + _title + ".plist";
998 _emitter = ParticleSystemQuad::create(filename);
999 _emitter->retain();
1000 addChild(_emitter, 10);
1001
1003}
1004
1005//------------------------------------------------------------------
1006//
1007// ParticleDemo
1008//
1009//------------------------------------------------------------------
1010
1011enum
1012{
1018
1019ParticleTests::ParticleTests()
1020{
1041 addTestCase("BoilingFoam", [](){return DemoParticleFromFile::create("BoilingFoam");});
1042 addTestCase("BurstPipe", [](){return DemoParticleFromFile::create("BurstPipe"); });
1043 addTestCase("Comet", [](){return DemoParticleFromFile::create("Comet"); });
1044 addTestCase("debian", [](){return DemoParticleFromFile::create("debian"); });
1045 addTestCase("ExplodingRing", [](){return DemoParticleFromFile::create("ExplodingRing"); });
1046 addTestCase("LavaFlow", [](){return DemoParticleFromFile::create("LavaFlow"); });
1047 addTestCase("SpinningPeas", [](){return DemoParticleFromFile::create("SpinningPeas"); });
1048 addTestCase("SpookyPeas", [](){return DemoParticleFromFile::create("SpookyPeas"); });
1049 addTestCase("Upsidedown", [](){return DemoParticleFromFile::create("Upsidedown"); });
1050 addTestCase("Flower", [](){return DemoParticleFromFile::create("Flower"); });
1051 addTestCase("Spiral", [](){return DemoParticleFromFile::create("Spiral"); });
1052 addTestCase("Galaxy", [](){return DemoParticleFromFile::create("Galaxy"); });
1053 addTestCase("Phoenix", [](){return DemoParticleFromFile::create("Phoenix"); });
1054 addTestCase("lines", [](){return DemoParticleFromFile::create("lines"); });
1055 addTestCase("ButterFly", [](){return DemoParticleFromFile::create("ButterFly"); });
1056 addTestCase("ButterFlyYFlipped", [](){return DemoParticleFromFile::create("ButterFlyYFlipped"); });
1062
1073
1076}
1077
1079{
1080 CC_SAFE_RELEASE(_emitter);
1081}
1082
1084{
1086
1087 MenuItemFont::setFontSize(32);
1088
1089 _color = LayerColor::create( Color4B(127,127,127,255) );
1090 this->addChild(_color);
1091
1092 _emitter = nullptr;
1093
1094 auto listener = EventListenerTouchAllAtOnce::create();
1095 listener->onTouchesBegan = CC_CALLBACK_2(ParticleDemo::onTouchesBegan, this);
1096 listener->onTouchesMoved = CC_CALLBACK_2(ParticleDemo::onTouchesMoved, this);
1097 listener->onTouchesEnded = CC_CALLBACK_2(ParticleDemo::onTouchesEnded, this);
1098 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
1099
1100 auto s = Director::getInstance()->getWinSize();
1101
1102 auto item4 = MenuItemToggle::createWithCallback( CC_CALLBACK_1(ParticleDemo::toggleCallback, this),
1103 MenuItemFont::create( "Free Movement" ),
1104 MenuItemFont::create( "Relative Movement" ),
1105 MenuItemFont::create( "Grouped Movement" ),
1106 nullptr );
1107
1108 auto menu = Menu::create(item4, nullptr);
1109
1110 menu->setPosition( Vec2::ZERO );
1111 item4->setPosition( Vec2( VisibleRect::left().x, VisibleRect::bottom().y+ 100) );
1112 item4->setAnchorPoint( Vec2(0,0) );
1113
1114 addChild( menu, 100 );
1115
1116 auto labelAtlas = LabelAtlas::create("0000", "fps_images.png", 12, 32, '.');
1117 addChild(labelAtlas, 100, kTagParticleCount);
1118 labelAtlas->setPosition(Vec2(s.width-66,50.0f));
1119
1120 // moving background
1121 _background = Sprite::create(s_back3);
1122 addChild(_background, 5);
1123 _background->setPosition( Vec2(s.width/2, s.height-180) );
1124
1125 auto move = MoveBy::create(4, Vec2(300,0) );
1126 auto move_back = move->reverse();
1127 auto seq = Sequence::create( move, move_back, nullptr);
1128 _background->runAction( RepeatForever::create(seq) );
1129
1130
1131 scheduleUpdate();
1132}
1133
1134std::string ParticleDemo::title() const
1135{
1136 return "Particle Demo";
1137}
1138
1139std::string ParticleDemo::subtitle() const
1140{
1141 return "No title";
1142}
1143
1144void ParticleDemo::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
1145{
1146 onTouchesEnded(touches, event);
1147}
1148
1149void ParticleDemo::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
1150{
1151 return onTouchesEnded(touches, event);
1152}
1153
1154void ParticleDemo::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
1155{
1156 auto touch = touches[0];
1157
1158 auto location = touch->getLocation();
1159
1160 auto pos = Vec2::ZERO;
1161 if (_background)
1162 {
1163 pos = _background->convertToWorldSpace(Vec2::ZERO);
1164 }
1165
1166 if (_emitter != nullptr)
1167 {
1168 _emitter->setPosition(location -pos);
1169 }
1170}
1171
1173{
1174 if (_emitter)
1175 {
1176 auto atlas = (LabelAtlas*)getChildByTag(kTagParticleCount);
1177 char str[5] = {0};
1178 sprintf(str, "%04d", _emitter->getParticleCount());
1179 atlas->setString(str);
1180 }
1181}
1182
1184{
1185 if (_emitter != nullptr)
1186 {
1187 if (_emitter->getPositionType() == ParticleSystem::PositionType::GROUPED)
1188 _emitter->setPositionType(ParticleSystem::PositionType::FREE);
1189 else if (_emitter->getPositionType() == ParticleSystem::PositionType::FREE)
1190 _emitter->setPositionType(ParticleSystem::PositionType::RELATIVE);
1191 else if (_emitter->getPositionType() == ParticleSystem::PositionType::RELATIVE)
1192 _emitter->setPositionType(ParticleSystem::PositionType::GROUPED );
1193 }
1194}
1195
1197{
1198 auto s = Director::getInstance()->getWinSize();
1199 if (_emitter != nullptr)
1200 {
1201 _emitter->setPosition( Vec2(s.width / 2, s.height / 2) );
1202 }
1203}
1204
1205// ParticleBatchHybrid
1206
1208{
1210
1211 _color->setColor(Color3B::BLACK);
1212 removeChild(_background, true);
1213 _background = nullptr;
1214
1215 _emitter = ParticleSystemQuad::create("Particles/LavaFlow.plist");
1216 _emitter->retain();
1217 auto batch = ParticleBatchNode::createWithTexture(_emitter->getTexture());
1218
1219 batch->addChild(_emitter);
1220
1221 addChild(batch, 10);
1222
1223 schedule(CC_SCHEDULE_SELECTOR(ParticleBatchHybrid::switchRender), 2.0f);
1224
1225 auto node = Node::create();
1226 addChild(node);
1227
1228 _parent1 = batch;
1229 _parent2 = node;
1230}
1231
1233{
1234 bool usingBatch = ( _emitter->getBatchNode() != nullptr );
1235 _emitter->removeFromParentAndCleanup(false);
1236
1237 auto newParent = (usingBatch ? _parent2 : _parent1 );
1238 newParent->addChild(_emitter);
1239
1240 log("Particle: Using new parent: %s", usingBatch ? "CCNode" : "CCParticleBatchNode");
1241}
1242
1244{
1245 return "Particle Batch";
1246}
1247
1249{
1250 return "Hybrid: batched and non batched every 2 seconds";
1251}
1252
1253// ParticleBatchMultipleEmitters
1254
1256{
1258
1259 _color->setColor(Color3B::BLACK);
1260 removeChild(_background, true);
1261 _background = nullptr;
1262
1263 auto emitter1 = ParticleSystemQuad::create("Particles/LavaFlow.plist");
1264 emitter1->setStartColor(Color4F(1,0,0,1));
1265 auto emitter2 = ParticleSystemQuad::create("Particles/LavaFlow.plist");
1266 emitter2->setStartColor(Color4F(0,1,0,1));
1267 auto emitter3 = ParticleSystemQuad::create("Particles/LavaFlow.plist");
1268 emitter3->setStartColor(Color4F(0,0,1,1));
1269
1270 auto s = Director::getInstance()->getWinSize();
1271
1272 emitter1->setPosition(Vec2( s.width/1.25f, s.height/1.25f));
1273 emitter2->setPosition(Vec2( s.width/2, s.height/2));
1274 emitter3->setPosition(Vec2( s.width/4, s.height/4));
1275
1276 auto batch = ParticleBatchNode::createWithTexture(emitter1->getTexture());
1277
1278 batch->addChild(emitter1, 0);
1279 batch->addChild(emitter2, 0);
1280 batch->addChild(emitter3, 0);
1281
1282 addChild(batch, 10);
1283}
1284
1286{
1287 return "Particle Batch";
1288}
1289
1291{
1292 return "Multiple emitters. One Batch";
1293}
1294
1295// ParticleReorder
1296
1298{
1300
1301 _order = 0;
1302 _color->setColor(Color3B::BLACK);
1303 removeChild(_background, true);
1304 _background = nullptr;
1305
1306 auto ignore = ParticleSystemQuad::create("Particles/SmallSun.plist");
1307 auto parent1 = Node::create();
1308 auto parent2 = ParticleBatchNode::createWithTexture(ignore->getTexture());
1309 ignore->unscheduleUpdate();
1310
1311 for( unsigned int i=0; i<2;i++)
1312 {
1313 auto parent = ( i==0 ? parent1 : parent2 );
1314
1315 auto emitter1 = ParticleSystemQuad::create("Particles/SmallSun.plist");
1316 emitter1->setStartColor(Color4F(1,0,0,1));
1317 emitter1->setBlendAdditive(false);
1318 auto emitter2 = ParticleSystemQuad::create("Particles/SmallSun.plist");
1319 emitter2->setStartColor(Color4F(0,1,0,1));
1320 emitter2->setBlendAdditive(false);
1321 auto emitter3 = ParticleSystemQuad::create("Particles/SmallSun.plist");
1322 emitter3->setStartColor(Color4F(0,0,1,1));
1323 emitter3->setBlendAdditive(false);
1324
1325 auto s = Director::getInstance()->getWinSize();
1326
1327 int neg = (i==0 ? 1 : -1 );
1328
1329 emitter1->setPosition(Vec2( s.width/2-30, s.height/2+60*neg));
1330 emitter2->setPosition(Vec2( s.width/2, s.height/2+60*neg));
1331 emitter3->setPosition(Vec2( s.width/2+30, s.height/2+60*neg));
1332
1333 parent->addChild(emitter1, 0, 1);
1334 parent->addChild(emitter2, 0, 2);
1335 parent->addChild(emitter3, 0, 3);
1336
1337 addChild(parent, 10, 1000+i);
1338 }
1339
1340 schedule(CC_SCHEDULE_SELECTOR(ParticleReorder::reorderParticles), 1.0f);
1341}
1342
1343std::string ParticleReorder::title() const
1344{
1345 return "Reordering particles";
1346}
1347
1348std::string ParticleReorder::subtitle() const
1349{
1350 return "Reordering particles with and without batches batches";
1351}
1352
1354{
1355 for( int i=0; i<2;i++) {
1356 auto parent = getChildByTag(1000+i);
1357
1358 auto child1 = parent->getChildByTag(1);
1359 auto child2 = parent->getChildByTag(2);
1360 auto child3 = parent->getChildByTag(3);
1361
1362 if( _order % 3 == 0 ) {
1363 parent->reorderChild(child1, 1);
1364 parent->reorderChild(child2, 2);
1365 parent->reorderChild(child3, 3);
1366
1367 } else if (_order % 3 == 1 ) {
1368 parent->reorderChild(child1, 3);
1369 parent->reorderChild(child2, 1);
1370 parent->reorderChild(child3, 2);
1371
1372 } else if (_order % 3 == 2 ) {
1373 parent->reorderChild(child1, 2);
1374 parent->reorderChild(child2, 3);
1375 parent->reorderChild(child3, 1);
1376 }
1377 }
1378
1379 _order++;
1380}
1381
1382class RainbowEffect : public ParticleSystemQuad
1383{
1384public:
1385 bool init()override;
1386 virtual bool initWithTotalParticles(int numberOfParticles) override;
1387 virtual void update(float dt) override;
1388};
1389
1391{
1392 return initWithTotalParticles(150);
1393}
1394
1395bool RainbowEffect::initWithTotalParticles(int numberOfParticles)
1396{
1397 if( ParticleSystemQuad::initWithTotalParticles(numberOfParticles) )
1398 {
1399 // additive
1400 setBlendAdditive(false);
1401
1402 // duration
1403 setDuration(ParticleSystem::DURATION_INFINITY);
1404
1405 // Gravity Mode
1406 setEmitterMode(ParticleSystem::Mode::GRAVITY);
1407
1408 // Gravity Mode: gravity
1409 setGravity(Vec2(0.0f,0.0f));
1410
1411 // Gravity mode: radial acceleration
1412 setRadialAccel(0);
1413 setRadialAccelVar(0);
1414
1415 // Gravity mode: speed of particles
1416 setSpeed(120);
1417 setSpeedVar(0);
1418
1419
1420 // angle
1421 setAngle(180);
1422 setAngleVar(0);
1423
1424 // emitter position
1425 auto winSize = Director::getInstance()->getWinSize();
1426 setPosition(Vec2(winSize.width/2, winSize.height/2));
1427 setPosVar(Vec2::ZERO);
1428
1429 // life of particles
1430 setLife(0.5f);
1431 setLifeVar(0);
1432
1433 // size, in pixels
1434 setStartSize(25.0f);
1435 setStartSizeVar(0);
1436 setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
1437
1438 // emits per seconds
1439 setEmissionRate(getTotalParticles()/getLife());
1440
1441 // color of particles
1442 setStartColor(Color4F(Color4B(50, 50, 50, 50)));
1443 setEndColor(Color4F(Color4B(0, 0, 0, 0)));
1444
1445 _startColorVar.r = 0.0f;
1446 _startColorVar.g = 0.0f;
1447 _startColorVar.b = 0.0f;
1448 _startColorVar.a = 0.0f;
1449 _endColorVar.r = 0.0f;
1450 _endColorVar.g = 0.0f;
1451 _endColorVar.b = 0.0f;
1452 _endColorVar.a = 0.0f;
1453
1454 setTexture(Director::getInstance()->getTextureCache()->addImage("Images/particles.png"));
1455 return true;
1456 }
1457
1458 return false;
1459}
1460
1462{
1463 _emitCounter = 0;
1464 ParticleSystemQuad::update(dt);
1465}
1466
1467
1469{
1471
1472 _color->setColor(Color3B::BLACK);
1473 removeChild(_background, true);
1474 _background = nullptr;
1475
1476 RainbowEffect *particle = new (std::nothrow) RainbowEffect();
1477 particle->initWithTotalParticles(50);
1478
1479 addChild(particle);
1480
1481 auto s = Director::getInstance()->getWinSize();
1482
1483 particle->setPosition(Vec2(s.width/2, s.height/2));
1484
1485 _emitter = particle;
1486}
1487
1488std::string Issue1201::title() const
1489{
1490 return "Issue 1201. Unfinished";
1491}
1492
1493std::string Issue1201::subtitle() const
1494{
1495 return "Unfinished test. Ignore it";
1496}
1497
1499{
1501
1502 _color->setColor(Color3B::BLACK);
1503 removeChild(_background, true);
1504 _background = nullptr;
1505
1506 Director::getInstance()->getTextureCache()->addImage("Images/particles.png");
1507
1508 for (int i = 0; i<5; i++) {
1509 auto particleSystem = ParticleSystemQuad::create("Particles/SpinningPeas.plist");
1510
1511 particleSystem->setPosition(Vec2(i*50.0f,i*50.0f));
1512
1513 particleSystem->setPositionType(ParticleSystem::PositionType::GROUPED);
1514 addChild(particleSystem);
1515 }
1516
1517 _emitter = nullptr;
1518
1519}
1520
1522{
1523 return "Multiple particle systems";
1524}
1525
1527{
1528 return "v1.1 test: FPS should be lower than next test";
1529}
1530
1532{
1533 auto atlas = (LabelAtlas*) getChildByTag(kTagParticleCount);
1534
1535 unsigned int count = 0;
1536
1537 for(const auto &child : _children) {
1538 auto item = dynamic_cast<ParticleSystem*>(child);
1539 if (item != nullptr)
1540 {
1541 count += item->getParticleCount();
1542 }
1543 }
1544
1545 char str[100] = {0};
1546 sprintf(str, "%4d", count);
1547 atlas->setString(str);
1548}
1549
1550// MultipleParticleSystemsBatched
1551
1553{
1555
1556 _color->setColor(Color3B::BLACK);
1557 removeChild(_background, true);
1558 _background = nullptr;
1559
1560 ParticleBatchNode *batchNode = ParticleBatchNode::createWithTexture(nullptr, 3000);
1561
1562 addChild(batchNode, 1, 2);
1563
1564 for (int i = 0; i<5; i++) {
1565
1566 auto particleSystem = ParticleSystemQuad::create("Particles/SpinningPeas.plist");
1567
1568 particleSystem->setPositionType(ParticleSystem::PositionType::GROUPED);
1569 particleSystem->setPosition(Vec2(i*50.0f,i*50.0f));
1570
1571 batchNode->setTexture(particleSystem->getTexture());
1572 batchNode->addChild(particleSystem);
1573 }
1574
1575 _emitter = nullptr;
1576}
1577
1579{
1580 auto atlas = (LabelAtlas*) getChildByTag(kTagParticleCount);
1581
1582 int count = 0;
1583
1584 auto batchNode = getChildByTag(2);
1585 for(const auto &child : batchNode->getChildren()) {
1586 auto item = dynamic_cast<ParticleSystem*>(child);
1587 if (item != nullptr)
1588 {
1589 count += item->getParticleCount();
1590 }
1591 }
1592
1593 char str[50] = {0};
1594 sprintf(str, "%4d", count);
1595 atlas->setString(str);
1596}
1597
1599{
1600 return "Multiple particle systems batched";
1601}
1602
1604{
1605 return "v1.1 test: should perform better than previous test";
1606}
1607
1608// AddAndDeleteParticleSystems
1609
1611{
1613
1614 _color->setColor(Color3B::BLACK);
1615 removeChild(_background, true);
1616 _background = nullptr;
1617
1618 //adds the texture inside the plist to the texture cache
1619 _batchNode = ParticleBatchNode::createWithTexture((Texture2D*)nullptr, 16000);
1620
1621 addChild(_batchNode, 1, 2);
1622
1623 for (int i = 0; i<6; i++) {
1624
1625 auto particleSystem = ParticleSystemQuad::create("Particles/Spiral.plist");
1626 _batchNode->setTexture(particleSystem->getTexture());
1627
1628 particleSystem->setPositionType(ParticleSystem::PositionType::GROUPED);
1629 particleSystem->setTotalParticles(200);
1630
1631 particleSystem->setPosition(Vec2(i*15 +100.0f,i*15+100.0f));
1632
1633 unsigned int randZ = rand() % 100;
1634 _batchNode->addChild(particleSystem, randZ, -1);
1635
1636 }
1637
1638 schedule(CC_SCHEDULE_SELECTOR(AddAndDeleteParticleSystems::removeSystem), 0.5f);
1639 _emitter = nullptr;
1640
1641}
1642
1644{
1645 ssize_t nChildrenCount = _batchNode->getChildren().size();
1646 if (nChildrenCount > 0)
1647 {
1648 CCLOG("remove random system");
1649 unsigned int uRand = rand() % (nChildrenCount - 1);
1650 _batchNode->removeChild(_batchNode->getChildren().at(uRand), true);
1651
1652 auto particleSystem = ParticleSystemQuad::create("Particles/Spiral.plist");
1653 //add new
1654
1655 particleSystem->setPositionType(ParticleSystem::PositionType::GROUPED);
1656 particleSystem->setTotalParticles(200);
1657
1658 particleSystem->setPosition(Vec2(rand() % 300 ,rand() % 400));
1659
1660 CCLOG("add a new system");
1661 unsigned int randZ = rand() % 100;
1662 _batchNode->addChild(particleSystem, randZ, -1);
1663 }
1664}
1665
1667{
1668 auto atlas = (LabelAtlas*) getChildByTag(kTagParticleCount);
1669
1670 int count = 0;
1671
1672 auto batchNode = getChildByTag(2);
1673 for(const auto &child : batchNode->getChildren()) {
1674 auto item = dynamic_cast<ParticleSystem*>(child);
1675 if (item != nullptr)
1676 {
1677 count += item->getParticleCount();
1678 }
1679 }
1680
1681 char str[100] = {0};
1682 sprintf(str, "%4d", count);
1683 atlas->setString(str);
1684}
1685
1687{
1688 return "Add and remove Particle System";
1689}
1690
1692{
1693 return "v1.1 test: every 2 sec 1 system disappear, 1 appears";
1694}
1695
1696// ReorderParticleSystems
1697
1699{
1701
1702 _color->setColor(Color3B::BLACK);
1703 removeChild(_background ,true);
1704 _background = nullptr;
1705
1706 _batchNode = ParticleBatchNode::create("Images/stars-grayscale.png" ,3000);
1707
1708 addChild(_batchNode, 1, 2);
1709
1710
1711 for (int i = 0; i<3; i++) {
1712
1713 auto particleSystem = ParticleSystemQuad::createWithTotalParticles(200);
1714 particleSystem->retain();
1715 particleSystem->setTexture(_batchNode->getTexture());
1716
1717 // duration
1718 particleSystem->setDuration(ParticleSystem::DURATION_INFINITY);
1719
1720 // radius mode
1721 particleSystem->setEmitterMode(ParticleSystem::Mode::RADIUS);
1722
1723 // radius mode: 100 pixels from center
1724 particleSystem->setStartRadius(100);
1725 particleSystem->setStartRadiusVar(0);
1726 particleSystem->setEndRadius(ParticleSystem::START_RADIUS_EQUAL_TO_END_RADIUS);
1727 particleSystem->setEndRadiusVar(0); // not used when start == end
1728
1729 // radius mode: degrees per second
1730 // 45 * 4 seconds of life = 180 degrees
1731 particleSystem->setRotatePerSecond(45);
1732 particleSystem->setRotatePerSecondVar(0);
1733
1734
1735 // angle
1736 particleSystem->setAngle(90);
1737 particleSystem->setAngleVar(0);
1738
1739 // emitter position
1740 particleSystem->setPosVar(Vec2::ZERO);
1741
1742 // life of particles
1743 particleSystem->setLife(4);
1744 particleSystem->setLifeVar(0);
1745
1746 // spin of particles
1747 particleSystem->setStartSpin(0);
1748 particleSystem->setStartSpinVar(0);
1749 particleSystem->setEndSpin(0);
1750 particleSystem->setEndSpinVar(0);
1751
1752 // color of particles
1753 float color[3] = {0,0,0};
1754 color[i] = 1;
1755 Color4F startColor(color[0], color[1], color[2], 1.0f);
1756 particleSystem->setStartColor(startColor);
1757
1758 Color4F startColorVar(0, 0, 0, 0);
1759 particleSystem->setStartColorVar(startColorVar);
1760
1761 Color4F endColor = startColor;
1762 particleSystem->setEndColor(endColor);
1763
1764 Color4F endColorVar = startColorVar;
1765 particleSystem->setEndColorVar(endColorVar);
1766
1767 // size, in pixels
1768 particleSystem->setStartSize(32);
1769 particleSystem->setStartSizeVar(0);
1770 particleSystem->setEndSize(ParticleSystem::START_SIZE_EQUAL_TO_END_SIZE);
1771
1772 // emits per second
1773 particleSystem->setEmissionRate(particleSystem->getTotalParticles()/particleSystem->getLife());
1774
1775 // additive
1776
1777 particleSystem->setPosition(Vec2(i*10+120.0f,200.0f));
1778
1779
1780 _batchNode->addChild(particleSystem);
1781 particleSystem->setPositionType(ParticleSystem::PositionType::FREE);
1782
1783 particleSystem->release();
1784
1785 //[pBNode addChild:particleSystem z:10 tag:0);
1786
1787 }
1788
1789 schedule(CC_SCHEDULE_SELECTOR(ReorderParticleSystems::reorderSystem), 2.0f);
1790 _emitter = nullptr;
1791
1792}
1793
1795{
1796 auto system = static_cast<ParticleSystem*>(_batchNode->getChildren().at(1));
1797 _batchNode->reorderChild(system, system->getLocalZOrder() - 1);
1798}
1799
1800
1802{
1803 auto atlas = static_cast<LabelAtlas*>(getChildByTag(kTagParticleCount));
1804
1805 int count = 0;
1806
1807 auto batchNode = getChildByTag(2);
1808 for(const auto &child : batchNode->getChildren()) {
1809 auto item = dynamic_cast<ParticleSystem*>(child);
1810 if (item != nullptr)
1811 {
1812 count += item->getParticleCount();
1813 }
1814 }
1815 char str[100] = {0};
1816 sprintf(str, "%4d", count);
1817 atlas->setString(str);
1818}
1819
1821{
1822 return "reorder systems";
1823}
1824
1826{
1827 return "changes every 2 seconds";
1828}
1829
1830// PremultipliedAlphaTest
1831
1833{
1834 return "premultiplied alpha and readd child test";
1835}
1836
1838{
1839 return "no black halo, particles should fade out\n animation should be normal";
1840}
1841
1843{
1844 if (_hasEmitter)
1845 {
1846 _emitter->removeFromParent();
1847 }
1848 else
1849 {
1850 this->addChild(_emitter);
1851 }
1852
1854}
1855
1857{
1859
1860 _color->setColor(Color3B::BLUE);
1861 this->removeChild(_background, true);
1862 _background = nullptr;
1863
1864 _emitter = ParticleSystemQuad::create("Particles/BoilingFoam.plist");
1865 _emitter->retain();
1866 // Particle Designer "normal" blend func causes black halo on premul textures (ignores multiplication)
1867 //this->emitter.blendFunc = (BlendFunc){ BlendFactor::SRC_ALPHA, BlendFactor::ONE_MINUS_SRC_ALPHA };
1868
1869 // Cocos2d "normal" blend func for premul causes alpha to be ignored (oversaturates colors)
1870 _emitter->setBlendFunc( BlendFunc::ALPHA_PREMULTIPLIED );
1871
1872 CCASSERT(_emitter->isOpacityModifyRGB(), "Particle texture does not have premultiplied alpha, test is useless");
1873
1874 // Toggle next line to see old behavior
1875 // this->emitter.opacityModifyRGB = NO;
1876
1877 _emitter->setStartColor(Color4F(1.0f, 1.0f, 1.0f, 1.0f));
1878 _emitter->setEndColor(Color4F(1.0f, 1.0f, 1.0f, 0.0f));
1879 _emitter->setStartColorVar(Color4F(0.0f, 0.0f, 0.0f, 0.0f));
1880 _emitter->setEndColorVar(Color4F(0.0f, 0.0f, 0.0f, 0.0f));
1881
1882 this->addChild(_emitter, 10);
1883 _hasEmitter = true;
1884
1885 schedule(CC_SCHEDULE_SELECTOR(PremultipliedAlphaTest::readdParticle), 1.0f);
1886}
1887
1888// PremultipliedAlphaTest2
1889
1891{
1893
1894 _color->setColor(Color3B::BLACK);
1895 this->removeChild(_background, true);
1896 _background = nullptr;
1897
1898 _emitter = ParticleSystemQuad::create("Particles/TestPremultipliedAlpha.plist");
1899 _emitter->retain();
1900 this->addChild(_emitter ,10);
1901}
1902
1904{
1905 return "premultiplied alpha 2";
1906}
1907
1909{
1910 return "Arrows should be faded";
1911}
1912
1913
1914// Issue3990
1915
1917{
1919
1920 _color->setColor(Color3B::BLACK);
1921 this->removeChild(_background, true);
1922 _background = nullptr;
1923
1924 _emitter = ParticleSystemQuad::create("Particles/Spiral.plist");
1925
1926 _emitter->setPositionType(ParticleSystem::PositionType::GROUPED);
1927 _emitter->setTotalParticles(1000);
1928 _emitter->setEmissionRate(_emitter->getTotalParticles() / _emitter->getLife());
1929 _emitter->setPosition(VisibleRect::center());
1930
1931 _emitter->retain();
1932 this->addChild(_emitter ,10);
1933}
1934
1935std::string Issue3990::title() const
1936{
1937 return "Issue3990, setTotalParticle should work";
1938}
1939
1940std::string Issue3990::subtitle() const
1941{
1942 return "Show '998' or '999' at bottom right side";
1943}
1944
1945
1946// ParticleVisibleTest
1948{
1950
1951 _emitter = ParticleFireworks::create();
1952 _emitter->retain();
1953 _background->addChild(_emitter, 10);
1954
1955 _emitter->setTexture( Director::getInstance()->getTextureCache()->addImage(s_stars1) );
1956
1957 schedule(CC_SCHEDULE_SELECTOR(ParticleVisibleTest::callback), 1);
1958
1960}
1961
1963{
1964 return "Issue4573";
1965}
1966
1968{
1969 return "Visible enable/disable";
1970}
1971
1973{
1974 _emitter->setVisible(!_emitter->isVisible());
1975}
1976
1977//
1978// ParticleAutoBatching
1979//
1981{
1983
1984 _color->setColor(Color3B::BLACK);
1985 this->removeChild(_background, true);
1986 _background = nullptr;
1987
1988 Size s = Director::getInstance()->getWinSize();
1989
1990 for(int i=0; i<10; i++) {
1991 auto particle = ParticleSystemQuad::create("Particles/SmallSun.plist");
1992 particle->setTotalParticles(100);
1993 particle->setPosition(Vec2(i*s.width/11, s.height/2));
1994 this->addChild(particle ,10);
1995 }
1996}
1997
1999{
2000 return "AutoBatching";
2001}
2002
2004{
2005 return "All 10 particles should be drawin in one batch";
2006}
2007
2008
2009//
2010// ParticleResetTotalParticles
2011//
2013{
2015
2016 _color->setColor(Color3B::BLACK);
2017 removeChild(_background, true);
2018 _background = nullptr;
2019
2020 auto p = ParticleFire::createWithTotalParticles(10);
2021 this->addChild(p);
2022
2023 auto add = MenuItemFont::create("add 10 particles",
2024 [p](Ref*)->void
2025 {
2026 p->setTotalParticles(p->getTotalParticles() + 10 );
2027 });
2028 add->setFontSizeObj(20);
2029 add->setPosition(Vec2(0, 25));
2030
2031 auto remove = MenuItemFont::create("remove 10 particles",
2032 [p](Ref*)->void
2033 {
2034 int count = p->getTotalParticles() - 10;
2035 if (count < 0) { count = 0; }
2036 p->setTotalParticles(count);
2037 });
2038 remove->setPosition(Vec2(0, -25));
2039 remove->setFontSizeObj(20);
2040
2041 auto menu = Menu::create(add, remove, nullptr);
2042 menu->setPosition(Vec2(VisibleRect::center()));
2043 this->addChild(menu);
2044
2045}
2046
2048{
2049 return "reset total particles";
2050}
2051
2053{
2054 return "it should work as well";
2055}
2056
2058{
2060
2061 _color->setColor(Color3B::BLACK);
2062 removeChild(_background, true);
2063 _background = nullptr;
2064
2065 auto winSize = Director::getInstance()->getWinSize();
2066
2067 auto particle = ParticleSystemQuad::create("Particles/BoilingFoam.plist");
2068 particle->setPosition(Vec2(winSize.width * 0.35f, winSize.height * 0.5f));
2069 addChild(particle);
2070
2071 _emitter = particle;
2072 _emitter->retain();
2073
2074 auto particle2 = ParticleSystemQuad::create("Particles/BoilingFoamStar.plist");
2075 particle2->setPosition(Vec2(winSize.width * 0.65f, winSize.height * 0.5f));
2076 addChild(particle2);
2077}
2078
2080{
2081 return "You should see two Particle Emitters using different texture.";
2082}
2083
2084//------------------------------------------------------------------
2085//
2086// ParticleSpriteFrame
2087//
2088//------------------------------------------------------------------
2090{
2092
2093 _emitter = ParticleSmoke::create();
2094 _emitter->retain();
2095 _background->addChild(_emitter, 10);
2096
2097 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Particles/SpriteFrame.plist");
2098
2099 _emitter->setDisplayFrame( SpriteFrameCache::getInstance()->getSpriteFrameByName("dot.png") );
2100
2102}
2103
2105{
2106 return "Particle from SpriteFrame";
2107}
2108
2110{
2111 return "Should not use entire texture atlas";
2112}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
@ IDC_RESTART
@ IDC_NEXT
@ IDC_TOGGLE
@ IDC_BACK
@ kTagParticleCount
USING_NS_CC
cocos2d::ParticleBatchNode * _batchNode
Definition: ParticleTest.h:326
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void update(float dt) override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
static DemoParticleFromFile * create(const std::string &file)
Definition: ParticleTest.h:188
virtual std::string subtitle() const override
void pauseEmitter(float time)
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string title() const override
virtual std::string title() const override
virtual std::string subtitle() const override
void updateQuads(float dt)
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void update(float dt) override
virtual std::string title() const override
virtual std::string title() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void update(float dt) override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string title() const override
cocos2d::Node * _parent1
Definition: ParticleTest.h:270
cocos2d::Node * _parent2
Definition: ParticleTest.h:271
virtual std::string subtitle() const override
void switchRender(float dt)
virtual void onEnter() override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
void onTouchesMoved(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
virtual void update(float dt) override
cocos2d::Sprite * _background
Definition: ParticleTest.h:36
virtual void onEnter() override
void onTouchesEnded(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void toggleCallback(cocos2d::Ref *sender)
virtual std::string subtitle() const override
cocos2d::ParticleSystemQuad * _emitter
Definition: ParticleTest.h:35
cocos2d::LayerColor * _color
Definition: ParticleTest.h:37
virtual std::string title() const override
void onTouchesBegan(const std::vector< cocos2d::Touch * > &touches, cocos2d::Event *event)
void setEmitterPosition()
virtual std::string subtitle() const override
virtual void onEnter() override
unsigned int _order
Definition: ParticleTest.h:293
virtual void onEnter() override
virtual std::string title() const override
void reorderParticles(float dt)
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void onEnter() override
virtual std::string subtitle() const override
void callback(float delta)
virtual void onEnter() override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual std::string title() const override
virtual void onEnter() override
void readdParticle(float delta)
virtual void onEnter() override
virtual std::string title() const override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void onEnter() override
virtual std::string subtitle() const override
virtual void update(float dt) override
virtual bool initWithTotalParticles(int numberOfParticles) override
bool init() override
virtual void onEnter() override
virtual void update(float dt) override
virtual std::string subtitle() const override
void reorderSystem(float time)
cocos2d::ParticleBatchNode * _batchNode
Definition: ParticleTest.h:339
virtual std::string title() const override
virtual void onEnter() override
Definition: BaseTest.cpp:430
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69
static cocos2d::Vec2 bottom()
Definition: VisibleRect.cpp:63
static cocos2d::Vec2 left()
Definition: VisibleRect.cpp:45
static const char s_fire[]
Definition: testResource.h:44
static const char s_back3[]
Definition: testResource.h:41
static const char s_stars1[]
Definition: testResource.h:42
static const char s_stars2[]
Definition: testResource.h:43
static const char s_snow[]
Definition: testResource.h:45