reopen.c 6.6 KB

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