Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

214 rivejä
5.6 KiB

5 vuotta sitten
  1. //========================================================================
  2. // Vsync enabling test
  3. // Copyright (c) Camilla Berglund <elmindreda@glfw.org>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //
  26. // This test renders a high contrast, horizontally moving bar, allowing for
  27. // visual verification of whether the set swap interval is indeed obeyed
  28. //
  29. //========================================================================
  30. #include <glad/glad.h>
  31. #include <GLFW/glfw3.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <math.h>
  35. #include "getopt.h"
  36. static int swap_tear;
  37. static int swap_interval;
  38. static double frame_rate;
  39. static void usage(void)
  40. {
  41. printf("Usage: tearing [-h] [-f]\n");
  42. printf("Options:\n");
  43. printf(" -f create full screen window\n");
  44. printf(" -h show this help\n");
  45. }
  46. static void update_window_title(GLFWwindow* window)
  47. {
  48. char title[256];
  49. sprintf(title, "Tearing detector (interval %i%s, %0.1f Hz)",
  50. swap_interval,
  51. (swap_tear && swap_interval < 0) ? " (swap tear)" : "",
  52. frame_rate);
  53. glfwSetWindowTitle(window, title);
  54. }
  55. static void set_swap_interval(GLFWwindow* window, int interval)
  56. {
  57. swap_interval = interval;
  58. glfwSwapInterval(swap_interval);
  59. update_window_title(window);
  60. }
  61. static void error_callback(int error, const char* description)
  62. {
  63. fprintf(stderr, "Error: %s\n", description);
  64. }
  65. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  66. {
  67. glViewport(0, 0, width, height);
  68. }
  69. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  70. {
  71. if (action != GLFW_PRESS)
  72. return;
  73. switch (key)
  74. {
  75. case GLFW_KEY_UP:
  76. {
  77. if (swap_interval + 1 > swap_interval)
  78. set_swap_interval(window, swap_interval + 1);
  79. break;
  80. }
  81. case GLFW_KEY_DOWN:
  82. {
  83. if (swap_tear)
  84. {
  85. if (swap_interval - 1 < swap_interval)
  86. set_swap_interval(window, swap_interval - 1);
  87. }
  88. else
  89. {
  90. if (swap_interval - 1 >= 0)
  91. set_swap_interval(window, swap_interval - 1);
  92. }
  93. break;
  94. }
  95. case GLFW_KEY_ESCAPE:
  96. glfwSetWindowShouldClose(window, 1);
  97. break;
  98. }
  99. }
  100. int main(int argc, char** argv)
  101. {
  102. int ch, width, height;
  103. float position;
  104. unsigned long frame_count = 0;
  105. double last_time, current_time;
  106. int fullscreen = GLFW_FALSE;
  107. GLFWmonitor* monitor = NULL;
  108. GLFWwindow* window;
  109. while ((ch = getopt(argc, argv, "fh")) != -1)
  110. {
  111. switch (ch)
  112. {
  113. case 'h':
  114. usage();
  115. exit(EXIT_SUCCESS);
  116. case 'f':
  117. fullscreen = GLFW_TRUE;
  118. break;
  119. }
  120. }
  121. glfwSetErrorCallback(error_callback);
  122. if (!glfwInit())
  123. exit(EXIT_FAILURE);
  124. if (fullscreen)
  125. {
  126. const GLFWvidmode* mode;
  127. monitor = glfwGetPrimaryMonitor();
  128. mode = glfwGetVideoMode(monitor);
  129. glfwWindowHint(GLFW_RED_BITS, mode->redBits);
  130. glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
  131. glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
  132. glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
  133. width = mode->width;
  134. height = mode->height;
  135. }
  136. else
  137. {
  138. width = 640;
  139. height = 480;
  140. }
  141. window = glfwCreateWindow(width, height, "", monitor, NULL);
  142. if (!window)
  143. {
  144. glfwTerminate();
  145. exit(EXIT_FAILURE);
  146. }
  147. glfwMakeContextCurrent(window);
  148. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  149. set_swap_interval(window, 0);
  150. last_time = glfwGetTime();
  151. frame_rate = 0.0;
  152. swap_tear = (glfwExtensionSupported("WGL_EXT_swap_control_tear") ||
  153. glfwExtensionSupported("GLX_EXT_swap_control_tear"));
  154. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  155. glfwSetKeyCallback(window, key_callback);
  156. glMatrixMode(GL_PROJECTION);
  157. glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
  158. glMatrixMode(GL_MODELVIEW);
  159. while (!glfwWindowShouldClose(window))
  160. {
  161. glClear(GL_COLOR_BUFFER_BIT);
  162. position = cosf((float) glfwGetTime() * 4.f) * 0.75f;
  163. glRectf(position - 0.25f, -1.f, position + 0.25f, 1.f);
  164. glfwSwapBuffers(window);
  165. glfwPollEvents();
  166. frame_count++;
  167. current_time = glfwGetTime();
  168. if (current_time - last_time > 1.0)
  169. {
  170. frame_rate = frame_count / (current_time - last_time);
  171. frame_count = 0;
  172. last_time = current_time;
  173. update_window_title(window);
  174. }
  175. }
  176. glfwTerminate();
  177. exit(EXIT_SUCCESS);
  178. }