Search code examples
cocos2d-x

Low FPS issues in cocos2dx


I have finished the following function, It is containing 26 if else block in update. So it reflects little drop in FPS.

I have checked the 26 object using dynamic casting. Depend on shape, i checked the logic.

Here is the coding.

void WagonNode::update(float dt)
{
int found = 0;
for (int32 i = 0; i < kMaxContactPoints100 && found < contactPointCount100_; i++)
{
    ContactPoint100* point = contactPoints100_ + i;
    b2Fixture *otherFix = point->otherFixture;
    if( otherFix )
    {

        found++;
        b2Body *otherBody =  otherFix->GetBody();
        BodyNode *otherNode = (BodyNode*) otherBody->GetUserData();

        if (dynamic_cast<ShapeA*>(otherNode) != NULL)
        {
            tagWord(CCString::create("A"));

        }
        else if (dynamic_cast<ShapeB*>(otherNode) != NULL)
        {
             tagWord(CCString::create("B"));

        }
        else if (dynamic_cast<ShapeC*>(otherNode) != NULL)
        {
            tagWord(CCString::create("C"));
        }
        else if (dynamic_cast<ShapeD*>(otherNode) != NULL)
        {
            tagWord(CCString::create("D"));
        }
        else if (dynamic_cast<ShapeE*>(otherNode) != NULL)
        {
             tagWord(CCString::create("E"));

        }
        else if (dynamic_cast<ShapeF*>(otherNode) != NULL)
        {
            tagWord(CCString::create("F"));
        }
        else if (dynamic_cast<ShapeG*>(otherNode) != NULL)
        {
             tagWord(CCString::create("G"));
        }
        else if (dynamic_cast<ShapeH*>(otherNode) != NULL)
        {

            tagWord(CCString::create("H"));

        }
        else if (dynamic_cast<ShapeI*>(otherNode) != NULL)
        {
            tagWord(CCString::create("I"));

        }
        else if (dynamic_cast<ShapeJ*>(otherNode) != NULL)
        {
             tagWord(CCString::create("J"));

        }
        else if (dynamic_cast<ShapeK*>(otherNode) != NULL)
        {
             tagWord(CCString::create("K"));

        }
        else if (dynamic_cast<ShapeL*>(otherNode) != NULL)
        {
             tagWord(CCString::create("L"));

        }
        else if (dynamic_cast<ShapeM*>(otherNode) != NULL)
        {
            tagWord(CCString::create("M"));

        }

      etc...


    }
}

}

If there is any changes in the above coding, it would help me a lot.

Can any one assist me to handle or stabilise the FPS?


Solution

  • Dynamic casting is slow and might be the problem here. In this case you could try using typeid to resolve the dynamic type:

    example:

        if (typeid (*othernode) == typeid (ShapeA)) 
        {
          tagWord(CCString::create("A"));
        } elseif
        ...
    

    Another thing is, you are creating potentially a lot of strings there, you might want to check if that causes the performance issues. Use a profiler to find out the actual bottleneck.