I am having trouble making my game's main loop to run at exactly N fps. Let's say i want game to run at constant 60 fps. Here is part of my code:
int ticksPerSecond = 60;
int skipTicks = 1000 / ticksPerSecond;
int maxFrameskip = 10;
// ...
while(true)
{
loops = 0;
while (Environment.TickCount > nextGameTick && loops < maxFrameskip)
{
OnUpdate();
nextGameTick += skipTicks;
loops++;
}
Render();
}
As a result i am getting around 64 fps, not 60. Why is that and what am i doing wrong here?