cocos 2dx--------Hello world简单流程分析(一)

 1.代码从此处进入,开始。。。

int main(int argc, char **argv)
{
    // create the application instance
    AppDelegate app;
    return Application::getInstance()->run();
}

1.由于AppDelegate app;是创建新的类,于是会进入构造函数AppDelegate::AppDelegate()  {  }

2.然后代码逻辑会执行到 Application::getInstance()->run()中的int Application::run();这里面比较关键的点就是bool AppDelegate::applicationDidFinishLaunching() {}函数;具体分析见下面代码的中文部分。。总的说就是初始化了所有的东西,,并运行游戏

bool AppDelegate::applicationDidFinishLaunching() {
    printf(" %s,%d \n",__PRETTY_FUNCTION__,__LINE__);
    
    auto director = Director::getInstance();  //初始化director
    auto glview = director->getOpenGLView();
    if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
        glview = GLViewImpl::createWithRect("MyGame", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height)); //设置窗口大小为designResolutionSize
        
#else
        glview = GLViewImpl::create("MyGame");
#endif
        director->setOpenGLView(glview);
    }

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    director->setAnimationInterval(1.0f / 60);//设置每帧的时间间隔:每秒60帧

    // Set the design resolution
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
    auto frameSize = glview->getFrameSize();
    // if the frame's height is larger than the height of medium size.
    if (frameSize.height > mediumResolutionSize.height)
    {        
        director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
    }
    // if the frame's height is larger than the height of small size.
    else if (frameSize.height > smallResolutionSize.height)
    {        
        director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
    }
    // if the frame's height is smaller than the height of medium size.
    else
    {        
        director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
    }

    register_all_packages();

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();//调用HelloWorld::init()

    // run
    director->runWithScene(scene);//游戏渲染的开始,把一个scene当参数传入,作为游戏的初始画面

    return true;
}

3.执行到HelloWorld::createScene()会由CREATE_FUNC(HelloWorld)的宏定义,去执行bool HelloWorld::init(),这里面添加了按钮,创建了“TTF”字符串,还有Sprite基本件。具体代码注释如下。。

bool HelloWorld::init()
{
    printf(" %s,%d \n",__PRETTY_FUNCTION__,__LINE__);
  
    // 1. super init first
    if ( !Scene::init() )
    {
        return false;
    }

    //读取确认的窗口尺寸大小
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // 添加按键,当按下,执行menuCloseCallback回调函数
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",  //未按下的显示
                                           "CloseSelected.png",//按下按键的显示
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));//按下就调用HelloWorld::menuCloseCallback函数

    if (closeItem == nullptr ||
        closeItem->getContentSize().width <= 0 ||
        closeItem->getContentSize().height <= 0)
    {
        problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
    }
    else
    {
        float x = origin.x + visibleSize.width - closeItem->getContentSize().width/2;
        float y = origin.y + closeItem->getContentSize().height/2;
        closeItem->setPosition(Vec2(x,y));
    }

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);

    menu->setPosition(Vec2::ZERO);  //确认按钮的位置
    this->addChild(menu, 1); //添加一个组件到当前sence

    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label

    auto label = Label::createWithTTF("QQ Hello World", "fonts/Marker Felt.ttf", 24);
    if (label == nullptr)
    {
        problemLoading("'fonts/Marker Felt.ttf'");
    }
    else
    {
        // position the label on the center of the screen
        label->setPosition(Vec2(origin.x + visibleSize.width/2,
                                origin.y + visibleSize.height - label->getContentSize().height));

        // add the label as a child to this layer
        this->addChild(label, 1);
    }

    #if 1
    // 用HelloWorld.png这张图创建一个sprite
    auto sprite = Sprite::create("HelloWorld.png");
    if (sprite == nullptr)
    {
        problemLoading("'HelloWorld.png'");
    }
    else
    {
        // 将sprite放在屏幕中间
        sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

        // 添加sprite到当前sence
        this->addChild(sprite, 0);
    }
    #endif
    
    printf(" %s,%d \n",__PRETTY_FUNCTION__,__LINE__);
    return true;

}

4.然后执行director->runWithScene(scene);即可启动整个游戏。。。

5.  然后代码在int Application::run()循环执行完了applicationDidFinishLaunching()。。。接着就在 while (!glview->windowShouldClose()){}处一直循环。直到按下退出按键即可退出。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值