Cocos2d-x学习笔记(二)AppDelegate类详解

 由源代码,可得到如下的类继承关系:

 

1. 在方法applicationDidFinishLaunching中,首先会调用CCDirector* pDirector = CCDirector::sharedDirector()来初始化导演类,导演类是一个单例,由下方法中可知,创建的是一个CCDisplayLinkDirector类对象。它是CCDirector的直接继承子类。

CCDirector* CCDirector::sharedDirector(void)
{
    if (!s_SharedDirector)
    {
        s_SharedDirector = new CCDisplayLinkDirector();
        s_SharedDirector->init();
    }

    return s_SharedDirector;
}

 关于DisplayLinkDirector:

DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display.

2. CCEGLView用于UI的显示,在win32平台是一个窗体,用于点击、按键和触摸等事件的处理。在Create()方法中,会创建窗体和初始化GL底层的东西。

调用CCEGLView* pEGLView = CCEGLView::sharedOpenGLView()完成操作。单例。一般看到sharedxxx创建的对象,都是单例。

CCEGLView* CCEGLView::sharedOpenGLView()
{
  
    if (s_pEglView == NULL)
    {
        s_pEglView = new CCEGLView();
        if(!s_pEglView->Create())
        {
            delete s_pEglView;
            s_pEglView = NULL;
        }
    }

    return s_pEglView;
}

CCEGLView的作用:

This class wraps the CAEAGLLayer from CoreAnimation into a convenient UIView subclass. The view content is basically an EAGL surface you render your OpenGL scene into. Note that setting the view non-opaque will only work if the EAGL surface has an alpha channel.

3. pDirector->setOpenGLView(pEGLView)设置OpenGL视图,将在2中创建的View对象传给Director,并完成做一些相关操作:

void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView)
{
    CCAssert(pobOpenGLView, "opengl view should not be null");

    if (m_pobOpenGLView != pobOpenGLView)
    {
        // Configuration. Gather GPU info
        CCConfiguration *conf = CCConfiguration::sharedConfiguration();
        conf->gatherGPUInfo();
        conf->dumpInfo();

        // EAGLView is not a CCObject
        if(m_pobOpenGLView)
            delete m_pobOpenGLView; // [openGLView_ release]
        m_pobOpenGLView = pobOpenGLView;

        // set size
        m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize();
        
        createStatsLabel();
        
        if (m_pobOpenGLView)
        {
            setGLDefaultValues();
        }  
        
        CHECK_GL_ERROR_DEBUG();

        m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
        m_pTouchDispatcher->setDispatchEvents(true);
    }
}

4. pDirector->setDisplayStats(true)打开显示开关。

/** Display the FPS on the bottom-left corner */
    inline void setDisplayStats(bool bDisplayStats) { m_bDisplayStats = bDisplayStats; }

5. pDirector->setAnimationInterval(1.0 / 60)设置帧频。

void CCApplication::setAnimationInterval(double interval)
{
    LARGE_INTEGER nFreq;
    QueryPerformanceFrequency(&nFreq);
    m_nAnimationInterval.QuadPart = (LONGLONG)(interval * nFreq.QuadPart);
}

6. CCScene *pScene = HelloWorld::scene()创建一个场景,这是一个自动回收的对象。

具体代码将在HelloWorld类中介绍。

7. pDirector->runWithScene(pScene)运行创建的Scene,游戏真正开始于此。

// scene management
void CCDirector::runWithScene(CCScene *pScene)
{
    CCAssert(pScene != NULL, "This command can only be used to start the CCDirector. There is already a scene present.");
    CCAssert(m_pRunningScene == NULL, "m_pRunningScene should be null");

    pushScene(pScene);
    startAnimation();
}

 



1. applicationDidEnterBackground方法将会在app进入到后台时调用。

CCDirector::sharedDirector()->stopAnimation()会停止动画。这里还可以停止背景音乐等。

void CCDisplayLinkDirector::stopAnimation(void)
{
    m_bInvalid = true;
}

 



1. applicationWillEnterForeground方法将会在app进入前台时调用。

CCDirector::sharedDirector()->startAnimation()开启动画。这里还可以开启背景音乐。

// should we implement 4 types of director ??
// I think DisplayLinkDirector is enough
// so we now only support DisplayLinkDirector
void CCDisplayLinkDirector::startAnimation(void)
{
    if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
    {
        CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
    }

    m_bInvalid = false;
#ifndef EMSCRIPTEN
    CCApplication::sharedApplication()->setAnimationInterval(m_dAnimationInterval);
#endif // EMSCRIPTEN
}

 

posted @ 2014-12-27 16:15  motein  阅读(914)  评论(0编辑  收藏  举报