AppDelegate->Application->ApplicationProtocol
2017-07-12 16:13
369 查看
AppDelegate->Application->ApplicationProtocol;
ApplicationProtocol 里面都是虚函数 没什么有用的东西
main.cpp Application::getInstance()->run() 开始启动应用。
那么在run里面有哪些呢?这是win平台下run的代码
安卓:
ios:
对比发现 这里可以只关心 applicationDidFinishLaunching 这个方法 其他的代码应该和各个平台有关 貌似Application 这个类里面 其他内容没啥好关心的 主要的代码都在 AppDelegate 里面 绕了一圈 又回到了 AppDelegate。
补充:Director::getInstance()->mainLoop() 进入引擎的主循环
不同平台 进入主循环的地方不一样 这里就不一一介绍了
ApplicationProtocol 里面都是虚函数 没什么有用的东西
class CC_DLL ApplicationProtocol { public: enum class Platform { OS_WINDOWS, /**< Windows */ OS_LINUX, /**< Linux */ OS_MAC, /**< Mac OS X*/ OS_ANDROID, /**< Android */ OS_IPHONE, /**< iPhone */ OS_IPAD, /**< iPad */ OS_BLACKBERRY, /**< BlackBerry */ OS_NACL, /**< Native Client in Chrome */ OS_EMSCRIPTEN, /**< Emscripten */ OS_TIZEN, /**< Tizen */ OS_WINRT, /**< Windows Runtime Applications */ OS_WP8 /**< Windows Phone 8 Applications */ }; virtual ~ApplicationProtocol(){ #if CC_ENABLE_SCRIPT_BINDING ScriptEngineManager::destroyInstance(); #endif /** clean auto release pool. */ PoolManager::destroyInstance(); } /** * 实例化导演类 场景初始化 */ virtual bool applicationDidFinishLaunching() = 0; /** * 游戏到后台后 会调用这个方法 */ virtual void applicationDidEnterBackground() = 0; /** * 从后台回到前台 */ virtual void applicationWillEnterForeground() = 0; /** * FPS */ virtual void setAnimationInterval(float interval) = 0; virtual void setAnimationInterval(float interval, SetIntervalReason reason) = 0; /** Subclass override the function to set OpenGL context attribution instead of use default value. * And now can only set six attributions:redBits,greenBits,blueBits,alphaBits,depthBits,stencilBits. * Default value are(5,6,5,0,16,0), usually use as follows: * void AppDelegate::initGLContextAttrs(){ * GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8}; * GLView::setGLContextAttrs(glContextAttrs); * } */ virtual void initGLContextAttrs() {} virtual LanguageType getCurrentLanguage() = 0; virtual const char * getCurrentLanguageCode() = 0; /** @brief Get target platform. * @js NA * @lua NA */ virtual Platform getTargetPlatform() = 0; virtual std::string getVersion() = 0; /** * Open url in default browser. */ virtual bool openURL(const std::string &url) = 0; };
main.cpp Application::getInstance()->run() 开始启动应用。
那么在run里面有哪些呢?这是win平台下run的代码
int Application::run() { PVRFrameEnableControlWindow(false); /////////////////////////////////////////////////////////////////////////// /////////////// changing timer resolution /////////////////////////////////////////////////////////////////////////// UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution TIMECAPS tc; UINT wTimerRes = 0; if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS))) { wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax); timeBeginPeriod(wTimerRes); } // 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(); glview->retain(); LONGLONG interval = 0LL; LONG waitMS = 0L; LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); while(!glview->windowShouldClose()) { QueryPerformanceCounter(&nNow); interval = nNow.QuadPart - nLast.QuadPart; if (interval >= _animationInterval.QuadPart) { nLast.QuadPart = nNow.QuadPart; director->mainLoop(); glview->pollEvents(); } else { waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L; if (waitMS > 1L) Sleep(waitMS); } } if (glview->isOpenGLReady()) { director->end(); director->mainLoop(); director = nullptr; } glview->release(); if (wTimerRes != 0) { timeEndPeriod(wTimerRes); } return 0; }
安卓:
int Application::run() { // Initialize instance and cocos2d. if (! applicationDidFinishLaunching()) { return 0; } return -1; }
ios:
int Application::run() { initGLContextAttrs(); if(!applicationDidFinishLaunching()) { return 1; } long lastTime = 0L; long curTime = 0L; auto director = Director::getInstance(); auto glview = director->getOpenGLView(); // Retain glview to avoid glview being released in the while loop glview->retain(); while (!glview->windowShouldClose()) { lastTime = getCurrentMillSecond(); director->mainLoop(); glview->pollEvents(); curTime = getCurrentMillSecond(); if (curTime - lastTime < _animationInterval) { usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000)); } } if (glview->isOpenGLReady()) { director->end(); director->mainLoop(); } glview->release(); return 0; }
对比发现 这里可以只关心 applicationDidFinishLaunching 这个方法 其他的代码应该和各个平台有关 貌似Application 这个类里面 其他内容没啥好关心的 主要的代码都在 AppDelegate 里面 绕了一圈 又回到了 AppDelegate。
补充:Director::getInstance()->mainLoop() 进入引擎的主循环
不同平台 进入主循环的地方不一样 这里就不一一介绍了
相关文章推荐
- Cocos2d-x之 CCApplicationProtocol-->CCApplication-->AppDelegate
- Assigning to 'AppDelegate *' from incompatible type 'id<UIApplicationDelegate>'
- 2 自己编写:AppDelegate,CCApplication,CCApplicationProtocol
- cocos2d-x打飞机实例总结(一):程序入口分析和AppDelegate,Application,ApplicationProtocol三个类的分析
- 2 自己编写:AppDelegate,CCApplication,CCApplicationProtocol
- 打印出现:You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 详解AppDelegate/UIApplication
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="initApp()">
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- [<UIApplicationDelegate> application:didReceiveRemoteNotification:fetchCompletionHandler:], but you
- UIApplication APPDelegate 协议
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id<******Delegate>'
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 'Could not load NIB in bundle: 'NSBundle < /Bundle/Application/ .app> (loaded)' with name 'Car
- iOS 极光推送集成打印You've implemented -[<UIApplicationDelegate> application:didReceiveRemoteNotification:fe
- 传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
- 48-程序启动过程原理UIApplication的代理(AppDelegate),UIApplicationMain底层