diff --git a/cocos/2d/platform/linux/CCApplication.cpp b/cocos/2d/platform/linux/CCApplication.cpp index 264ba20627..3fb269fef1 100644 --- a/cocos/2d/platform/linux/CCApplication.cpp +++ b/cocos/2d/platform/linux/CCApplication.cpp @@ -70,18 +70,27 @@ int Application::run() return 0; } + long lastTime = 0L; + long curTime = 0L; + auto director = Director::getInstance(); auto glview = director->getOpenGLView(); + + // Retain glview to avoid glview being released in the while loop glview->retain(); while (!glview->windowShouldClose()) { - long iLastTime = getCurrentMillSecond(); - director->mainLoop(); + lastTime = getCurrentMillSecond(); + + // Poll event before mainloop glview->pollEvents(); - long iCurTime = getCurrentMillSecond(); - if (iCurTime-iLastTime<_animationInterval){ - usleep((_animationInterval - iCurTime+iLastTime)*1000); + director->mainLoop(); + + curTime = getCurrentMillSecond(); + if (curTime - lastTime < _animationInterval) + { + usleep((_animationInterval - curTime + lastTime)*1000); } } /* Only work on Desktop diff --git a/cocos/2d/platform/mac/CCApplication.mm b/cocos/2d/platform/mac/CCApplication.mm index 7c9157759c..817b5d2806 100644 --- a/cocos/2d/platform/mac/CCApplication.mm +++ b/cocos/2d/platform/mac/CCApplication.mm @@ -68,17 +68,28 @@ int Application::run() { return 0; } - GLView* glview = Director::getInstance()->getOpenGLView(); + + long lastTime = 0L; + long curTime = 0L; + + auto director = Director::getInstance(); + auto glview = director->getOpenGLView(); + + // Retain glview to avoid glview being released in the while loop glview->retain(); while (!glview->windowShouldClose()) { - long iLastTime = getCurrentMillSecond(); - Director::getInstance()->mainLoop(); + lastTime = getCurrentMillSecond(); + + // Poll event before mainloop glview->pollEvents(); - long iCurTime = getCurrentMillSecond(); - if (iCurTime-iLastTime<_animationInterval){ - usleep(static_cast((_animationInterval - iCurTime+iLastTime)*1000)); + director->mainLoop(); + + curTime = getCurrentMillSecond(); + if (curTime - lastTime < _animationInterval) + { + usleep(static_cast((_animationInterval - curTime + lastTime)*1000)); } } @@ -89,10 +100,12 @@ int Application::run() */ if (glview->isOpenGLReady()) { - Director::getInstance()->end(); - Director::getInstance()->mainLoop(); + director->end(); + director->mainLoop(); } + glview->release(); + return true; } diff --git a/cocos/2d/platform/win32/CCApplication.cpp b/cocos/2d/platform/win32/CCApplication.cpp index fd03e10786..84d0dc308e 100644 --- a/cocos/2d/platform/win32/CCApplication.cpp +++ b/cocos/2d/platform/win32/CCApplication.cpp @@ -78,6 +78,8 @@ int Application::run() auto director = Director::getInstance(); auto glview = director->getOpenGLView(); + + // Retain glview to avoid glview being released in the while loop glview->retain(); while(!glview->windowShouldClose()) @@ -86,8 +88,10 @@ int Application::run() if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart) { nLast.QuadPart = nNow.QuadPart; - director->mainLoop(); + + // Poll event before mainloop glview->pollEvents(); + director->mainLoop(); } else {