reopen.c 6.6 KB

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