2012-04-19 14:35:52 +08:00
|
|
|
#include "PerformanceTouchesTest.h"
|
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
// Enable profiles for this file
|
|
|
|
#undef CC_PROFILER_DISPLAY_TIMERS
|
|
|
|
#define CC_PROFILER_DISPLAY_TIMERS() Profiler::getInstance()->displayTimers()
|
|
|
|
#undef CC_PROFILER_PURGE_ALL
|
|
|
|
#define CC_PROFILER_PURGE_ALL() Profiler::getInstance()->releaseAllTimers()
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START
|
|
|
|
#define CC_PROFILER_START(__name__) ProfilingBeginTimingBlock(__name__)
|
|
|
|
#undef CC_PROFILER_STOP
|
|
|
|
#define CC_PROFILER_STOP(__name__) ProfilingEndTimingBlock(__name__)
|
|
|
|
#undef CC_PROFILER_RESET
|
|
|
|
#define CC_PROFILER_RESET(__name__) ProfilingResetTimingBlock(__name__)
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START_CATEGORY
|
|
|
|
#define CC_PROFILER_START_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingBeginTimingBlock(__name__); } while(0)
|
|
|
|
#undef CC_PROFILER_STOP_CATEGORY
|
|
|
|
#define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingEndTimingBlock(__name__); } while(0)
|
|
|
|
#undef CC_PROFILER_RESET_CATEGORY
|
|
|
|
#define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingResetTimingBlock(__name__); } while(0)
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START_INSTANCE
|
|
|
|
#define CC_PROFILER_START_INSTANCE(__id__, __name__) do{ ProfilingBeginTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
#undef CC_PROFILER_STOP_INSTANCE
|
|
|
|
#define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do{ ProfilingEndTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
#undef CC_PROFILER_RESET_INSTANCE
|
|
|
|
#define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do{ ProfilingResetTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
enum
|
|
|
|
{
|
2013-09-15 17:49:36 +08:00
|
|
|
TEST_COUNT = 3,
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int s_nTouchCurCase = 0;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesMainScene
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
void TouchesMainScene::showCurrentTest()
|
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
Layer* layer = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
switch (_curCase)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
case 0:
|
2013-07-23 08:25:44 +08:00
|
|
|
layer = new TouchesPerformTest1(true, TEST_COUNT, _curCase);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
2013-07-23 08:25:44 +08:00
|
|
|
layer = new TouchesPerformTest2(true, TEST_COUNT, _curCase);
|
2012-04-19 14:35:52 +08:00
|
|
|
break;
|
2013-09-15 17:49:36 +08:00
|
|
|
case 2:
|
|
|
|
layer = new TouchesPerformTest3(true, TEST_COUNT, _curCase);
|
|
|
|
break;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
s_nTouchCurCase = _curCase;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
if (layer)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto scene = Scene::create();
|
2013-07-23 08:25:44 +08:00
|
|
|
scene->addChild(layer);
|
|
|
|
layer->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
Director::getInstance()->replaceScene(scene);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchesMainScene::onEnter()
|
|
|
|
{
|
|
|
|
PerformBasicLayer::onEnter();
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// add title
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create(title().c_str(), "Arial", 32);
|
2011-03-25 13:59:08 +08:00
|
|
|
addChild(label, 1);
|
2013-07-12 14:11:55 +08:00
|
|
|
label->setPosition(Point(s.width/2, s.height-50));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-03-25 13:59:08 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_plabel = LabelBMFont::create("00.0", "fonts/arial16.fnt");
|
2013-07-12 14:11:55 +08:00
|
|
|
_plabel->setPosition(Point(s.width/2, s.height/2));
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_plabel);
|
2011-03-25 13:59:08 +08:00
|
|
|
|
|
|
|
elapsedTime = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
numberOfTouchesB = numberOfTouchesM = numberOfTouchesE = numberOfTouchesC = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
void TouchesMainScene::update(float dt)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2011-03-25 13:59:08 +08:00
|
|
|
elapsedTime += dt;
|
|
|
|
|
|
|
|
if ( elapsedTime > 1.0f)
|
|
|
|
{
|
|
|
|
float frameRateB = numberOfTouchesB / elapsedTime;
|
|
|
|
float frameRateM = numberOfTouchesM / elapsedTime;
|
|
|
|
float frameRateE = numberOfTouchesE / elapsedTime;
|
|
|
|
float frameRateC = numberOfTouchesC / elapsedTime;
|
|
|
|
elapsedTime = 0;
|
|
|
|
numberOfTouchesB = numberOfTouchesM = numberOfTouchesE = numberOfTouchesC = 0;
|
|
|
|
|
|
|
|
char str[32] = {0};
|
|
|
|
sprintf(str, "%.1f %.1f %.1f %.1f", frameRateB, frameRateM, frameRateE, frameRateC);
|
2013-06-15 14:03:30 +08:00
|
|
|
_plabel->setString(str);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TouchesMainScene::title()
|
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesPerformTest1
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
void TouchesPerformTest1::onEnter()
|
|
|
|
{
|
|
|
|
TouchesMainScene::onEnter();
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2013-09-15 17:49:36 +08:00
|
|
|
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
setSwallowsTouches(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TouchesPerformTest1::title()
|
|
|
|
{
|
|
|
|
return "Targeted touches";
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool TouchesPerformTest1::onTouchBegan(Touch* touch, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
|
|
|
numberOfTouchesB++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest1::onTouchMoved(Touch* touch, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
|
|
|
numberOfTouchesM++;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest1::onTouchEnded(Touch* touch, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
|
|
|
numberOfTouchesE++;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest1::onTouchCancelled(Touch* touch, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
|
|
|
numberOfTouchesC++;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesPerformTest2
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
2011-03-25 13:59:08 +08:00
|
|
|
void TouchesPerformTest2::onEnter()
|
|
|
|
{
|
|
|
|
TouchesMainScene::onEnter();
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2011-03-25 13:59:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TouchesPerformTest2::title()
|
|
|
|
{
|
|
|
|
return "Standard touches";
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest2::onTouchesBegan(const std::vector<Touch*>& touches, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
numberOfTouchesB += touches.size();
|
2011-03-25 13:59:08 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest2::onTouchesMoved(const std::vector<Touch*>& touches, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
numberOfTouchesM += touches.size();
|
2011-03-25 13:59:08 +08:00
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest2::onTouchesEnded(const std::vector<Touch*>& touches, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
numberOfTouchesE += touches.size();
|
2011-03-25 13:59:08 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TouchesPerformTest2::onTouchesCancelled(const std::vector<Touch*>& touches, Event* event)
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
numberOfTouchesC += touches.size();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesPerformTest3
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class TouchableLayer : public Layer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool onTouchBegan(Touch *touch, Event *event)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual void onTouchMoved(Touch *touch, Event *event) {}
|
|
|
|
virtual void onTouchEnded(Touch *touch, Event *event) {}
|
|
|
|
virtual void onTouchCancelled(Touch *touch, Event *event) {}
|
|
|
|
|
|
|
|
virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *event) {}
|
|
|
|
virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *event) {}
|
|
|
|
virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *event) {}
|
|
|
|
virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *event) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TouchesPerformTest3::onEnter()
|
|
|
|
{
|
|
|
|
PerformBasicLayer::onEnter();
|
2013-09-15 21:56:02 +08:00
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// add title
|
|
|
|
auto label = LabelTTF::create(title().c_str(), "Arial", 32);
|
|
|
|
addChild(label, 1);
|
|
|
|
label->setPosition(Point(s.width/2, s.height-50));
|
|
|
|
|
|
|
|
#define TOUCH_PROFILER_NAME "TouchProfileName"
|
|
|
|
#define TOUCHABLE_NODE_NUM 1000
|
|
|
|
|
2013-09-15 21:56:02 +08:00
|
|
|
srand(time(nullptr));
|
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i)
|
|
|
|
{
|
|
|
|
int zorder = rand() % TOUCHABLE_NODE_NUM;
|
|
|
|
auto layer = new TouchableLayer();
|
|
|
|
layer->setTouchEnabled(true);
|
|
|
|
layer->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
addChild(layer, zorder);
|
|
|
|
layer->release();
|
|
|
|
}
|
|
|
|
|
2013-09-15 21:56:02 +08:00
|
|
|
auto emitEventlabel = LabelTTF::create("Emit Touch Event", "", 24);
|
|
|
|
auto menuItem = MenuItemLabel::create(emitEventlabel, [](Object* sender){
|
2013-09-15 18:26:17 +08:00
|
|
|
|
2013-09-15 21:56:02 +08:00
|
|
|
CC_PROFILER_PURGE_ALL();
|
2013-09-15 18:26:17 +08:00
|
|
|
|
2013-09-15 21:56:02 +08:00
|
|
|
std::vector<Touch*> touches;
|
|
|
|
for (int i = 0; i < TouchEvent::MAX_TOUCHES; ++i)
|
|
|
|
{
|
|
|
|
Touch* touch = new Touch();
|
|
|
|
touch->setTouchInfo(i, 10, (i+1) * 10);
|
|
|
|
touches.push_back(touch);
|
|
|
|
}
|
|
|
|
|
|
|
|
TouchEvent event;
|
|
|
|
event.setEventCode(TouchEvent::EventCode::BEGAN);
|
|
|
|
event.setTouches(touches);
|
|
|
|
|
|
|
|
auto dispatcher = EventDispatcher::getInstance();
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
|
|
|
CC_PROFILER_START(TOUCH_PROFILER_NAME);
|
|
|
|
|
2013-09-18 17:45:25 +08:00
|
|
|
dispatcher->dispatchEvent(&event, false);
|
2013-09-15 21:56:02 +08:00
|
|
|
|
|
|
|
CC_PROFILER_STOP(TOUCH_PROFILER_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_DISPLAY_TIMERS();
|
|
|
|
|
|
|
|
for (auto& touch : touches)
|
|
|
|
{
|
|
|
|
touch->release();
|
|
|
|
}
|
|
|
|
});
|
2013-09-15 17:49:36 +08:00
|
|
|
|
2013-09-15 21:56:02 +08:00
|
|
|
menuItem->setPosition(Point(0, -20));
|
|
|
|
auto menu = Menu::create(menuItem, NULL);
|
|
|
|
addChild(menu);
|
2013-09-15 17:49:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TouchesPerformTest3::title()
|
|
|
|
{
|
|
|
|
return "Touch Event Perf Test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TouchesPerformTest3::showCurrentTest()
|
|
|
|
{
|
|
|
|
Layer* layer = NULL;
|
|
|
|
switch (_curCase)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
layer = new TouchesPerformTest1(true, TEST_COUNT, _curCase);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
layer = new TouchesPerformTest2(true, TEST_COUNT, _curCase);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
layer = new TouchesPerformTest3(true, TEST_COUNT, _curCase);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
s_nTouchCurCase = _curCase;
|
|
|
|
|
|
|
|
if (layer)
|
|
|
|
{
|
|
|
|
auto scene = Scene::create();
|
|
|
|
scene->addChild(layer);
|
|
|
|
layer->release();
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void runTouchesTest()
|
|
|
|
{
|
|
|
|
s_nTouchCurCase = 0;
|
2013-08-16 16:05:27 +08:00
|
|
|
auto scene = Scene::create();
|
|
|
|
auto layer = new TouchesPerformTest1(true, TEST_COUNT, s_nTouchCurCase);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
scene->addChild(layer);
|
|
|
|
layer->release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
Director::getInstance()->replaceScene(scene);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|