// TICKS_PER_SEC is the update rate, I have it set at 25Hzvoid run(void){ const int64_t timestep = tickfreq()/TICKS_PER_SEC; // TODO: calc once static int64_t next_tick = 0; int count = 0; float a; if (!next_tick) next_tick = tickcount(); while (tickcount() >= next_tick && count < MAX_FRAMESKIP) { update(); next_tick += timestep; ++count; } a = (tickcount()+timestep-next_tick)/(double)timestep; draw(a); // I use a to interpolate between previous and current state glfwSwapBuffers(); // Window messages gets pumped here by glfw}
Here is the main loop:
start = tickcount(); invfreq = 1/(double)tickfreq(); done = 0; while (!done) { done = !glfwGetWindowParam(GLFW_OPENED); if (glfwGetKey(GLFW_KEY_ESC)) done = 1; run(); if (framelimit > 0) { while ((tickcount()-start)*invfreq < 1/((float)framelimit*1.2f)) Sleep(1); while ((tickcount()-start)*invfreq < 1/(float)framelimit) ; } { char str[128]; double fps = 1/((tickcount()-start)*invfreq); sprintf(str, "fps: %.2Lf", fps); glfwSetWindowTitle(str); } start = tickcount(); }
I try to check if v-sync is on, if it is I set framelimit to 0, otherwise I set it to 60 (or let the user set the limit).