#include "stdio.h" #include "string.h" #include "glfw3.h" #include "windows.h" GLFWwindow* window = 0; //frame counter vars static unsigned int frame_count = 0; static int current_fps = 0; static double start_time = 0; //vars for sleep calculations static double target_frame_rate = 60; static double frame_start = 0; void calc_frame_rate() { frame_count++; double elapsed = (glfwGetTime() - start_time); if (elapsed > 1) { current_fps = frame_count; start_time = glfwGetTime(); frame_count = 0; } } void calc_sleep() { double wait_time = 1.0 / (target_frame_rate); double curr_frame_time = glfwGetTime() - frame_start; double dur = 1000.0 * ( wait_time - curr_frame_time ) + 0.5; int durDW = (int)dur; if( durDW > 0 ) // ensures that we don't have a dur > 0.0 which converts to a durDW of 0. { Sleep( (DWORD) durDW ); } double frame_end = glfwGetTime(); frame_start = frame_end; } int main(int argc, char* args[]) { glfwInit(); window = glfwCreateWindow(640, 480, "", 0, 0); glfwMakeContextCurrent(window); glfwSwapInterval(0); glfwWindowHint(GLFW_RESIZABLE, 0); while(!glfwWindowShouldClose(window)) { calc_frame_rate(); char str[64] = {}; sprintf(str, "%i", current_fps); glfwSetWindowTitle(window, str); glfwSwapBuffers(window); calc_sleep(); glfwPollEvents(); } return 0; }