trying to limit my framerate (FPS) to a set maximum …my game loop is something like this:
using namespace chrono;
auto startTime=system_clock::now();
while(true)
{
//do game stuff...
//render frame...
auto endTime = system_clock::now();
duration<double, std::milli> delta = endTime - startTime;
startTime = EndTime;
double frameTime = delta.count();
//I have tried to cap framerate using something like this:
double FPSMAX=1000.0/60.0;
if (delta.count() < FPSMAX)
{
duration<double, std::milli> delta_ms(FPSMAX - delta.count() );
auto delta_ms_duration = duration_cast<milliseconds>delta_ms
this_thread::sleep_for(milliseconds(delta_ms_duration.count()));
}
}
The timer part works fine and I use the “frameTime” variable to multiply with movement speeds and so on. The problem is when I try the framerate capping part…while it does seem to cap FPS everything becomes jerky… My FPS shows about 60 FPS but something is definitely wrong here.