COCOS2D-X HelloWorld项目的启动流程

项目创建完毕后。见Class目录。

主要是AppDelegate(应用导演)和HelloWorldScene(示例层)。

AppDelegate 是 Cocos2D-x的内部入口。

从操作系统到Cocos 会先进入 AppDelegate。

也就是当 '引擎' 初始化完毕后,会调用AppDelegate的 ApplicationDidFinishLaunching 方法。

在 AppDelegate 中 执行游戏的初始化,设置分辨率并启动场景。






[AppDelegate.cpp]

bool AppDelegate::applicationDidFinishLaunching() {

    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();

    //初始化OpenGLs视图 如果成功就会根据对应平台完成对象实现。
    if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
        glview = GLViewImpl::createWithRect("SmartGame", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
        glview = GLViewImpl::create("SmartGame");
#endif
    //导演了设置OpenGL
        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.0 / 60);

    // Set the design resolution
    // 设置  屏幕分辨率
    glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
    Size frameSize = glview->getFrameSize();

    //如果OpenGl拿出的高度大于中等大小才会执行的代码块,应该是适屏设置。
    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
    // 创建HelloWorld场景,这个场景对象会交给Cocosed-x管理,不需要手动释放
    auto scene = HelloWorld::createScene();

    // 运行这个场景
    director->runWithScene(scene);

    return true;
}





当导演runWithScene 运行该场景时。

会先进入该场景的ini 初始化方法。


[HelloWorldScene.cpp]


bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first ,必须先执行父类的初始化。
    if ( !Layer::init() )
    {
        return false;
    }
    // 屏幕的可见区域值
    Size visibleSize = Director::getInstance()->getVisibleSize();
    // 原点的位置
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    // 关闭按钮是一个 MenuItemImage (菜单项) 对象。
    // create 方法中的前两个参数代表着选中与非选中状态显示的图。
    // 当他被单击的时候,CC_CALLBACK_1开始执行回调函数,即helloWorld的menuCloseCallback
    // 函数原型
    /**
                                *参数 Ref* 代表单击事件都发送者,即MenuItemImage!

        void HelloWorld::menuCloseCallback(Ref* sender){
           Director::getInstance()->end();
        }
    */
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png",
                                           "CloseSelected.png",
                                           CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                                origin.y + closeItem->getContentSize().height/2));

    // create menu, it's an autorelease object
    auto menu = Menu::create(closeItem, NULL);
    // 设置类菜单位置,以后记住这代表右下角。
    menu->setPosition(Vec2::ZERO);
    // 此行代表加到场景中。
    // 第二个参数代表绘制层的顺序,默认参数值为0,
    // 表示最高优先层导入,该值越大表示该层在最后加载(在最高一层),一般背景图层是首先加载,
    // 其他元素在加载在背景层上面。因为背景在后面加载的话,会覆盖掉前面加载的元素,看不到想要的图层组合效果。
    this->addChild(menu, 1);


    // 创建文本,选择指定字体库,设置字号
    auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

    // 设置位置, X 剧中 , y 偏上
    // 此处标记 Vec2的居中布局位置 : (原点x轴的起始位置向右偏移至总屏幕可见范围一半的位置
    // 即 origin.x + visibleSize.width/2
    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);

    // add "HelloWorld" splash screen"
    // 这里创建里一个精灵,用于显示纹理(就是贴图)。
    auto sprite = Sprite::create("HelloWorld.png");

    // x y 轴起点偏移至屏幕可见范围的一半,上下左右都居中
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(sprite, 0);
    
    return true;
}



本文内容源自: http://spu.dangdang.com/23947634.html

猜你喜欢

转载自784838898.iteye.com/blog/2391224