2010-09-07 11:34:51 -04:00
|
|
|
//========================================================================
|
|
|
|
// Window re-opener (open/close stress test)
|
2016-11-21 10:23:59 -05:00
|
|
|
// Copyright (c) Camilla Löwy <elmindreda@glfw.org>
|
2010-09-07 11:34:51 -04:00
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
//
|
|
|
|
// This test came about as the result of bug #1262773
|
|
|
|
//
|
|
|
|
// It closes and re-opens the GLFW window every five seconds, alternating
|
2015-01-05 15:55:15 -05:00
|
|
|
// between windowed and full screen mode
|
2010-09-07 11:34:51 -04:00
|
|
|
//
|
|
|
|
// It also times and logs opening and closing actions and attempts to separate
|
|
|
|
// user initiated window closing from its own
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
2019-04-14 11:34:38 -04:00
|
|
|
#include <glad/gl.h>
|
2013-05-22 16:46:34 -04:00
|
|
|
#include <GLFW/glfw3.h>
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-05-30 14:41:39 -04:00
|
|
|
#include <time.h>
|
2010-09-07 11:34:51 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
#include "linmath.h"
|
|
|
|
|
|
|
|
static const char* vertex_shader_text =
|
2016-12-05 08:16:46 -05:00
|
|
|
"#version 110\n"
|
2016-09-20 20:44:27 -04:00
|
|
|
"uniform mat4 MVP;\n"
|
|
|
|
"attribute vec2 vPos;\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char* fragment_shader_text =
|
2016-12-05 08:16:46 -05:00
|
|
|
"#version 110\n"
|
2016-09-20 20:44:27 -04:00
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" gl_FragColor = vec4(1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const vec2 vertices[4] =
|
2012-12-29 19:42:14 -05:00
|
|
|
{
|
2016-09-20 20:44:27 -04:00
|
|
|
{ -0.5f, -0.5f },
|
|
|
|
{ 0.5f, -0.5f },
|
|
|
|
{ 0.5f, 0.5f },
|
|
|
|
{ -0.5f, 0.5f }
|
|
|
|
};
|
2012-12-29 19:42:14 -05:00
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
static void error_callback(int error, const char* description)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2016-09-20 20:44:27 -04:00
|
|
|
fprintf(stderr, "Error: %s\n", description);
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|
2013-03-06 17:29:37 -05:00
|
|
|
static void window_close_callback(GLFWwindow* window)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
|
|
|
printf("Close callback triggered\n");
|
|
|
|
}
|
|
|
|
|
2013-05-30 11:19:12 -04:00
|
|
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
|
|
|
if (action != GLFW_PRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2011-01-03 16:22:14 -05:00
|
|
|
case GLFW_KEY_Q:
|
|
|
|
case GLFW_KEY_ESCAPE:
|
2015-08-23 13:30:04 -04:00
|
|
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
2010-09-07 11:34:51 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 07:45:12 -05:00
|
|
|
static void close_window(GLFWwindow* window)
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
|
|
|
double base = glfwGetTime();
|
2013-03-01 07:45:12 -05:00
|
|
|
glfwDestroyWindow(window);
|
2010-09-07 11:34:51 -04:00
|
|
|
printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int count = 0;
|
2016-09-20 20:44:27 -04:00
|
|
|
double base;
|
2013-03-01 07:45:12 -05:00
|
|
|
GLFWwindow* window;
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-06-12 14:36:43 -04:00
|
|
|
srand((unsigned int) time(NULL));
|
2013-05-30 14:41:39 -04:00
|
|
|
|
2012-12-29 19:42:14 -05:00
|
|
|
glfwSetErrorCallback(error_callback);
|
2012-09-27 15:37:36 -04:00
|
|
|
|
2013-01-02 11:29:24 -05:00
|
|
|
if (!glfwInit())
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
|
|
|
|
|
2010-09-07 11:34:51 -04:00
|
|
|
for (;;)
|
|
|
|
{
|
2015-10-11 15:08:15 -04:00
|
|
|
int width, height;
|
2013-01-05 15:13:28 -05:00
|
|
|
GLFWmonitor* monitor = NULL;
|
2016-09-20 20:44:27 -04:00
|
|
|
GLuint vertex_shader, fragment_shader, program, vertex_buffer;
|
|
|
|
GLint mvp_location, vpos_location;
|
2012-09-27 15:37:36 -04:00
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
if (count & 1)
|
2013-02-14 11:22:22 -05:00
|
|
|
{
|
|
|
|
int monitorCount;
|
|
|
|
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
|
|
|
|
monitor = monitors[rand() % monitorCount];
|
|
|
|
}
|
2012-09-27 15:37:36 -04:00
|
|
|
|
2015-10-11 15:08:15 -04:00
|
|
|
if (monitor)
|
|
|
|
{
|
|
|
|
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
|
|
|
|
width = mode->width;
|
|
|
|
height = mode->height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = 640;
|
|
|
|
height = 480;
|
|
|
|
}
|
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
base = glfwGetTime();
|
|
|
|
|
|
|
|
window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
|
2013-03-01 07:45:12 -05:00
|
|
|
if (!window)
|
2012-10-21 21:20:16 -04:00
|
|
|
{
|
|
|
|
glfwTerminate();
|
2010-09-11 08:32:05 -04:00
|
|
|
exit(EXIT_FAILURE);
|
2012-10-21 21:20:16 -04:00
|
|
|
}
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
if (monitor)
|
|
|
|
{
|
|
|
|
printf("Opening full screen window on monitor %s took %0.3f seconds\n",
|
|
|
|
glfwGetMonitorName(monitor),
|
|
|
|
glfwGetTime() - base);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Opening regular window took %0.3f seconds\n",
|
|
|
|
glfwGetTime() - base);
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwSetWindowCloseCallback(window, window_close_callback);
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
2019-04-14 11:34:38 -04:00
|
|
|
gladLoadGL(glfwGetProcAddress);
|
2016-09-20 20:44:27 -04:00
|
|
|
glfwSwapInterval(1);
|
|
|
|
|
|
|
|
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
|
|
|
|
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
|
|
|
|
glCompileShader(vertex_shader);
|
|
|
|
|
|
|
|
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
|
|
|
|
glCompileShader(fragment_shader);
|
|
|
|
|
|
|
|
program = glCreateProgram();
|
|
|
|
glAttachShader(program, vertex_shader);
|
|
|
|
glAttachShader(program, fragment_shader);
|
|
|
|
glLinkProgram(program);
|
|
|
|
|
|
|
|
mvp_location = glGetUniformLocation(program, "MVP");
|
|
|
|
vpos_location = glGetAttribLocation(program, "vPos");
|
|
|
|
|
|
|
|
glGenBuffers(1, &vertex_buffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(vpos_location);
|
|
|
|
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
sizeof(vertices[0]), (void*) 0);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
|
|
|
glfwSetTime(0.0);
|
|
|
|
|
|
|
|
while (glfwGetTime() < 5.0)
|
|
|
|
{
|
2016-09-20 20:44:27 -04:00
|
|
|
float ratio;
|
|
|
|
int width, height;
|
|
|
|
mat4x4 m, p, mvp;
|
|
|
|
|
|
|
|
glfwGetFramebufferSize(window, &width, &height);
|
|
|
|
ratio = width / (float) height;
|
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
2010-09-07 11:34:51 -04:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2016-09-20 20:44:27 -04:00
|
|
|
mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
|
|
|
|
|
|
|
|
mat4x4_identity(m);
|
|
|
|
mat4x4_rotate_Z(m, m, (float) glfwGetTime());
|
|
|
|
mat4x4_mul(mvp, p, m);
|
|
|
|
|
|
|
|
glUseProgram(program);
|
|
|
|
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
|
|
|
|
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-03-01 07:45:12 -05:00
|
|
|
glfwSwapBuffers(window);
|
2010-09-09 13:25:13 -04:00
|
|
|
glfwPollEvents();
|
2010-09-07 11:34:51 -04:00
|
|
|
|
2013-03-01 07:45:12 -05:00
|
|
|
if (glfwWindowShouldClose(window))
|
2010-09-07 11:34:51 -04:00
|
|
|
{
|
2013-03-01 07:45:12 -05:00
|
|
|
close_window(window);
|
2010-09-07 11:34:51 -04:00
|
|
|
printf("User closed window\n");
|
2012-10-21 21:20:16 -04:00
|
|
|
|
|
|
|
glfwTerminate();
|
2010-09-11 08:32:05 -04:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Closing window\n");
|
2013-03-01 07:45:12 -05:00
|
|
|
close_window(window);
|
2010-09-07 11:34:51 -04:00
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
2013-01-02 11:29:24 -05:00
|
|
|
|
|
|
|
glfwTerminate();
|
2010-09-07 11:34:51 -04:00
|
|
|
}
|
|
|
|
|