cocos2d-x(2.x) CCApplication源码分析

前端之家收集整理的这篇文章主要介绍了cocos2d-x(2.x) CCApplication源码分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件

#ifndef __CC_APPLICATION_WIN32_H__
#define __CC_APPLICATION_WIN32_H__

#include "CCStdC.h"
#include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h"
#include <string>

NS_CC_BEGIN

class CCRect;

class CC_DLL CCApplication : public CCApplicationProtocol
{
public:
    CCApplication();
    virtual ~CCApplication();

    /**
    @brief    Run the message loop. 运行游戏 开始游戏主循环
    */
    virtual int run();

    /**
    @brief    Get current applicaiton instance. 得到一个应用程序单例
    @return Current application instance pointer.
    */
    static CCApplication* sharedApplication();

    /* override functions */
    virtual void setAnimationInterval(double interval);
    virtual ccLanguageType getCurrentLanguage();
    
    /**
     @brief Get target platform  得到目标平台
     */
    virtual TargetPlatform getTargetPlatform();

    /**
     *  Sets the Resource root path.  设置资源(Resource)根目录
     *  @deprecated Please use CCFileUtils::sharedFileUtils()->setSearchPaths() instead.
     */
    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);

    /** 
     *  Gets the Resource root path.   得到资源(Resource)根目录
     *  @deprecated Please use CCFileUtils::sharedFileUtils()->getSearchPaths() instead. 
     */
    CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);

    void setStartupScriptFilename(const std::string& startupScriptFile);

    const std::string& getStartupScriptFilename(void)
    {
        return m_startupScriptFilename;
    }

protected:
    HINSTANCE           m_hInstance;
    HACCEL              m_hAccelTable;
    LARGE_INTEGER       m_nAnimationInterval;
    std::string         m_resourceRootPath;
    std::string         m_startupScriptFilename;

    static CCApplication * sm_pSharedApplication;
};

NS_CC_END

#endif    // __CC_APPLICATION_WIN32_H__

重要的run()函数实现:

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

    // Main message loop: 主循环消息
    MSG msg;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nLast;
    LARGE_INTEGER nNow;
<span style="white-space:pre">	</span>
    QueryPerformanceFrequency(&nFreq);<span style="white-space:pre">	</span>//高精度频率
    QueryPerformanceCounter(&nLast);<span style="white-space:pre">	</span>//高精度计数

    // Initialize instance and cocos2d.<span style="white-space:pre">	</span>  如果程序初始化失败 那么return
    if (!applicationDidFinishLaunching())
    {
        return 0;
    }

    CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();<span style="white-space:pre">	</span>//得到一个已经被封装的OpenGLView的实例
    pMainWnd->centerWindow();<span style="white-space:pre">					</span>//将窗口摆放到屏幕中心
    ShowWindow(pMainWnd->getHWnd(),SW_SHOW);<span style="white-space:pre">			</span>//显示窗口

    while (1)
    {
        if (! PeekMessage(&msg,NULL,PM_REMOVE))<span style="white-space:pre">		</span>//如果当前msg中无消息
        {
            // Get current time tick.
            QueryPerformanceCounter(&nNow);<span style="white-space:pre">			</span>//更新高精度计数

            // If it's the time to draw next frame,draw it,else sleep a while.<span style="white-space:pre">	</span>
            if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)<span style="white-space:pre">		</span>//如果离上次间隔时间,超过所设定的FPS间隔时间
            {
                nLast.QuadPart = nNow.QuadPart;<span style="white-space:pre">			</span>//更新上次更新时间
                CCDirector::sharedDirector()->mainLoop();<span style="white-space:pre">	</span>//驱动游戏 更新一帧
            }
            else
            {
                Sleep(0);
            }
            continue;
        }

        if (WM_QUIT == msg.message)
        {
            // Quit message loop.
            break;
        }

        // Deal with windows message.
        if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd,m_hAccelTable,&msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

CCApplication的作用看上去跟这个类的名字一样重要,cocos2d-x的心脏run()函数就在这个类中实现。

猜你在找的Cocos2d-x相关文章