[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-07-09 标题cocos2dx学习笔记cocos2dx3.x版本程序初始化流程分类编程 / C C / cocos2dx 标签cocos2dxcocos2dx学习笔记cocos2dx3.x版本程序初始化流程首先是main.cpp我们先看下AppDelegate的实现代码继续看下Application的实现cocos2dx3.x版本的程序的初始化和运行流程和2.x版本类似。之前有过笔记不过这里还是简单的笔记下首先是main.cppintAPIENTRY_tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,intnCmdShow){UNREFERENCED_PARAMETER(hPrevInstance);UNREFERENCED_PARAMETER(lpCmdLine);// create the application instanceAppDelegate app;returnApplication::getInstance()-run();}这里申请了一个变量app然后调用了run方法。我们先看下AppDelegate的实现代码classAppDelegate:privatecocos2d::Application{//AppDelegate 继承自Application}AppDelegate::AppDelegate(){//空的构造函数}boolAppDelegate::applicationDidFinishLaunching(){// initialize directorautodirectorDirector::getInstance();autoglviewdirector-getOpenGLView();if(!glview){glviewGLView::create(TheWeaponRentalShop);director-setOpenGLView(glview);}// turn on display FPS// director-setDisplayStats(true);// set FPS. the default value is 1.0/60 if you dont call thisdirector-setAnimationInterval(1.0/60);// create a scene. its an autorelease objectautosceneHelloWorld::createScene();// rundirector-runWithScene(scene);returntrue;}继续看下Application的实现classCC_DLLApplication:publicApplicationProtocol{public:Application();virtual~Application();intrun();staticApplication*getInstance();。。。。。protected:。。。。。staticApplication*sm_pSharedApplication;};// sharedApplication pointer//静态成员变量初始化Application*Application::sm_pSharedApplication0;Application::Application():_instance(nullptr),_accelTable(nullptr){_instanceGetModuleHandle(nullptr);_animationInterval.QuadPart0;CC_ASSERT(!sm_pSharedApplication);sm_pSharedApplicationthis;//调用构造函数时会将this指针传递给sm_pSharedApplication。非常重要}Application::~Application(){CC_ASSERT(thissm_pSharedApplication);sm_pSharedApplicationNULL;}intApplication::run(){。。。。。// Initialize instance and cocos2d.//先初始化程序运行逻辑if(!applicationDidFinishLaunching()){return0;}autodirectorDirector::getInstance();autoglviewdirector-getOpenGLView();// Retain glview to avoid glview being released in the while loopglview-retain();//程序主循环while(!glview-windowShouldClose()){QueryPerformanceCounter(nNow);if(nNow.QuadPart-nLast.QuadPart_animationInterval.QuadPart)//帧数控制{nLast.QuadPartnNow.QuadPart;director-mainLoop();//程序主循环中调用director的mainLoop方法实现游戏业务glview-pollEvents();//发送各种事件滑动单击等}else{Sleep(0);}}//游戏结束后的清理工作// Director should still do a cleanup if the window was closed manually.if(glview-isOpenGLReady()){director-end();director-mainLoop();directornullptr;}glview-release();returntrue;}Application*Application::getInstance(){CC_ASSERT(sm_pSharedApplication);returnsm_pSharedApplication;//返回静态成员变量}这里就很清晰了在main函数的初始化中初始化了一个实例app会调用到Application的构造函数中这样Application中的sm_pSharedApplication就会被赋值为该app。之后调用run方法就会县调用到Appdelegate::applicationDidFinishLaunching,然后进入游戏主循环。