Advertisement
Guest User

tiny glfw prog

a guest
Jun 20th, 2014
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "string.h"
  3.  
  4. #include "glfw3.h"
  5.  
  6. #include "windows.h"
  7.  
  8. GLFWwindow* window = 0;
  9.  
  10. //frame counter vars
  11. static unsigned int frame_count = 0;
  12. static int current_fps = 0;
  13. static double start_time = 0;
  14.  
  15. //vars for sleep calculations
  16. static double target_frame_rate = 60;
  17. static double frame_start = 0;
  18.  
  19. void calc_frame_rate()
  20. {
  21.     frame_count++;
  22.  
  23.     double elapsed = (glfwGetTime() - start_time);
  24.  
  25.     if (elapsed > 1)
  26.     {
  27.         current_fps = frame_count;
  28.         start_time = glfwGetTime();
  29.         frame_count = 0;
  30.     }
  31. }
  32.  
  33. void calc_sleep()
  34. {
  35.     double wait_time = 1.0 / (target_frame_rate);
  36.     double curr_frame_time = glfwGetTime() - frame_start;
  37.     double dur = 1000.0 * ( wait_time - curr_frame_time ) + 0.5;
  38.     int durDW = (int)dur;
  39.     if( durDW > 0 ) // ensures that we don't have a dur > 0.0 which converts to a durDW of 0.
  40.     {
  41.         Sleep( (DWORD) durDW  );
  42.     }
  43.  
  44.     double frame_end = glfwGetTime();
  45.     frame_start = frame_end;
  46. }
  47.  
  48. int main(int argc, char* args[])
  49. {
  50.     glfwInit();
  51.     window = glfwCreateWindow(640, 480, "", 0, 0);
  52.     glfwMakeContextCurrent(window);
  53.     glfwSwapInterval(0);
  54.     glfwWindowHint(GLFW_RESIZABLE, 0);
  55.  
  56.     while(!glfwWindowShouldClose(window))
  57.     {
  58.         calc_frame_rate();
  59.  
  60.             char str[64] = {};
  61.             sprintf(str, "%i", current_fps);
  62.         glfwSetWindowTitle(window, str);
  63.  
  64.             glfwSwapBuffers(window);
  65.  
  66.         calc_sleep();
  67.         glfwPollEvents();
  68.     }
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement