加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x札记 (一)----HelloWorld浅析

发布时间:2020-12-14 16:46:35 所属栏目:百科 来源:网络整理
导读:以下内容基于cocos2d-x 2.2.5+Visual Studio2012,除特别注明外均为原创,如有纰漏,请m本人 -_- 步骤一、运行起始 打开.../cocos2d-x 2.2.5/cocos2d-win32.vc2012.sln,可以看到里面有个HelloCpp,右键“设为启动项目”,Ctrl+F5非调试运行之后可以看到运行

以下内容基于cocos2d-x 2.2.5+Visual Studio2012,除特别注明外均为原创,如有纰漏,请m本人-_-


步骤一、运行起始

打开.../cocos2d-x 2.2.5/cocos2d-win32.vc2012.sln,可以看到里面有个HelloCpp,右键“设为启动项目”,Ctrl+F5非调试运行之后可以看到运行成功界面。

这里我们主要来了解程序运行过程,首先c语言以main.cpp文件为程序入口(因为里面有_tWinMain函数),打开win32/main.cpp

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)。

接着,通过

AppDelegate app;

实例化AppDelegate,这个东西强行翻译成中文就是“App委托”,是谁的委托呢?来详细看看AppDelegate

class  AppDelegate : private cocos2d::CCApplication

我们可以看到AppDelegate是作为CCApplication的委托,每个程序顶层都会有个Application在统一管理,cocos2d-x也不例外,来看看CCApplication在管理什么

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中必须重写这几个方法:

applicationDidFinishLaunching
applicationDidEnterBackground
applicationWillEnterForeground
ok,上面解释了cocos2d-x为什么要有一个AppDelegate,我们继续看main.cpp


刚刚看到实例化了一个AppDelegate,接着

CCEGLView* eglView = CCEGLView::sharedOpenGLView();
    eglView->setViewName("HelloCpp");
    eglView->setFrameSize(2048,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读