2014-02-10 11:42:15 +08:00
|
|
|
#include "PerformanceScenarioTest.h"
|
|
|
|
#include "../testResource.h"
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TEST_COUNT = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int s_nScenarioCurCase = 0;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ScenarioMenuLayer
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
void ScenarioMenuLayer::showCurrentTest()
|
|
|
|
{
|
|
|
|
Scene* scene = NULL;
|
|
|
|
|
|
|
|
switch (_curCase)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
scene = ScenarioTest::scene();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s_nScenarioCurCase = _curCase;
|
|
|
|
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioMenuLayer::onEnter()
|
|
|
|
{
|
|
|
|
PerformBasicLayer::onEnter();
|
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// Title
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32);
|
2014-02-10 11:42:15 +08:00
|
|
|
addChild(label, 1);
|
2014-04-15 18:23:40 +08:00
|
|
|
label->setPosition(Vector2(s.width/2, s.height-50));
|
2014-02-10 11:42:15 +08:00
|
|
|
|
|
|
|
// Subtitle
|
|
|
|
std::string strSubTitle = subtitle();
|
|
|
|
if(strSubTitle.length())
|
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
auto l = Label::createWithTTF(strSubTitle.c_str(), "fonts/Thonburi.ttf", 16);
|
2014-02-10 11:42:15 +08:00
|
|
|
addChild(l, 1);
|
2014-04-15 18:23:40 +08:00
|
|
|
l->setPosition(Vector2(s.width/2, s.height-80));
|
2014-02-10 11:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
performTests();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ScenarioMenuLayer::title() const
|
|
|
|
{
|
|
|
|
return "no title";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ScenarioMenuLayer::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ScenarioTest
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
2014-02-12 16:14:04 +08:00
|
|
|
int ScenarioTest::_initParticleNum = 500;
|
|
|
|
int ScenarioTest::_parStepNum = 500;
|
|
|
|
int ScenarioTest::_initSpriteNum = 2000;
|
|
|
|
int ScenarioTest::_spriteStepNum = 500;
|
|
|
|
int ScenarioTest::_initParsysNum = 10;
|
|
|
|
int ScenarioTest::_parsysStepNum = 5;
|
|
|
|
|
2014-02-10 11:42:15 +08:00
|
|
|
void ScenarioTest::performTests()
|
|
|
|
{
|
2014-02-12 16:14:04 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(ScenarioTest::onTouchesMoved, this);
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
|
|
|
_particleNumber = _initParticleNum;
|
|
|
|
|
|
|
|
// get the window size & origin position
|
2014-02-10 14:04:21 +08:00
|
|
|
auto s = Director::getInstance()->getVisibleSize();
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
// add tile map
|
|
|
|
_map1 = TMXTiledMap::create("TileMaps/iso-test.tmx");
|
2014-04-15 18:23:40 +08:00
|
|
|
_map1->setAnchorPoint( Vector2(0.5, 0.5) );
|
2014-02-12 16:14:04 +08:00
|
|
|
_map1->setPosition(origin);
|
|
|
|
this->addChild(_map1);
|
2014-02-10 11:42:15 +08:00
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
_map2 = TMXTiledMap::create("TileMaps/iso-test2.tmx");
|
2014-04-15 18:23:40 +08:00
|
|
|
_map2->setAnchorPoint( Vector2(0.5, 0.5) );
|
2014-02-12 16:14:04 +08:00
|
|
|
_map2->setPosition(origin);
|
|
|
|
this->addChild(_map2);
|
|
|
|
|
|
|
|
// add toggle menu item
|
2014-02-10 11:42:15 +08:00
|
|
|
MenuItemFont::setFontSize(20);
|
|
|
|
_itemToggle = MenuItemToggle::createWithCallback(nullptr,
|
|
|
|
MenuItemFont::create( "Add/Remove Sprite" ),
|
|
|
|
MenuItemFont::create( "Add/Remove Particle"),
|
2014-02-12 16:14:04 +08:00
|
|
|
MenuItemFont::create( "Add/Remove Particle System"),
|
2014-02-10 11:42:15 +08:00
|
|
|
NULL);
|
2014-04-15 18:23:40 +08:00
|
|
|
_itemToggle->setAnchorPoint(Vector2(0.0f, 0.5f));
|
|
|
|
_itemToggle->setPosition(Vector2(origin.x, origin.y + s.height / 2));
|
2014-02-10 11:42:15 +08:00
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
// add decrease & increase menu item
|
2014-02-10 11:42:15 +08:00
|
|
|
MenuItemFont::setFontSize(65);
|
2014-02-20 10:53:49 +08:00
|
|
|
auto decrease = MenuItemFont::create(" - ", [&](Ref *sender) {
|
2014-02-10 11:42:15 +08:00
|
|
|
int idx = _itemToggle->getSelectedIndex();
|
|
|
|
switch (idx) {
|
|
|
|
case 0:
|
|
|
|
removeSprites();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
removeParticles();
|
|
|
|
break;
|
2014-02-12 16:14:04 +08:00
|
|
|
case 2:
|
|
|
|
removeParticleSystem();
|
|
|
|
break;
|
2014-02-10 11:42:15 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2014-04-15 18:23:40 +08:00
|
|
|
decrease->setPosition(Vector2(origin.x + s.width / 2 - 80, origin.y + 80));
|
2014-02-10 11:42:15 +08:00
|
|
|
decrease->setColor(Color3B(0,200,20));
|
2014-02-20 10:53:49 +08:00
|
|
|
auto increase = MenuItemFont::create(" + ", [&](Ref *sender) {
|
2014-02-10 11:42:15 +08:00
|
|
|
int idx = _itemToggle->getSelectedIndex();
|
|
|
|
switch (idx) {
|
|
|
|
case 0:
|
2014-02-12 16:14:04 +08:00
|
|
|
addNewSprites(_spriteStepNum);
|
2014-02-10 11:42:15 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
addParticles();
|
|
|
|
break;
|
2014-02-12 16:14:04 +08:00
|
|
|
case 2:
|
|
|
|
addParticleSystem(_parsysStepNum);
|
|
|
|
break;
|
2014-02-10 11:42:15 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
increase->setColor(Color3B(0,200,20));
|
2014-04-15 18:23:40 +08:00
|
|
|
increase->setPosition(Vector2(origin.x + s.width / 2 + 80, origin.y + 80));
|
2014-02-10 11:42:15 +08:00
|
|
|
|
|
|
|
auto menu = Menu::create(_itemToggle, decrease, increase, NULL);
|
2014-04-15 18:23:40 +08:00
|
|
|
menu->setPosition(Vector2(0.0f, 0.0f));
|
2014-02-10 11:42:15 +08:00
|
|
|
addChild(menu, 10);
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
|
|
|
|
// add tip labels
|
2014-04-09 23:31:05 +08:00
|
|
|
_spriteLabel = Label::createWithTTF("Sprites : 0", "fonts/arial.ttf", 15);
|
2014-04-15 18:23:40 +08:00
|
|
|
_spriteLabel->setAnchorPoint(Vector2(0.0f, 0.5f));
|
2014-02-12 16:14:04 +08:00
|
|
|
addChild(_spriteLabel, 10);
|
2014-04-15 18:23:40 +08:00
|
|
|
_spriteLabel->setPosition(Vector2(origin.x, origin.y + s.height/2 + 70));
|
2014-02-12 16:14:04 +08:00
|
|
|
|
|
|
|
char str[32] = { 0 };
|
2014-02-10 11:42:15 +08:00
|
|
|
sprintf(str, "Particles : %d", _particleNumber);
|
2014-04-09 23:31:05 +08:00
|
|
|
_particleLabel = Label::createWithTTF(str, "fonts/arial.ttf", 15);
|
2014-04-15 18:23:40 +08:00
|
|
|
_particleLabel->setAnchorPoint(Vector2(0.0f, 0.5f));
|
2014-02-10 11:42:15 +08:00
|
|
|
addChild(_particleLabel, 10);
|
2014-04-15 18:23:40 +08:00
|
|
|
_particleLabel->setPosition(Vector2(origin.x, origin.y + s.height/2 + 45));
|
2014-02-12 16:14:04 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
_parsysLabel = Label::createWithTTF("Particle System : 0", "fonts/arial.ttf", 15);
|
2014-04-15 18:23:40 +08:00
|
|
|
_parsysLabel->setAnchorPoint(Vector2(0.0f, 0.5f));
|
2014-02-12 16:14:04 +08:00
|
|
|
addChild(_parsysLabel, 10);
|
2014-04-15 18:23:40 +08:00
|
|
|
_parsysLabel->setPosition(Vector2(origin.x, origin.y + s.height/2 + 20));
|
2014-02-12 16:14:04 +08:00
|
|
|
|
|
|
|
// add sprites
|
|
|
|
addNewSprites(_initSpriteNum);
|
|
|
|
|
|
|
|
// add particle system
|
|
|
|
addParticleSystem(_initParsysNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioTest::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
|
|
|
|
{
|
|
|
|
auto touch = touches[0];
|
|
|
|
|
|
|
|
auto diff = touch->getDelta();
|
|
|
|
auto currentPos1 = _map1->getPosition();
|
|
|
|
_map1->setPosition(currentPos1 + diff);
|
|
|
|
|
|
|
|
auto currentPos2 = _map2->getPosition();
|
|
|
|
_map2->setPosition(currentPos2 + diff);
|
2014-02-10 11:42:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioTest::addParticles()
|
|
|
|
{
|
2014-02-12 16:14:04 +08:00
|
|
|
_particleNumber += _parStepNum;
|
2014-02-10 11:42:15 +08:00
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
for (auto par : _parsysArray) {
|
|
|
|
par->setTotalParticles(_particleNumber);
|
|
|
|
}
|
2014-02-10 11:42:15 +08:00
|
|
|
|
|
|
|
char str[25] = { 0 };
|
|
|
|
sprintf(str, "Particles : %d", _particleNumber);
|
|
|
|
_particleLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioTest::removeParticles()
|
|
|
|
{
|
|
|
|
if (_particleNumber <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
int removeNum = MIN(_particleNumber, _parStepNum);
|
2014-02-10 11:42:15 +08:00
|
|
|
_particleNumber -= removeNum;
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
for (auto par : _parsysArray) {
|
|
|
|
par->setTotalParticles(_particleNumber);
|
|
|
|
}
|
2014-02-10 11:42:15 +08:00
|
|
|
|
|
|
|
char str[25] = { 0 };
|
|
|
|
sprintf(str, "Particles : %d", _particleNumber);
|
|
|
|
_particleLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
void ScenarioTest::addNewSprites(int num)
|
2014-02-10 11:42:15 +08:00
|
|
|
{
|
2014-02-12 16:14:04 +08:00
|
|
|
auto s = Director::getInstance()->getVisibleSize();
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
2014-02-10 11:42:15 +08:00
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
for (int i = 0; i < num; ++i) {
|
2014-02-10 11:42:15 +08:00
|
|
|
int idx = (int)(CCRANDOM_0_1() * 1400.0f / 100.0f);
|
|
|
|
int x = (idx%5) * 85;
|
|
|
|
int y = (idx/5) * 121;
|
|
|
|
|
|
|
|
auto sprite = Sprite::create("Images/grossini_dance_atlas.png", Rect(x,y,85,121) );
|
|
|
|
addChild( sprite );
|
|
|
|
|
|
|
|
float randomx = CCRANDOM_0_1();
|
|
|
|
float randomy = CCRANDOM_0_1();
|
2014-04-15 18:23:40 +08:00
|
|
|
sprite->setPosition(origin + Vector2(randomx * s.width, randomy * s.height));
|
2014-02-10 11:42:15 +08:00
|
|
|
|
|
|
|
ActionInterval* action;
|
|
|
|
float random = CCRANDOM_0_1();
|
|
|
|
|
|
|
|
if( random < 0.20 )
|
|
|
|
action = ScaleBy::create(3, 2);
|
|
|
|
else if(random < 0.40)
|
|
|
|
action = RotateBy::create(3, 360);
|
|
|
|
else if( random < 0.60)
|
|
|
|
action = Blink::create(1, 3);
|
|
|
|
else if( random < 0.8 )
|
|
|
|
action = TintBy::create(2, 0, -255, -255);
|
|
|
|
else
|
|
|
|
action = FadeOut::create(2);
|
|
|
|
auto action_back = action->reverse();
|
|
|
|
auto seq = Sequence::create( action, action_back, NULL );
|
|
|
|
|
|
|
|
sprite->runAction( RepeatForever::create(seq) );
|
|
|
|
|
|
|
|
_spriteArray.pushBack(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[20] = {0};
|
|
|
|
sprintf(str, "Sprites : %d", (int)_spriteArray.size());
|
|
|
|
_spriteLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioTest::removeSprites()
|
|
|
|
{
|
2014-03-20 16:24:55 +08:00
|
|
|
ssize_t number = _spriteArray.size();
|
2014-02-10 11:42:15 +08:00
|
|
|
if (number <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:24:55 +08:00
|
|
|
ssize_t removeNum = MIN(number, _spriteStepNum);
|
2014-02-10 11:42:15 +08:00
|
|
|
for (int i = 0; i < removeNum; ++i) {
|
|
|
|
auto sprite = _spriteArray.getRandomObject();
|
|
|
|
removeChild(sprite);
|
|
|
|
_spriteArray.eraseObject(sprite);
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[20] = {0};
|
|
|
|
sprintf(str, "Sprites : %d", (int)_spriteArray.size());
|
|
|
|
_spriteLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
2014-02-12 16:14:04 +08:00
|
|
|
static const std::string _particleFiles[] = {
|
|
|
|
"Particles/BoilingFoam.plist",
|
|
|
|
"Particles/Galaxy.plist",
|
|
|
|
"Particles/SmallSun.plist",
|
|
|
|
"Particles/lines.plist",
|
|
|
|
"Particles/Comet.plist",
|
|
|
|
"Particles/LavaFlow.plist",
|
|
|
|
"Particles/SpinningPeas.plist",
|
|
|
|
"Particles/Flower.plist",
|
|
|
|
"Particles/Phoenix.plist",
|
|
|
|
"Particles/debian.plist",
|
|
|
|
};
|
|
|
|
void ScenarioTest::addParticleSystem(int num)
|
|
|
|
{
|
|
|
|
int filesSize = sizeof(_particleFiles) / sizeof(std::string);
|
|
|
|
auto s = Director::getInstance()->getVisibleSize();
|
|
|
|
auto origin = Director::getInstance()->getVisibleOrigin();
|
|
|
|
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
float randomIdx = CCRANDOM_0_1();
|
|
|
|
int idx = (filesSize - 1) * randomIdx;
|
|
|
|
std::string fileName = _particleFiles[idx];
|
|
|
|
auto par = ParticleSystemQuad::create(fileName);
|
|
|
|
|
|
|
|
float randomx = CCRANDOM_0_1();
|
|
|
|
float randomy = CCRANDOM_0_1();
|
2014-04-15 18:23:40 +08:00
|
|
|
par->setPosition(origin + Vector2(s.width * randomx, s.height * randomy));
|
2014-02-12 16:14:04 +08:00
|
|
|
par->setTotalParticles(_particleNumber);
|
|
|
|
addChild(par, 9);
|
|
|
|
|
|
|
|
_parsysArray.pushBack(par);
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[40] = {0};
|
|
|
|
sprintf(str, "Particle System : %d", (int)_parsysArray.size());
|
|
|
|
_parsysLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScenarioTest::removeParticleSystem()
|
|
|
|
{
|
2014-03-20 16:24:55 +08:00
|
|
|
ssize_t number = _parsysArray.size();
|
2014-02-12 16:14:04 +08:00
|
|
|
if (number <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-20 16:24:55 +08:00
|
|
|
ssize_t removeNum = MIN(number, _parsysStepNum);
|
2014-02-12 16:14:04 +08:00
|
|
|
for (int i = 0; i < removeNum; ++i) {
|
|
|
|
auto par = _parsysArray.getRandomObject();
|
|
|
|
removeChild(par);
|
|
|
|
_parsysArray.eraseObject(par);
|
|
|
|
}
|
|
|
|
|
|
|
|
char str[40] = {0};
|
|
|
|
sprintf(str, "Particle System : %d", (int)_parsysArray.size());
|
|
|
|
_parsysLabel->setString(str);
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:42:15 +08:00
|
|
|
std::string ScenarioTest::title() const
|
|
|
|
{
|
|
|
|
return "Scenario Performance Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//std::string ScenarioTest::subtitle() const
|
|
|
|
//{
|
|
|
|
// return "See console for results";
|
|
|
|
//}
|
|
|
|
|
|
|
|
Scene* ScenarioTest::scene()
|
|
|
|
{
|
|
|
|
auto scene = Scene::create();
|
|
|
|
ScenarioTest *layer = new ScenarioTest(false, TEST_COUNT, s_nScenarioCurCase);
|
|
|
|
scene->addChild(layer);
|
|
|
|
layer->release();
|
|
|
|
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
|
|
|
|
void runScenarioTest()
|
|
|
|
{
|
|
|
|
s_nScenarioCurCase = 0;
|
|
|
|
auto scene = ScenarioTest::scene();
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
}
|