cocos2d-x札記 (一)----HelloWorld淺析

    如下內容基於cocos2d-x 2.2.5+Visual Studio 2012,除特別註明外均爲原創,若有紕漏,請m本人 -_- linux


    步驟1、運行起始 express

    打開.../cocos2d-x 2.2.5/cocos2d-win32.vc2012.sln,能夠看到裏面有個HelloCpp,右鍵「設爲啓動項目」,Ctrl+F5非調試運行以後能夠看到運行成功界面。 windows

    這裏咱們主要來了解程序運行過程,首先c語言以main.cpp文件爲程序入口(由於裏面有_tWinMain函數),打開win32/main.cpp app

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    ...
    ...

    // create the application instance
    AppDelegate app;
    CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("HelloCpp");
    eglView->setFrameSize(2048, 1536);
    // The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
    // So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
    eglView->setFrameZoomFactor(0.4f);
    return CCApplication::sharedApplication()->run();
}

    首先,APIENTRY是一個宏定義,定義函數調用方式,_tWinMain是一個宏定義,定義入口函數名稱wWinMain,接着跟一堆的函數參數(有興趣深刻了解的可baidu或F12)。 函數

    接着,經過 ui

AppDelegate app;

    實例化AppDelegate,這個東西強行翻譯成中文就是「App委託」,是誰的委託呢?來詳細看看AppDelegate this

class  AppDelegate : private cocos2d::CCApplication

    咱們能夠看到AppDelegate是做爲CCApplication的委託,每一個程序頂層都會有個Application在統一管理,cocos2d-x也不例外,來看看CCApplication在管理什麼 spa

class  AppDelegate : private cocos2d::CCApplication
class CC_DLL CCApplication : public CCApplicationProtocol
class CC_DLL CCApplicationProtocol
{
public:

    virtual ~CCApplicationProtocol() {}

    /**
    @brief    Implement CCDirector and CCScene init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    */
    virtual bool applicationDidFinishLaunching() = 0;

    /**
    @brief  The function be called when the application enter background
    @param  the pointer of the application
    */
    virtual void applicationDidEnterBackground() = 0;

    /**
    @brief  The function be called when the application enter foreground
    @param  the pointer of the application
    */
    virtual void applicationWillEnterForeground() = 0;

    /**
    @brief    Callback by CCDirector for limit FPS.
    @interval       The time, expressed in seconds, between current frame and next. 
    */
    virtual void setAnimationInterval(double interval) = 0;

    /**
    @brief Get current language config
    @return Current language config
    */
    virtual ccLanguageType getCurrentLanguage() = 0;
    
    /**
     @brief Get target platform
     */
    virtual TargetPlatform getTargetPlatform() = 0;
};

    原來CCApplication把自身的生命週期事件applicationDidFinishLaunching等委託給了AppDelegate,因此在AppDelegate中必須重寫這幾個方法: .net

applicationDidFinishLaunching
applicationDidEnterBackground
applicationWillEnterForeground
    ok,上面解釋了cocos2d-x爲何要有一個AppDelegate,咱們繼續看main.cpp


    剛剛看到實例化了一個AppDelegate,接着 翻譯

CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("HelloCpp");
    eglView->setFrameSize(2048, 1536);
    // The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
    // So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
    eglView->setFrameZoomFactor(0.4f);

    實例化了一個單例對象CCEGLView

bool CCEGLView::Create()
{
    
        ...
        HINSTANCE hInstance = GetModuleHandle( NULL );
        WNDCLASS  wc;        // Windows Class Structure

        // Redraw On Size, And Own DC For Window.
        wc.style          = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
        wc.lpfnWndProc    = _WindowProc;                    // WndProc Handles Messages
        wc.cbClsExtra     = 0;                              // No Extra Window Data
        wc.cbWndExtra     = 0;                                // No Extra Window Data
        wc.hInstance      = hInstance;                        // Set The Instance
        wc.hIcon          = LoadIcon( NULL, IDI_WINLOGO );    // Load The Default Icon
        wc.hCursor        = LoadCursor( NULL, IDC_ARROW );    // Load The Arrow Pointer
        wc.hbrBackground  = NULL;                           // No Background Required For GL
        wc.lpszMenuName   = m_menu;                         //
        wc.lpszClassName  = kWindowClassName;               // Set The Class Name
        ...

    return bRet;
}


    上面能夠看出CCEGLView其實就是一個基於OpenGL建立的一個windows窗體,既然是窗體,就能夠設置名稱,大小等,再看main.cpp中最後也是最核心的一個東西


return CCApplication::sharedApplication()->run();
int CCApplication::run()
{
    // Initialize instance and cocos2d.
    if (! applicationDidFinishLaunching())
    {
        return 0;
    }
    
    return -1;
}
bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

    pDirector->setOpenGLView(pEGLView);
   
    ....
	
    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}
    到這裏就顯示HelloWorld界面了,下一節咱們說說CCDirector


     博客其餘文章列表
     http://my.oschina.net

相關文章
相關標籤/搜索