以下是TestCpp工程的main函数实现:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setFrameSize(2048, 1536);
// The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.
// So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.
eglView->setFrameZoomFactor(0.4f);
return CCApplication::sharedApplication()->run();
}
其中AppDelegate继承自CCApplication,
class AppDelegate : private cocos2d::CCApplication
{
......
}
在CCApplication定义一静态变量sm_pSharedApplication,
static CCApplication * sm_pSharedApplication;
CCApplication的构造函数为:
CCApplication::CCApplication()
: m_hInstance(NULL)
, m_hAccelTable(NULL)
{
m_hInstance = GetModuleHandle(NULL);
m_nAnimationInterval.QuadPart = 0;
CC_ASSERT(! sm_pSharedApplication);
sm_pSharedApplication = this;
}
所以静态变量sm_pSharedApplication指向AppDelegate 对象app。
静态成员函数sharedApplication可以获得该静态指针。
CCApplication* CCApplication::sharedApplication()
{
CC_ASSERT(sm_pSharedApplication);
return sm_pSharedApplication;
}
主函数中的这一句,
CCApplication::sharedApplication()->run();
将启动应用的消息循环,开始处理消息,下面应用的逻辑就是由消息来驱动进行了!