您的位置:首页 > 移动开发 > Cocos引擎

Cocos2D-x HelloWorld代码执行过程

2015-12-04 10:38 375 查看
先从入口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 pointer
Application * 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()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: