Platformer in OpenGL
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

192 linhas
5.0 KiB

5 anos atrás
  1. //========================================================================
  2. // Window re-opener (open/close stress 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 came about as the result of bug #1262773
  27. //
  28. // It closes and re-opens the GLFW window every five seconds, alternating
  29. // between windowed and full screen mode
  30. //
  31. // It also times and logs opening and closing actions and attempts to separate
  32. // user initiated window closing from its own
  33. //
  34. //========================================================================
  35. #include <glad/glad.h>
  36. #include <GLFW/glfw3.h>
  37. #include <time.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. static void error_callback(int error, const char* description)
  41. {
  42. fprintf(stderr, "Error: %s\n", description);
  43. }
  44. static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  45. {
  46. glViewport(0, 0, width, height);
  47. }
  48. static void window_close_callback(GLFWwindow* window)
  49. {
  50. printf("Close callback triggered\n");
  51. }
  52. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  53. {
  54. if (action != GLFW_PRESS)
  55. return;
  56. switch (key)
  57. {
  58. case GLFW_KEY_Q:
  59. case GLFW_KEY_ESCAPE:
  60. glfwSetWindowShouldClose(window, GLFW_TRUE);
  61. break;
  62. }
  63. }
  64. static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
  65. {
  66. double base;
  67. GLFWwindow* window;
  68. base = glfwGetTime();
  69. window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
  70. if (!window)
  71. return NULL;
  72. glfwMakeContextCurrent(window);
  73. gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  74. glfwSwapInterval(1);
  75. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  76. glfwSetWindowCloseCallback(window, window_close_callback);
  77. glfwSetKeyCallback(window, key_callback);
  78. if (monitor)
  79. {
  80. printf("Opening full screen window on monitor %s took %0.3f seconds\n",
  81. glfwGetMonitorName(monitor),
  82. glfwGetTime() - base);
  83. }
  84. else
  85. {
  86. printf("Opening regular window took %0.3f seconds\n",
  87. glfwGetTime() - base);
  88. }
  89. return window;
  90. }
  91. static void close_window(GLFWwindow* window)
  92. {
  93. double base = glfwGetTime();
  94. glfwDestroyWindow(window);
  95. printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
  96. }
  97. int main(int argc, char** argv)
  98. {
  99. int count = 0;
  100. GLFWwindow* window;
  101. srand((unsigned int) time(NULL));
  102. glfwSetErrorCallback(error_callback);
  103. if (!glfwInit())
  104. exit(EXIT_FAILURE);
  105. for (;;)
  106. {
  107. int width, height;
  108. GLFWmonitor* monitor = NULL;
  109. if (count % 2 == 0)
  110. {
  111. int monitorCount;
  112. GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
  113. monitor = monitors[rand() % monitorCount];
  114. }
  115. if (monitor)
  116. {
  117. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  118. width = mode->width;
  119. height = mode->height;
  120. }
  121. else
  122. {
  123. width = 640;
  124. height = 480;
  125. }
  126. window = open_window(width, height, monitor);
  127. if (!window)
  128. {
  129. glfwTerminate();
  130. exit(EXIT_FAILURE);
  131. }
  132. glMatrixMode(GL_PROJECTION);
  133. glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
  134. glMatrixMode(GL_MODELVIEW);
  135. glfwSetTime(0.0);
  136. while (glfwGetTime() < 5.0)
  137. {
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. glPushMatrix();
  140. glRotatef((GLfloat) glfwGetTime() * 100.f, 0.f, 0.f, 1.f);
  141. glRectf(-0.5f, -0.5f, 1.f, 1.f);
  142. glPopMatrix();
  143. glfwSwapBuffers(window);
  144. glfwPollEvents();
  145. if (glfwWindowShouldClose(window))
  146. {
  147. close_window(window);
  148. printf("User closed window\n");
  149. glfwTerminate();
  150. exit(EXIT_SUCCESS);
  151. }
  152. }
  153. printf("Closing window\n");
  154. close_window(window);
  155. count++;
  156. }
  157. glfwTerminate();
  158. }