这是AppDelegate.h的代码:
#ifndef _APP_DELEGATE_H_#define _APP_DELEGATE_H_#include "cocos2d.h"/**@brief The cocos2d Application.Private inheritance here hides part of interface from Director.*/class AppDelegate : private cocos2d::Application{public: AppDelegate(); virtual ~AppDelegate(); virtual void initGLContextAttrs(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief Called when the application moves to the background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief Called when the application reenters the foreground @param the pointer of the application */ virtual void applicationWillEnterForeground();};#endif // _APP_DELEGATE_H_可以看到它继承自Application,那么我们去探求一下Application类。
首先我们得找到cocos2d.h,引擎头文件目录在cmake里,所以代码里可以只写文件名,到时候构建的时候,会自动探求头文件所在位置,我们的cocos2d.h在cocos2d/cocos目录下。
打开cocos2d.h:
可以看到宏会根据平台不同,导入不同的头文件,我这里是linux,所以打开platform/linux/CCApplication-linux.h:
#pragma once#include "platform/CCCommon.h"#include "platform/CCApplicationProtocol.h"#include NS_CC_BEGINclass Rect;class Application : public ApplicationProtocol{public: /** * @js ctor */ Application(); /** * @js NA * @lua NA */ virtual ~Application(); /** @brief Callback by Director for limit FPS. @param interval The time, which expressed in second in second, between current frame and next. */ virtual void setAnimationInterval(float interval) override; /** @brief Run the message loop. */ int run(); /** @brief Get current application instance. @return Current application instance pointer. */ static Application* getInstance(); /** @deprecated Use getInstance() instead */ CC_DEPRECATED_ATTRIBUTE static Application* sharedApplication(); /* override functions */ virtual LanguageType getCurrentLanguage() override; /** @brief Get current language iso 639-1 code @return Current language iso 639-1 code */ virtual const char * getCurrentLanguageCode() override; /** @brief Get application version */ virtual std::string getVersion() override; /** @brief Open url in default browser @param String with url to open. @return true if the resource located by the URL was successfully opened; otherwise false. */ virtual bool openURL(const std::string &url) override; /** * Sets the Resource root path. * @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead. */ CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir); /** * Gets the Resource root path. * @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead. */ CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(); /** @brief Get target platform */ virtual Platform getTargetPlatform() override;protected: long _animationInterval; //micro second std::string _resourceRootPath; static Application * sm_pSharedApplication;};NS_CC_ENDApplication继承的类只是一个接口,我们这里就看Application就可以了。
我们看一下构造函数的界说:
// sharedApplication pointerApplication * Application::sm_pSharedApplication = nullptr;Application::Application(): _animationInterval(1.0f/60.0f*1000.0f){ CC_ASSERT(! sm_pSharedApplication); sm_pSharedApplication = this;}首先是初始化一个static Application的指针为nullptr,接着在构造函数里,把这个指针初始化为this,也就是这个类的对象,所以这个指针是个全局单例对象,而在我们的步伐里,这个指针着实就是我们main.cpp里的app对象。接着看一下getInstance函数:
//////////////////////////////////////////////////////////////////////////// static member function//////////////////////////////////////////////////////////////////////////Application* Application::getInstance(){ CC_ASSERT(sm_pSharedApplication); return sm_pSharedApplication;}这是个静态函数,这个函数返回我们的全局对象。所以main里面的run函数,实际上是Application的run:
int Application::run(){ initGLContextAttrs(); // Initialize instance and cocos2d. if (! applicationDidFinishLaunching()) { return 0; } 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((_animationInterval - curTime + lastTime)*1000); } } /* Only work on Desktop * Director::mainLoop is really one frame logic * when we want to close the window, we should call Director::end(); * then call Director::mainLoop to do release of internal resources */ if (glview->isOpenGLReady()) { director->end(); director->mainLoop(); director = nullptr; } glview->release(); return EXIT_SUCCESS;}值得注意的是Application里面的虚函数,实际上run里面调用的是由AppDelegate实现的。
里面的循环逻辑实际上主要是director->mainLoop(),下结教程我们会解说director的mainLoop。