2018-01-29 16:25:32 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-07-09 22:23:34 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2018-01-29 16:25:32 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-04-23 18:03:03 +08:00
|
|
|
#include "SpritePolygonTest.h"
|
|
|
|
#include "../testResource.h"
|
2015-06-04 15:43:31 +08:00
|
|
|
#include "ui/CocosGUI.h"
|
2015-04-23 18:03:03 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-04-23 18:03:03 +08:00
|
|
|
SpritePolygonTest::SpritePolygonTest()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest1);
|
2015-06-04 15:43:31 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonTest2);
|
2015-07-31 15:49:44 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonTest3);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest4);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest5);
|
2015-04-28 15:18:40 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonPerformanceTestDynamic);
|
|
|
|
ADD_TEST_CASE(SpritePerformanceTestDynamic);
|
2016-02-09 03:25:37 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonTestNoCrash);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTestTPIsland);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTestAutoPolyIsland);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTestFrameAnim);
|
2016-06-29 08:32:13 +08:00
|
|
|
ADD_TEST_CASE(Issue14017Test);
|
2015-04-23 18:03:03 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
SpritePolygonTestCase::SpritePolygonTestCase()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_isDebugDraw = true;
|
2015-08-26 18:14:25 +08:00
|
|
|
_isNeedDebugMenu = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SpritePolygonTestCase::~SpritePolygonTestCase()
|
|
|
|
{
|
|
|
|
_drawNodes.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestCase::onEnter()
|
|
|
|
{
|
|
|
|
TestCase::onEnter();
|
2021-12-28 16:06:23 +08:00
|
|
|
Director::getInstance()->setClearColor(Color4F(102.0f / 255.0f, 184.0f / 255.0f, 204.0f / 255.0f, 1.0f));
|
2015-08-26 18:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestCase::onExit()
|
2015-06-04 15:43:31 +08:00
|
|
|
{
|
|
|
|
Director::getInstance()->setClearColor(Color4F::BLACK);
|
2015-08-26 18:14:25 +08:00
|
|
|
TestCase::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePolygonTestCase::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (TestCase::init())
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
|
|
|
if (_isNeedDebugMenu)
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 10);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto label = Label::createWithTTF(ttfConfig, "DebugDraw OFF");
|
|
|
|
auto menuItem = MenuItemLabel::create(label, [=](Ref* ref) {
|
|
|
|
if (_isDebugDraw)
|
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
_isDebugDraw = false;
|
|
|
|
label->setString("DebugDraw ON");
|
|
|
|
for (int i = 0; i < _drawNodes.size(); i++)
|
|
|
|
{
|
|
|
|
_drawNodes.at(i)->setVisible(false);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
_isDebugDraw = true;
|
|
|
|
label->setString("DebugDraw OFF");
|
|
|
|
for (int i = 0; i < _drawNodes.size(); i++)
|
|
|
|
{
|
|
|
|
_drawNodes.at(i)->setVisible(true);
|
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
|
|
|
menu->setPosition(Vec2::ZERO);
|
|
|
|
menuItem->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
|
2021-12-28 16:06:23 +08:00
|
|
|
menuItem->setPosition(VisibleRect::leftBottom() + Vec2(0.0f, VisibleRect::leftTop().y / 4));
|
2015-08-26 18:14:25 +08:00
|
|
|
this->addChild(menu, 9999);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-06-04 15:43:31 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
void SpritePolygonTestCase::updateDrawNode()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_isDebugDraw && _drawNodes.size() > 0)
|
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
for (int i = 0; i < _drawNodes.size(); i++)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto drawnode = _drawNodes.at(i);
|
|
|
|
auto sp = (Sprite*)drawnode->getParent();
|
|
|
|
if (!sp)
|
|
|
|
return;
|
|
|
|
const auto& polygoninfo = sp->getPolygonInfo();
|
|
|
|
drawnode->clear();
|
|
|
|
const auto count = polygoninfo.triangles.indexCount / 3;
|
|
|
|
const auto indices = polygoninfo.triangles.indices;
|
|
|
|
const auto verts = polygoninfo.triangles.verts;
|
|
|
|
for (ssize_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
// draw 3 lines
|
|
|
|
Vec3 from = verts[indices[i * 3]].vertices;
|
|
|
|
Vec3 to = verts[indices[i * 3 + 1]].vertices;
|
|
|
|
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x, to.y), Color4F::BLUE);
|
|
|
|
|
|
|
|
from = verts[indices[i * 3 + 1]].vertices;
|
|
|
|
to = verts[indices[i * 3 + 2]].vertices;
|
|
|
|
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x, to.y), Color4F::GREEN);
|
|
|
|
|
|
|
|
from = verts[indices[i * 3 + 2]].vertices;
|
|
|
|
to = verts[indices[i * 3]].vertices;
|
|
|
|
drawnode->drawLine(Vec2(from.x, from.y), Vec2(to.x, to.y), Color4F::RED);
|
|
|
|
}
|
2015-08-26 18:14:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePolygonTestDemo::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (SpritePolygonTestCase::init())
|
|
|
|
{
|
2016-04-28 09:49:55 +08:00
|
|
|
_polygonSprite = nullptr;
|
2015-08-26 18:14:25 +08:00
|
|
|
initSprites();
|
|
|
|
initTouches();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestDemo::initTouches()
|
2015-06-04 15:43:31 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_polygonSprite)
|
|
|
|
{
|
|
|
|
auto touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
touchListener->onTouchBegan = [&](Touch* touch, Event* event) { return true; };
|
|
|
|
touchListener->onTouchMoved = [&](Touch* touch, Event* event) {
|
|
|
|
auto pos = touch->getDelta();
|
2016-04-28 09:49:55 +08:00
|
|
|
float newScale = clampf(_polygonSprite->getScale() + pos.x * 0.01f, 0.1f, 2.f);
|
|
|
|
_polygonSprite->setScale(newScale);
|
|
|
|
_normalSprite->setScale(newScale);
|
|
|
|
updateDrawNode();
|
|
|
|
};
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
SpritePolygonTest1::SpritePolygonTest1()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon Creation";
|
2015-08-26 18:14:25 +08:00
|
|
|
_subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename))";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest1::initSprites()
|
2015-06-04 15:43:31 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto offset = Vec2(0.15 * s.width, 0);
|
2015-08-26 18:14:25 +08:00
|
|
|
auto filename = s_pathGrossini;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// Sprite
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename);
|
2015-08-26 18:14:25 +08:00
|
|
|
_polygonSprite = Sprite::create(pinfo);
|
|
|
|
_polygonSprite->setTag(101);
|
|
|
|
addChild(_polygonSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_polygonSprite->setPosition(Vec2(s) / 2 + offset);
|
2015-08-26 18:14:25 +08:00
|
|
|
|
|
|
|
_normalSprite = Sprite::create(filename);
|
|
|
|
_normalSprite->setTag(100);
|
|
|
|
addChild(_normalSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_normalSprite->setPosition(Vec2(s) / 2 - offset);
|
2015-08-26 18:14:25 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2015-08-26 18:14:25 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->setTag(_normalSprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
_normalSprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
auto sppDrawNode = DrawNode::create();
|
|
|
|
sppDrawNode->setTag(_polygonSprite->getTag());
|
|
|
|
sppDrawNode->clear();
|
|
|
|
_polygonSprite->addChild(sppDrawNode);
|
|
|
|
_drawNodes.pushBack(sppDrawNode);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// Label
|
2015-06-04 15:43:31 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
|
|
|
std::string temp = "Sprite:\nPixels drawn: ";
|
2021-12-28 16:06:23 +08:00
|
|
|
auto spSize = _normalSprite->getContentSize();
|
|
|
|
auto spArea = Label::createWithTTF(ttfConfig, temp + Value((int)spSize.width * (int)spSize.height).asString());
|
2015-08-26 18:14:25 +08:00
|
|
|
_normalSprite->addChild(spArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
spArea->setAnchorPoint(Vec2(0, 1));
|
|
|
|
|
|
|
|
temp = "SpritePolygon:\nPixels drawn: ";
|
|
|
|
auto vertCount = "\nverts:" + Value((int)pinfo.getVertCount()).asString();
|
|
|
|
auto sppArea = Label::createWithTTF(ttfConfig, temp + Value((int)pinfo.getArea()).asString() + vertCount);
|
2015-08-26 18:14:25 +08:00
|
|
|
_polygonSprite->addChild(sppArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
sppArea->setAnchorPoint(Vec2(0, 1));
|
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
updateDrawNode();
|
2015-06-04 15:43:31 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
SpritePolygonTest2::SpritePolygonTest2()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon Creation with a rect";
|
2015-06-04 15:43:31 +08:00
|
|
|
_subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename, rect))";
|
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
void SpritePolygonTest2::initSprites()
|
2015-06-04 15:43:31 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto offset = Vec2(0.15 * s.width, 0);
|
2015-06-04 15:43:31 +08:00
|
|
|
auto filename = s_pathGrossini;
|
2021-12-28 16:06:23 +08:00
|
|
|
Rect head = Rect(30, 25, 25, 25);
|
|
|
|
|
|
|
|
// Sprite
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename, head);
|
2015-08-26 18:14:25 +08:00
|
|
|
_polygonSprite = Sprite::create(pinfo);
|
|
|
|
_polygonSprite->setTag(101);
|
|
|
|
addChild(_polygonSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_polygonSprite->setPosition(Vec2(s) / 2 + offset);
|
|
|
|
|
|
|
|
_normalSprite = Sprite::create(filename, head);
|
2015-08-26 18:14:25 +08:00
|
|
|
_normalSprite->setTag(100);
|
|
|
|
addChild(_normalSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_normalSprite->setPosition(Vec2(s) / 2 - offset);
|
2015-08-26 18:14:25 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2015-08-26 18:14:25 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
spDrawNode->setTag(_normalSprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
_normalSprite->addChild(spDrawNode);
|
|
|
|
|
|
|
|
auto sppDrawNode = DrawNode::create();
|
|
|
|
_drawNodes.pushBack(sppDrawNode);
|
|
|
|
sppDrawNode->setTag(_polygonSprite->getTag());
|
|
|
|
sppDrawNode->clear();
|
|
|
|
_polygonSprite->addChild(sppDrawNode);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// Label
|
2015-06-04 15:43:31 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
|
|
|
std::string temp = "Sprite:\nPixels drawn: ";
|
2021-12-28 16:06:23 +08:00
|
|
|
auto spSize = _normalSprite->getContentSize();
|
|
|
|
auto spArea = Label::createWithTTF(ttfConfig, temp + Value((int)spSize.width * (int)spSize.height).asString());
|
2015-08-26 18:14:25 +08:00
|
|
|
_normalSprite->addChild(spArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
spArea->setAnchorPoint(Vec2(0, 1));
|
|
|
|
|
|
|
|
temp = "SpritePolygon:\nPixels drawn: ";
|
|
|
|
auto vertCount = "\nverts:" + Value((int)pinfo.getVertCount()).asString();
|
|
|
|
auto sppArea = Label::createWithTTF(ttfConfig, temp + Value((int)pinfo.getArea()).asString() + vertCount);
|
2015-08-26 18:14:25 +08:00
|
|
|
_polygonSprite->addChild(sppArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
sppArea->setAnchorPoint(Vec2(0, 1));
|
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePolygonTestSlider::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (SpritePolygonTestCase::init())
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
|
|
|
initSliders();
|
|
|
|
initSprites();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestSlider::initSliders()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto vsize = Director::getInstance()->getVisibleSize();
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ui::Slider* slider = axis::ui::Slider::create();
|
2015-08-26 18:14:25 +08:00
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
slider->setPosition(Vec2(vsize.width / 2, vsize.height / 4));
|
2019-05-10 11:31:10 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
|
2021-12-28 16:06:23 +08:00
|
|
|
_epsilonLabel = Label::createWithTTF(ttfConfig, "Epsilon: 2.0");
|
2015-08-26 18:14:25 +08:00
|
|
|
addChild(_epsilonLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
_epsilonLabel->setPosition(Vec2(vsize.width / 2, vsize.height / 4 + 15));
|
2015-08-26 18:14:25 +08:00
|
|
|
addChild(slider);
|
2019-05-10 11:31:10 +08:00
|
|
|
|
|
|
|
slider->addEventListener(CC_CALLBACK_2(SpritePolygonTestSlider::changeEpsilon, this));
|
2021-12-28 16:06:23 +08:00
|
|
|
slider->setPercent((int)(sqrtf(1.0f / 19.0f) * 100));
|
2015-08-26 18:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestSlider::makeSprites(const std::string* list, const int count, const float y)
|
|
|
|
{
|
2016-02-09 03:25:37 +08:00
|
|
|
auto vsize = Director::getInstance()->getVisibleSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
float offset = (vsize.width / (count + 1)) * (i + 1);
|
|
|
|
auto sp = makeSprite(list[i], Vec2(offset, y));
|
2015-08-26 18:14:25 +08:00
|
|
|
addChild(sp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void SpritePolygonTestSlider::changeEpsilon(axis::Ref* pSender, axis::ui::Slider::EventType type)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
if (type == axis::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2022-07-11 17:50:21 +08:00
|
|
|
axis::ui::Slider* slider = dynamic_cast<axis::ui::Slider*>(pSender);
|
2021-12-28 16:06:23 +08:00
|
|
|
float epsilon = powf(slider->getPercent() / 100.0, 2) * 19.0f + 1.0f;
|
|
|
|
for (auto child : _children)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (child->getName().size())
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Sprite* sp = (Sprite*)child;
|
|
|
|
auto file = sp->getName();
|
2015-08-26 18:14:25 +08:00
|
|
|
auto pinfo = AutoPolygon::generatePolygon(file, Rect::ZERO, epsilon);
|
|
|
|
sp->setPolygonInfo(pinfo);
|
|
|
|
updateLabel(sp, pinfo);
|
|
|
|
}
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
_epsilonLabel->setString("Epsilon: " + Value(epsilon).asString());
|
2015-08-26 18:14:25 +08:00
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
void SpritePolygonTestSlider::updateLabel(const axis::Sprite* sp, const PolygonInfo& pinfo)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
Label* label = (Label*)(sp->getChildByName(sp->getName()));
|
2015-08-26 18:14:25 +08:00
|
|
|
auto filename = sp->getName();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto size = pinfo.getRect().size / Director::getInstance()->getContentScaleFactor();
|
2021-12-28 17:20:17 +08:00
|
|
|
label->setString((std::string)filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
|
2021-12-28 16:06:23 +08:00
|
|
|
"\nPixels: " + Value((int)(pinfo.getArea() / (size.width * size.height) * 100)).asString() + "%");
|
2015-08-26 18:14:25 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 17:20:17 +08:00
|
|
|
Sprite* SpritePolygonTestSlider::makeSprite(std::string_view filename, const Vec2& pos)
|
2015-08-26 18:14:25 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
// Sprite
|
|
|
|
auto quadSize = Sprite::create(filename)->getContentSize();
|
2015-08-26 18:14:25 +08:00
|
|
|
int originalSize = quadSize.width * quadSize.height;
|
2021-12-28 16:06:23 +08:00
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename);
|
|
|
|
auto ret = Sprite::create(pinfo);
|
2015-08-26 18:14:25 +08:00
|
|
|
ret->setName(filename);
|
|
|
|
ret->setTag(_tagIndex);
|
|
|
|
_tagIndex++;
|
|
|
|
ret->setPosition(pos);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// DrawNode
|
2015-08-26 18:14:25 +08:00
|
|
|
auto drawNode = DrawNode::create();
|
|
|
|
_drawNodes.pushBack(drawNode);
|
|
|
|
drawNode->setTag(ret->getTag());
|
|
|
|
drawNode->clear();
|
|
|
|
ret->addChild(drawNode);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
// Label
|
2015-08-26 18:14:25 +08:00
|
|
|
auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto spArea = Label::createWithTTF(
|
2021-12-28 17:20:17 +08:00
|
|
|
ttfConfig, (std::string)filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
|
2021-12-28 16:06:23 +08:00
|
|
|
"\nPixels: " + Value((int)(pinfo.getArea() / originalSize * 100)).asString() + "%");
|
2015-08-26 18:14:25 +08:00
|
|
|
ret->addChild(spArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
spArea->setAnchorPoint(Vec2(0.0f, 1.0f));
|
2015-08-26 18:14:25 +08:00
|
|
|
spArea->setName(filename);
|
2019-09-24 11:31:35 +08:00
|
|
|
ret->setAnchorPoint(Vec2(0.5f, 0.0f));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
updateDrawNode();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
return ret;
|
2015-06-04 15:43:31 +08:00
|
|
|
}
|
|
|
|
|
2015-07-31 15:49:44 +08:00
|
|
|
SpritePolygonTest3::SpritePolygonTest3()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "Optimization Value (default:2.0)";
|
2015-07-31 15:49:44 +08:00
|
|
|
_subtitle = "";
|
2015-08-26 18:14:25 +08:00
|
|
|
_tagIndex = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest3::initSprites()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto vsize = Director::getInstance()->getVisibleSize();
|
|
|
|
std::string list[] = {"Images/arrows.png", "Images/CyanTriangle.png", s_pathB2, "Images/elephant1_Diffuse.png"};
|
|
|
|
int count = sizeof(list) / sizeof(list[0]);
|
|
|
|
makeSprites(list, count, vsize.height / 2);
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
|
2015-07-31 15:49:44 +08:00
|
|
|
SpritePolygonTest4::SpritePolygonTest4()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "Optimization Value (default:2.0)";
|
2015-07-31 15:49:44 +08:00
|
|
|
_subtitle = "";
|
2015-08-26 18:14:25 +08:00
|
|
|
_tagIndex = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest4::initSprites()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto vsize = Director::getInstance()->getVisibleSize();
|
|
|
|
int count = 3;
|
|
|
|
std::string list[] = {s_pathGrossini, "Images/grossinis_sister1.png", "Images/grossinis_sister2.png"};
|
|
|
|
makeSprites(list, count, vsize.height / 5 * 2);
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SpritePolygonTest5::SpritePolygonTest5()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon Actions";
|
2015-07-31 15:49:44 +08:00
|
|
|
_subtitle = "Touch screen to add sprite with random action.";
|
2015-08-26 18:14:25 +08:00
|
|
|
_tagIndex = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePolygonTest5::init()
|
|
|
|
{
|
|
|
|
if (SpritePolygonTestCase::init())
|
|
|
|
{
|
|
|
|
_polygonInfo = AutoPolygon::generatePolygon(s_pathGrossini);
|
|
|
|
loadDefaultSprites();
|
|
|
|
initTouch();
|
|
|
|
scheduleUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest5::initTouch()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
touchListener->onTouchBegan = [&](Touch* touch, Event* event) { return true; };
|
|
|
|
touchListener->onTouchEnded = [&](Touch* touch, Event* event) {
|
2015-07-31 15:49:44 +08:00
|
|
|
auto pos = touch->getLocation();
|
|
|
|
addSpritePolygon(pos);
|
|
|
|
};
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest5::loadDefaultSprites()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getVisibleSize();
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-31 15:49:44 +08:00
|
|
|
const int DEFAULT_SPRITEPOLYGON_COUNT = 8;
|
|
|
|
Sprite* sprites[DEFAULT_SPRITEPOLYGON_COUNT];
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 0; i < DEFAULT_SPRITEPOLYGON_COUNT; i++)
|
|
|
|
{
|
2015-07-31 15:49:44 +08:00
|
|
|
sprites[i] = Sprite::create(_polygonInfo);
|
2015-08-26 18:14:25 +08:00
|
|
|
sprites[i]->setTag(_tagIndex);
|
|
|
|
_tagIndex++;
|
2015-07-31 15:49:44 +08:00
|
|
|
sprites[i]->setPosition(s.width * CCRANDOM_0_1(), s.height * CCRANDOM_0_1());
|
|
|
|
this->addChild(sprites[i]);
|
2015-08-26 18:14:25 +08:00
|
|
|
auto drawNode = DrawNode::create();
|
|
|
|
_drawNodes.pushBack(drawNode);
|
|
|
|
drawNode->setTag(sprites[i]->getTag());
|
|
|
|
drawNode->clear();
|
|
|
|
sprites[i]->addChild(drawNode);
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
|
|
|
sprites[0]->setColor(Color3B::RED);
|
|
|
|
sprites[1]->setOpacity(100);
|
|
|
|
sprites[2]->setTexture(Director::getInstance()->getTextureCache()->addImage("Images/grossinis_sister1.png"));
|
2021-12-28 16:06:23 +08:00
|
|
|
sprites[3]->setTextureRect(Rect(0.0f, 0.0f, 100.0f, 30.0f));
|
2015-07-31 15:49:44 +08:00
|
|
|
sprites[4]->setScale(0.5f, 2.0f);
|
|
|
|
sprites[5]->setFlippedY(true);
|
|
|
|
sprites[6]->setSkewX(60);
|
|
|
|
sprites[7]->setRotation(90);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
updateDrawNode();
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest5::addSpritePolygon(const Vec2& pos)
|
|
|
|
{
|
|
|
|
auto sprite = Sprite::create(_polygonInfo);
|
2015-08-26 18:14:25 +08:00
|
|
|
sprite->setTag(_tagIndex);
|
|
|
|
_tagIndex++;
|
2015-07-31 15:49:44 +08:00
|
|
|
sprite->setPosition(pos);
|
|
|
|
this->addChild(sprite);
|
2015-08-26 18:14:25 +08:00
|
|
|
auto drawNode = DrawNode::create();
|
|
|
|
_drawNodes.pushBack(drawNode);
|
|
|
|
drawNode->setTag(sprite->getTag());
|
|
|
|
drawNode->clear();
|
|
|
|
sprite->addChild(drawNode);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-07-31 15:49:44 +08:00
|
|
|
ActionInterval* action;
|
|
|
|
float random = CCRANDOM_0_1();
|
2021-12-28 16:06:23 +08:00
|
|
|
if (random < 0.20)
|
2015-07-31 15:49:44 +08:00
|
|
|
action = ScaleBy::create(3, 2);
|
2021-12-28 16:06:23 +08:00
|
|
|
else if (random < 0.40)
|
2015-07-31 15:49:44 +08:00
|
|
|
action = RotateBy::create(3, 360);
|
2021-12-28 16:06:23 +08:00
|
|
|
else if (random < 0.60)
|
2015-07-31 15:49:44 +08:00
|
|
|
action = Blink::create(1, 3);
|
2021-12-28 16:06:23 +08:00
|
|
|
else if (random < 0.8)
|
2015-07-31 15:49:44 +08:00
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
2021-12-28 16:06:23 +08:00
|
|
|
auto seq = Sequence::create(action, action->reverse(), nullptr);
|
2015-08-26 18:14:25 +08:00
|
|
|
sprite->runAction(RepeatForever::create(seq));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTest5::update(float dt)
|
|
|
|
{
|
|
|
|
updateDrawNode();
|
2015-07-31 15:49:44 +08:00
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
SpritePolygonPerformance::SpritePolygonPerformance()
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 10);
|
2015-08-26 18:14:25 +08:00
|
|
|
_perfLabel = Label::createWithTTF(ttfConfig, "performance test");
|
|
|
|
addChild(_perfLabel);
|
2021-12-28 16:06:23 +08:00
|
|
|
_perfLabel->setPosition(Director::getInstance()->getVisibleSize().width / 2, 80);
|
|
|
|
|
|
|
|
_spriteCount = _vertCount = _triCount = _pixelCount = _continuousLowDt = 0;
|
|
|
|
auto size = Director::getInstance()->getVisibleSize();
|
|
|
|
_elapsedTime = 0;
|
|
|
|
_posX = _leftX = size.width * 0.15;
|
|
|
|
_rightX = size.width * 0.85;
|
|
|
|
_posY = size.height / 2;
|
|
|
|
prevDt = 0.016f;
|
|
|
|
goRight = true;
|
|
|
|
ended = false;
|
2015-08-26 18:14:25 +08:00
|
|
|
_continuousHighDtTime = 0.0;
|
2021-12-28 16:06:23 +08:00
|
|
|
_waitingTime = 0.0;
|
|
|
|
|
2015-08-26 18:14:25 +08:00
|
|
|
_isNeedDebugMenu = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SpritePolygonPerformance::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (SpritePolygonTestCase::init())
|
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
initIncrementStats();
|
|
|
|
scheduleUpdate();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
void SpritePolygonPerformance::updateLabel()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
std::string temp = "Nodes: " + Value(_spriteCount).asString() + " Triangles: " + Value(_triCount).asString() +
|
|
|
|
"\nPixels: " + Value(_pixelCount).asString() + " Vertices: " + Value(_vertCount).asString();
|
|
|
|
if (!ended)
|
|
|
|
_perfLabel->setString(
|
|
|
|
"Nodes: " + Value(_spriteCount).asString() + " Triangles: " + Value(_triCount).asString() +
|
|
|
|
"\nPixels: " + Value(_pixelCount).asString() + " Vertices: " + Value(_vertCount).asString());
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Node* SpritePolygonPerformance::makeSprite()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
return nullptr;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
void SpritePolygonPerformance::update(float dt)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
dt = dt * 0.3 + prevDt * 0.7;
|
2015-04-28 15:18:40 +08:00
|
|
|
prevDt = dt;
|
2015-08-26 18:14:25 +08:00
|
|
|
_elapsedTime += dt;
|
2021-12-28 16:06:23 +08:00
|
|
|
int loops = (0.025 - dt) * 1000;
|
|
|
|
if (dt < 0.025 && loops > 0)
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_continuousHighDtTime = clampf(_continuousHighDtTime - dt * 2, 0.0, 1.0);
|
|
|
|
_waitingTime = clampf(_waitingTime - dt, 0.0, 5.0);
|
2015-08-26 18:14:25 +08:00
|
|
|
_continuousLowDt++;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_continuousHighDtTime += dt;
|
2015-08-26 18:14:25 +08:00
|
|
|
_continuousLowDt = 0;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_continuousLowDt >= 5 && loops > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < loops; i++)
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
if (_posX >= _rightX)
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
|
|
|
goRight = false;
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
else if (_posX <= _leftX)
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
|
|
|
goRight = true;
|
|
|
|
}
|
|
|
|
auto s = makeSprite();
|
|
|
|
addChild(s);
|
|
|
|
s->setPosition(_posX, _posY);
|
2021-12-28 16:06:23 +08:00
|
|
|
if (goRight)
|
2015-04-28 15:18:40 +08:00
|
|
|
_posX++;
|
|
|
|
else
|
|
|
|
_posX--;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
incrementStats();
|
|
|
|
}
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// if we have 10 continuous low dt, then we will start to create more sprites
|
|
|
|
else if (_continuousHighDtTime >= .5 || _waitingTime > 3.0)
|
|
|
|
{
|
2015-04-28 15:18:40 +08:00
|
|
|
// its now 1 seconds with high DT time, time to end
|
|
|
|
ended = true;
|
|
|
|
unscheduleUpdate();
|
2021-12-28 16:06:23 +08:00
|
|
|
_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());
|
2015-04-28 15:18:40 +08:00
|
|
|
_subtitleLabel->setString("Test ended");
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
else
|
|
|
|
{
|
2015-08-26 18:14:25 +08:00
|
|
|
_waitingTime += dt;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
void SpritePolygonPerformance::incrementStats()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_spriteCount++;
|
2015-08-26 18:14:25 +08:00
|
|
|
_vertCount += _incVert;
|
|
|
|
_triCount += _incTri;
|
|
|
|
_pixelCount += _incPix;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonPerformanceTestDynamic::initIncrementStats()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_pinfo = AutoPolygon::generatePolygon(s_pathGrossini);
|
2015-06-11 14:38:38 +08:00
|
|
|
_incVert = _pinfo.getVertCount();
|
2021-12-28 16:06:23 +08:00
|
|
|
_incTri = _pinfo.getTrianglesCount();
|
|
|
|
_incPix = _pinfo.getArea();
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SpritePolygonPerformanceTestDynamic::SpritePolygonPerformanceTestDynamic()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "Dynamic SpritePolygon Performance";
|
2015-04-28 15:18:40 +08:00
|
|
|
_subtitle = "Test running, please wait until it ends";
|
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
Sprite* SpritePolygonPerformanceTestDynamic::makeSprite()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
auto ret = Sprite::create(_pinfo);
|
2021-12-28 16:06:23 +08:00
|
|
|
ret->runAction(RepeatForever::create(RotateBy::create(1.0f, 360.0f)));
|
2015-04-28 15:18:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
SpritePerformanceTestDynamic::SpritePerformanceTestDynamic()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "Dynamic Sprite Performance";
|
2015-04-28 15:18:40 +08:00
|
|
|
_subtitle = "Test running, please wait until it ends";
|
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePerformanceTestDynamic::initIncrementStats()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto t = Sprite::create(s_pathGrossini);
|
2015-04-28 15:18:40 +08:00
|
|
|
_incVert = 4;
|
2021-12-28 16:06:23 +08:00
|
|
|
_incTri = 2;
|
|
|
|
_incPix = t->getContentSize().width * t->getContentSize().height;
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
2015-07-31 15:49:44 +08:00
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
Sprite* SpritePerformanceTestDynamic::makeSprite()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto ret = Sprite::create(s_pathGrossini);
|
|
|
|
ret->runAction(RepeatForever::create(RotateBy::create(1.0f, 360.0f)));
|
2015-04-28 15:18:40 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2016-02-09 03:25:37 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// SpritePolygonTestNoCrash
|
|
|
|
//
|
|
|
|
SpritePolygonTestNoCrash::SpritePolygonTestNoCrash()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon ";
|
2016-02-09 03:25:37 +08:00
|
|
|
_subtitle = "AutoPolygon: should not crash";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestNoCrash::initSprites()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon("Images/sprite_polygon_crash.png", Rect::ZERO, 0.5);
|
2016-02-09 03:25:37 +08:00
|
|
|
auto sprite = Sprite::create(pinfo);
|
|
|
|
addChild(sprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
sprite->setPosition(s.width / 2, s.height / 2);
|
2016-02-09 03:25:37 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2016-02-09 03:25:37 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->setTag(sprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
sprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
|
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SpritePolygonTestTPIsland
|
|
|
|
//
|
|
|
|
SpritePolygonTestTPIsland::SpritePolygonTestTPIsland()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon ";
|
2016-02-09 03:25:37 +08:00
|
|
|
_subtitle = "TexturePacker: Sprite with island";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestTPIsland::initSprites()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2016-02-09 03:25:37 +08:00
|
|
|
auto cache = SpriteFrameCache::getInstance();
|
|
|
|
cache->addSpriteFramesWithFile("Images/test_polygon.plist");
|
|
|
|
|
|
|
|
auto sprite = Sprite::createWithSpriteFrame(cache->getSpriteFrameByName("island_polygon.png"));
|
|
|
|
addChild(sprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
sprite->setPosition(s.width / 2, s.height / 2);
|
2016-02-09 03:25:37 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2016-02-09 03:25:37 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->setTag(sprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
sprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
|
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SpritePolygonTestAutoPolyIsland
|
|
|
|
//
|
|
|
|
SpritePolygonTestAutoPolyIsland::SpritePolygonTestAutoPolyIsland()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon";
|
2016-02-09 03:25:37 +08:00
|
|
|
_subtitle = "AutoPolygon: Sprite with island";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestAutoPolyIsland::initSprites()
|
|
|
|
{
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto pinfo = AutoPolygon::generatePolygon("Images/island_polygon.png", Rect::ZERO, 1);
|
2016-02-09 03:25:37 +08:00
|
|
|
auto sprite = Sprite::create(pinfo);
|
|
|
|
addChild(sprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
sprite->setPosition(s.width / 2, s.height / 2);
|
2016-02-09 03:25:37 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2016-02-09 03:25:37 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->setTag(sprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
sprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
|
|
|
|
updateDrawNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// SpritePolygonTestFrameAnim
|
|
|
|
//
|
|
|
|
SpritePolygonTestFrameAnim::SpritePolygonTestFrameAnim()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "SpritePolygon";
|
2016-02-09 03:25:37 +08:00
|
|
|
_subtitle = "SpriteFrame animation";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpritePolygonTestFrameAnim::initSprites()
|
|
|
|
{
|
|
|
|
auto screen = Director::getInstance()->getWinSize();
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto rotate = RotateBy::create(10, 360);
|
|
|
|
auto action = RepeatForever::create(rotate);
|
2016-02-09 03:25:37 +08:00
|
|
|
char str[100] = {0};
|
|
|
|
|
|
|
|
auto cache = SpriteFrameCache::getInstance();
|
|
|
|
cache->addSpriteFramesWithFile("animations/grossini_dance_poly.plist");
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
Sprite* sprite;
|
|
|
|
for (int i = 0; i < 10; i++)
|
2016-02-09 03:25:37 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
sprintf(str, "grossini_dance_%02d.png", i + 1);
|
2016-02-09 03:25:37 +08:00
|
|
|
sprite = Sprite::createWithSpriteFrameName(str);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
sprite->setPosition(Vec2(screen.width / 6 * (i % 5 + 1), screen.height * 2 / 3 - screen.height * (i / 5) / 3));
|
2016-02-09 03:25:37 +08:00
|
|
|
|
|
|
|
auto point = Sprite::create("Images/r1.png");
|
2021-12-28 16:06:23 +08:00
|
|
|
point->setScale(0.1f);
|
|
|
|
point->setPosition(sprite->getPosition());
|
2016-02-09 03:25:37 +08:00
|
|
|
addChild(point, 10);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
sprite->runAction(action->clone());
|
2016-02-09 03:25:37 +08:00
|
|
|
addChild(sprite, i);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2016-02-09 03:25:37 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->clear();
|
|
|
|
sprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<SpriteFrame*> animFrames(5);
|
2021-12-28 16:06:23 +08:00
|
|
|
for (int i = 9; i < 14; i++)
|
2016-02-09 03:25:37 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
sprintf(str, "grossini_dance_%02d.png", i + 1);
|
2016-02-09 03:25:37 +08:00
|
|
|
animFrames.pushBack(cache->getSpriteFrameByName(str));
|
|
|
|
}
|
|
|
|
auto animation = Animation::createWithSpriteFrames(animFrames, 0.3f);
|
|
|
|
sprite->runAction(RepeatForever::create(Animate::create(animation)));
|
2021-08-18 10:16:21 +08:00
|
|
|
|
|
|
|
updateDrawNode();
|
2016-02-09 03:25:37 +08:00
|
|
|
}
|
2016-06-29 08:32:13 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Issue14017Test
|
|
|
|
//
|
|
|
|
Issue14017Test::Issue14017Test()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_title = "Issue 14017";
|
2016-06-29 08:32:13 +08:00
|
|
|
_subtitle = "Autopolygon around the banana";
|
|
|
|
}
|
|
|
|
|
|
|
|
void Issue14017Test::initSprites()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto offset = Vec2(0.15 * s.width, 0);
|
2016-06-29 08:32:13 +08:00
|
|
|
auto filename = "Images/bug14017.png";
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// Sprite
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename);
|
2016-06-29 08:32:13 +08:00
|
|
|
_polygonSprite = Sprite::create(pinfo);
|
|
|
|
_polygonSprite->setTag(101);
|
|
|
|
addChild(_polygonSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_polygonSprite->setPosition(Vec2(s) / 2 + offset);
|
2016-06-29 08:32:13 +08:00
|
|
|
|
|
|
|
_normalSprite = Sprite::create(filename);
|
|
|
|
_normalSprite->setTag(100);
|
|
|
|
addChild(_normalSprite);
|
2021-12-28 16:06:23 +08:00
|
|
|
_normalSprite->setPosition(Vec2(s) / 2 - offset);
|
2016-06-29 08:32:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// DrawNode
|
2016-06-29 08:32:13 +08:00
|
|
|
auto spDrawNode = DrawNode::create();
|
|
|
|
spDrawNode->setTag(_normalSprite->getTag());
|
|
|
|
spDrawNode->clear();
|
|
|
|
_normalSprite->addChild(spDrawNode);
|
|
|
|
_drawNodes.pushBack(spDrawNode);
|
|
|
|
|
|
|
|
auto sppDrawNode = DrawNode::create();
|
|
|
|
sppDrawNode->setTag(_polygonSprite->getTag());
|
|
|
|
sppDrawNode->clear();
|
|
|
|
_polygonSprite->addChild(sppDrawNode);
|
|
|
|
_drawNodes.pushBack(sppDrawNode);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
// Label
|
2016-06-29 08:32:13 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
|
|
|
std::string temp = "Sprite:\nPixels drawn: ";
|
2021-12-28 16:06:23 +08:00
|
|
|
auto spSize = _normalSprite->getContentSize();
|
|
|
|
auto spArea = Label::createWithTTF(ttfConfig, temp + Value((int)spSize.width * (int)spSize.height).asString());
|
2016-06-29 08:32:13 +08:00
|
|
|
_normalSprite->addChild(spArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
spArea->setAnchorPoint(Vec2(0, 1));
|
2016-06-29 08:32:13 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
temp = "SpritePolygon:\nPixels drawn: ";
|
|
|
|
auto vertCount = "\nverts:" + Value((int)pinfo.getVertCount()).asString();
|
|
|
|
auto sppArea = Label::createWithTTF(ttfConfig, temp + Value((int)pinfo.getArea()).asString() + vertCount);
|
2016-06-29 08:32:13 +08:00
|
|
|
_polygonSprite->addChild(sppArea);
|
2021-12-28 16:06:23 +08:00
|
|
|
sppArea->setAnchorPoint(Vec2(0, 1));
|
2016-06-29 08:32:13 +08:00
|
|
|
|
|
|
|
updateDrawNode();
|
|
|
|
}
|