void CustomEventDispatchingPerfTest::generateTestFunctions()
{
    TestFunction testFunctions[] = {
        { "custom-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerCustom::create("custom_event_test_scenegraph", [](EventCustom* event){});
                
                // Create new nodes listen to custom event
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            EventCustom event("custom_event_test_scenegraph");
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&event);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "custom-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerCustom::create("custom_event_test_fixed", [](EventCustom* event){});
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            EventCustom event("custom_event_test_fixed");
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&event);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
    };
    
    for (const auto& func : testFunctions)
    {
        _testFunctions.push_back(func);
    }
}
////////////////////////////////////////////////////////
//
// RemoveSpriteSheet
//
////////////////////////////////////////////////////////
void RemoveSpriteSheet::update(float dt)
{
    //srandom(0);

    // 100 percent
    int totalToAdd = currentQuantityOfNodes * 1;

    if( totalToAdd > 0 )
    {
        Sprite **sprites = new Sprite*[totalToAdd];

        // Don't include the sprite creation time as part of the profiling
        for(int i=0;i<totalToAdd;i++)
        {
            sprites[i] = Sprite::createWithTexture(batchNode->getTexture(), Rect(0,0,32,32));
        }

        // add them with random Z (very important!)
        for( int i=0; i < totalToAdd;i++ )
        {
            batchNode->addChild( sprites[i], CCRANDOM_MINUS1_1() * 50, kTagBase+i);
        }

        // remove them
        CC_PROFILER_START( this->profilerName() );
        for( int i=0;i <  totalToAdd;i++)
        {
            batchNode->removeChild( sprites[i], true);
        }
        CC_PROFILER_STOP( this->profilerName() );

        delete [] sprites;
    }
}
Esempio n. 3
0
void Label::onDraw()
{
    CC_PROFILER_START("Label - draw");

    // Optimization: Fast Dispatch
    if( _batchNodes.size() == 1 && _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    CC_NODE_DRAW_SETUP();

    if (_useDistanceField && _currLabelEffect != LabelEffect::NORMAL)
    {
        _shaderProgram->setUniformLocationWith3f(_uniformEffectColor, _effectColor.r/255.0f,_effectColor.g/255.0f,_effectColor.b/255.0f);
    }

    for(const auto &child: _children)
    {
        child->updateTransform();
    }

    GL::blendFunc( _blendFunc.src, _blendFunc.dst );

    for (const auto& batchNode:_batchNodes)
    {
        batchNode->getTextureAtlas()->drawQuads();
    }

    CC_PROFILER_STOP("Label - draw");
}
Esempio n. 4
0
void Label::onDraw(const Mat4& transform, bool transformUpdated)
{
    CC_PROFILER_START("Label - draw");

    // Optimization: Fast Dispatch
    if( _batchNodes.size() == 1 && _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    auto glprogram = getGLProgram();
    glprogram->use();
    GL::blendFunc( _blendFunc.src, _blendFunc.dst );

    if(_shadowEnabled && _shadowBlurRadius <= 0)
    {
        glprogram->setUniformLocationWith4f(_uniformTextColor,
            _shadowColor.r, _shadowColor.g, _shadowColor.b, _shadowColor.a);
        glprogram->setUniformLocationWith4f(_uniformEffectColor,
            _shadowColor.r, _shadowColor.g, _shadowColor.b, _shadowColor.a);

        getGLProgram()->setUniformsForBuiltins(_shadowTransform);
        for (const auto &child : _children)
        {
            child->updateTransform();
        }
        for (const auto& batchNode : _batchNodes)
        {
            batchNode->getTextureAtlas()->drawQuads();
        }
    }

    if (_currentLabelType == LabelType::TTF)
    {
        glprogram->setUniformLocationWith4f(_uniformTextColor,
            _textColorF.r, _textColorF.g, _textColorF.b, _textColorF.a);
    }

    if (_currLabelEffect == LabelEffect::OUTLINE || _currLabelEffect == LabelEffect::GLOW)
    {
        glprogram->setUniformLocationWith4f(_uniformEffectColor,
            _effectColorF.r, _effectColorF.g, _effectColorF.b, _effectColorF.a);
    }

    glprogram->setUniformsForBuiltins(transform);

    for(const auto &child: _children)
    {
        if(child->getTag() >= 0)
            child->updateTransform();
    }

    for (const auto& batchNode:_batchNodes)
    {
        batchNode->getTextureAtlas()->drawQuads();
    }

    CC_PROFILER_STOP("Label - draw");
}
Esempio n. 5
0
void XSpriteBatchNode::draw()
{
	XFUNC_START();
	CC_PROFILER_START("XSpriteBatchNode - draw");

	if (m_pobTextureAtlas->getTotalQuads() == 0)
	{
		return;
	}
	do 
	{
		ccGLEnable(m_eGLServerState);
		if (m_pShaderKTX)
		{
			CCGLProgram* pProg = m_pShaderKTX->GetProgram();
			if (pProg)
			{
				pProg->use();
				pProg->setUniformsForBuiltins();
			}
		}
		else
		{
			if (getShaderProgram())
			{
				getShaderProgram()->use();
				getShaderProgram()->setUniformsForBuiltins();
			}			
		}
		if(m_pShaderOnce)
		{
			m_pShaderOnce->use();
			m_pShaderOnce->setUniformsForBuiltins();
			if(m_pShaderOnce->m_pShader)
				m_pShaderOnce->m_pShader->RunBeforeDraw();
			m_pShaderOnce = NULL;
		}
		//else if(m_pobTextureAtlas->getTexture())
		//{
		//	static XShaderBloom stShaderBloom;
		//	stShaderBloom.UseShader();
		//	stShaderBloom.SetTexSize(SCRPIX_W, SCRPIX_H);
		//}
	} while (0);
	arrayMakeObjectsPerformSelector(m_pChildren, updateTransform, CCSprite*);
	ccGLBlendFunc(m_blendFunc.src, m_blendFunc.dst);
	if (m_pTexKtxAlpha)
	{
		ccGLBindTexture2DN(1, m_pTexKtxAlpha->getName());
		if (m_pShaderKTX)
		{
			m_pShaderKTX->RunBeforeDraw();
		}	
	}
	m_pobTextureAtlas->drawQuads();
	CC_PROFILER_STOP("XSpriteBatchNode - draw");
	
	XFUNC_END();
}
void InvokeStdFunctionPerfTest::onUpdate(float dt)
{
    CC_PROFILER_START(_profileName.c_str());
    for (int i = 0; i < LOOP_COUNT; ++i)
    {
        _callback(dt);
    }
    CC_PROFILER_STOP(_profileName.c_str());
}
void InvokeMemberFunctionPerfTest::onUpdate(float dt)
{
    CC_PROFILER_START(_profileName.c_str());
    for (int i = 0; i < LOOP_COUNT; ++i)
    {
        (_target->*_selector)(dt);
    }
    CC_PROFILER_STOP(_profileName.c_str());
}
void SimulateNewSchedulerCallbackPerfTest::onUpdate(float dt)
{
    CC_PROFILER_START(_profileName.c_str());
    for (int i = 0; i < LOOP_COUNT; ++i)
    {
        _callback(dt);
    }
    CC_PROFILER_STOP(_profileName.c_str());
}
void VisitSceneGraph::update(float dt)
{
    CC_PROFILER_START( this->profilerName() );
    this->visit();
    CC_PROFILER_STOP( this->profilerName() );

    // Call `Renderer::clean` to prevent crash if current scene is destroyed.
    // The render commands associated with current scene should be cleaned.
    Director::getInstance()->getRenderer()->clean();
}
Esempio n. 10
0
void ParticleBatchNode::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
    CC_PROFILER_START("CCParticleBatchNode - draw");

    if( _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }
    _batchCommand.init(_globalZOrder, getGLProgram(), _blendFunc, _textureAtlas, _modelViewTransform, flags);
    renderer->addCommand(&_batchCommand);
    CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
void NodeCreateTest::update(float dt)
{
    // iterate using fast enumeration protocol

    Node **nodes = new Node*[quantityOfNodes];

    CC_PROFILER_START(this->profilerName());
    for( int i=0; i<quantityOfNodes; ++i)
        nodes[i] = Node::create();
    CC_PROFILER_STOP(this->profilerName());

    delete [] nodes;
}
////////////////////////////////////////////////////////
//
// CallFuncsSpriteSheetForEach
//
////////////////////////////////////////////////////////
void CallFuncsSpriteSheetForEach::update(float dt)
{
    // iterate using fast enumeration protocol
    auto& children = batchNode->getChildren();

    CC_PROFILER_START(this->profilerName());

    std::for_each(std::begin(children), std::end(children), [](Node* obj) {
        obj->getPosition();
    });

    CC_PROFILER_STOP(this->profilerName());
}
////////////////////////////////////////////////////////
//
// IterateSpriteSheetForEach
//
////////////////////////////////////////////////////////
void IterateSpriteSheetForEach::update(float dt)
{
    // iterate using fast enumeration protocol
    auto& children = batchNode->getChildren();

    CC_PROFILER_START(this->profilerName());

    std::for_each(std::begin(children), std::end(children), [](Node *child) {
        auto sprite = static_cast<Sprite*>(child);
        sprite->setVisible(false);
    });

    CC_PROFILER_STOP(this->profilerName());
}
////////////////////////////////////////////////////////
//
// IterateSpriteSheetIterator
//
////////////////////////////////////////////////////////
void IterateSpriteSheetIterator::update(float dt)
{
    // iterate using fast enumeration protocol
    auto& children = batchNode->getChildren();

    CC_PROFILER_START(this->profilerName());

    for( auto it=std::begin(children); it != std::end(children); ++it)
    {
        auto sprite = static_cast<Sprite*>(*it);
        sprite->setVisible(false);
    }

    CC_PROFILER_STOP(this->profilerName());
}
void SpriteCreateTest::update(float dt)
{
    // iterate using fast enumeration protocol

    Sprite **sprites = new Sprite*[quantityOfNodes];

    Sprite::create("Images/grossini.png");
    
    CC_PROFILER_START(this->profilerName());
    for( int i=0; i<quantityOfNodes; ++i)
        sprites[i] = Sprite::create("Images/grossini.png");
    CC_PROFILER_STOP(this->profilerName());

    delete [] sprites;
}
Esempio n. 16
0
void Label::onDraw(const kmMat4& transform, bool transformUpdated)
{
    CC_PROFILER_START("Label - draw");

    // Optimization: Fast Dispatch
    if( _batchNodes.size() == 1 && _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    _shaderProgram->use();
    GL::blendFunc( _blendFunc.src, _blendFunc.dst );
    bool trans = false;

    if (_currLabelEffect == LabelEffect::OUTLINE)
    {
         _shaderProgram->setUniformLocationWith4f(_uniformEffectColor, _outlineColor.r/255.0f,_outlineColor.g/255.0f,_outlineColor.b/255.0f,_outlineColor.a/255.0f);
    }
    else if (_currLabelEffect == LabelEffect::GLOW)
    {
        _shaderProgram->setUniformLocationWith3f(_uniformEffectColor, _effectColor.r/255.0f,_effectColor.g/255.0f,_effectColor.b/255.0f);
    }
    else if(_currLabelEffect == LabelEffect::SHADOW && _shadowBlurRadius <= 0)
    {
        trans = true;
        drawShadowWithoutBlur();
    }

    _shaderProgram->setUniformsForBuiltins(transform);
   
    for(const auto &child: _children)
    {
        if(child->getTag() >= 0)
            child->updateTransform();
    }  

    for (const auto& batchNode:_batchNodes)
    {
        batchNode->getTextureAtlas()->drawQuads();
    }

    if (trans)
    {
        kmGLPopMatrix();
    }    

    CC_PROFILER_STOP("Label - draw");
}
////////////////////////////////////////////////////////
//
// IterateSpriteSheetForLoop
//
////////////////////////////////////////////////////////
void IterateSpriteSheetForLoop::update(float dt)
{
    // iterate using fast enumeration protocol
    auto& children = batchNode->getChildren();

    CC_PROFILER_START(this->profilerName());

    for( const auto &object : children )
    {
        auto o = static_cast<Ref*>(object);
        auto sprite = static_cast<Sprite*>(o);
        sprite->setVisible(false);
    }

    CC_PROFILER_STOP(this->profilerName());
}
Esempio n. 18
0
void CCParticleBatchNode::draw(void)
{
    CC_PROFILER_START("CCParticleBatchNode - draw");

    if( m_pTextureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    CC_NODE_DRAW_SETUP();

    ccGLBlendFunc( m_tBlendFunc.src, m_tBlendFunc.dst );

    m_pTextureAtlas->drawQuads();

    CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
Esempio n. 19
0
void ParticleBatchNode::draw(void)
{
    CC_PROFILER_START("CCParticleBatchNode - draw");

    if( _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    CC_NODE_DRAW_SETUP();

    GL::blendFunc( _blendFunc.src, _blendFunc.dst );

    _textureAtlas->drawQuads();

    CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
void SpriteDeallocTest::update(float dt)
{
    // iterate using fast enumeration protocol

    Sprite **sprites = new Sprite*[quantityOfNodes];

    for( int i=0; i<quantityOfNodes; ++i) {
        sprites[i] = new Sprite;
        sprites[i]->init();
    }

    CC_PROFILER_START(this->profilerName());
    for( int i=0; i<quantityOfNodes; ++i)
        sprites[i]->release();
    CC_PROFILER_STOP(this->profilerName());

    delete [] sprites;
}
void ParticleBatchNode::draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
{
    CC_PROFILER_START("CCParticleBatchNode - draw");

    if( _textureAtlas->getTotalQuads() == 0 )
    {
        return;
    }

    _batchCommand.init(
                       _globalZOrder,
                       _shaderProgram,
                       _blendFunc,
                       _textureAtlas,
                       _modelViewTransform);
    renderer->addCommand(&_batchCommand);
    CC_PROFILER_STOP("CCParticleBatchNode - draw");
}
////////////////////////////////////////////////////////
//
// GetSpriteSheet
//
////////////////////////////////////////////////////////
void GetSpriteSheet::update(float dt)
{
    // reset seed
    //srandom(0);

    // 100% percent
    int totalToAdd = currentQuantityOfNodes * 1;

    if( totalToAdd > 0 )
    {
        Sprite **sprites = new Sprite*[totalToAdd];
        int *zs = new int[totalToAdd];

        // Don't include the sprite creation time and random as part of the profiling
        for(int i=0; i<totalToAdd; i++)
        {
            sprites[i] = Sprite::createWithTexture(batchNode->getTexture(), Rect(0,0,32,32));
            zs[i]      = CCRANDOM_MINUS1_1() * 50;
        }

        for( int i=0; i < totalToAdd;i++ )
        {
            batchNode->addChild( sprites[i], zs[i], kTagBase+i);
        }

        batchNode->sortAllChildren();

        CC_PROFILER_START( this->profilerName() );
        for( int i=0; i < totalToAdd;i++ )
        {
            batchNode->getChildByTag(kTagBase+1);
        }
        CC_PROFILER_STOP(this->profilerName());

        // remove them
        for( int i=0;i <  totalToAdd;i++)
        {
            batchNode->removeChild( sprites[i], true);
        }

        delete [] sprites;
        delete [] zs;
    }
}
Esempio n. 23
0
void CABatchView::draw(void)
{
    CC_PROFILER_START("CABatchView - draw");

    CC_RETURN_IF(!m_pobImageAtlas);
    CC_RETURN_IF(m_pobImageAtlas->getTotalQuads() == 0);
    
    CC_NODE_DRAW_SETUP();

    CAVector<CAView*>::const_iterator itr;
    for (itr=m_obSubviews.begin(); itr!=m_obSubviews.end(); itr++)
        (*itr)->updateTransform();

    ccGLBlendFunc( m_blendFunc.src, m_blendFunc.dst );

    m_pobImageAtlas->drawQuads();

    CC_PROFILER_STOP("CABatchView - draw");
}
void IsometryNode::draw(void)
{
    CC_PROFILER_START("CCSpriteBatchNode - draw");
    
    // Optimization: Fast Dispatch
    /*if( m_pobTextureAtlas->getTotalQuads() == 0 )
    {
        return;
    }*/
    
    CC_NODE_DRAW_SETUP();
    
    arrayMakeObjectsPerformSelector(m_pChildren, updateTransform, CCSprite*);
    
    //ccGLBlendFunc( m_blendFunc.src, m_blendFunc.dst );
    
    //m_pobTextureAtlas->drawQuads();
    Process();
    
    CC_PROFILER_STOP("CCSpriteBatchNode - draw");
}
void ArrayPerfTest::generateTestFunctions()
{
    auto createArray = [this](){
        __Array* ret = Array::create();
        
        for( int i=0; i<quantityOfNodes; ++i)
        {
            auto node = Node::create();
            node->setTag(i);
            ret->addObject(node);
        }
        return ret;
    };
    
    TestFunction testFunctions[] = {
        { "addObject",    [=](){
            __Array* nodeVector = Array::create();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->addObject(Node::create());
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "insertObject",      [=](){
            __Array* nodeVector = Array::create();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->insertObject(Node::create(), 0);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "setObject",     [=](){
            __Array* nodeVector = createArray();
            
            srand((unsigned)time(nullptr));
            ssize_t index = rand() % quantityOfNodes;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->setObject(Node::create(), index);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "getIndexOfObject",    [=](){
            __Array* nodeVector = createArray();
            Ref* objToGet = nodeVector->getObjectAtIndex(quantityOfNodes/3);
            ssize_t index = 0;
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                index = nodeVector->getIndexOfObject(objToGet);
            CC_PROFILER_STOP(this->profilerName());
            // Uses `index` to avoids `getIndex` invoking was optimized in release mode
            if (index == quantityOfNodes/3)
            {
                nodeVector->removeAllObjects();
            }
        } } ,
        { "getObjectAtIndex",          [=](){
            __Array* nodeVector = createArray();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->getObjectAtIndex(quantityOfNodes/3);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "containsObject",    [=](){
            __Array* nodeVector = createArray();
            Ref* objToGet = nodeVector->getObjectAtIndex(quantityOfNodes/3);
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->containsObject(objToGet);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "removeObject", [=](){
            __Array* nodeVector = createArray();
            Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
            
            for (int i = 0; i < quantityOfNodes; ++i)
            {
                nodes[i] = static_cast<Node*>(nodeVector->getObjectAtIndex(i));
            }
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->removeObject(nodes[i]);
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
            
            free(nodes);
        } } ,
        { "removeObjectAtIndex",       [=](){
            __Array* nodeVector = createArray();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->removeObjectAtIndex(0);
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
            
        } } ,
        { "removeAllObjects",       [=](){
            __Array* nodeVector = createArray();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->removeAllObjects();
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
        } } ,
        { "swap by index",        [=](){
            __Array* nodeVector = createArray();
            
            int swapIndex1 = quantityOfNodes / 3;
            int swapIndex2 = quantityOfNodes / 3 * 2;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->swap(swapIndex1, swapIndex2);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "swap by object",        [=](){
            __Array* nodeVector = createArray();
            
            Ref* swapNode1 = nodeVector->getObjectAtIndex(quantityOfNodes / 3);
            Ref* swapNode2 = nodeVector->getObjectAtIndex(quantityOfNodes / 3 * 2);
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->exchangeObject(swapNode1, swapNode2);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "reverseObjects",     [=](){
            __Array* nodeVector = createArray();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector->reverseObjects();
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "CCARRAY_FOREACH",     [=](){
            __Array* nodeVector = createArray();
            Ref* obj;
            CC_PROFILER_START(this->profilerName());
            
            CCARRAY_FOREACH(nodeVector, obj)
            {
                static_cast<Node*>(obj)->setTag(111);
            }
            CC_PROFILER_STOP(this->profilerName());
        } } ,
void KeyboardEventDispatchingPerfTest::generateTestFunctions()
{
    TestFunction testFunctions[] = {
        { "keyboard-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerKeyboard::create();
                listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){};
                listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){};
                
                // Create new nodes listen to keyboard event
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            EventKeyboard event(EventKeyboard::KeyCode::KEY_RETURN, true);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&event);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "keyboard-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerKeyboard::create();
                listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event){};
                listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event){};
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            EventKeyboard event(EventKeyboard::KeyCode::KEY_RETURN, true);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&event);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
    };
    
    for (const auto& func : testFunctions)
    {
        _testFunctions.push_back(func);
    }
}
void TouchEventDispatchingPerfTest::generateTestFunctions()
{
    TestFunction testFunctions[] = {
        { "OneByOne-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchOneByOne::create();
                listener->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listener->onTouchMoved = [](Touch* touch, Event* event){};
                listener->onTouchEnded = [](Touch* touch, Event* event){};

                // Create new touchable nodes
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;

            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);

            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "OneByOne-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchOneByOne::create();
                listener->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listener->onTouchMoved = [](Touch* touch, Event* event){};
                listener->onTouchEnded = [](Touch* touch, Event* event){};
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "AllAtOnce-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchAllAtOnce::create();
                listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                // Create new touchable nodes
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listener->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "AllAtOnce-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listener = EventListenerTouchAllAtOnce::create();
                listener->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listener->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                for (int i = 0; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listener->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "TouchModeMix-scenegraph",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listenerOneByOne = EventListenerTouchOneByOne::create();
                listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){};
                listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){};
                
                auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create();
                listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                int i = 0;
                // Create new touchable nodes
                for (; i < this->_quantityOfNodes/2; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listenerOneByOne->clone(), node);
                }
                
                for (; i < this->_quantityOfNodes; ++i)
                {
                    auto node = Node::create();
                    node->setTag(1000 + i);
                    this->addChild(node);
                    this->_nodes.push_back(node);
                    dispatcher->addEventListenerWithSceneGraphPriority(listenerAllAtOnce->clone(), node);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "TouchModeMix-fixed",    [=](){
            auto dispatcher = Director::getInstance()->getEventDispatcher();
            if (_quantityOfNodes != _lastRenderedCount)
            {
                auto listenerOneByOne = EventListenerTouchOneByOne::create();
                listenerOneByOne->onTouchBegan = [](Touch* touch, Event* event){
                    return false;
                };
                
                listenerOneByOne->onTouchMoved = [](Touch* touch, Event* event){};
                listenerOneByOne->onTouchEnded = [](Touch* touch, Event* event){};
                
                auto listenerAllAtOnce = EventListenerTouchAllAtOnce::create();
                listenerAllAtOnce->onTouchesBegan = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesMoved = [](const std::vector<Touch*> touches, Event* event){};
                listenerAllAtOnce->onTouchesEnded = [](const std::vector<Touch*> touches, Event* event){};
                
                int i = 0;

                for (; i < this->_quantityOfNodes/2; ++i)
                {
                    auto l = listenerOneByOne->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                for (; i < this->_quantityOfNodes; ++i)
                {
                    auto l = listenerAllAtOnce->clone();
                    this->_fixedPriorityListeners.push_back(l);
                    dispatcher->addEventListenerWithFixedPriority(l, i+1);
                }
                
                _lastRenderedCount = _quantityOfNodes;
            }
            
            Size size = Director::getInstance()->getWinSize();
            EventTouch touchEvent;
            touchEvent.setEventCode(EventTouch::EventCode::BEGAN);
            std::vector<Touch*> touches;
            
            for (int i = 0; i < 4; ++i)
            {
                Touch* touch = new (std::nothrow) Touch();
                touch->autorelease();
                touch->setTouchInfo(i, rand() % 200, rand() % 200);
                touches.push_back(touch);
            }
            touchEvent.setTouches(touches);
            
            CC_PROFILER_START(this->profilerName());
            dispatcher->dispatchEvent(&touchEvent);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
    };
    
    for (const auto& func : testFunctions)
    {
        _testFunctions.push_back(func);
    }
}
void VisitSceneGraph::update(float dt)
{
    CC_PROFILER_START( this->profilerName() );
    this->visit();
    CC_PROFILER_STOP( this->profilerName() );
}
void TemplateVectorPerfTest::generateTestFunctions()
{
    auto createVector = [this](){
        Vector<Node*> ret;
        
        for( int i=0; i<quantityOfNodes; ++i)
        {
            auto node = Node::create();
            node->setTag(i);
            ret.pushBack(node);
        }
        return ret;
    };
    
    TestFunction testFunctions[] = {
        { "pushBack",    [=](){
            Vector<Node*> nodeVector;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.pushBack(Node::create());
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "insert",      [=](){
            Vector<Node*> nodeVector;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.insert(0, Node::create());
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "replace",     [=](){
            Vector<Node*> nodeVector = createVector();
            
            srand((unsigned)time(nullptr));
            ssize_t index = rand() % quantityOfNodes;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.replace(index, Node::create());
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        { "getIndex",    [=](){
            
            Vector<Node*> nodeVector = createVector();
            Node* objToGet = nodeVector.at(quantityOfNodes/3);
            ssize_t index = 0;
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                index = nodeVector.getIndex(objToGet);
            CC_PROFILER_STOP(this->profilerName());
            
            // Uses `index` to avoids `getIndex` invoking was optimized in release mode
            if (index == quantityOfNodes/3)
            {
                nodeVector.clear();
            }
        } } ,
        { "find",        [=](){
            Vector<Node*> nodeVector = createVector();
            Node* objToGet = nodeVector.at(quantityOfNodes/3);
            Vector<Node*>::iterator iter;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                iter = nodeVector.find(objToGet);
            CC_PROFILER_STOP(this->profilerName());
            
            // Uses `iter` to avoids `find` invoking was optimized in release mode
            if (*iter == objToGet)
            {
                nodeVector.clear();
            }
            
        } } ,
        { "at",          [=](){
            Vector<Node*> nodeVector = createVector();
            Node* objToGet = nullptr;
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                objToGet = nodeVector.at(quantityOfNodes/3);
            CC_PROFILER_STOP(this->profilerName());
            
            // Uses `objToGet` to avoids `at` invoking was optimized in release mode
            if (nodeVector.getIndex(objToGet) == quantityOfNodes/3)
            {
                nodeVector.clear();
            }
        } } ,
        { "contains",    [=](){
            Vector<Node*> nodeVector = createVector();
            Node* objToGet = nodeVector.at(quantityOfNodes/3);
            
            bool ret = false;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                ret = nodeVector.contains(objToGet);
            CC_PROFILER_STOP(this->profilerName());
            
            // Uses `ret` to avoids `contains` invoking was optimized in release mode
            if (ret)
            {
                nodeVector.clear();
            }
        } } ,
        { "eraseObject", [=](){
            Vector<Node*> nodeVector = createVector();
            Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
            
            for (int i = 0; i < quantityOfNodes; ++i)
            {
                nodes[i] = nodeVector.at(i);
            }
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.eraseObject(nodes[i]);
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector.empty(), "nodeVector was not empty.");
            
            free(nodes);
        } } ,
        { "erase",       [=](){
            Vector<Node*> nodeVector = createVector();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.erase(nodeVector.begin());
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector.empty(), "nodeVector was not empty.");

        } } ,
        { "clear",       [=](){
            Vector<Node*> nodeVector = createVector();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.clear();
            CC_PROFILER_STOP(this->profilerName());
            
            CCASSERT(nodeVector.empty(), "nodeVector was not empty.");
        } } ,
        { "swap by index",        [=](){
            Vector<Node*> nodeVector = createVector();
            
            int swapIndex1 = quantityOfNodes / 3;
            int swapIndex2 = quantityOfNodes / 3 * 2;
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.swap(swapIndex1, swapIndex2);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "swap by object",        [=](){
            Vector<Node*> nodeVector = createVector();
            
            Node* swapNode1 = nodeVector.at(quantityOfNodes / 3);
            Node* swapNode2 = nodeVector.at(quantityOfNodes / 3 * 2);
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.swap(swapNode1, swapNode2);
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "reverse",     [=](){
            Vector<Node*> nodeVector = createVector();
            
            CC_PROFILER_START(this->profilerName());
            for( int i=0; i<quantityOfNodes; ++i)
                nodeVector.reverse();
            CC_PROFILER_STOP(this->profilerName());
        } } ,
        
        { "c++11 Range Loop",     [=](){
            Vector<Node*> nodeVector = createVector();
            
            CC_PROFILER_START(this->profilerName());
            for (const auto& e : nodeVector)
            {
                e->setTag(111);
            }
            CC_PROFILER_STOP(this->profilerName());
        } } ,
    };
    
    for (const auto& func : testFunctions)
    {
        _testFunctions.push_back(func);
    }
}
Esempio n. 30
0
void TouchesPerformTest3::onEnter()
{
    PerformBasicLayer::onEnter();
    
    
    auto s = Director::getInstance()->getWinSize();
    
    // add title
    auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32);
    addChild(label, 1);
    label->setPosition(Vec2(s.width/2, s.height-50));
    
#define TOUCH_PROFILER_NAME  "TouchProfileName"
#define TOUCHABLE_NODE_NUM 1000
    
    srand((unsigned)time(nullptr));
    
    for (int i = 0; i < TOUCHABLE_NODE_NUM; ++i)
    {
        int zorder = rand() % TOUCHABLE_NODE_NUM;
        auto layer = new (std::nothrow) TouchableLayer();
        
        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);
        _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, layer);
        
        addChild(layer, zorder);
        layer->release();
    }
    
    auto emitEventlabel = Label::createWithSystemFont("Emit Touch Event", "", 24);
    auto menuItem = MenuItemLabel::create(emitEventlabel, [this](Ref* sender){
        
        CC_PROFILER_PURGE_ALL();
        
        std::vector<Touch*> touches;
        for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i)
        {
            Touch* touch = new (std::nothrow) Touch();
            touch->setTouchInfo(i, 10, (i+1) * 10);
            touches.push_back(touch);
        }
        
        EventTouch event;
        event.setEventCode(EventTouch::EventCode::BEGAN);
        event.setTouches(touches);
        
        for (int i = 0; i < 100; ++i)
        {
            CC_PROFILER_START(TOUCH_PROFILER_NAME);
            
            _eventDispatcher->dispatchEvent(&event);
            
            CC_PROFILER_STOP(TOUCH_PROFILER_NAME);
        }
        
        CC_PROFILER_DISPLAY_TIMERS();
        
        for (auto& touch : touches)
        {
            touch->release();
        }
    });
    
    menuItem->setPosition(Vec2(0, -20));
    auto menu = Menu::create(menuItem, nullptr);
    addChild(menu);
}