上篇文章分析到了定时器的定义,这篇的重点就是定时器是如何运行起来的。
1.从main中寻找定时器的回调
讲定时器的运行,就不得不触及到cocos2dx的main函数了,因为定时器是主线程上运行的,并不是单独线程的,所以它的调用必然会在main函数中,每帧调用。
以下代码就是win32平台下的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();
}
直接调用了run函数,直接进入到run中
int Application::run()
{
while(!glview->windowShouldClose())
{
QueryPerformanceCounter(&nNow);
if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{
nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);
director->mainLoop(); //看这里
glview->pollEvents();
}
else
{
Sleep(1);
}
}
return 0;
}
run函数中其他不相关的调用我已经去掉了,可以看到mainLoop函数才是真正的主循环
void Di