2012-04-19 14:35:52 +08:00
|
|
|
#include "PerformanceTouchesTest.h"
|
|
|
|
|
2015-04-09 12:23:47 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
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)
|
|
|
|
|
2015-05-11 16:26:14 +08:00
|
|
|
PerformceTouchesTests::PerformceTouchesTests()
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-05-11 16:26:14 +08:00
|
|
|
ADD_TEST_CASE(TouchesPerformTest1);
|
|
|
|
ADD_TEST_CASE(TouchesPerformTest2);
|
|
|
|
ADD_TEST_CASE(TouchesPerformTest3);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesMainScene
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TouchesMainScene::onEnter()
|
|
|
|
{
|
2015-05-11 16:26:14 +08:00
|
|
|
TestCase::onEnter();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2011-03-25 13:59:08 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
|
2014-03-28 10:28:44 +08:00
|
|
|
_plabel = Label::createWithBMFont("fonts/arial16.fnt","00.0");
|
2014-05-15 01:07:09 +08:00
|
|
|
_plabel->setPosition(Vec2(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TouchesPerformTest1
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
void TouchesPerformTest1::onEnter()
|
|
|
|
{
|
|
|
|
TouchesMainScene::onEnter();
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->setSwallowTouches(true);
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(TouchesPerformTest1::onTouchBegan, this);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(TouchesPerformTest1::onTouchMoved, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(TouchesPerformTest1::onTouchEnded, this);
|
|
|
|
listener->onTouchCancelled = CC_CALLBACK_2(TouchesPerformTest1::onTouchCancelled, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TouchesPerformTest1::title() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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();
|
2013-10-23 16:14:03 +08:00
|
|
|
|
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(TouchesPerformTest2::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(TouchesPerformTest2::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(TouchesPerformTest2::onTouchesEnded, this);
|
|
|
|
listener->onTouchesCancelled = CC_CALLBACK_2(TouchesPerformTest2::onTouchesCancelled, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2011-03-25 13:59:08 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TouchesPerformTest2::title() const
|
2011-03-25 13:59:08 +08:00
|
|
|
{
|
|
|
|
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:
|
2013-10-23 16:14:03 +08:00
|
|
|
bool onTouchBegan(Touch *touch, Event *event)
|
2013-09-15 17:49:36 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-10-23 16:14:03 +08:00
|
|
|
void onTouchMoved(Touch *touch, Event *event) {}
|
|
|
|
void onTouchEnded(Touch *touch, Event *event) {}
|
|
|
|
void onTouchCancelled(Touch *touch, Event *event) {}
|
2013-09-15 17:49:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void TouchesPerformTest3::onEnter()
|
|
|
|
{
|
2015-05-11 16:26:14 +08:00
|
|
|
TestCase::onEnter();
|
2013-09-15 17:49:36 +08:00
|
|
|
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
#define TOUCH_PROFILER_NAME "TouchProfileName"
|
|
|
|
#define TOUCHABLE_NODE_NUM 1000
|
|
|
|
|
2014-03-20 16:24:55 +08:00
|
|
|
srand((unsigned)time(nullptr));
|
2013-09-15 21:56:02 +08:00
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i)
|
|
|
|
{
|
|
|
|
int zorder = rand() % TOUCHABLE_NODE_NUM;
|
2014-08-28 07:31:57 +08:00
|
|
|
auto layer = new (std::nothrow) TouchableLayer();
|
2013-10-23 16:14:03 +08:00
|
|
|
|
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(TouchableLayer::onTouchBegan, layer);
|
|
|
|
listener->onTouchMoved = CC_CALLBACK_2(TouchableLayer::onTouchMoved, layer);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(TouchableLayer::onTouchEnded, layer);
|
|
|
|
listener->onTouchCancelled = CC_CALLBACK_2(TouchableLayer::onTouchCancelled, layer);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer);
|
2013-10-23 16:14:03 +08:00
|
|
|
|
2013-09-15 17:49:36 +08:00
|
|
|
addChild(layer, zorder);
|
|
|
|
layer->release();
|
|
|
|
}
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto emitEventlabel = Label::createWithSystemFont("Emit Touch Event", "", 24);
|
2014-02-20 10:53:49 +08:00
|
|
|
auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Ref* 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;
|
2013-09-20 19:19:31 +08:00
|
|
|
for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i)
|
2013-09-15 21:56:02 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Touch* touch = new (std::nothrow) Touch();
|
2013-09-15 21:56:02 +08:00
|
|
|
touch->setTouchInfo(i, 10, (i+1) * 10);
|
|
|
|
touches.push_back(touch);
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
EventTouch event;
|
|
|
|
event.setEventCode(EventTouch::EventCode::BEGAN);
|
2013-09-15 21:56:02 +08:00
|
|
|
event.setTouches(touches);
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i)
|
|
|
|
{
|
|
|
|
CC_PROFILER_START(TOUCH_PROFILER_NAME);
|
|
|
|
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->dispatchEvent(&event);
|
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
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
menuItem->setPosition(Vec2(0, -20));
|
2014-07-10 00:45:27 +08:00
|
|
|
auto menu = Menu::create(menuItem, nullptr);
|
2013-09-15 21:56:02 +08:00
|
|
|
addChild(menu);
|
2013-09-15 17:49:36 +08:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:52:10 +08:00
|
|
|
std::string TouchesPerformTest3::title() const
|
2013-09-15 17:49:36 +08:00
|
|
|
{
|
|
|
|
return "Touch Event Perf Test";
|
|
|
|
}
|