reopen.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //========================================================================
  2. // Window re-opener (open/close stress test)
  3. // Copyright (c) Camilla Löwy <[email protected]>
  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. #define GLAD_GL_IMPLEMENTATION
  36. #include <glad/gl.h>
  37. #define GLFW_INCLUDE_NONE
  38. #include <GLFW/glfw3.h>
  39. #include <time.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include "linmath.h"
  43. static const char* vertex_shader_text =
  44. "#version 110\n"
  45. "uniform mat4 MVP;\n"
  46. "attribute vec2 vPos;\n"
  47. "void main()\n"
  48. "{\n"
  49. " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
  50. "}\n";
  51. static const char* fragment_shader_text =
  52. "#version 110\n"
  53. "void main()\n"
  54. "{\n"
  55. " gl_FragColor = vec4(1.0);\n"
  56. "}\n";
  57. static const vec2 vertices[4] =
  58. {
  59. { -0.5f, -0.5f },
  60. { 0.5f, -0.5f },
  61. { 0.5f, 0.5f },
  62. { -0.5f, 0.5f }
  63. };
  64. static void error_callback(int error, const char* description)
  65. {
  66. fprintf(stderr, "Error: %s\n", description);
  67. }
  68. static void window_close_callback(GLFWwindow* window)
  69. {
  70. printf("Close callback triggered\n");
  71. }
  72. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  73. {
  74. if (action != GLFW_PRESS)
  75. return;
  76. switch (key)
  77. {
  78. case GLFW_KEY_Q:
  79. case GLFW_KEY_ESCAPE:
  80. glfwSetWindowShouldClose(window, GLFW_TRUE);
  81. break;
  82. }
  83. }
  84. static void close_window(GLFWwindow* window)
  85. {
  86. double base = glfwGetTime();
  87. glfwDestroyWindow(window);
  88. printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
  89. }
  90. int main(int argc, char** argv)
  91. {
  92. int count = 0;
  93. double base;
  94. GLFWwindow* window;
  95. srand((unsigned int) time(NULL));
  96. glfwSetErrorCallback(error_callback);
  97. if (!glfwInit())
  98. exit(EXIT_FAILURE);
  99. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  100. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  101. for (;;)
  102. {
  103. int width, height;
  104. GLFWmonitor* monitor = NULL;
  105. GLuint vertex_shader, fragment_shader, program, vertex_buffer;
  106. GLint mvp_location, vpos_location;
  107. if (count & 1)
  108. {
  109. int monitorCount;
  110. GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
  111. monitor = monitors[rand() % monitorCount];
  112. }
  113. if (monitor)
  114. {
  115. const GLFWvidmode* mode = glfwGetVideoMode(monitor);
  116. width = mode->width;
  117. height = mode->height;
  118. }
  119. else
  120. {
  121. width = 640;
  122. height = 480;
  123. }
  124. base = glfwGetTime();
  125. window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
  126. if (!window)
  127. {
  128. glfwTerminate();
  129. exit(EXIT_FAILURE);
  130. }
  131. if (monitor)
  132. {
  133. printf("Opening full screen window on monitor %s took %0.3f seconds\n",
  134. glfwGetMonitorName(monitor),
  135. glfwGetTime() - base);
  136. }
  137. else
  138. {
  139. printf("Opening regular window took %0.3f seconds\n",
  140. glfwGetTime() - base);
  141. }
  142. glfwSetWindowCloseCallback(window, window_close_callback);
  143. glfwSetKeyCallback(window, key_callback);
  144. glfwMakeContextCurrent(window);
  145. gladLoadGL(glfwGetProcAddress);
  146. glfwSwapInterval(1);
  147. vertex_shader = glCreateShader(GL_VERTEX_SHADER);
  148. glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
  149. glCompileShader(vertex_shader);
  150. fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
  151. glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
  152. glCompileShader(fragment_shader);
  153. program = glCreateProgram();
  154. glAttachShader(program, vertex_shader);
  155. glAttachShader(program, fragment_shader);
  156. glLinkProgram(program);
  157. mvp_location = glGetUniformLocation(program, "MVP");
  158. vpos_location = glGetAttribLocation(program, "vPos");
  159. glGenBuffers(1, &vertex_buffer);
  160. glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
  161. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  162. glEnableVertexAttribArray(vpos_location);
  163. glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
  164. sizeof(vertices[0]), (void*) 0);
  165. glfwSetTime(0.0);
  166. while (glfwGetTime() < 5.0)
  167. {
  168. float ratio;
  169. int width, height;
  170. mat4x4 m, p, mvp;
  171. glfwGetFramebufferSize(window, &width, &height);
  172. ratio = width / (float) height;
  173. glViewport(0, 0, width, height);
  174. glClear(GL_COLOR_BUFFER_BIT);
  175. mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
  176. mat4x4_identity(m);
  177. mat4x4_rotate_Z(m, m, (float) glfwGetTime());
  178. mat4x4_mul(mvp, p, m);
  179. glUseProgram(program);
  180. glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
  181. glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
  182. glfwSwapBuffers(window);
  183. glfwPollEvents();
  184. if (glfwWindowShouldClose(window))
  185. {
  186. close_window(window);
  187. printf("User closed window\n");
  188. glfwTerminate();
  189. exit(EXIT_SUCCESS);
  190. }
  191. }
  192. printf("Closing window\n");
  193. close_window(window);
  194. count++;
  195. }
  196. glfwTerminate();
  197. }