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