Replaced window close parameter with mutable flag.

Replaced the GLFW_SHOULD_CLOSE window parameter with the
glfwWindowShouldClose and glfwSetWindowShouldClose functions, allowing
the setting of the close flag from any point in the program.
This commit is contained in:
Camilla Berglund
2013-03-01 13:45:12 +01:00
parent f8f81e5754
commit 6fadf37bda
26 changed files with 155 additions and 215 deletions
+1 -1
View File
@@ -108,7 +108,7 @@ int main(void)
set_swap_interval(window, swap_interval);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
+2 -11
View File
@@ -34,8 +34,6 @@
#include "getopt.h"
static GLboolean closed = GL_FALSE;
static void usage(void)
{
printf("Usage: clipboard [-h]\n");
@@ -52,12 +50,6 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", description);
}
static int window_close_callback(GLFWwindow* window)
{
closed = GL_TRUE;
return GL_FALSE;
}
static void key_callback(GLFWwindow* window, int key, int action)
{
if (action != GLFW_PRESS)
@@ -66,7 +58,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
switch (key)
{
case GLFW_KEY_ESCAPE:
closed = GL_TRUE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
case GLFW_KEY_V:
@@ -139,7 +131,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glMatrixMode(GL_PROJECTION);
glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f);
@@ -147,7 +138,7 @@ int main(int argc, char** argv)
glClearColor(0.5f, 0.5f, 0.5f, 0);
while (!closed)
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
+1 -1
View File
@@ -416,7 +416,7 @@ int main(void)
printf("Main loop starting\n");
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
glfwWaitEvents();
glfwTerminate();
+1 -1
View File
@@ -128,7 +128,7 @@ int main(int argc, char** argv)
gluOrtho2D(0.f, 1.f, 0.f, 0.5f);
glMatrixMode(GL_MODELVIEW);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
GLfloat time = (GLfloat) glfwGetTime();
+2 -12
View File
@@ -33,8 +33,6 @@
#include <stdio.h>
#include <stdlib.h>
static GLboolean running = GL_TRUE;
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
@@ -57,7 +55,7 @@ static void window_key_callback(GLFWwindow* window, int key, int action)
case GLFW_KEY_ESCAPE:
{
printf("%0.3f: User pressed Escape\n", glfwGetTime());
running = GL_FALSE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
@@ -70,13 +68,6 @@ static void window_key_callback(GLFWwindow* window, int key, int action)
}
}
static int window_close_callback(GLFWwindow* window)
{
printf("%0.3f: User closed window\n", glfwGetTime());
running = GL_FALSE;
return GL_TRUE;
}
int main(void)
{
GLFWwindow* window;
@@ -100,9 +91,8 @@ int main(void)
glfwSetWindowFocusCallback(window, window_focus_callback);
glfwSetKeyCallback(window, window_key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
while (running)
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
+2 -10
View File
@@ -37,7 +37,6 @@
#define STEP_SIZE 0.1f
static GLboolean closed = GL_FALSE;
static GLfloat gamma_value = 1.0f;
static void usage(void)
@@ -61,12 +60,6 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", description);
}
static int window_close_callback(GLFWwindow* window)
{
closed = GL_TRUE;
return GL_FALSE;
}
static void key_callback(GLFWwindow* window, int key, int action)
{
if (action != GLFW_PRESS)
@@ -76,7 +69,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
{
case GLFW_KEY_ESCAPE:
{
closed = GL_TRUE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
@@ -157,7 +150,6 @@ int main(int argc, char** argv)
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowSizeCallback(window, size_callback);
glMatrixMode(GL_PROJECTION);
@@ -166,7 +158,7 @@ int main(int argc, char** argv)
glClearColor(0.5f, 0.5f, 0.5f, 0);
while (!closed)
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
+2 -11
View File
@@ -35,8 +35,6 @@
#include "getopt.h"
static GLboolean closed = GL_FALSE;
static void usage(void)
{
printf("Usage: iconify [-h] [-f]\n");
@@ -47,12 +45,6 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", description);
}
static int window_close_callback(GLFWwindow* window)
{
closed = GL_TRUE;
return GL_FALSE;
}
static void key_callback(GLFWwindow* window, int key, int action)
{
printf("%0.2f Key %s\n",
@@ -68,7 +60,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
glfwIconifyWindow(window);
break;
case GLFW_KEY_ESCAPE:
closed = GL_TRUE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
}
@@ -147,7 +139,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowFocusCallback(window, window_focus_callback);
glfwSetWindowIconifyCallback(window, window_iconify_callback);
@@ -157,7 +148,7 @@ int main(int argc, char** argv)
glEnable(GL_SCISSOR_TEST);
while (!closed)
while (!glfwWindowShouldClose(window))
{
glfwGetWindowSize(window, &width, &height);
+1 -1
View File
@@ -211,7 +211,7 @@ int main(void)
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
+16 -26
View File
@@ -35,8 +35,6 @@
#include "getopt.h"
static GLFWwindow* window_handle = NULL;
enum Mode
{
LIST_MODE,
@@ -75,19 +73,10 @@ static void window_size_callback(GLFWwindow* window, int width, int height)
glViewport(0, 0, width, height);
}
static int window_close_callback(GLFWwindow* window)
{
window_handle = NULL;
return GL_TRUE;
}
static void key_callback(GLFWwindow* window, int key, int action)
{
if (key == GLFW_KEY_ESCAPE)
{
glfwDestroyWindow(window);
window_handle = NULL;
}
glfwSetWindowShouldClose(window, GL_TRUE);
}
static void list_modes(GLFWmonitor* monitor)
@@ -124,6 +113,7 @@ static void list_modes(GLFWmonitor* monitor)
static void test_modes(GLFWmonitor* monitor)
{
int i, count;
GLFWwindow* window;
const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
for (i = 0; i < count; i++)
@@ -140,11 +130,11 @@ static void test_modes(GLFWmonitor* monitor)
glfwGetMonitorName(monitor),
format_mode(mode));
window_handle = glfwCreateWindow(mode->width, mode->height,
"Video Mode Test",
glfwGetPrimaryMonitor(),
NULL);
if (!window_handle)
window = glfwCreateWindow(mode->width, mode->height,
"Video Mode Test",
glfwGetPrimaryMonitor(),
NULL);
if (!window)
{
printf("Failed to enter mode %u: %s\n",
(unsigned int) i,
@@ -152,11 +142,10 @@ static void test_modes(GLFWmonitor* monitor)
continue;
}
glfwSetWindowSizeCallback(window_handle, window_size_callback);
glfwSetWindowCloseCallback(window_handle, window_close_callback);
glfwSetKeyCallback(window_handle, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window_handle);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetTime(0.0);
@@ -164,10 +153,10 @@ static void test_modes(GLFWmonitor* monitor)
while (glfwGetTime() < 5.0)
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window_handle);
glfwSwapBuffers(window);
glfwPollEvents();
if (!window_handle)
if (glfwWindowShouldClose(window))
{
printf("User terminated program\n");
@@ -180,7 +169,7 @@ static void test_modes(GLFWmonitor* monitor)
glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
glfwGetWindowSize(window_handle, &current.width, &current.height);
glfwGetWindowSize(window, &current.width, &current.height);
if (current.redBits != mode->redBits ||
current.greenBits != mode->greenBits ||
@@ -200,8 +189,9 @@ static void test_modes(GLFWmonitor* monitor)
printf("Closing window\n");
glfwDestroyWindow(window_handle);
window_handle = NULL;
glfwDestroyWindow(window);
window = NULL;
glfwPollEvents();
}
}
+19 -18
View File
@@ -36,12 +36,9 @@
#include <stdlib.h>
static GLboolean reopen = GL_FALSE;
static GLFWwindow* window_handle = NULL;
static int cursor_x;
static int cursor_y;
static GLboolean open_window(void);
static void toggle_cursor(GLFWwindow* window)
{
if (glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED)
@@ -95,33 +92,36 @@ static void window_size_callback(GLFWwindow* window, int width, int height)
glViewport(0, 0, width, height);
}
static GLboolean open_window(void)
static GLFWwindow* open_window(void)
{
window_handle = glfwCreateWindow(640, 480, "Peter Detector", NULL, NULL);
if (!window_handle)
return GL_FALSE;
GLFWwindow* window = glfwCreateWindow(640, 480, "Peter Detector", NULL, NULL);
if (!window)
return NULL;
glfwMakeContextCurrent(window_handle);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwGetCursorPos(window_handle, &cursor_x, &cursor_y);
glfwGetCursorPos(window, &cursor_x, &cursor_y);
printf("Cursor position: %i %i\n", cursor_x, cursor_y);
glfwSetWindowSizeCallback(window_handle, window_size_callback);
glfwSetCursorPosCallback(window_handle, cursor_position_callback);
glfwSetKeyCallback(window_handle, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetKeyCallback(window, key_callback);
return GL_TRUE;
return window;
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
if (!open_window())
window = open_window();
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
@@ -129,17 +129,18 @@ int main(void)
glClearColor(0.f, 0.f, 0.f, 0.f);
while (!glfwGetWindowParam(window_handle, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window_handle);
glfwSwapBuffers(window);
glfwWaitEvents();
if (reopen)
{
glfwDestroyWindow(window_handle);
if (!open_window())
glfwDestroyWindow(window);
window = open_window();
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
+21 -25
View File
@@ -38,9 +38,6 @@
#include <stdio.h>
#include <stdlib.h>
static GLFWwindow* window_handle = NULL;
static GLboolean closed = GL_FALSE;
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
@@ -54,8 +51,7 @@ static void window_size_callback(GLFWwindow* window, int width, int height)
static int window_close_callback(GLFWwindow* window)
{
printf("Close callback triggered\n");
closed = GL_TRUE;
return 0;
return GL_TRUE;
}
static void key_callback(GLFWwindow* window, int key, int action)
@@ -67,48 +63,47 @@ static void key_callback(GLFWwindow* window, int key, int action)
{
case GLFW_KEY_Q:
case GLFW_KEY_ESCAPE:
closed = GL_TRUE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
}
static GLboolean open_window(int width, int height, GLFWmonitor* monitor)
static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
{
double base;
GLFWwindow* window;
base = glfwGetTime();
window_handle = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
if (!window_handle)
return GL_FALSE;
window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
if (!window)
return NULL;
glfwMakeContextCurrent(window_handle);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetWindowSizeCallback(window_handle, window_size_callback);
glfwSetWindowCloseCallback(window_handle, window_close_callback);
glfwSetKeyCallback(window_handle, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback);
printf("Opening %s mode window took %0.3f seconds\n",
monitor ? "fullscreen" : "windowed",
glfwGetTime() - base);
return GL_TRUE;
return window;
}
static void close_window(void)
static void close_window(GLFWwindow* window)
{
double base = glfwGetTime();
glfwDestroyWindow(window_handle);
window_handle = NULL;
glfwDestroyWindow(window);
printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
}
int main(int argc, char** argv)
{
int count = 0;
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
@@ -126,7 +121,8 @@ int main(int argc, char** argv)
monitor = monitors[rand() % monitorCount];
}
if (!open_window(640, 480, monitor))
window = open_window(640, 480, monitor);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
@@ -147,12 +143,12 @@ int main(int argc, char** argv)
glRectf(-0.5f, -0.5f, 1.f, 1.f);
glPopMatrix();
glfwSwapBuffers(window_handle);
glfwSwapBuffers(window);
glfwPollEvents();
if (closed)
if (glfwWindowShouldClose(window))
{
close_window();
close_window(window);
printf("User closed window\n");
glfwTerminate();
@@ -161,7 +157,7 @@ int main(int argc, char** argv)
}
printf("Closing window\n");
close_window();
close_window(window);
count++;
}
+3 -10
View File
@@ -38,7 +38,6 @@
#define OFFSET 50
static GLFWwindow* windows[2];
static GLboolean closed = GL_FALSE;
static void error_callback(int error, const char* description)
{
@@ -48,13 +47,7 @@ static void error_callback(int error, const char* description)
static void key_callback(GLFWwindow* window, int key, int action)
{
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
closed = GL_TRUE;
}
static int window_close_callback(GLFWwindow* window)
{
closed = GL_TRUE;
return GL_FALSE;
glfwSetWindowShouldClose(window, GL_TRUE);
}
static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, int posY)
@@ -71,7 +64,6 @@ static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, i
glfwSetWindowPos(window, posX, posY);
glfwShowWindow(window);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback);
return window;
@@ -172,7 +164,8 @@ int main(int argc, char** argv)
glfwMakeContextCurrent(windows[0]);
while (!closed)
while (!glfwWindowShouldClose(windows[0]) &&
!glfwWindowShouldClose(windows[1]))
{
glfwMakeContextCurrent(windows[0]);
draw_quad(texture);
+1 -1
View File
@@ -91,7 +91,7 @@ int main(void)
glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
+1 -1
View File
@@ -124,7 +124,7 @@ int main(void)
for (i = 0; i < count; i++)
{
if (glfwGetWindowParam(threads[i].window, GLFW_SHOULD_CLOSE))
if (glfwWindowShouldClose(threads[i].window))
running = GL_FALSE;
}
}
+1 -1
View File
@@ -63,7 +63,7 @@ int main(void)
glfwSetWindowSizeCallback(window, window_size_callback);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
+1 -1
View File
@@ -85,7 +85,7 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(windows[i]);
if (glfwGetWindowParam(windows[i], GLFW_SHOULD_CLOSE))
if (glfwWindowShouldClose(windows[i]))
running = GL_FALSE;
}