AppDelegate->Application->ApplicationProtocol

AppDelegate->Application->ApplicationProtocol;
ApplicationProtocol 里面都是虚函数 没什么有用的东西

class CC_DLL ApplicationProtocol
{
public:
    enum class Platform
    {
        OS_WINDOWS,     /**< Windows */
        OS_LINUX,       /**< Linux */
        OS_MAC,         /**< Mac OS X*/
        OS_ANDROID,     /**< Android */
        OS_IPHONE,      /**< iPhone */
        OS_IPAD,        /**< iPad */
        OS_BLACKBERRY,  /**< BlackBerry */
        OS_NACL,        /**< Native Client in Chrome */
        OS_EMSCRIPTEN,  /**< Emscripten */
        OS_TIZEN,       /**< Tizen */
        OS_WINRT,       /**< Windows Runtime Applications */
        OS_WP8          /**< Windows Phone 8 Applications */
    };
    virtual ~ApplicationProtocol(){
#if CC_ENABLE_SCRIPT_BINDING
        ScriptEngineManager::destroyInstance();
#endif
        /** clean auto release pool. */
        PoolManager::destroyInstance();
    }

    /**
    * 实例化导演类 场景初始化
    */
    virtual bool applicationDidFinishLaunching() = 0;

    /**
    * 游戏到后台后 会调用这个方法 
    */
    virtual void applicationDidEnterBackground() = 0;

    /**
    * 从后台回到前台
    */
    virtual void applicationWillEnterForeground() = 0;

    /**
    * FPS
    */
    virtual void setAnimationInterval(float interval) = 0;
    virtual void setAnimationInterval(float interval, SetIntervalReason reason) = 0;

    /** Subclass override the function to set OpenGL context attribution instead of use default value.
    * And now can only set six attributions:redBits,greenBits,blueBits,alphaBits,depthBits,stencilBits.
    * Default value are(5,6,5,0,16,0), usually use as follows:
    * void AppDelegate::initGLContextAttrs(){
    *     GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
    *     GLView::setGLContextAttrs(glContextAttrs);
    * }
    */
    virtual void initGLContextAttrs() {}

    virtual LanguageType getCurrentLanguage() = 0;
    virtual const char * getCurrentLanguageCode() = 0;

    /**
     @brief Get target platform.
     * @js NA
     * @lua NA
     */
    virtual Platform getTargetPlatform() = 0;


    virtual std::string getVersion() = 0;

    /**
     * Open url in default browser.
     */
    virtual bool openURL(const std::string &url) = 0;
};

main.cpp Application::getInstance()->run() 开始启动应用。
那么在run里面有哪些呢?这是win平台下run的代码

int Application::run()
{
    PVRFrameEnableControlWindow(false);

    ///////////////////////////////////////////////////////////////////////////
    /////////////// changing timer resolution
    ///////////////////////////////////////////////////////////////////////////
    UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution
    TIMECAPS tc;
    UINT wTimerRes = 0;
    if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS)))
    {
        wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
        timeBeginPeriod(wTimerRes);
    }

    // Main message loop:
    LARGE_INTEGER nLast;
    LARGE_INTEGER nNow;

    QueryPerformanceCounter(&nLast);

    initGLContextAttrs();

    // Initialize instance and cocos2d.
    if (!applicationDidFinishLaunching())
    {
        return 1;
    }

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

    glview->retain();

    LONGLONG interval = 0LL;
    LONG waitMS = 0L;

    LARGE_INTEGER freq;
    QueryPerformanceFrequency(&freq);

    while(!glview->windowShouldClose())
    {
        QueryPerformanceCounter(&nNow);
        interval = nNow.QuadPart - nLast.QuadPart;
        if (interval >= _animationInterval.QuadPart)
        {
            nLast.QuadPart = nNow.QuadPart;
            director->mainLoop();
            glview->pollEvents();
        }
        else
        {
            waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
            if (waitMS > 1L)
                Sleep(waitMS);
        }
    }

    if (glview->isOpenGLReady())
    {
        director->end();
        director->mainLoop();
        director = nullptr;
    }
    glview->release();
    if (wTimerRes != 0)
    {
        timeEndPeriod(wTimerRes);
    }
    return 0;
}

安卓:

int Application::run()
{
    // Initialize instance and cocos2d.
    if (! applicationDidFinishLaunching())
    {
        return 0;
    }

    return -1;
}

ios:

int Application::run()
{
    initGLContextAttrs();
    if(!applicationDidFinishLaunching())
    {
        return 1;
    }

    long lastTime = 0L;
    long curTime = 0L;

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

    // Retain glview to avoid glview being released in the while loop
    glview->retain();

    while (!glview->windowShouldClose())
    {
        lastTime = getCurrentMillSecond();

        director->mainLoop();
        glview->pollEvents();

        curTime = getCurrentMillSecond();
        if (curTime - lastTime < _animationInterval)
        {
            usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
        }
    }
    if (glview->isOpenGLReady())
    {
        director->end();
        director->mainLoop();
    }

    glview->release();

    return 0;
}

对比发现 这里可以只关心 applicationDidFinishLaunching 这个方法 其他的代码应该和各个平台有关 貌似Application 这个类里面 其他内容没啥好关心的 主要的代码都在 AppDelegate 里面 绕了一圈 又回到了 AppDelegate。

补充:Director::getInstance()->mainLoop() 进入引擎的主循环
不同平台 进入主循环的地方不一样 这里就不一一介绍了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值