Cocos2D-x HelloWorld代码执行过程
2015-12-04 10:38
375 查看
先从入口main.cpp开始,main.cpp代码:
代码很简单,主要就是定义了一个AppDelegate类型的类,然后调用了run,那么定义的AppDelegate类是如何被run起来的呢?
首先AppDelegate类继续自Application类,AppDelegate app会调用父类的构造函数也就是Application构造函数,Application构造函数如下
sm_pSharedApplication 是一个Application类型的指针,这里被赋值为刚定义的AppDelegate app,再看getInstance接口:
返回的就是刚开始定义的AppDelegate app,然后找看run做了什么,run的代码如下:
run函数首先调用了applicationDidFinishLaunching,这个函数在AppDelegate类里定义为:
也就是AppDelegate中最先被执行的函数,这个函数中通过调用HelloWorld::createScene()来执行具体的动作,createScene代码:
auto scene = Scene::create();中调用了HelloWorld的init函数,create()函数代码如下:
综上,以cocos2d-x的HelloWorld为例,代码的执行过程如下:
main—->AppDelegate::applicationDidFinishLaunching()->HelloWorld::createScene()—->HelloWorld::init()
#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()
相关文章推荐
- 【cocos2d-x打包问题】之“serializing cdt project settings” has encountered a problem
- cocos2d-js开发总结
- 【Cocos2d-x 3.x】 事件处理机制源码分析
- cocos2d-x札记 (二)----CCDirector深入
- cocos2d-x 学习笔记3
- COCOS2D-3.9 FileUtils 分析(一)
- 【Cocos2d-x学习记录】基于Mac OS iPhone开发环境搭建
- cocos2d 消息映射
- quick-cocos2dx修改java文件后如何生效
- cocos2d-x getVisibleSize , getVisibleOrigin 和坐标位置日记
- Cocos2D中Node的userObject实例变量使用时一个要注意的地方
- Cocos2D中Node的userObject实例变量使用时一个要注意的地方
- Cocos2D中Node的userObject实例变量使用时一个要注意的地方
- Cocos2d-x3.0 新成员Value,Vector,Map详解
- cocos-js 环境搭建
- cocos2d js 对android和ios混编记录
- cocos2dx 工具
- cocos2d-js添加360广告联盟插屏(通过jsb反射机制)
- 我在Cocos2d-x luabinding box2d时挖到过的坑
- cocos2d-x XX_selector函数体中无法访问类的成员变量问题