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();
}