PuzzleSDK
SpritePolygonTest.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 "SpritePolygonTest.h"
26#include "../testResource.h"
27#include "ui/CocosGUI.h"
28
30
31SpritePolygonTest::SpritePolygonTest()
32{
45}
46
48{
49 _isDebugDraw = true;
50 _isNeedDebugMenu = true;
51}
52
54{
55 _drawNodes.clear();
56}
57
59{
61 Director::getInstance()->setClearColor(Color4F(102.0f/255.0f, 184.0f/255.0f, 204.0f/255.0f, 1.0f));
62}
63
65{
66 Director::getInstance()->setClearColor(Color4F::BLACK);
67 TestCase::onExit();
68}
69
71{
72 if(TestCase::init())
73 {
75 {
76 TTFConfig ttfConfig("fonts/arial.ttf", 10);
77 auto label = Label::createWithTTF(ttfConfig,"DebugDraw OFF");
78 auto menuItem = MenuItemLabel::create(label, [=](Ref *ref){
79 if (_isDebugDraw){
80 _isDebugDraw = false;
81 label->setString("DebugDraw ON");
82 for (int i = 0; i < _drawNodes.size(); i++)
83 {
84 _drawNodes.at(i)->setVisible(false);
85 }
86 }else{
87 _isDebugDraw = true;
88 label->setString("DebugDraw OFF");
89 for (int i = 0; i < _drawNodes.size(); i++)
90 {
91 _drawNodes.at(i)->setVisible(true);
93 }
94 }
95 });
96
97 auto menu = Menu::create(menuItem, nullptr);
98 menu->setPosition(Vec2::ZERO);
99 menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
100 menuItem->setPosition( VisibleRect::leftBottom() + Vec2(0.0f, VisibleRect::leftTop().y/4));
101 this->addChild(menu, 9999);
102 }
103 return true;
104 }
105 return false;
106}
107
109{
110 if (_isDebugDraw && _drawNodes.size() > 0) {
111 for (int i = 0; i < _drawNodes.size(); i++)
112 {
113 auto drawnode = _drawNodes.at(i);
114 auto sp = (Sprite*)drawnode->getParent();
115 if(!sp) return;
116 const auto& polygoninfo = sp->getPolygonInfo();
117 drawnode->clear();
118 const auto count = polygoninfo.triangles.indexCount/3;
119 const auto indices = polygoninfo.triangles.indices;
120 const auto verts = polygoninfo.triangles.verts;
121 for(ssize_t i = 0; i < count; i++)
122 {
123 //draw 3 lines
124 Vec3 from = verts[indices[i*3]].vertices;
125 Vec3 to = verts[indices[i*3+1]].vertices;
126 drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN);
127
128 from = verts[indices[i*3+1]].vertices;
129 to = verts[indices[i*3+2]].vertices;
130 drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN);
131
132 from = verts[indices[i*3+2]].vertices;
133 to = verts[indices[i*3]].vertices;
134 drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x,to.y), Color4F::GREEN);
135 }
136 }
137 }
138}
139
141{
143 _polygonSprite = nullptr;
144 initSprites();
145 initTouches();
146 return true;
147 }
148 return false;
149}
150
152{
153 if(_polygonSprite) {
154 auto touchListener = EventListenerTouchOneByOne::create();
155 touchListener->onTouchBegan = [&](Touch* touch, Event* event){
156 return true;
157 };
158 touchListener->onTouchMoved = [&](Touch* touch, Event* event){
159 auto pos = touch->getDelta();
160 float newScale = clampf(_polygonSprite->getScale() + pos.x * 0.01f, 0.1f, 2.f);
161 _polygonSprite->setScale(newScale);
162 _normalSprite->setScale(newScale);
164 };
165 _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
166 }
167}
168
170{
171 _title = "SpritePolygon Creation";
172 _subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename))";
173}
174
176{
177 auto s = Director::getInstance()->getWinSize();
178 auto offset = Vec2(0.15*s.width,0);
179 auto filename = s_pathGrossini;
180
181 //Sprite
182 auto pinfo = AutoPolygon::generatePolygon(filename);
183 _polygonSprite = Sprite::create(pinfo);
184 _polygonSprite->setTag(101);
185 addChild(_polygonSprite);
186 _polygonSprite->setPosition(Vec2(s)/2 + offset);
187
188 _normalSprite = Sprite::create(filename);
189 _normalSprite->setTag(100);
190 addChild(_normalSprite);
191 _normalSprite->setPosition(Vec2(s)/2 - offset);
192
193 //DrawNode
194 auto spDrawNode = DrawNode::create();
195 spDrawNode->setTag(_normalSprite->getTag());
196 spDrawNode->clear();
197 _normalSprite->addChild(spDrawNode);
198 _drawNodes.pushBack(spDrawNode);
199
200 auto sppDrawNode = DrawNode::create();
201 sppDrawNode->setTag(_polygonSprite->getTag());
202 sppDrawNode->clear();
203 _polygonSprite->addChild(sppDrawNode);
204 _drawNodes.pushBack(sppDrawNode);
205
206 //Label
207 TTFConfig ttfConfig("fonts/arial.ttf", 8);
208 std::string temp = "Sprite:\nPixels drawn: ";
209 auto spSize = _normalSprite->getContentSize();
210 auto spArea = Label::createWithTTF(ttfConfig, temp+Value((int)spSize.width*(int)spSize.height).asString());
211 _normalSprite->addChild(spArea);
212 spArea->setAnchorPoint(Vec2(0,1));
213
214 temp = "SpritePolygon:\nPixels drawn: ";
215 auto vertCount = "\nverts:"+Value((int)pinfo.getVertCount()).asString();
216 auto sppArea = Label::createWithTTF(ttfConfig, temp+Value((int)pinfo.getArea()).asString()+vertCount);
217 _polygonSprite->addChild(sppArea);
218 sppArea->setAnchorPoint(Vec2(0,1));
219
221}
222
224{
225 _title = "SpritePolygon Creation with a rect";
226 _subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename, rect))";
227}
228
230{
231 auto s = Director::getInstance()->getWinSize();
232 auto offset = Vec2(0.15*s.width,0);
233 auto filename = s_pathGrossini;
234 Rect head = Rect(30,25,25,25);
235
236 //Sprite
237 auto pinfo = AutoPolygon::generatePolygon(filename, head);
238 _polygonSprite = Sprite::create(pinfo);
239 _polygonSprite->setTag(101);
240 addChild(_polygonSprite);
241 _polygonSprite->setPosition(Vec2(s)/2 + offset);
242
243 _normalSprite = Sprite::create(filename,head);
244 _normalSprite->setTag(100);
245 addChild(_normalSprite);
246 _normalSprite->setPosition(Vec2(s)/2 - offset);
247
248 //DrawNode
249 auto spDrawNode = DrawNode::create();
250 _drawNodes.pushBack(spDrawNode);
251 spDrawNode->setTag(_normalSprite->getTag());
252 spDrawNode->clear();
253 _normalSprite->addChild(spDrawNode);
254
255 auto sppDrawNode = DrawNode::create();
256 _drawNodes.pushBack(sppDrawNode);
257 sppDrawNode->setTag(_polygonSprite->getTag());
258 sppDrawNode->clear();
259 _polygonSprite->addChild(sppDrawNode);
260
261 //Label
262 TTFConfig ttfConfig("fonts/arial.ttf", 8);
263 std::string temp = "Sprite:\nPixels drawn: ";
264 auto spSize = _normalSprite->getContentSize();
265 auto spArea = Label::createWithTTF(ttfConfig, temp+Value((int)spSize.width*(int)spSize.height).asString());
266 _normalSprite->addChild(spArea);
267 spArea->setAnchorPoint(Vec2(0,1));
268
269 temp = "SpritePolygon:\nPixels drawn: ";
270 auto vertCount = "\nverts:"+Value((int)pinfo.getVertCount()).asString();
271 auto sppArea = Label::createWithTTF(ttfConfig, temp+Value((int)pinfo.getArea()).asString()+vertCount);
272 _polygonSprite->addChild(sppArea);
273 sppArea->setAnchorPoint(Vec2(0,1));
274
276}
277
279{
281 {
282 initSliders();
283 initSprites();
284 return true;
285 }
286 return false;
287}
288
290{
291 auto vsize =Director::getInstance()->getVisibleSize();
292 cocos2d::ui::Slider* slider = cocos2d::ui::Slider::create();
293 slider->loadBarTexture("cocosui/sliderTrack.png");
294 slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
295 slider->loadProgressBarTexture("cocosui/sliderProgress.png");
296 slider->setPosition(Vec2(vsize.width/2, vsize.height/4));
297
298 auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
299 _epsilonLabel = Label::createWithTTF(ttfConfig, "Epsilon: 2.0");
300 addChild(_epsilonLabel);
301 _epsilonLabel->setPosition(Vec2(vsize.width/2, vsize.height/4 + 15));
302 addChild(slider);
303
304 slider->addEventListener(CC_CALLBACK_2(SpritePolygonTestSlider::changeEpsilon, this));
305 slider->setPercent((int)(sqrtf(1.0f/19.0f)*100));
306
307}
308
309void SpritePolygonTestSlider::makeSprites(const std::string* list, const int count, const float y)
310{
311 auto vsize = Director::getInstance()->getVisibleSize();
312 for(int i = 0; i < count; i++)
313 {
314 float offset = (vsize.width/(count+1)) * (i+1);
315 auto sp = makeSprite(list[i], Vec2(offset, y));
316 addChild(sp);
317 }
318}
319
320void SpritePolygonTestSlider::changeEpsilon(cocos2d::Ref *pSender, cocos2d::ui::Slider::EventType type)
321{
322 if (type == cocos2d::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
323 {
324 cocos2d::ui::Slider* slider = dynamic_cast<cocos2d::ui::Slider*>(pSender);
325 float epsilon = powf(slider->getPercent()/100.0,2)*19.0f + 1.0f;
326 for(auto child : _children)
327 {
328 if(child->getName().size())
329 {
330 Sprite *sp = (Sprite*)child;
331 auto file = sp->getName();
332 auto pinfo = AutoPolygon::generatePolygon(file, Rect::ZERO, epsilon);
333 sp->setPolygonInfo(pinfo);
334 updateLabel(sp, pinfo);
335 }
336 }
337 _epsilonLabel->setString("Epsilon: "+ Value(epsilon).asString());
339 }
340}
341
342void SpritePolygonTestSlider::updateLabel(const cocos2d::Sprite *sp, const PolygonInfo &pinfo)
343{
344 Label *label = (Label*)(sp->getChildByName(sp->getName()));
345 auto filename = sp->getName();
346 auto size = pinfo.getRect().size/Director::getInstance()->getContentScaleFactor();
347 label->setString(filename+"\nVerts: "+Value((int)pinfo.getVertCount()).asString()+ "\nPixels: "+Value((int)(pinfo.getArea()/(size.width*size.height)*100)).asString()+"%");
348}
349
350Sprite* SpritePolygonTestSlider::makeSprite(const std::string &filename, const Vec2 &pos)
351{
352 //Sprite
353 auto quadSize = Sprite::create(filename)->getContentSize();
354 int originalSize = quadSize.width * quadSize.height;
355 auto pinfo = AutoPolygon::generatePolygon(filename);
356 auto ret = Sprite::create(pinfo);
357 ret->setName(filename);
358 ret->setTag(_tagIndex);
359 _tagIndex++;
360 ret->setPosition(pos);
361
362 //DrawNode
363 auto drawNode = DrawNode::create();
364 _drawNodes.pushBack(drawNode);
365 drawNode->setTag(ret->getTag());
366 drawNode->clear();
367 ret->addChild(drawNode);
368
369 //Label
370 auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
371 auto spArea = Label::createWithTTF(ttfConfig, filename+"\nVerts: "+Value((int)pinfo.getVertCount()).asString()+ "\nPixels: "+Value((int)(pinfo.getArea()/originalSize*100)).asString()+"%");
372 ret->addChild(spArea);
373 spArea->setAnchorPoint(Vec2(0.0f,1.0f));
374 spArea->setName(filename);
375 ret->setAnchorPoint(Vec2(0.5f, 0.0f));
376
378
379 return ret;
380}
381
383{
384 _title = "Optimization Value (default:2.0)";
385 _subtitle = "";
386 _tagIndex = 100;
387}
388
390{
391 auto vsize =Director::getInstance()->getVisibleSize();
392 std::string list[] = {
393 "Images/arrows.png",
394 "Images/CyanTriangle.png",
395 s_pathB2,
396 "Images/elephant1_Diffuse.png"
397 };
398 int count = sizeof(list)/sizeof(list[0]);
399 makeSprites(list, count, vsize.height/2);
400}
401
403{
404 _title = "Optimization Value (default:2.0)";
405 _subtitle = "";
406 _tagIndex = 100;
407}
408
410{
411 auto vsize =Director::getInstance()->getVisibleSize();
412 int count = 3;
413 std::string list[] = {
415 "Images/grossinis_sister1.png",
416 "Images/grossinis_sister2.png"
417 };
418 makeSprites(list, count, vsize.height/5*2);
419}
420
422{
423 _title = "SpritePolygon Actions";
424 _subtitle = "Touch screen to add sprite with random action.";
425 _tagIndex = 100;
426}
427
429{
431 {
432 _polygonInfo = AutoPolygon::generatePolygon(s_pathGrossini);
434 initTouch();
435 scheduleUpdate();
436 return true;
437 }
438 return false;
439}
440
442{
443 auto touchListener = EventListenerTouchOneByOne::create();
444 touchListener->onTouchBegan = [&](Touch* touch, Event* event){
445 return true;
446 };
447 touchListener->onTouchEnded = [&](Touch* touch, Event* event){
448 auto pos = touch->getLocation();
449 addSpritePolygon(pos);
450 };
451 _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
452}
453
455{
456 auto s = Director::getInstance()->getVisibleSize();
457
458 const int DEFAULT_SPRITEPOLYGON_COUNT = 8;
459 Sprite* sprites[DEFAULT_SPRITEPOLYGON_COUNT];
460
461 for (int i = 0; i < DEFAULT_SPRITEPOLYGON_COUNT; i++) {
462 sprites[i] = Sprite::create(_polygonInfo);
463 sprites[i]->setTag(_tagIndex);
464 _tagIndex++;
465 sprites[i]->setPosition(s.width * CCRANDOM_0_1(), s.height * CCRANDOM_0_1());
466 this->addChild(sprites[i]);
467 auto drawNode = DrawNode::create();
468 _drawNodes.pushBack(drawNode);
469 drawNode->setTag(sprites[i]->getTag());
470 drawNode->clear();
471 sprites[i]->addChild(drawNode);
472 }
473 sprites[0]->setColor(Color3B::RED);
474 sprites[1]->setOpacity(100);
475 sprites[2]->setTexture(Director::getInstance()->getTextureCache()->addImage("Images/grossinis_sister1.png"));
476 sprites[3]->setTextureRect(Rect(0.0f,0.0f,100.0f,30.0f));
477 sprites[4]->setScale(0.5f, 2.0f);
478 sprites[5]->setFlippedY(true);
479 sprites[6]->setSkewX(60);
480 sprites[7]->setRotation(90);
481
483}
484
486{
487 auto sprite = Sprite::create(_polygonInfo);
488 sprite->setTag(_tagIndex);
489 _tagIndex++;
490 sprite->setPosition(pos);
491 this->addChild(sprite);
492 auto drawNode = DrawNode::create();
493 _drawNodes.pushBack(drawNode);
494 drawNode->setTag(sprite->getTag());
495 drawNode->clear();
496 sprite->addChild(drawNode);
497
498 ActionInterval* action;
499 float random = CCRANDOM_0_1();
500 if( random < 0.20 )
501 action = ScaleBy::create(3, 2);
502 else if(random < 0.40)
503 action = RotateBy::create(3, 360);
504 else if( random < 0.60)
505 action = Blink::create(1, 3);
506 else if( random < 0.8 )
507 action = TintBy::create(2, 0, -255, -255);
508 else
509 action = FadeOut::create(2);
510 auto seq = Sequence::create( action, action->reverse(), nullptr );
511 sprite->runAction(RepeatForever::create(seq));
512}
513
515{
517}
518
520{
521 TTFConfig ttfConfig("fonts/arial.ttf", 10);
522 _perfLabel = Label::createWithTTF(ttfConfig, "performance test");
523 addChild(_perfLabel);
524 _perfLabel->setPosition(Director::getInstance()->getVisibleSize().width/2, 80);
525
527 auto size = Director::getInstance()->getVisibleSize();
528 _elapsedTime = 0;
529 _posX = _leftX = size.width*0.15;
530 _rightX = size.width*0.85;
531 _posY = size.height/2;
532 prevDt = 0.016f;
533 goRight = true;
534 ended = false;
536 _waitingTime = 0.0;
537
538 _isNeedDebugMenu = false;
539}
540
542{
545 scheduleUpdate();
546 return true;
547 }
548 return false;
549}
550
552{
553 std::string temp = "Nodes: " + Value(_spriteCount).asString() + " Triangles: " + Value(_triCount).asString() + "\nPixels: " + Value(_pixelCount).asString() + " Vertices: " + Value(_vertCount).asString();
554 if(!ended)
555 _perfLabel->setString("Nodes: " + Value(_spriteCount).asString() + " Triangles: " + Value(_triCount).asString() + "\nPixels: " + Value(_pixelCount).asString() + " Vertices: " + Value(_vertCount).asString());
556}
557
559{
560 return nullptr;
561}
562
564{
565 dt = dt*0.3 + prevDt*0.7;
566 prevDt = dt;
567 _elapsedTime += dt;
568 int loops = (0.025-dt)*1000;
569 if(dt < 0.025 && loops>0)
570 {
571 _continuousHighDtTime = clampf(_continuousHighDtTime-dt*2, 0.0, 1.0);
572 _waitingTime = clampf(_waitingTime-dt, 0.0, 5.0);
574 }
575 else
576 {
579 }
580 if (_continuousLowDt >= 5 && loops > 0) {
581 for(int i = 0; i < loops; i++)
582 {
583 if(_posX >= _rightX)
584 {
585 goRight = false;
586 }
587 else if(_posX <= _leftX)
588 {
589 goRight = true;
590 }
591 auto s = makeSprite();
592 addChild(s);
593 s->setPosition(_posX, _posY);
594 if(goRight)
595 _posX++;
596 else
597 _posX--;
598
600 }
601 updateLabel();
602 }
603
604 //if we have 10 continuous low dt, then we will start to create more sprites
605 else if(_continuousHighDtTime >= .5 || _waitingTime > 3.0){
606 // its now 1 seconds with high DT time, time to end
607 ended = true;
608 unscheduleUpdate();
609 _perfLabel->setString("Test ended in " + Value(_elapsedTime).asString() + " seconds\nNodes: " + Value(_spriteCount).asString() + " Triangles: " + Value(_triCount).asString() + "\nPixels: " + Value(_pixelCount).asString() + " Vertices: " + Value(_vertCount).asString());
610 _subtitleLabel->setString("Test ended");
611 }
612 else{
613 _waitingTime += dt;
614 }
615}
616
618{
619 _spriteCount ++;
623}
624
626{
627 _pinfo = AutoPolygon::generatePolygon(s_pathGrossini);
628 _incVert = _pinfo.getVertCount();
629 _incTri = _pinfo.getTrianglesCount();
630 _incPix = _pinfo.getArea();
631}
632
634{
635 _title = "Dynamic SpritePolygon Performance";
636 _subtitle = "Test running, please wait until it ends";
637}
638
640{
641 auto ret = Sprite::create(_pinfo);
642 ret->runAction(RepeatForever::create(RotateBy::create(1.0f,360.0f)));
643 return ret;
644}
645
647{
648 _title = "Dynamic Sprite Performance";
649 _subtitle = "Test running, please wait until it ends";
650}
651
653{
654 auto t = Sprite::create(s_pathGrossini);
655 _incVert = 4;
656 _incTri = 2;
657 _incPix = t->getContentSize().width * t->getContentSize().height;
658}
659
661{
662 auto ret = Sprite::create(s_pathGrossini);
663 ret->runAction(RepeatForever::create(RotateBy::create(1.0f,360.0f)));
664 return ret;
665}
666
667//
668// SpritePolygonTestNoCrash
669//
671{
672 _title = "SpritePolygon ";
673 _subtitle = "AutoPolygon: should not crash";
674}
675
677{
678 auto s = Director::getInstance()->getWinSize();
679 auto pinfo = AutoPolygon::generatePolygon("Images/sprite_polygon_crash.png", Rect::ZERO, 1);
680 auto sprite = Sprite::create(pinfo);
681 addChild(sprite);
682 sprite->setPosition(s.width/2, s.height/2);
683
684 //DrawNode
685 auto spDrawNode = DrawNode::create();
686 spDrawNode->setTag(sprite->getTag());
687 spDrawNode->clear();
688 sprite->addChild(spDrawNode);
689 _drawNodes.pushBack(spDrawNode);
690
692}
693
694//
695// SpritePolygonTestTPIsland
696//
698{
699 _title = "SpritePolygon ";
700 _subtitle = "TexturePacker: Sprite with island";
701}
702
704{
705 auto s = Director::getInstance()->getWinSize();
706 auto cache = SpriteFrameCache::getInstance();
707 cache->addSpriteFramesWithFile("Images/test_polygon.plist");
708
709 auto sprite = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("island_polygon.png"));
710 addChild(sprite);
711 sprite->setPosition(s.width/2, s.height/2);
712
713 //DrawNode
714 auto spDrawNode = DrawNode::create();
715 spDrawNode->setTag(sprite->getTag());
716 spDrawNode->clear();
717 sprite->addChild(spDrawNode);
718 _drawNodes.pushBack(spDrawNode);
719
721}
722
723//
724// SpritePolygonTestAutoPolyIsland
725//
727{
728 _title = "SpritePolygon";
729 _subtitle = "AutoPolygon: Sprite with island";
730}
731
733{
734 auto s = Director::getInstance()->getWinSize();
735
736 auto pinfo = AutoPolygon::generatePolygon("Images/island_polygon.png", Rect::ZERO, 1);
737 auto sprite = Sprite::create(pinfo);
738 addChild(sprite);
739 sprite->setPosition(s.width/2, s.height/2);
740
741 //DrawNode
742 auto spDrawNode = DrawNode::create();
743 spDrawNode->setTag(sprite->getTag());
744 spDrawNode->clear();
745 sprite->addChild(spDrawNode);
746 _drawNodes.pushBack(spDrawNode);
747
749}
750
751//
752// SpritePolygonTestFrameAnim
753//
755{
756 _title = "SpritePolygon";
757 _subtitle = "SpriteFrame animation";
758}
759
761{
762 auto screen = Director::getInstance()->getWinSize();
763
764 auto rotate = RotateBy::create(10, 360);
765 auto action = RepeatForever::create(rotate);
766 char str[100] = {0};
767
768 auto cache = SpriteFrameCache::getInstance();
769 cache->addSpriteFramesWithFile("animations/grossini_dance_poly.plist");
770
771 Sprite *sprite;
772 for(int i=0;i<10;i++)
773 {
774 sprintf(str, "grossini_dance_%02d.png", i+1);
775 sprite = Sprite::createWithSpriteFrameName(str);
776
777 sprite->setPosition(Vec2(screen.width/6*(i%5+1), screen.height*2/3 - screen.height*(i/5)/3));
778
779 auto point = Sprite::create("Images/r1.png");
780 point->setScale( 0.1f );
781 point->setPosition( sprite->getPosition() );
782 addChild(point, 10);
783
784 sprite->runAction( action->clone() );
785 addChild(sprite, i);
786
787 //DrawNode
788 auto spDrawNode = DrawNode::create();
789 spDrawNode->clear();
790 sprite->addChild(spDrawNode);
791 _drawNodes.pushBack(spDrawNode);
792 }
793
795
796
797 Vector<SpriteFrame*> animFrames(5);
798 for(int i = 9; i < 14; i++)
799 {
800 sprintf(str, "grossini_dance_%02d.png", i+1);
801 animFrames.pushBack(cache->getSpriteFrameByName(str));
802 }
803 auto animation = Animation::createWithSpriteFrames(animFrames, 0.3f);
804 sprite->runAction(RepeatForever::create(Animate::create(animation)));
805
806}
807
808//
809// Issue14017Test
810//
812{
813 _title = "Issue 14017";
814 _subtitle = "Autopolygon around the banana";
815}
816
818{
819 auto s = Director::getInstance()->getWinSize();
820 auto offset = Vec2(0.15*s.width,0);
821 auto filename = "Images/bug14017.png";
822
823 //Sprite
824 auto pinfo = AutoPolygon::generatePolygon(filename);
825 _polygonSprite = Sprite::create(pinfo);
826 _polygonSprite->setTag(101);
827 addChild(_polygonSprite);
828 _polygonSprite->setPosition(Vec2(s)/2 + offset);
829
830 _normalSprite = Sprite::create(filename);
831 _normalSprite->setTag(100);
832 addChild(_normalSprite);
833 _normalSprite->setPosition(Vec2(s)/2 - offset);
834
835 //DrawNode
836 auto spDrawNode = DrawNode::create();
837 spDrawNode->setTag(_normalSprite->getTag());
838 spDrawNode->clear();
839 _normalSprite->addChild(spDrawNode);
840 _drawNodes.pushBack(spDrawNode);
841
842 auto sppDrawNode = DrawNode::create();
843 sppDrawNode->setTag(_polygonSprite->getTag());
844 sppDrawNode->clear();
845 _polygonSprite->addChild(sppDrawNode);
846 _drawNodes.pushBack(sppDrawNode);
847
848 //Label
849 TTFConfig ttfConfig("fonts/arial.ttf", 8);
850 std::string temp = "Sprite:\nPixels drawn: ";
851 auto spSize = _normalSprite->getContentSize();
852 auto spArea = Label::createWithTTF(ttfConfig, temp+Value((int)spSize.width*(int)spSize.height).asString());
853 _normalSprite->addChild(spArea);
854 spArea->setAnchorPoint(Vec2(0,1));
855
856 temp = "SpritePolygon:\nPixels drawn: ";
857 auto vertCount = "\nverts:"+Value((int)pinfo.getVertCount()).asString();
858 auto sppArea = Label::createWithTTF(ttfConfig, temp+Value((int)pinfo.getArea()).asString()+vertCount);
859 _polygonSprite->addChild(sppArea);
860 sppArea->setAnchorPoint(Vec2(0,1));
861
863}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
virtual void initSprites() override
virtual void initIncrementStats() override
virtual cocos2d::Sprite * makeSprite() override
virtual void initIncrementStats()
virtual void update(float dt) override
virtual bool init() override
virtual void initIncrementStats() override
cocos2d::Sprite * makeSprite() override
void initSprites() override
virtual void initSprites() override
void initSprites() override
void initSprites() override
virtual bool init() override
cocos2d::PolygonInfo _polygonInfo
void addSpritePolygon(const cocos2d::Vec2 &pos)
void update(float dt) override
virtual void initSprites() override
virtual void onEnter() override
virtual void onExit() override
cocos2d::Vector< cocos2d::DrawNode * > _drawNodes
virtual bool init() override
virtual void initSprites()
cocos2d::Sprite * _normalSprite
cocos2d::Sprite * _polygonSprite
virtual bool init() override
virtual void initSprites() override
virtual void initSprites() override
cocos2d::Sprite * makeSprite(const std::string &filename, const cocos2d::Vec2 &pos)
cocos2d::Label * _epsilonLabel
void makeSprites(const std::string *list, const int count, const float y)
void changeEpsilon(Ref *pSender, cocos2d::ui::Slider::EventType type)
void updateLabel(const cocos2d::Sprite *sp, const cocos2d::PolygonInfo &pinfo)
virtual bool init() override
virtual void initSprites() override
virtual void onEnter() override
Definition: BaseTest.cpp:430
cocos2d::Label * _subtitleLabel
Definition: BaseTest.h:107
static cocos2d::Vec2 leftTop()
Definition: VisibleRect.cpp:75
static cocos2d::Vec2 leftBottom()
Definition: VisibleRect.cpp:87
static const char s_pathB2[]
Definition: testResource.h:32
static const char s_pathGrossini[]
Definition: testResource.h:28