void GameTimer::waitUntilEndOfFrame() { auto dt = calculateDeltaTime(); while(dt + 0.001 < m_targetDeltaTime) { m_stallFunc(dt); dt = calculateDeltaTime(); } }
//////////////////////////////////////////////////////// // // TextureTest // //////////////////////////////////////////////////////// void TextureTest::performTestsPNG(const char* filename) { struct timeval now; Texture2D *texture; auto cache = Director::getInstance()->getTextureCache(); Texture2D::PixelFormat defaultFormat = Texture2D::getDefaultAlphaPixelFormat(); log("RGBA 8888"); Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA8888); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) log(" ms:%f", calculateDeltaTime(&now) ); else log(" ERROR"); cache->removeTexture(texture); log("RGBA 4444"); Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGBA4444); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) log(" ms:%f", calculateDeltaTime(&now) ); else log(" ERROR"); cache->removeTexture(texture); log("RGBA 5551"); Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB5A1); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) log(" ms:%f", calculateDeltaTime(&now) ); else log(" ERROR"); cache->removeTexture(texture); log("RGB 565"); Texture2D::setDefaultAlphaPixelFormat(Texture2D::PixelFormat::RGB565); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) log(" ms:%f", calculateDeltaTime(&now) ); else log(" ERROR"); cache->removeTexture(texture); Texture2D::setDefaultAlphaPixelFormat(defaultFormat); }
//////////////////////////////////////////////////////// // // TextureTest // //////////////////////////////////////////////////////// void TextureTest::performTestsPNG(const char* filename) { struct timeval now; CCTexture2D *texture; CCTextureCache *cache = CCTextureCache::sharedTextureCache(); CCLog("RGBA 8888"); CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA8888); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) CCLog(" ms:%f\n", calculateDeltaTime(&now) ); else CCLog(" ERROR\n"); cache->removeTexture(texture); //CCLog("RGBA 4444"); //CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGBA4444); //gettimeofday(&now, NULL); //texture = cache->addImage(filename); //if( texture ) // CCLog(" ms:%f\n", calculateDeltaTime(&now) ); //else // CCLog(" ERROR\n"); //cache->removeTexture(texture); /* CCLog("RGBA 5551"); CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGB5A1); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) CCLog(" ms:%f\n", calculateDeltaTime(&now) ); else CCLog(" ERROR\n"); cache->removeTexture(texture);*/ CCLog("RGB 565"); CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_RGB565); gettimeofday(&now, NULL); texture = cache->addImage(filename); if( texture ) CCLog(" ms:%f\n", calculateDeltaTime(&now) ); else CCLog(" ERROR\n"); cache->removeTexture(texture); // restore default CCTexture2D::setDefaultAlphaPixelFormat(kCCTexture2DPixelFormat_Default); }
// Draw the SCene void CCDirector::drawScene(void) { // calculate "global" dt calculateDeltaTime(); //tick before glClear: issue #533 if (! m_bPaused) { CCScheduler::sharedScheduler()->tick(m_fDeltaTime); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* to avoid flickr, nextScene MUST be here: after tick and before draw. XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */ if (m_pNextScene) { setNextScene(); } glPushMatrix(); applyOrientation(); // By default enable VertexArray, ColorArray, TextureCoordArray and Texture2D CC_ENABLE_DEFAULT_GL_STATES(); // draw the scene if (m_pRunningScene) { m_pRunningScene->visit(); } // draw the notifications node if (m_pNotificationNode) { m_pNotificationNode->visit(); } if (m_bDisplayFPS) { showFPS(); } #if CC_ENABLE_PROFILERS showProfilers(); #endif CC_DISABLE_DEFAULT_GL_STATES(); glPopMatrix(); m_uTotalFrames++; // swap buffers if (m_pobOpenGLView) { m_pobOpenGLView->swapBuffers(); } }
//---------------------------------------------------------------------------- // Callback method called when system is idle. void Window::idleCallback(void) { delta_time = calculateDeltaTime(); root->update(delta_time); displayCallback(); // call display routine to redraw cube }
bool Director::drawScene() { // CALCULATE DELTA TIME calculateDeltaTime(); // DISPATCH EVENTS EventManager::Instance().dispatchEvents(); // SCHEDULE if(!m_isPaused) { Scheduler::Instance().tick( m_dt ); ActionManager::Instance().update( m_dt ); } if(m_dirtyFlags) updateProjection(); if(p_nextScene) setNextScene(); // RENDERING #if FZ_RENDER_ON_DEMAND bool newContent = m_sceneIsDirty; if(m_sceneIsDirty) { #endif #if !FZ_GL_SHADERS glLoadIdentity(); #endif MS::loadBaseMatrix(m_transformMV); // CLEAR OPENGL BUFFERS fzGLClearColor(m_clearColor); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if(p_runningScene) { p_runningScene->internalVisit(); CHECK_GL_ERROR_DEBUG(); } FZ_ASSERT(MS::getLevel() == 1, "A matrix hasn't been popped. Review the push/pop code."); // SHOW FPS if( m_displayFPS ) showFPS(); #if FZ_RENDER_ON_DEMAND m_sceneIsDirty = false; } #endif PerformManager::Instance().clean(); #if FZ_RENDER_ON_DEMAND return newContent; #else return true; #endif }
//绘制场景 每帧被调用 void CCDirector::drawScene(){ // calculate global delta time m_fDeltaTime calculateDeltaTime(); //执行调度,更新每个节点注册的调度动作 CCMoveTo CCScaleTo and so on m_pScheduler->update(m_fDeltaTime); //clear buffers to preset values glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 如果有要进入下个场景 就设置当前场景为下个场景 nice // if (m_pNextScene) // { // setNextScene(); // } //draw the scene if (m_pRunningScene) { m_pRunningScene->visit(); } // // // draw the notifications node // Useful to hook a notification node // 通知节点 游戏只有一个此节点 在最后渲染 所以渲染跟zorder无关的(需要注意) // if (m_pNotificationNode) // { // m_pNotificationNode->visit(); // } // 显示FPS(frame rate) SPF(每帧秒数) numberOfDraws // if (m_bDisplayStats) // { // showStats(); // } // // swap buffers // if (m_pobOpenGLView) // { // m_pobOpenGLView->swapBuffers(); // } // // 计算MPF // if (m_bDisplayStats) // { // calculateMPF(); // } }
/// Brief: Calculates deltaTime, smoothDeltaTime and accumulates the frame time. /// When 1 second has passed the FPS is calculated and saved and counters are reset. /// Before doing any calculations startFrame() calls waitForEndOfFrame in /// order to fix the frame time before starting the next. void GameTimer::startFrame() { waitUntilEndOfFrame(); m_deltaTime = calculateDeltaTime(); m_frameStart = m_getTimeFunc(); if((m_timeAcc) > seconds_t(1.0f)) { m_fps = m_frameAcc; m_timeAcc = seconds_t(0.0f); m_frameAcc = 0; } m_timeAcc += m_deltaTime; ++m_frameAcc; m_smoothDeltaTime = calculateSmoothDeltaTime(); }
void wyDirector::drawFrame() { // process auto release pool wyClearAutoReleasePool(); // check frame rate setting, but neglect it if in screenshot mode #ifndef WY_CFLAG_SCREENSHOT_MODE if(m_maxFrameRate > 0) { int64_t now = wyUtils::currentTimeMillis(); m_savedDelta += now - m_lastFrameTime; m_lastFrameTime = now; if(m_savedDelta < m_minFrameInterval) { // sleep to save power usleep((m_minFrameInterval - m_savedDelta) * 1000); // recalculate saved delta now = wyUtils::currentTimeMillis(); m_savedDelta += now - m_lastFrameTime; m_lastFrameTime = now; m_savedDelta %= m_minFrameInterval; } else { m_savedDelta %= m_minFrameInterval; } } #endif // need check texture? if(m_needCheckTexture) { gTextureManager->invalidateAllTextures(); m_needCheckTexture = false; } // set default state glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); // if we have next scene to set, reset delta time if(!m_UIPaused) { if(m_nextScene != NULL) { m_nextDeltaTimeZero = true; } } // calculate global delta calculateDeltaTime(); // if in screenshot mode, use 60fps always #ifdef WY_CFLAG_SCREENSHOT_MODE m_delta = 1.f / 60.f; #endif if (!m_UIPaused) { // update scheduler and actions if(!m_paused) { gScheduler->tickLocked(m_delta * m_tickFactor); } // to avoid flicker, nextScene MUST be here: after tick and before draw if(m_nextScene != NULL) { gotoNextScene(); } } if(!m_paused) { // draw the scene if(m_runningScene != NULL) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); m_runningScene->visit(); } /* * can't move action tick before visit running scene. sometimes the internal * state may not consistent because scheduler already updated before visit. * an example is: move a particle system with an action, can see particle system * shaked consistently. */ if(!m_UIPaused) gActionManager->tick(m_delta * m_tickFactor); #ifndef WY_CFLAG_SCREENSHOT_MODE // calcuate fps or not // but don't calculate fps if in screenshot mode if(m_calculateFPS) calculateFPS(); // show fps or not // but don't show fps if in screenshot mode if(m_displayFPS) showFPS(); // make screenshot if(m_makeScreenshot) { m_makeScreenshot = false; // make screenshot wyUtils::makeScreenshot(m_screenshotPath, m_screenshotRect); // notify notifyDirectorScreenCaptured(); // free path wyFree((void*)m_screenshotPath); m_screenshotPath = NULL; } #endif // #ifndef WY_CFLAG_SCREENSHOT_MODE } // make screenshot #ifdef WY_CFLAG_SCREENSHOT_MODE char fsPath[128]; sprintf(fsPath, "/sdcard/WiEngine/%06d.png", sScreenshotFrameIndex++); const char* path = wyUtils::mapLocalPath(fsPath); wyUtils::makeScreenshot(path); wyFree((void*)path); #endif // #ifdef WY_CFLAG_SCREENSHOT_MODE // process events gEventDispatcher->processEventsLocked(); }