Cocos2D-x HelloWorld代码执行过程

Cocos2D-x HelloWorld代码执行过程,第1张

概述先从入口main.cpp开始,main.cpp代码: #include "main.h"#include "AppDelegate.h"#include "cocos2d.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,

先从入口main.cpp开始,main.cpp代码:

#include "main.h"#include "AppDelegate.h"#include "cocos2d.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdline,int       nCmdshow){    UNREFERENCED_ParaMETER(hPrevInstance);    UNREFERENCED_ParaMETER(lpCmdline);    // create the application instance    AppDelegate app;    return Application::getInstance()->run();}

代码很简单,主要就是定义了一个AppDelegate类型的类,然后调用了run,那么定义的AppDelegate类是如何被run起来的呢?
首先AppDelegate类继续自Application类,AppDelegate app会调用父类的构造函数也就是Application构造函数,Application构造函数如下

// sharedApplication pointerApplication * Application::sm_pSharedApplication = 0;Application::Application(): _instance(nullptr),_acceltable(nullptr){    _instance    = GetModuleHandle(nullptr);    _animationInterval.QuadPart = 0;    CC_ASSERT(! sm_pSharedApplication);    sm_pSharedApplication = this;}

sm_pSharedApplication 是一个Application类型的指针,这里被赋值为刚定义的AppDelegate app,再看getInstance接口:

//////////////////////////////////////////////////////////////////////////// static member function//////////////////////////////////////////////////////////////////////////Application* Application::getInstance(){    CC_ASSERT(sm_pSharedApplication);    return sm_pSharedApplication;}

返回的就是刚开始定义的AppDelegate app,然后找看run做了什么,run的代码如下:

int Application::run(){    PVRFrameEnableControlWindow(false);    // Main message loop:    LARGE_INTEGER nLast;    LARGE_INTEGER nNow;    queryPerformanceCounter(&nLast);    initGLContextAttrs();    // Initialize instance and cocos2d.    if (!applicationDIDFinishLaunching())    {        return 1;    }    auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    // Retain glvIEw to avoID glvIEw being released in the while loop    glvIEw->retain();    while(!glvIEw->windowshouldClose())    {        queryPerformanceCounter(&nNow);        if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)        {            nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);            director->mainLoop();            glvIEw->pollEvents();        }        else        {            Sleep(1);        }    }    // Director should still do a cleanup if the window was closed manually.    if (glvIEw->isOpenglready())    {        director->end();        director->mainLoop();        director = nullptr;    }    glvIEw->release();    return 0;}

run函数首先调用了applicationDIDFinishLaunching,这个函数在AppDelegate类里定义为:

bool AppDelegate::applicationDIDFinishLaunching() {    // initialize director    auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    if(!glvIEw) {#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_liNUX)        glvIEw = GLVIEwImpl::createWithRect("MyFGame",Rect(0,0,designResolutionSize.wIDth,designResolutionSize.height));#else        glvIEw = GLVIEwImpl::create("MyFGame");#endif        director->setopenGLVIEw(glvIEw);    }    // turn on display FPS    director->setdisplayStats(true);    // set FPS. the default value is 1.0/60 if you don't call this    director->setAnimationInterval(1.0 / 60);    // Set the design resolution    glvIEw->setDesignResolutionSize(designResolutionSize.wIDth,designResolutionSize.height,ResolutionPolicy::NO_border);    Size frameSize = glvIEw->getFrameSize();    // if the frame's height is larger than the height of medium size.    if (frameSize.height > mediumResolutionSize.height)    {                director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height,largeResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is larger than the height of small size.    else if (frameSize.height > smallResolutionSize.height)    {                director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height,mediumResolutionSize.wIDth/designResolutionSize.wIDth));    }    // if the frame's height is smaller than the height of medium size.    else    {                director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height,smallResolutionSize.wIDth/designResolutionSize.wIDth));    }    register_all_packages();    // create a scene. it's an autorelease object    auto scene = HelloWorld::createScene();    // run    director->runWithScene(scene);    return true;}

也就是AppDelegate中最先被执行的函数,这个函数中通过调用HelloWorld::createScene()来执行具体的动作,createScene代码:

Scene* HelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();    // 'layer' is an autorelease object    auto layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}

auto scene = Scene::create();中调用了HelloWorld的init函数,create()函数代码如下:

Scene* Scene::create(){    Scene *ret = new (std::nothrow) Scene();    if (ret && ret->init())    {        ret->autorelease();        return ret;    }    else    {        CC_SAFE_DELETE(ret);        return nullptr;    }}

综上,以cocos2d-x的HelloWorld为例,代码的执行过程如下: main—->AppDelegate::applicationDIDFinishLaunching()->HelloWorld::createScene()—->HelloWorld::init()

总结

以上是内存溢出为你收集整理的Cocos2D-x HelloWorld代码执行过程全部内容,希望文章能够帮你解决Cocos2D-x HelloWorld代码执行过程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1075429.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存