From bbb970301cb935507bc172213d05b1255ba955c9 Mon Sep 17 00:00:00 2001 From: cosmosninja Date: Sun, 4 May 2008 00:08:49 +0000 Subject: [PATCH] If drawing more than 125 frames per second, process the game every 10 miliseconds (100 frames per second, because that's the minimum amount of time we will be able to get away with); this avoids STK using 100% of the cpu without reason; won't really make a difference when the game is slow. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1808 178a84e3-b1eb-0310-8ba1-8eac791a3b58 --- src/game_manager.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/game_manager.cpp b/src/game_manager.cpp index e28307be9..447950d9e 100644 --- a/src/game_manager.cpp +++ b/src/game_manager.cpp @@ -64,13 +64,26 @@ void GameManager::run() bool music_on = false; m_curr_time = SDL_GetTicks(); + float dt; while(!m_abort) { inputDriver->input(); m_prev_time = m_curr_time; m_curr_time = SDL_GetTicks(); - float dt = (m_curr_time - m_prev_time ) * 0.001f; + dt = m_curr_time - m_prev_time; + + //This avoid wasting CPU cycles + //1000 miliseconds / 125 frames = 125 miliseconds per frame + if( dt < 8.0f) + { + //SDL_Delay has a granularity of 10ms on most platforms, so most + //likely when frames go faster than 125 frames, at times it might + //limit the frames to 100. + SDL_Delay((Uint32)(10)); + dt = 10.0f; + } + dt *= 0.001f; if (!music_on && !race_manager->raceIsActive()) {