Search code examples
c++cocos2d-x

what is the point of the bool bRet do while loop in cocos2d-X?


I'm a noob to Cocos2D-X, but I've been programming for quite some time... I was wondering what the point of this code is:

my confusion is mostly with this part:

bool bRet = false; do { } while(0)

here is the whole method to give some context:

  bool GameScene::init()
    {
        CCLog("GameScene::init");
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////
        CC_BREAK_IF(! CCLayer::init());

            // Initialize the parent - gets the sprite sheet loaded, sets the background and inits the clouds
            MainScene::init();

            // Start off as game suspended
            gameSuspended = true;

            // Get the bird sprite
            CCSprite *bird = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("bird.png"));
            this->addChild(bird, 4, kBird);

            // Initialize the platforms
            initPlatforms();

            // Create the bonus sprite
            CCSprite *bonus;

            // Load in the bonus images, 5, 10, 50, 100
            for(int i=0; i<kNumBonuses; i++) 
            {
                    bonus = CCSprite::createWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(bonus_image[i]));
                    this->addChild(bonus,4, kBonusStartTag+i);
                    bonus->setVisible(false);
            }

            // Create the Score Label
            CCLabelBMFont* scoreLabel = CCLabelBMFont::labelWithString("0",  "Images/bitmapFont.fnt");
            this->addChild(scoreLabel, 5, kScoreLabel);

            // Center the label
            scoreLabel->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width/2,CCDirector::sharedDirector()->getWinSize().height - 50));

            // Start the GameScene stepping
            schedule(schedule_selector(GameScene::step));

            // Enable the touch events
            setTouchEnabled(true);
            // Enable accelerometer events
            setAccelerometerEnabled(true);

            // Start the game
            startGame();

    bRet = true;
} while (0);

return bRet;
}

this code comes from: https://code.google.com/p/tweejump-cocos2dx/source/browse/trunk/Classes/GameScene.cpp

it's an open source game.

I understand that bRet stands for bool return value, but I'm confused on a few things... One reason I'm confused by this is why even program like this? secondly how does the while loop know when bRet == false if it's just equal to 0... am I missing something?

My other question is how do you know when to use the syntax CCdataType* varName = ..., vs. CCdataType *pVarName = ... I know that the second one is a pointer, but maybe I'm missing something... I don't understand the difference. is the first one a deference statement?


Solution

  • Your example misses the essential part which explains everything - the real logic within code. I am not an expert on Cocos, but from what I can see, it is typically used like this:

    bool bRet = false;
    do 
    {
        CC_BREAK_IF(!conditionA); // same as  if (!conditionA) break;
        ... some code which possibly sets bRet
        CC_BREAK_IF(!conditionB);        
        ... some other code which possibly sets bRet
        CC_BREAK_IF(!conditionC);        
        ... some other code which possibly sets bRet
        bRet = true;
    } while (0);
    return bRet;
    

    in this case, it allows code to jump to return statement without needing to resort to goto, or nesting a bunch of if statements. Compare it to this:

    bool bRet = false;
    if (conditionA); 
    {
        ... some code which possibly sets bRet
        if (conditionB)
        {
            ... some other code which possibly sets bRet
            if (conditionC);        
            {
                ... some other code which possibly sets bRet
            }
        }
    }
    bRet = true;
    
    return bRet;