AppDelegate 繼承自Application, 負責控制遊戲的生命週期。
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
/**<span style="white-space:pre"> @brief 啓動結束以後,實現導演類和場景的初始化</span>
* @return true 成功,APP繼續
<pre name="code" class="cpp"> * @return false 失敗,APP停止
*/ virtual bool applicationDidFinishLaunching(); /** @return false 失敗,APP停止
*/
virtual void applicationDidEnterBackground();
/** @brief APP從後臺迴歸以後
*/
virtual void applicationWillEnterForeground();
在main函數中被調用
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 繼承於Application.main調用了Application的run()函數;
在看到Application的構造函數
Application::Application()
: _instance(nullptr)
, _accelTable(nullptr)
{
_instance = GetModuleHandle(nullptr);
_animationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
所以main函數中AppDelegate app;這句,主要是爲了實例化Application的單利對象,然後調用run(),run裏面又會調用applicationDidFinishLaunching,和mainLoop().
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching())
{
return 0;
}
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;
director->mainLoop();
glview->pollEvents();
}