I am struggling to get a buttery smooth framerate with chrono.
I have a simple animated rectangle that is adding an angle (multipled by delta time) to a sin wave and after seemingly random intervals of time i get a blip. (anywhere between 10 seconds and 5 minutes).
Here is my loop (based on Gaffers Fix your timestep!)
using namespace std::chrono;
using Clock = high_resolution_clock;
init();
auto kMaxDeltatime = duration<long,std::ratio<1,60>>(1);
auto lastEndTime = Clock::now();
auto accumulator = microseconds(0);
while (!glfwWindowShouldClose(tsxWindow->window)) {
auto newEndTime = Clock::now();
const auto frameTime = newEndTime - lastEndTime;
lastEndTime = newEndTime;
accumulator += duration_cast<microseconds>(frameTime);
while ( accumulator >= kMaxDeltatime )
{
//accumulator -= duration_cast<microseconds>(kMaxDeltatime);
Update(duration_cast<milliseconds>(frameTime).count());
accumulator -= duration_cast<microseconds>(frameTime);
}
Draw(duration_cast<milliseconds>(kMaxDeltatime).count());
glfwPollEvents();
}
and my objects update:
void Object::Update(long delta_time) {
angle += 0.05 * (float)delta_time;
angle = fmod(angle,360);
double scale = 1;
transformWorld = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));
transformWorld = glm::translate(transformWorld, glm::vec3(sin(angle*PI/180.0)*scale, 0.0f, 0.0f));
// fprintf(stdout, "deltatime: %ld \n angle: %f \n WorldTransform: \n %s \n", delta_time, angle, glm::to_string(transformWorld).c_str());
// fflush(stdout);
}
and my smoothness is best with a high resolution clock (not a steady one) but even still after a long while (~5 minutes) it can stutter (not tear).
I am using linux and clang++ (swap interval is 1).