2010-09-07 15:34:51 +00: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
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
2010-09-10 11:16:03 +00:00
|
|
|
#include <GL/glfw3.h>
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-09-09 17:22:47 +00:00
|
|
|
static GLFWwindow window_handle = NULL;
|
2010-09-07 15:34:51 +00:00
|
|
|
static GLboolean closed = GL_FALSE;
|
|
|
|
|
|
|
|
static const char* get_mode_name(int mode)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
2010-09-10 11:24:19 +00:00
|
|
|
case GLFW_WINDOWED:
|
2010-09-07 15:34:51 +00:00
|
|
|
return "windowed";
|
|
|
|
case GLFW_FULLSCREEN:
|
|
|
|
return "fullscreen";
|
|
|
|
default:
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-09 17:22:47 +00:00
|
|
|
static void window_size_callback(GLFWwindow window, int width, int height)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
2010-09-09 17:22:47 +00:00
|
|
|
static int window_close_callback(GLFWwindow window)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
printf("Close callback triggered\n");
|
2010-09-09 17:22:47 +00:00
|
|
|
window_handle = NULL;
|
2010-09-07 15:34:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-09 17:22:47 +00:00
|
|
|
static void key_callback(GLFWwindow window, int key, int action)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
|
|
|
if (action != GLFW_PRESS)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2011-01-03 21:22:14 +00:00
|
|
|
case GLFW_KEY_Q:
|
|
|
|
case GLFW_KEY_ESCAPE:
|
2010-09-07 15:34:51 +00:00
|
|
|
closed = GL_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-06 12:58:34 +00:00
|
|
|
static GLboolean open_window(int width, int height, int mode)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2011-09-06 12:58:34 +00:00
|
|
|
double base;
|
|
|
|
|
|
|
|
if (!glfwInit())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError()));
|
|
|
|
return GL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
base = glfwGetTime();
|
2010-09-07 15:34:51 +00:00
|
|
|
|
2010-10-04 16:17:53 +00:00
|
|
|
window_handle = glfwOpenWindow(width, height, mode, "Window Re-opener", NULL);
|
2010-09-09 17:22:47 +00:00
|
|
|
if (!window_handle)
|
2010-09-07 15:34:51 +00:00
|
|
|
{
|
2010-09-11 12:32:05 +00:00
|
|
|
fprintf(stderr, "Failed to open %s mode GLFW window: %s\n", get_mode_name(mode), glfwErrorString(glfwGetError()));
|
2011-09-06 12:58:34 +00:00
|
|
|
return GL_FALSE;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
2010-10-24 16:28:55 +00:00
|
|
|
glfwSetWindowSizeCallback(window_size_callback);
|
|
|
|
glfwSetWindowCloseCallback(window_close_callback);
|
|
|
|
glfwSetKeyCallback(key_callback);
|
2010-09-07 15:34:51 +00:00
|
|
|
glfwSwapInterval(1);
|
|
|
|
|
|
|
|
printf("Opening %s mode window took %0.3f seconds\n",
|
|
|
|
get_mode_name(mode),
|
|
|
|
glfwGetTime() - base);
|
|
|
|
|
2011-09-06 12:58:34 +00:00
|
|
|
return GL_TRUE;
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void close_window(void)
|
|
|
|
{
|
|
|
|
double base = glfwGetTime();
|
|
|
|
|
2010-09-09 17:22:47 +00:00
|
|
|
glfwCloseWindow(window_handle);
|
|
|
|
window_handle = NULL;
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
|
2011-09-06 12:58:34 +00:00
|
|
|
|
|
|
|
glfwTerminate();
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2010-09-10 11:24:19 +00:00
|
|
|
if (!open_window(640, 480, (count & 1) ? GLFW_FULLSCREEN : GLFW_WINDOWED))
|
2010-09-11 12:32:05 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2010-09-07 15:34:51 +00: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();
|
|
|
|
|
|
|
|
glfwSwapBuffers();
|
2010-09-09 17:25:13 +00:00
|
|
|
glfwPollEvents();
|
2010-09-07 15:34:51 +00:00
|
|
|
|
|
|
|
if (closed)
|
|
|
|
{
|
2011-09-06 12:58:34 +00:00
|
|
|
close_window();
|
2010-09-07 15:34:51 +00:00
|
|
|
printf("User closed window\n");
|
2010-09-11 12:32:05 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2010-09-07 15:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Closing window\n");
|
|
|
|
close_window();
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|