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