diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index b6ccbe42..07bb9356 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -302,42 +302,38 @@ int main(int argc, char *argv[]) } #endif - volatile Uint32 time, timePrev = 0; + volatile Uint32 time = 0; + volatile Uint32 timePrev = 0; + volatile Uint32 accumulator = 0; game.infocus = true; key.isActive = true; game.gametimer = 0; while(!key.quitProgram) { + timePrev = time; time = SDL_GetTicks(); // Update network per frame. NETWORK_update(); - //framerate limit to 30 - Uint32 timetaken = time - timePrev; - if(game.gamestate==EDITORMODE) + //timestep limit to 30 + const float rawdeltatime = static_cast(time - timePrev); + accumulator += rawdeltatime; + + Uint32 timesteplimit; + if (game.gamestate == EDITORMODE) { - if (timetaken < 24) - { - volatile Uint32 delay = 24 - timetaken; - SDL_Delay( delay ); - time = SDL_GetTicks(); - } - timePrev = time; - - }else{ - if (timetaken < game.gameframerate) - { - volatile Uint32 delay = game.gameframerate - timetaken; - SDL_Delay( delay ); - time = SDL_GetTicks(); - } - timePrev = time; - + timesteplimit = 24; + } + else + { + timesteplimit = game.gameframerate; } - + while (accumulator >= timesteplimit) + { + accumulator = fmodf(accumulator, timesteplimit); key.Poll(); if(key.toggleFullscreen) @@ -556,6 +552,9 @@ int main(int argc, char *argv[]) graphics.processfade(); game.gameclock(); gameScreen.FlipScreen(); + } + const float deltatime = rawdeltatime/1000.0f * 34.0f / timesteplimit; + const float alpha = static_cast(accumulator) / timesteplimit; } game.savestats();