1、入口函数main.cpp。
AppDelegate app; //初始化app,继承自Application的单例,不同test有不同的app。
return Application::getInstance()->run();
2、application。
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); //windows获取精准时间。
initGLContextAttrs(); //初始化openGL的上下文环境
// Initialize instance and cocos2d.
if (!applicationDidFinishLaunching()) //重要的初始化。
{
return 1;
}
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain();
LONGLONG interval = 0LL;
LONG waitMS = 0L;
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq); //windows精准计时频率。
while(!glview->windowShouldClose()) //循环
{
QueryPerformanceCounter(&nNow);
interval = nNow.QuadPart - nLast.QuadPart; //每次循环的间隔
if (interval >= _animationInterval.QuadPart) //这次循环间隔大于等于设置的时间间隔
{
nLast.QuadPart = nNow.QuadPart;
director->mainLoop(); // 导演类的主循环
glview->pollEvents();
}
else //否则刷空或者等待一会
{
// The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code,
// but it's still not precise enough. For example, if the precision of timer is 1ms,
// Sleep(3) may make a sleep of 2ms or 4ms. Therefore, we subtract 1ms here to make Sleep time shorter.
// If 'waitMS' is equal or less than 1ms, don't sleep and run into next loop to
// boost CPU to next frame accurately.
waitMS = (_animationInterval.QuadPart - interval) * 1000LL / freq.QuadPart - 1L;
// 比预设的刷的快多了<