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, 0, 0, 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()函数就在这个类中实现。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值