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
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
SpritePolygonTest::SpritePolygonTest()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest1);
|
2015-06-04 15:43:31 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonTest2);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest3);
|
|
|
|
ADD_TEST_CASE(SpritePolygonTest4);
|
2015-04-28 15:18:40 +08:00
|
|
|
ADD_TEST_CASE(SpritePolygonPerformanceTestDynamic);
|
|
|
|
ADD_TEST_CASE(SpritePerformanceTestDynamic);
|
2015-04-23 18:03:03 +08:00
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonTestCase::onBackCallback(cocos2d::Ref *sender)
|
|
|
|
{
|
|
|
|
TestCase::onBackCallback(sender);
|
|
|
|
Director::getInstance()->setClearColor(Color4F::BLACK);
|
|
|
|
}
|
|
|
|
void SpritePolygonTestDemo::initTouchDebugDraw()
|
|
|
|
{
|
|
|
|
auto touchListener = EventListenerTouchOneByOne::create();
|
|
|
|
touchListener->onTouchBegan = [&](Touch* touch, Event* event){
|
|
|
|
sp->debugDraw(true);
|
|
|
|
spp->debugDraw(true);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
touchListener->onTouchMoved = [&](Touch* touch, Event* event){
|
|
|
|
auto pos = touch->getDelta();
|
|
|
|
float newScale = clampf(spp->getScale() + pos.x * 0.01f, 0.1f, 2.f);
|
|
|
|
spp->setScale(newScale);
|
|
|
|
sp->setScale(newScale);
|
|
|
|
};
|
|
|
|
touchListener->onTouchEnded = [&](Touch* touch, Event* event){
|
|
|
|
spp->debugDraw(false);
|
|
|
|
sp->debugDraw(false);
|
|
|
|
};
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void SpritePolygonTest1::make2Sprites()
|
|
|
|
{
|
|
|
|
Director::getInstance()->setClearColor(Color4F(102.f/255, 184.f/255, 204.f/255, 255.f));
|
|
|
|
auto filename = s_pathGrossini;
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename);
|
|
|
|
spp = Sprite::create(pinfo);
|
|
|
|
addChild(spp);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto offset = Vec2(0.15*s.width,0);
|
|
|
|
spp->setPosition(Vec2(s)/2 + offset);
|
|
|
|
|
|
|
|
|
|
|
|
sp = Sprite::create(filename);
|
|
|
|
addChild(sp);
|
|
|
|
sp->setPosition(Vec2(s)/2 - offset);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
|
|
|
std::string temp = "Sprite:\nPixels drawn: ";
|
|
|
|
auto spSize = sp->getContentSize();
|
|
|
|
auto spArea = Label::createWithTTF(ttfConfig, temp+Value((int)spSize.width*(int)spSize.height).asString());
|
|
|
|
sp->addChild(spArea);
|
|
|
|
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);
|
|
|
|
spp->addChild(sppArea);
|
|
|
|
sppArea->setAnchorPoint(Vec2(0,1));
|
|
|
|
|
|
|
|
initTouchDebugDraw();
|
|
|
|
}
|
|
|
|
SpritePolygonTest1::SpritePolygonTest1()
|
|
|
|
{
|
|
|
|
_title = "SpritePolygon Creation";
|
|
|
|
_subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename))";
|
|
|
|
make2Sprites();
|
|
|
|
}
|
|
|
|
SpritePolygonTest2::SpritePolygonTest2()
|
|
|
|
{
|
|
|
|
_title = "SpritePolygon Creation with a rect";
|
|
|
|
_subtitle = "Sprite::create(AutoPolygon::generatePolygon(filename, rect))";
|
|
|
|
make2Sprites();
|
|
|
|
}
|
|
|
|
void SpritePolygonTest2::make2Sprites()
|
|
|
|
{
|
|
|
|
Director::getInstance()->setClearColor(Color4F(102.f/255, 184.f/255, 204.f/255, 255.f));
|
|
|
|
auto filename = s_pathGrossini;
|
|
|
|
Rect head = Rect(30,25,25,25);
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename, head);
|
|
|
|
spp = Sprite::create(pinfo);
|
|
|
|
addChild(spp);
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
auto offset = Vec2(0.15*s.width,0);
|
|
|
|
spp->setPosition(Vec2(s)/2 + offset);
|
|
|
|
|
|
|
|
|
|
|
|
sp = Sprite::create(filename,head);
|
|
|
|
addChild(sp);
|
|
|
|
sp->setPosition(Vec2(s)/2 - offset);
|
|
|
|
|
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 8);
|
|
|
|
std::string temp = "Sprite:\nPixels drawn: ";
|
|
|
|
auto spSize = sp->getContentSize();
|
|
|
|
auto spArea = Label::createWithTTF(ttfConfig, temp+Value((int)spSize.width*(int)spSize.height).asString());
|
|
|
|
sp->addChild(spArea);
|
|
|
|
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);
|
|
|
|
spp->addChild(sppArea);
|
|
|
|
sppArea->setAnchorPoint(Vec2(0,1));
|
|
|
|
|
|
|
|
initTouchDebugDraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-28 15:18:40 +08:00
|
|
|
SpritePolygonPerformance::SpritePolygonPerformance()
|
|
|
|
{
|
2015-04-28 20:10:56 +08:00
|
|
|
Director::getInstance()->setClearColor(Color4F(102.f/255, 184.f/255, 204.f/255, 255.f));
|
2015-04-28 15:18:40 +08:00
|
|
|
TTFConfig ttfConfig("fonts/arial.ttf", 10);
|
|
|
|
perfLabel = Label::createWithTTF(ttfConfig, "performance test");
|
|
|
|
addChild(perfLabel);
|
|
|
|
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;
|
2015-04-28 20:10:56 +08:00
|
|
|
prevDt = 0.016f;
|
2015-04-28 15:18:40 +08:00
|
|
|
goRight = true;
|
|
|
|
ended = false;
|
|
|
|
scheduleUpdate();
|
|
|
|
continuousHighDtTime = 0.0;
|
|
|
|
waitingTime = 0.0;
|
|
|
|
}
|
|
|
|
void SpritePolygonPerformance::updateLabel()
|
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
std::string temp = "Nodes: " + Value(spriteCount).asString() + " Triangles: " + Value(triCount).asString() + "\nPixels: " + Value(pixelCount).asString() + " Vertices: " + Value(vertCount).asString();
|
2015-04-28 15:18:40 +08:00
|
|
|
if(!ended)
|
|
|
|
perfLabel->setString("Nodes: " + Value(spriteCount).asString() + " Triangles: " + Value(triCount).asString() + "\nPixels: " + Value(pixelCount).asString() + " Vertices: " + Value(vertCount).asString());
|
|
|
|
}
|
|
|
|
Node *SpritePolygonPerformance::makeSprite()
|
|
|
|
{
|
|
|
|
return Node::create();
|
|
|
|
}
|
|
|
|
void SpritePolygonPerformance::update(float dt)
|
|
|
|
{
|
|
|
|
dt = dt*0.3 + prevDt*0.7;
|
|
|
|
prevDt = dt;
|
|
|
|
elapsedTime += dt;
|
|
|
|
int loops = (0.025-dt)*1000;
|
|
|
|
if(dt < 0.025 && loops>0)
|
|
|
|
{
|
|
|
|
continuousHighDtTime = clampf(continuousHighDtTime-dt*2, 0.0, 1.0);
|
|
|
|
waitingTime = clampf(waitingTime-dt, 0.0, 5.0);
|
|
|
|
continuousLowDt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
continuousHighDtTime+=dt;
|
|
|
|
continuousLowDt = 0;
|
|
|
|
}
|
|
|
|
if (continuousLowDt >= 5 && loops > 0) {
|
|
|
|
for(int i = 0; i < loops; i++)
|
|
|
|
{
|
|
|
|
if(_posX >= _rightX)
|
|
|
|
{
|
|
|
|
goRight = false;
|
|
|
|
}
|
|
|
|
else if(_posX <= _leftX)
|
|
|
|
{
|
|
|
|
goRight = true;
|
|
|
|
}
|
|
|
|
auto s = makeSprite();
|
|
|
|
addChild(s);
|
|
|
|
s->setPosition(_posX, _posY);
|
|
|
|
if(goRight)
|
|
|
|
_posX++;
|
|
|
|
else
|
|
|
|
_posX--;
|
|
|
|
|
|
|
|
incrementStats();
|
|
|
|
}
|
|
|
|
updateLabel();
|
|
|
|
}
|
|
|
|
|
|
|
|
//if we have 10 continuous low dt, then we will start to create more sprites
|
|
|
|
else if(continuousHighDtTime >= .5 || waitingTime > 3.0){
|
|
|
|
// its now 1 seconds with high DT time, time to end
|
|
|
|
ended = true;
|
|
|
|
unscheduleUpdate();
|
|
|
|
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());
|
|
|
|
_subtitleLabel->setString("Test ended");
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
waitingTime += dt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void SpritePolygonPerformance::incrementStats()
|
|
|
|
{
|
|
|
|
spriteCount ++;
|
|
|
|
vertCount += _incVert;
|
|
|
|
triCount += _incTri;
|
|
|
|
pixelCount += _incPix;
|
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonPerformanceTestDynamic::initIncrementStats()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2015-06-11 14:38:38 +08:00
|
|
|
_incVert = _pinfo.getVertCount();
|
|
|
|
_incTri = _pinfo.getTriaglesCount();
|
|
|
|
_incPix = _pinfo.getArea();
|
2015-04-28 15:18:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SpritePolygonPerformanceTestDynamic::SpritePolygonPerformanceTestDynamic()
|
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
_pinfo = AutoPolygon::generatePolygon(s_pathGrossini);
|
2015-04-28 15:18:40 +08:00
|
|
|
_title = "Dynamic SpritePolygon Performance";
|
|
|
|
_subtitle = "Test running, please wait until it ends";
|
|
|
|
initIncrementStats();
|
|
|
|
}
|
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);
|
2015-04-28 15:18:40 +08:00
|
|
|
ret->runAction(RepeatForever::create(RotateBy::create(1.0,360.0)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
SpritePerformanceTestDynamic::SpritePerformanceTestDynamic()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
_title = "Dynamic Sprite Performance";
|
2015-04-28 15:18:40 +08:00
|
|
|
_subtitle = "Test running, please wait until it ends";
|
|
|
|
initIncrementStats();
|
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePerformanceTestDynamic::initIncrementStats()
|
2015-04-28 15:18:40 +08:00
|
|
|
{
|
|
|
|
auto t = Sprite::create(s_pathGrossini);
|
|
|
|
_incVert = 4;
|
|
|
|
_incTri = 2;
|
|
|
|
_incPix = t->getContentSize().width * t->getContentSize().height;
|
|
|
|
}
|
|
|
|
Sprite* SpritePerformanceTestDynamic::makeSprite()
|
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
auto ret = Sprite::create(s_pathGrossini);
|
2015-04-28 15:18:40 +08:00
|
|
|
ret->runAction(RepeatForever::create(RotateBy::create(1.0,360.0)));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
SpritePolygonTestSlider::SpritePolygonTestSlider()
|
2015-04-23 18:03:03 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
_ttfConfig = TTFConfig("fonts/arial.ttf", 8);
|
|
|
|
_title = "Optimization Value (default:2.0)";
|
|
|
|
_subtitle = "";
|
2015-04-24 17:39:43 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
auto vsize =Director::getInstance()->getVisibleSize();
|
|
|
|
cocos2d::ui::Slider* slider = cocos2d::ui::Slider::create();
|
|
|
|
slider->loadBarTexture("cocosui/sliderTrack.png");
|
|
|
|
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
|
|
|
|
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
|
|
|
|
slider->setPosition(Vec2(vsize.width/2, vsize.height/4/* + slider->getSize().height * 2.0f*/));
|
2015-04-24 17:39:43 +08:00
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
slider->addEventListener(CC_CALLBACK_2(SpritePolygonTestSlider::changeEpsilon, this));
|
2015-06-09 11:23:37 +08:00
|
|
|
slider->setPercent((int)(sqrtf(1.0f/19.0f)*100));
|
2015-06-08 15:08:16 +08:00
|
|
|
|
|
|
|
_epsilonLabel = Label::createWithTTF(_ttfConfig, "Epsilon: 2.0");
|
|
|
|
addChild(_epsilonLabel);
|
|
|
|
_epsilonLabel->setPosition(Vec2(vsize.width/2, vsize.height/4 + 15));
|
2015-06-04 15:43:31 +08:00
|
|
|
addChild(slider);
|
2015-04-24 17:39:43 +08:00
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonTestSlider::makeSprites(const std::string* list, const int count, const float y)
|
2015-04-24 17:39:43 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
auto vsize =Director::getInstance()->getVisibleSize();
|
|
|
|
float offset = (vsize.width-100)/(count-1);
|
|
|
|
for(int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
auto sp = makeSprite(list[i], Vec2(50+offset*i, y));
|
|
|
|
addChild(sp);
|
|
|
|
sp->debugDraw(true);
|
|
|
|
}
|
2015-04-23 18:03:03 +08:00
|
|
|
}
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonTestSlider::changeEpsilon(cocos2d::Ref *pSender, cocos2d::ui::Slider::EventType type)
|
2015-04-23 18:03:03 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
if (type == cocos2d::ui::Slider::EventType::ON_PERCENTAGE_CHANGED)
|
2015-04-27 17:30:38 +08:00
|
|
|
{
|
2015-06-04 15:43:31 +08:00
|
|
|
cocos2d::ui::Slider* slider = dynamic_cast<cocos2d::ui::Slider*>(pSender);
|
2015-06-09 11:23:37 +08:00
|
|
|
float epsilon = powf(slider->getPercent()/100.0,2)*19.0f + 1.0f;
|
2015-06-04 15:43:31 +08:00
|
|
|
for(auto child : _children)
|
|
|
|
{
|
|
|
|
if(child->getName().size())
|
|
|
|
{
|
|
|
|
Sprite *sp = (Sprite*)child;
|
|
|
|
auto file = sp->getName();
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(file, Rect::ZERO, epsilon);
|
|
|
|
sp->setPolygonInfo(pinfo);
|
|
|
|
sp->debugDraw(true);
|
|
|
|
updateLabel(sp, pinfo);
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 15:08:16 +08:00
|
|
|
_epsilonLabel->setString("Epsilon: "+ Value(epsilon).asString());
|
2015-04-27 17:30:38 +08:00
|
|
|
}
|
2015-04-24 18:17:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
void SpritePolygonTestSlider::updateLabel(const cocos2d::Sprite *sp, const PolygonInfo &pinfo)
|
|
|
|
{
|
|
|
|
Label *label = (Label*)(sp->getChildren().at(0));
|
|
|
|
auto filename = sp->getName();
|
|
|
|
auto size = pinfo.rect.size/Director::getInstance()->getContentScaleFactor();
|
|
|
|
label->setString(filename+"\nVerts: "+Value((int)pinfo.getVertCount()).asString()+ "\nPixels: "+Value((int)(pinfo.getArea()/(size.width*size.height)*100)).asString()+"%");
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite* SpritePolygonTestSlider::makeSprite(const std::string &filename, const Vec2 &pos)
|
|
|
|
{
|
|
|
|
auto quadSize = Sprite::create(filename)->getContentSize();
|
|
|
|
int originalSize = quadSize.width * quadSize.height;
|
|
|
|
auto pinfo = AutoPolygon::generatePolygon(filename);
|
|
|
|
auto ret = Sprite::create(pinfo);
|
|
|
|
ret->setPosition(pos);
|
|
|
|
|
|
|
|
auto spArea = Label::createWithTTF(_ttfConfig, filename+"\nVerts: "+Value((int)pinfo.getVertCount()).asString()+ "\nPixels: "+Value((int)(pinfo.getArea()/originalSize*100)).asString()+"%");
|
|
|
|
ret->addChild(spArea);
|
|
|
|
spArea->setAnchorPoint(Vec2(0,1));
|
|
|
|
ret->setName(filename);
|
|
|
|
ret->setAnchorPoint(Vec2(0.5, 0));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
SpritePolygonTest3::SpritePolygonTest3()
|
|
|
|
{
|
|
|
|
_ttfConfig = TTFConfig("fonts/arial.ttf", 8);
|
|
|
|
_title = "Optimization Value (default:2.0)";
|
|
|
|
_subtitle = "";
|
|
|
|
auto vsize =Director::getInstance()->getVisibleSize();
|
|
|
|
std::string list[] = {
|
|
|
|
"Images/arrows.png",
|
|
|
|
"Images/CyanTriangle.png",
|
|
|
|
s_pathB2,
|
2015-06-08 15:08:16 +08:00
|
|
|
"Images/elephant1_Diffuse.png"
|
2015-06-02 10:10:09 +08:00
|
|
|
};
|
2015-06-08 15:08:16 +08:00
|
|
|
int count = 4;
|
2015-06-04 15:43:31 +08:00
|
|
|
makeSprites(list, count, vsize.height/2);
|
|
|
|
}
|
|
|
|
SpritePolygonTest4::SpritePolygonTest4()
|
|
|
|
{
|
|
|
|
_ttfConfig = TTFConfig("fonts/arial.ttf", 8);
|
|
|
|
_title = "Optimization Value (default:2.0)";
|
|
|
|
_subtitle = "";
|
|
|
|
auto vsize =Director::getInstance()->getVisibleSize();
|
|
|
|
|
|
|
|
int count = 3;
|
|
|
|
std::string list[] = {
|
|
|
|
s_pathGrossini,
|
|
|
|
"Images/grossinis_sister1.png",
|
|
|
|
"Images/grossinis_sister2.png"
|
2015-06-02 10:10:09 +08:00
|
|
|
};
|
|
|
|
|
2015-06-04 15:43:31 +08:00
|
|
|
makeSprites(list, count, vsize.height/5*2);
|
2015-06-02 10:10:09 +08:00
|
|
|
}
|
2015-04-28 15:18:40 +08:00
|
|
|
|