void CInput::waitForAnyInput() { float acc = 0.0f; float start = timerTicks(); float elapsed = 0.0f; float curr = 0.0f; bool done = false; while(1) { const float logicLatency = gTimer.LogicLatency(); curr = timerTicks(); if(gTimer.resetLogicSignal()) start = curr; elapsed = curr - start; start = timerTicks(); acc += elapsed; // Perform the game cycle while( acc > logicLatency ) { // Poll Inputs //gInput.pollEvents(); //pollEvents(); // TODO: We might introduce a nice timer here, but really required, because if we get here, // everything is halted anyways. It only might reduce the amount of CPU cycles to reduce... if(getPressedAnyCommand()) { done = true; } acc -= logicLatency; } if(done) break; elapsed = timerTicks() - start; int waitTime = logicLatency - elapsed; // wait time remaining in current loop if( waitTime > 0 ) timerDelay(waitTime); } }
bool CTimer::HasTimeElapsed(int msecs) { unsigned int CurTime = timerTicks(); if ((signed int)(CurTime - m_LastSecTime) >= msecs) { m_LastSecTime = CurTime; return true; } return false; }
// will return nonzero once per second bool CTimer::HasSecElapsed(void) { unsigned int CurTime = timerTicks(); if ((signed int)(CurTime - m_LastSecTime) >= MSPERSEC) { m_LastSecTime = CurTime; return true; } return false; }
CTimer::CTimer() { m_FPSCountTime = m_LoopPS = m_LPS = m_FPS = 0; m_SyncCount = m_LoopCount = m_LogicCount = m_FrameCount = 0; m_FrameCountSkip = m_SkipPS = 0; m_FrameSkip = false; m_Ticks = 0; setRates(DEFAULT_LPS, DEFAULT_FPS, DEFAULT_SYNC); #if defined(WIZ) WIZ_ptimer_init(); #endif m_SyncStartTime = m_LoopStartTime = timerTicks(); g_pLogFile->textOut(GREEN, true, "Starting timer driver...\n"); }
/** * \brief This is the main run cycle of the game, * no matter what happens in the game logic or * which engine is chosen, it always get to this point * Mainly timer and logic processes are performed here. */ void CGame::run() { float acc = 0.0f; float start = 0.0f; float elapsed = 0.0f; float total_elapsed = 0.0f; float curr = 0.0f; int counter = 0; while(1) { const float logicLatency = g_pTimer->LogicLatency(); const float renderLatency = g_pTimer->RenderLatency(); curr = timerTicks(); if(g_pTimer->resetLogicSingal()) start = curr; elapsed = curr - start; start = timerTicks(); // GameState previous; // GameState current; /*if ( elapsed > 0.25 ) elapsed = 0.25; // note: max frame time to avoid spiral of death */ acc += elapsed; // Perform the game cycle while( acc > logicLatency ) { // Poll Inputs g_pInput->pollEvents(); // Ponder Game Control mGameControl.ponder(); /* previousState = currentState; integrate( currentState, logicLatency ); */ // Apply graphical effects if any. It does not render, it only prepares for the rendering task. g_pGfxEngine->ponder(); acc -= logicLatency; } // Now we render the whole GameControl Object to the blit surface mGameControl.render(); // Apply graphical effects if any. g_pGfxEngine->render(); // Pass all the surfaces to one. Some special surfaces are used and are collected here g_pVideoDriver->collectSurfaces(); // Now you really render the screen // When enabled, it also will apply Filters g_pVideoDriver->updateDisplay(); /* const double alpha = acc / logicLatency; State state = currentState*alpha + previousState * ( 1.0 - alpha ); render( state ); */ elapsed = timerTicks() - start; total_elapsed += elapsed; if( mGameControl.mustShutdown() ) break; int waitTime = renderLatency - elapsed; // wait time remaining in current loop if( waitTime > 0 ) timerDelay(waitTime); total_elapsed += static_cast<float>(waitTime); // This will refresh the fps display, so it stays readable and calculates an average value. counter++; if(counter >= 100) { counter = 0; g_pTimer->setTimeforLastLoop(total_elapsed/100.0f); total_elapsed = 0.0f; } } }
////////////////////////////////////////////////////////// // Those are for measuring the time in the game itself. // ////////////////////////////////////////////////////////// void CTimer::ResetSecondsTimer(void) { m_LastSecTime = timerTicks(); }
void CTimer::TimeToDelay( void ) { signed int duration; signed int starttime; signed int delay; unsigned int curtime = timerTicks(); m_LoopCount++; m_SyncCount++; // If the Sync rate is met, check time took if (m_SyncCount>=m_SyncRate) { duration = m_SyncDuration; starttime = m_SyncStartTime; } else { duration = m_LoopDuration; starttime = m_LoopStartTime; } // Delay for the remaining time delay = duration + m_Ticks - (signed int)(curtime - starttime); // Cap the delay if (delay>duration) { delay = duration/4; } if (delay>=0) { m_Ticks = 0; m_FrameSkip = false; if (delay>0) timerDelay(delay); } else if (delay<0) { m_FrameSkip = true; m_Ticks += (signed int)(curtime - starttime) - duration; } // If the Sync rate is met, check time took if (m_SyncCount>=m_SyncRate) { m_SyncCount = 0; m_SyncStartTime = m_LoopStartTime = timerTicks(); } else { m_LoopStartTime = timerTicks(); } // Display the loops/logic/frames per second if( curtime - m_FPSCountTime >= MSPERSEC ) { m_LoopPS = m_LoopCount; m_LPS = m_LogicCount; m_FPS = m_FrameCount; m_SkipPS = m_FrameCountSkip; m_LoopCount = m_LogicCount = m_FrameCount = m_FrameCountSkip = 0; m_FPSCountTime = curtime; /* // FrameCap Limit Down if (delay>duration/2) setRates(DEFAULT_LPS, m_FrameRate+20, DEFAULT_SYNC); // FrameCap Limit Down if (m_SkipPS>m_FrameRate/4) setRates(DEFAULT_LPS, m_FrameRate-20, DEFAULT_SYNC); */ #ifdef DEBUG //g_pLogFile->ftextOut( "LOOP %d LPS %d FPS %d Skip %d\n", m_LoopPS, m_LPS, m_FPS, m_SkipPS ); #endif } }
/** * \brief This is the main run cycle of the game, * no matter what happens in the game logic or * which engine is chosen, it always get to this point * Mainly timer and logic processes are performed here. */ void GsApp::runMainCycle() { // I hope the engine has been set. Otherwise quit the app assert(mpCurEngine); mpCurEngine->start(); float acc = 0.0f; float start = 0.0f; float elapsed = 0.0f; float total_elapsed = 0.0f; float curr = 0.0f; int counter = 0; while(1) { const float logicLatency = gTimer.LogicLatency(); const float renderLatency = gTimer.RenderLatency(); curr = timerTicks(); if(gTimer.resetLogicSignal()) start = curr; elapsed = curr - start; start = timerTicks(); acc += elapsed; // Perform the game cycle while( acc > logicLatency ) { // Poll Inputs gInput.pollEvents(); // Process App Events gEventManager.processSinks(); // Ponder Game Control ponder(logicLatency); acc -= logicLatency; } // Now we render the whole GameControl Object to the blit surface render(); // Apply graphical effects if any. gEffectController.render(); // Pass all the surfaces to one. Some special surfaces are used and are collected here gVideoDriver.collectSurfaces(); // Now you really render the screen // When enabled, it also will apply Filters gVideoDriver.updateDisplay(); elapsed = timerTicks() - start; total_elapsed += elapsed; if( mustShutdown() ) break; int waitTime = renderLatency - elapsed; // wait time remaining in current loop if( waitTime > 0 ) timerDelay(waitTime); total_elapsed += static_cast<float>(waitTime); // This will refresh the fps display, so it stays readable and calculates an average value. counter++; if(counter >= 100) { counter = 0; gTimer.setTimeforLastLoop(total_elapsed/100.0f); total_elapsed = 0.0f; } } cleanup(); }