Yo, I'm having a bit of trouble trying to figure out how to limit my fps in my game engine. The engine runs at about 1000 fps, and I can already hear my graphics card squealing . This obviously isn't good, and won't be good for anyone else playing a game made with my engine.
I wan't to know how to cap the framerate of the rendering part of the game engine, so that way the game renders far less often.
I am using a variable time step like so:
void GameSystem::GameLoop()
{
previousTime = glfwGetTime();
while(!glfwWindowShouldClose(glfwWindow))
{
currentTime = glfwGetTime();
deltaTime = (currentTime - previousTime);
previousTime = currentTime;
profiler->TimeStampStartAccumulate(PROFILER_TIMESTAMP_FPS_UNLIMITED);
InputMouseMovementProcessing();
UpdateGameEntities();
renderingEngine->StartRender();
glfwPollEvents();
profiler->TimeStampEndAccumulate(PROFILER_TIMESTAMP_FPS_UNLIMITED);
profiler->EndOfFrame();
}
delete profiler;
//CleanUpGameSystem();
glfwTerminate();
}
So it's a pretty basic gameloop.
Things i've tried that never worked:
-Sleep(1). Unfortunately, this always takes away around 1 ms to about more than 16.66666 (60 fps), Definitely NOT
acceptable. Sleeping isn't an option.
-A spinloop. Looping through a while loop and accumulating how much time elapsed until 16.66666 ms is reached, then render. This sucks because it isn't even accurate, since you have to perform operations to accumulate the time elapsed, which pollutes the time accumulations.
-Fixing a timestep for Rendering:
while(deltaTimeAccumulated > 16.66666)
{
if(deltaTimeAccumulated > 200.0)
{
//Break if entering spiral of death
deltaTimeAccumulated = 0;
break;
}
renderingEngine->StartRender();
deltaTimeAccumulated -= 16.66666;
}
This does work to some extent, except that I encounter stuttering every half second or so. This is caused by frame skipping every time the number of frames per render exceeds the average number of frames per render (imagine looping 5 times per render, 300 times in a row. Update 4 times per render, only once, and you will notice a stutter). I cannot find any way to fix this, because there is no guarantee that the number of frames per render will be the same, since elapsed time will always vary :/.
I am using GLFWGetTime(); which is high precision up to doubles, so that's not the problem.
~~~
The only solution I could come up with (a rather shoddy one) is to cheat and check if the framerate is dangerously high (like 500 or so), and just run some expensive function just to pool more time to the cpu instead of the gpu.
I'm sure i'm not the only one who has encountered this issue. Does anyone have any ideas?