From f966ff894a341265f0d90b93110ddae9c90a742c Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 19 May 2013 19:18:18 +0200 Subject: [PATCH] Added refresh rate calculation to tearing test. --- tests/tearing.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/tests/tearing.c b/tests/tearing.c index 93584285..8d46e63b 100644 --- a/tests/tearing.c +++ b/tests/tearing.c @@ -35,19 +35,25 @@ #include static int swap_interval; +static double frame_rate; -static void set_swap_interval(GLFWwindow* window, int interval) +static void update_window_title(GLFWwindow* window) { char title[256]; - swap_interval = interval; - glfwSwapInterval(swap_interval); - - sprintf(title, "Tearing detector (interval %i)", swap_interval); + sprintf(title, "Tearing detector (interval %i, %0.1f Hz)", + swap_interval, frame_rate); glfwSetWindowTitle(window, title); } +static void set_swap_interval(GLFWwindow* window, int interval) +{ + swap_interval = interval; + glfwSwapInterval(swap_interval); + update_window_title(window); +} + static void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); @@ -67,6 +73,8 @@ static void key_callback(GLFWwindow* window, int key, int action) int main(void) { float position; + unsigned long frame_count = 0; + double last_time, current_time; GLFWwindow* window; glfwSetErrorCallback(error_callback); @@ -82,7 +90,10 @@ int main(void) } glfwMakeContextCurrent(window); - set_swap_interval(window, swap_interval); + set_swap_interval(window, 0); + + last_time = glfwGetTime(); + frame_rate = 0.0; glfwSetWindowSizeCallback(window, window_size_callback); glfwSetKeyCallback(window, key_callback); @@ -100,6 +111,17 @@ int main(void) glfwSwapBuffers(window); glfwPollEvents(); + + frame_count++; + + current_time = glfwGetTime(); + if (current_time - last_time > 1.0) + { + frame_rate = frame_count / (current_time - last_time); + frame_count = 0; + last_time = current_time; + update_window_title(window); + } } glfwTerminate();