• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //========================================================================
2 // Window re-opener (open/close stress test)
3 // Copyright (c) Camilla Berglund <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/glad.h>
37 #include <GLFW/glfw3.h>
38 
39 #include <time.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 
error_callback(int error,const char * description)43 static void error_callback(int error, const char* description)
44 {
45     fprintf(stderr, "Error: %s\n", description);
46 }
47 
framebuffer_size_callback(GLFWwindow * window,int width,int height)48 static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
49 {
50     glViewport(0, 0, width, height);
51 }
52 
window_close_callback(GLFWwindow * window)53 static void window_close_callback(GLFWwindow* window)
54 {
55     printf("Close callback triggered\n");
56 }
57 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)58 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
59 {
60     if (action != GLFW_PRESS)
61         return;
62 
63     switch (key)
64     {
65         case GLFW_KEY_Q:
66         case GLFW_KEY_ESCAPE:
67             glfwSetWindowShouldClose(window, GLFW_TRUE);
68             break;
69     }
70 }
71 
open_window(int width,int height,GLFWmonitor * monitor)72 static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
73 {
74     double base;
75     GLFWwindow* window;
76 
77     base = glfwGetTime();
78 
79     window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
80     if (!window)
81         return NULL;
82 
83     glfwMakeContextCurrent(window);
84     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
85     glfwSwapInterval(1);
86 
87     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
88     glfwSetWindowCloseCallback(window, window_close_callback);
89     glfwSetKeyCallback(window, key_callback);
90 
91     if (monitor)
92     {
93         printf("Opening full screen window on monitor %s took %0.3f seconds\n",
94                glfwGetMonitorName(monitor),
95                glfwGetTime() - base);
96     }
97     else
98     {
99         printf("Opening regular window took %0.3f seconds\n",
100                glfwGetTime() - base);
101     }
102 
103     return window;
104 }
105 
close_window(GLFWwindow * window)106 static void close_window(GLFWwindow* window)
107 {
108     double base = glfwGetTime();
109     glfwDestroyWindow(window);
110     printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
111 }
112 
main(int argc,char ** argv)113 int main(int argc, char** argv)
114 {
115     int count = 0;
116     GLFWwindow* window;
117 
118     srand((unsigned int) time(NULL));
119 
120     glfwSetErrorCallback(error_callback);
121 
122     if (!glfwInit())
123         exit(EXIT_FAILURE);
124 
125     for (;;)
126     {
127         int width, height;
128         GLFWmonitor* monitor = NULL;
129 
130         if (count % 2 == 0)
131         {
132             int monitorCount;
133             GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
134             monitor = monitors[rand() % monitorCount];
135         }
136 
137         if (monitor)
138         {
139             const GLFWvidmode* mode = glfwGetVideoMode(monitor);
140             width = mode->width;
141             height = mode->height;
142         }
143         else
144         {
145             width = 640;
146             height = 480;
147         }
148 
149         window = open_window(width, height, monitor);
150         if (!window)
151         {
152             glfwTerminate();
153             exit(EXIT_FAILURE);
154         }
155 
156         glMatrixMode(GL_PROJECTION);
157         glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
158         glMatrixMode(GL_MODELVIEW);
159 
160         glfwSetTime(0.0);
161 
162         while (glfwGetTime() < 5.0)
163         {
164             glClear(GL_COLOR_BUFFER_BIT);
165 
166             glPushMatrix();
167             glRotatef((GLfloat) glfwGetTime() * 100.f, 0.f, 0.f, 1.f);
168             glRectf(-0.5f, -0.5f, 1.f, 1.f);
169             glPopMatrix();
170 
171             glfwSwapBuffers(window);
172             glfwPollEvents();
173 
174             if (glfwWindowShouldClose(window))
175             {
176                 close_window(window);
177                 printf("User closed window\n");
178 
179                 glfwTerminate();
180                 exit(EXIT_SUCCESS);
181             }
182         }
183 
184         printf("Closing window\n");
185         close_window(window);
186 
187         count++;
188     }
189 
190     glfwTerminate();
191 }
192 
193