1b877906bSopenharmony_ci//======================================================================== 2b877906bSopenharmony_ci// Window re-opener (open/close stress test) 3b877906bSopenharmony_ci// Copyright (c) Camilla Löwy <elmindreda@glfw.org> 4b877906bSopenharmony_ci// 5b877906bSopenharmony_ci// This software is provided 'as-is', without any express or implied 6b877906bSopenharmony_ci// warranty. In no event will the authors be held liable for any damages 7b877906bSopenharmony_ci// arising from the use of this software. 8b877906bSopenharmony_ci// 9b877906bSopenharmony_ci// Permission is granted to anyone to use this software for any purpose, 10b877906bSopenharmony_ci// including commercial applications, and to alter it and redistribute it 11b877906bSopenharmony_ci// freely, subject to the following restrictions: 12b877906bSopenharmony_ci// 13b877906bSopenharmony_ci// 1. The origin of this software must not be misrepresented; you must not 14b877906bSopenharmony_ci// claim that you wrote the original software. If you use this software 15b877906bSopenharmony_ci// in a product, an acknowledgment in the product documentation would 16b877906bSopenharmony_ci// be appreciated but is not required. 17b877906bSopenharmony_ci// 18b877906bSopenharmony_ci// 2. Altered source versions must be plainly marked as such, and must not 19b877906bSopenharmony_ci// be misrepresented as being the original software. 20b877906bSopenharmony_ci// 21b877906bSopenharmony_ci// 3. This notice may not be removed or altered from any source 22b877906bSopenharmony_ci// distribution. 23b877906bSopenharmony_ci// 24b877906bSopenharmony_ci//======================================================================== 25b877906bSopenharmony_ci// 26b877906bSopenharmony_ci// This test came about as the result of bug #1262773 27b877906bSopenharmony_ci// 28b877906bSopenharmony_ci// It closes and re-opens the GLFW window every five seconds, alternating 29b877906bSopenharmony_ci// between windowed and full screen mode 30b877906bSopenharmony_ci// 31b877906bSopenharmony_ci// It also times and logs opening and closing actions and attempts to separate 32b877906bSopenharmony_ci// user initiated window closing from its own 33b877906bSopenharmony_ci// 34b877906bSopenharmony_ci//======================================================================== 35b877906bSopenharmony_ci 36b877906bSopenharmony_ci#define GLAD_GL_IMPLEMENTATION 37b877906bSopenharmony_ci#include <glad/gl.h> 38b877906bSopenharmony_ci#define GLFW_INCLUDE_NONE 39b877906bSopenharmony_ci#include <GLFW/glfw3.h> 40b877906bSopenharmony_ci 41b877906bSopenharmony_ci#include <time.h> 42b877906bSopenharmony_ci#include <stdio.h> 43b877906bSopenharmony_ci#include <stdlib.h> 44b877906bSopenharmony_ci 45b877906bSopenharmony_ci#include "linmath.h" 46b877906bSopenharmony_ci 47b877906bSopenharmony_cistatic const char* vertex_shader_text = 48b877906bSopenharmony_ci"#version 110\n" 49b877906bSopenharmony_ci"uniform mat4 MVP;\n" 50b877906bSopenharmony_ci"attribute vec2 vPos;\n" 51b877906bSopenharmony_ci"void main()\n" 52b877906bSopenharmony_ci"{\n" 53b877906bSopenharmony_ci" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 54b877906bSopenharmony_ci"}\n"; 55b877906bSopenharmony_ci 56b877906bSopenharmony_cistatic const char* fragment_shader_text = 57b877906bSopenharmony_ci"#version 110\n" 58b877906bSopenharmony_ci"void main()\n" 59b877906bSopenharmony_ci"{\n" 60b877906bSopenharmony_ci" gl_FragColor = vec4(1.0);\n" 61b877906bSopenharmony_ci"}\n"; 62b877906bSopenharmony_ci 63b877906bSopenharmony_cistatic const vec2 vertices[4] = 64b877906bSopenharmony_ci{ 65b877906bSopenharmony_ci { -0.5f, -0.5f }, 66b877906bSopenharmony_ci { 0.5f, -0.5f }, 67b877906bSopenharmony_ci { 0.5f, 0.5f }, 68b877906bSopenharmony_ci { -0.5f, 0.5f } 69b877906bSopenharmony_ci}; 70b877906bSopenharmony_ci 71b877906bSopenharmony_cistatic void error_callback(int error, const char* description) 72b877906bSopenharmony_ci{ 73b877906bSopenharmony_ci fprintf(stderr, "Error: %s\n", description); 74b877906bSopenharmony_ci} 75b877906bSopenharmony_ci 76b877906bSopenharmony_cistatic void window_close_callback(GLFWwindow* window) 77b877906bSopenharmony_ci{ 78b877906bSopenharmony_ci printf("Close callback triggered\n"); 79b877906bSopenharmony_ci} 80b877906bSopenharmony_ci 81b877906bSopenharmony_cistatic void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 82b877906bSopenharmony_ci{ 83b877906bSopenharmony_ci if (action != GLFW_PRESS) 84b877906bSopenharmony_ci return; 85b877906bSopenharmony_ci 86b877906bSopenharmony_ci switch (key) 87b877906bSopenharmony_ci { 88b877906bSopenharmony_ci case GLFW_KEY_Q: 89b877906bSopenharmony_ci case GLFW_KEY_ESCAPE: 90b877906bSopenharmony_ci glfwSetWindowShouldClose(window, GLFW_TRUE); 91b877906bSopenharmony_ci break; 92b877906bSopenharmony_ci } 93b877906bSopenharmony_ci} 94b877906bSopenharmony_ci 95b877906bSopenharmony_cistatic void close_window(GLFWwindow* window) 96b877906bSopenharmony_ci{ 97b877906bSopenharmony_ci double base = glfwGetTime(); 98b877906bSopenharmony_ci glfwDestroyWindow(window); 99b877906bSopenharmony_ci printf("Closing window took %0.3f seconds\n", glfwGetTime() - base); 100b877906bSopenharmony_ci} 101b877906bSopenharmony_ci 102b877906bSopenharmony_ciint main(int argc, char** argv) 103b877906bSopenharmony_ci{ 104b877906bSopenharmony_ci int count = 0; 105b877906bSopenharmony_ci double base; 106b877906bSopenharmony_ci GLFWwindow* window; 107b877906bSopenharmony_ci 108b877906bSopenharmony_ci srand((unsigned int) time(NULL)); 109b877906bSopenharmony_ci 110b877906bSopenharmony_ci glfwSetErrorCallback(error_callback); 111b877906bSopenharmony_ci 112b877906bSopenharmony_ci if (!glfwInit()) 113b877906bSopenharmony_ci exit(EXIT_FAILURE); 114b877906bSopenharmony_ci 115b877906bSopenharmony_ci glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 116b877906bSopenharmony_ci glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 117b877906bSopenharmony_ci 118b877906bSopenharmony_ci for (;;) 119b877906bSopenharmony_ci { 120b877906bSopenharmony_ci int width, height; 121b877906bSopenharmony_ci GLFWmonitor* monitor = NULL; 122b877906bSopenharmony_ci GLuint vertex_shader, fragment_shader, program, vertex_buffer; 123b877906bSopenharmony_ci GLint mvp_location, vpos_location; 124b877906bSopenharmony_ci 125b877906bSopenharmony_ci if (count & 1) 126b877906bSopenharmony_ci { 127b877906bSopenharmony_ci int monitorCount; 128b877906bSopenharmony_ci GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); 129b877906bSopenharmony_ci monitor = monitors[rand() % monitorCount]; 130b877906bSopenharmony_ci } 131b877906bSopenharmony_ci 132b877906bSopenharmony_ci if (monitor) 133b877906bSopenharmony_ci { 134b877906bSopenharmony_ci const GLFWvidmode* mode = glfwGetVideoMode(monitor); 135b877906bSopenharmony_ci width = mode->width; 136b877906bSopenharmony_ci height = mode->height; 137b877906bSopenharmony_ci } 138b877906bSopenharmony_ci else 139b877906bSopenharmony_ci { 140b877906bSopenharmony_ci width = 640; 141b877906bSopenharmony_ci height = 480; 142b877906bSopenharmony_ci } 143b877906bSopenharmony_ci 144b877906bSopenharmony_ci base = glfwGetTime(); 145b877906bSopenharmony_ci 146b877906bSopenharmony_ci window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); 147b877906bSopenharmony_ci if (!window) 148b877906bSopenharmony_ci { 149b877906bSopenharmony_ci glfwTerminate(); 150b877906bSopenharmony_ci exit(EXIT_FAILURE); 151b877906bSopenharmony_ci } 152b877906bSopenharmony_ci 153b877906bSopenharmony_ci if (monitor) 154b877906bSopenharmony_ci { 155b877906bSopenharmony_ci printf("Opening full screen window on monitor %s took %0.3f seconds\n", 156b877906bSopenharmony_ci glfwGetMonitorName(monitor), 157b877906bSopenharmony_ci glfwGetTime() - base); 158b877906bSopenharmony_ci } 159b877906bSopenharmony_ci else 160b877906bSopenharmony_ci { 161b877906bSopenharmony_ci printf("Opening regular window took %0.3f seconds\n", 162b877906bSopenharmony_ci glfwGetTime() - base); 163b877906bSopenharmony_ci } 164b877906bSopenharmony_ci 165b877906bSopenharmony_ci glfwSetWindowCloseCallback(window, window_close_callback); 166b877906bSopenharmony_ci glfwSetKeyCallback(window, key_callback); 167b877906bSopenharmony_ci 168b877906bSopenharmony_ci glfwMakeContextCurrent(window); 169b877906bSopenharmony_ci gladLoadGL(glfwGetProcAddress); 170b877906bSopenharmony_ci glfwSwapInterval(1); 171b877906bSopenharmony_ci 172b877906bSopenharmony_ci vertex_shader = glCreateShader(GL_VERTEX_SHADER); 173b877906bSopenharmony_ci glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 174b877906bSopenharmony_ci glCompileShader(vertex_shader); 175b877906bSopenharmony_ci 176b877906bSopenharmony_ci fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 177b877906bSopenharmony_ci glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 178b877906bSopenharmony_ci glCompileShader(fragment_shader); 179b877906bSopenharmony_ci 180b877906bSopenharmony_ci program = glCreateProgram(); 181b877906bSopenharmony_ci glAttachShader(program, vertex_shader); 182b877906bSopenharmony_ci glAttachShader(program, fragment_shader); 183b877906bSopenharmony_ci glLinkProgram(program); 184b877906bSopenharmony_ci 185b877906bSopenharmony_ci mvp_location = glGetUniformLocation(program, "MVP"); 186b877906bSopenharmony_ci vpos_location = glGetAttribLocation(program, "vPos"); 187b877906bSopenharmony_ci 188b877906bSopenharmony_ci glGenBuffers(1, &vertex_buffer); 189b877906bSopenharmony_ci glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 190b877906bSopenharmony_ci glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 191b877906bSopenharmony_ci 192b877906bSopenharmony_ci glEnableVertexAttribArray(vpos_location); 193b877906bSopenharmony_ci glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 194b877906bSopenharmony_ci sizeof(vertices[0]), (void*) 0); 195b877906bSopenharmony_ci 196b877906bSopenharmony_ci glfwSetTime(0.0); 197b877906bSopenharmony_ci 198b877906bSopenharmony_ci while (glfwGetTime() < 5.0) 199b877906bSopenharmony_ci { 200b877906bSopenharmony_ci float ratio; 201b877906bSopenharmony_ci int width, height; 202b877906bSopenharmony_ci mat4x4 m, p, mvp; 203b877906bSopenharmony_ci 204b877906bSopenharmony_ci glfwGetFramebufferSize(window, &width, &height); 205b877906bSopenharmony_ci ratio = width / (float) height; 206b877906bSopenharmony_ci 207b877906bSopenharmony_ci glViewport(0, 0, width, height); 208b877906bSopenharmony_ci glClear(GL_COLOR_BUFFER_BIT); 209b877906bSopenharmony_ci 210b877906bSopenharmony_ci mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); 211b877906bSopenharmony_ci 212b877906bSopenharmony_ci mat4x4_identity(m); 213b877906bSopenharmony_ci mat4x4_rotate_Z(m, m, (float) glfwGetTime()); 214b877906bSopenharmony_ci mat4x4_mul(mvp, p, m); 215b877906bSopenharmony_ci 216b877906bSopenharmony_ci glUseProgram(program); 217b877906bSopenharmony_ci glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 218b877906bSopenharmony_ci glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 219b877906bSopenharmony_ci 220b877906bSopenharmony_ci glfwSwapBuffers(window); 221b877906bSopenharmony_ci glfwPollEvents(); 222b877906bSopenharmony_ci 223b877906bSopenharmony_ci if (glfwWindowShouldClose(window)) 224b877906bSopenharmony_ci { 225b877906bSopenharmony_ci close_window(window); 226b877906bSopenharmony_ci printf("User closed window\n"); 227b877906bSopenharmony_ci 228b877906bSopenharmony_ci glfwTerminate(); 229b877906bSopenharmony_ci exit(EXIT_SUCCESS); 230b877906bSopenharmony_ci } 231b877906bSopenharmony_ci } 232b877906bSopenharmony_ci 233b877906bSopenharmony_ci printf("Closing window\n"); 234b877906bSopenharmony_ci close_window(window); 235b877906bSopenharmony_ci 236b877906bSopenharmony_ci count++; 237b877906bSopenharmony_ci } 238b877906bSopenharmony_ci 239b877906bSopenharmony_ci glfwTerminate(); 240b877906bSopenharmony_ci} 241b877906bSopenharmony_ci 242