2010-09-07 17:34:51 +02:00
|
|
|
//========================================================================
|
|
|
|
// Window re-opener (open/close stress test)
|
|
|
|
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// between windowed and fullscreen mode
|
|
|
|
//
|
|
|
|
// It also times and logs opening and closing actions and attempts to separate
|
|
|
|
// user initiated window closing from its own
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
2013-05-22 22:46:34 +02:00
|
|
|
#include <GLFW/glfw3.h>
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-05-30 20:41:39 +02:00
|
|
|
#include <time.h>
|
2010-09-07 17:34:51 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-12-30 01:42:14 +01:00
|
|
|
static void error_callback(int error, const char* description)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: %s\n", description);
|
|
|
|
}
|
|
|
|
|
2013-06-03 12:51:57 +02:00
|
|
|
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
2013-03-06 23:29:37 +01:00
|
|
|
static void window_close_callback(GLFWwindow* window)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
printf("Close callback triggered\n");
|
|
|
|
}
|
|
|
|
|
2013-05-30 17:19:12 +02:00
|
|
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
if (action != GLFW_PRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2011-01-03 22:22:14 +01:00
|
|
|
case GLFW_KEY_Q:
|
|
|
|
case GLFW_KEY_ESCAPE:
|
2013-03-01 13:45:12 +01:00
|
|
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
2010-09-07 17:34:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2011-09-06 14:58:34 +02:00
|
|
|
double base;
|
2013-03-01 13:45:12 +01:00
|
|
|
GLFWwindow* window;
|
2011-09-06 14:58:34 +02:00
|
|
|
|
|
|
|
base = glfwGetTime();
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
|
|
|
|
if (!window)
|
|
|
|
return NULL;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
glfwMakeContextCurrent(window);
|
2012-08-10 15:29:45 +02:00
|
|
|
glfwSwapInterval(1);
|
|
|
|
|
2013-06-03 12:51:57 +02:00
|
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
2013-03-01 13:45:12 +01:00
|
|
|
glfwSetWindowCloseCallback(window, window_close_callback);
|
|
|
|
glfwSetKeyCallback(window, key_callback);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-05-19 09:18:57 +02: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);
|
|
|
|
}
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
return window;
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
static void close_window(GLFWwindow* window)
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
|
|
|
double base = glfwGetTime();
|
2013-03-01 13:45:12 +01:00
|
|
|
glfwDestroyWindow(window);
|
2010-09-07 17:34:51 +02:00
|
|
|
printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int count = 0;
|
2013-03-01 13:45:12 +01:00
|
|
|
GLFWwindow* window;
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-05-30 20:41:39 +02:00
|
|
|
srand(time(NULL));
|
|
|
|
|
2012-12-30 01:42:14 +01:00
|
|
|
glfwSetErrorCallback(error_callback);
|
2012-09-27 21:37:36 +02:00
|
|
|
|
2013-01-02 17:29:24 +01:00
|
|
|
if (!glfwInit())
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2010-09-07 17:34:51 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
2013-01-05 21:13:28 +01:00
|
|
|
GLFWmonitor* monitor = NULL;
|
2012-09-27 21:37:36 +02:00
|
|
|
|
|
|
|
if (count & 1)
|
2013-02-14 17:22:22 +01:00
|
|
|
{
|
|
|
|
int monitorCount;
|
|
|
|
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
|
|
|
|
monitor = monitors[rand() % monitorCount];
|
|
|
|
}
|
2012-09-27 21:37:36 +02:00
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
window = open_window(640, 480, monitor);
|
|
|
|
if (!window)
|
2012-10-22 03:20:16 +02:00
|
|
|
{
|
|
|
|
glfwTerminate();
|
2010-09-11 14:32:05 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2012-10-22 03:20:16 +02:00
|
|
|
}
|
2010-09-07 17:34:51 +02:00
|
|
|
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
|
|
|
|
glfwSetTime(0.0);
|
|
|
|
|
|
|
|
while (glfwGetTime() < 5.0)
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
|
|
|
glPushMatrix();
|
|
|
|
glRotatef((GLfloat) glfwGetTime() * 100.f, 0.f, 0.f, 1.f);
|
|
|
|
glRectf(-0.5f, -0.5f, 1.f, 1.f);
|
|
|
|
glPopMatrix();
|
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
glfwSwapBuffers(window);
|
2010-09-09 19:25:13 +02:00
|
|
|
glfwPollEvents();
|
2010-09-07 17:34:51 +02:00
|
|
|
|
2013-03-01 13:45:12 +01:00
|
|
|
if (glfwWindowShouldClose(window))
|
2010-09-07 17:34:51 +02:00
|
|
|
{
|
2013-03-01 13:45:12 +01:00
|
|
|
close_window(window);
|
2010-09-07 17:34:51 +02:00
|
|
|
printf("User closed window\n");
|
2012-10-22 03:20:16 +02:00
|
|
|
|
|
|
|
glfwTerminate();
|
2010-09-11 14:32:05 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Closing window\n");
|
2013-03-01 13:45:12 +01:00
|
|
|
close_window(window);
|
2010-09-07 17:34:51 +02:00
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
2013-01-02 17:29:24 +01:00
|
|
|
|
|
|
|
glfwTerminate();
|
2010-09-07 17:34:51 +02:00
|
|
|
}
|
|
|
|
|