Merge pull request #5803 from dumganhar/develop

PollEvents should invoked before main loop and little improvement in CCApplication.cpp/.mm.
This commit is contained in:
James Chen 2014-03-14 20:02:04 +08:00
commit d91f5b9a68
3 changed files with 40 additions and 14 deletions

View File

@ -70,18 +70,27 @@ int Application::run()
return 0; return 0;
} }
long lastTime = 0L;
long curTime = 0L;
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getOpenGLView(); auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain(); glview->retain();
while (!glview->windowShouldClose()) while (!glview->windowShouldClose())
{ {
long iLastTime = getCurrentMillSecond(); lastTime = getCurrentMillSecond();
director->mainLoop();
// Poll event before mainloop
glview->pollEvents(); glview->pollEvents();
long iCurTime = getCurrentMillSecond(); director->mainLoop();
if (iCurTime-iLastTime<_animationInterval){
usleep((_animationInterval - iCurTime+iLastTime)*1000); curTime = getCurrentMillSecond();
if (curTime - lastTime < _animationInterval)
{
usleep((_animationInterval - curTime + lastTime)*1000);
} }
} }
/* Only work on Desktop /* Only work on Desktop

View File

@ -68,17 +68,28 @@ int Application::run()
{ {
return 0; 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(); glview->retain();
while (!glview->windowShouldClose()) while (!glview->windowShouldClose())
{ {
long iLastTime = getCurrentMillSecond(); lastTime = getCurrentMillSecond();
Director::getInstance()->mainLoop();
// Poll event before mainloop
glview->pollEvents(); glview->pollEvents();
long iCurTime = getCurrentMillSecond(); director->mainLoop();
if (iCurTime-iLastTime<_animationInterval){
usleep(static_cast<useconds_t>((_animationInterval - iCurTime+iLastTime)*1000)); curTime = getCurrentMillSecond();
if (curTime - lastTime < _animationInterval)
{
usleep(static_cast<useconds_t>((_animationInterval - curTime + lastTime)*1000));
} }
} }
@ -89,10 +100,12 @@ int Application::run()
*/ */
if (glview->isOpenGLReady()) if (glview->isOpenGLReady())
{ {
Director::getInstance()->end(); director->end();
Director::getInstance()->mainLoop(); director->mainLoop();
} }
glview->release(); glview->release();
return true; return true;
} }

View File

@ -78,6 +78,8 @@ int Application::run()
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getOpenGLView(); auto glview = director->getOpenGLView();
// Retain glview to avoid glview being released in the while loop
glview->retain(); glview->retain();
while(!glview->windowShouldClose()) while(!glview->windowShouldClose())
@ -86,8 +88,10 @@ int Application::run()
if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart) if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
{ {
nLast.QuadPart = nNow.QuadPart; nLast.QuadPart = nNow.QuadPart;
director->mainLoop();
// Poll event before mainloop
glview->pollEvents(); glview->pollEvents();
director->mainLoop();
} }
else else
{ {