From 2410e2aaf4318f418f322ba9c856b9423e84bb7b Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Fri, 10 Aug 2012 13:31:15 +0200 Subject: [PATCH] Replaced automatic closing with window parameter. --- examples/boing.c | 19 ++++--------------- examples/splitview.c | 26 ++++++-------------------- examples/triangle.c | 21 ++++++--------------- include/GL/glfw3.h | 1 + readme.html | 1 + src/cocoa_window.m | 5 ++--- src/internal.h | 1 + src/win32_window.c | 5 ++--- src/window.c | 44 +++++++++++++++----------------------------- src/x11_window.c | 2 +- tests/accuracy.c | 2 +- tests/clipboard.c | 2 +- tests/events.c | 8 +------- tests/fsaa.c | 2 +- tests/gamma.c | 2 +- tests/iconify.c | 2 +- tests/joysticks.c | 2 +- tests/peter.c | 2 +- tests/tearing.c | 2 +- tests/title.c | 2 +- tests/windows.c | 14 ++++---------- 21 files changed, 53 insertions(+), 112 deletions(-) diff --git a/examples/boing.c b/examples/boing.c index 8348df66..c0d3f0ee 100644 --- a/examples/boing.c +++ b/examples/boing.c @@ -89,7 +89,6 @@ DRAW_BALL_ENUM drawBallHow; double t; double t_old = 0.f; double dt; -static GLboolean running = GL_TRUE; /* Random number generator */ #ifndef RAND_MAX @@ -246,16 +245,6 @@ void reshape( GLFWwindow window, int w, int h ) } -/***************************************************************************** - * Window close callback - *****************************************************************************/ -static int window_close_callback(GLFWwindow window) -{ - running = GL_FALSE; - return GL_TRUE; -} - - /***************************************************************************** * Draw the Boing ball. * @@ -588,7 +577,6 @@ int main( void ) exit( EXIT_FAILURE ); } - glfwSetWindowCloseCallback( window_close_callback ); glfwSetWindowSizeCallback( reshape ); glfwWindowHint(GLFW_DEPTH_BITS, 16); @@ -611,7 +599,7 @@ int main( void ) init(); /* Main loop */ - do + for (;;) { /* Timing */ t = glfwGetTime(); @@ -627,9 +615,10 @@ int main( void ) /* Check if we are still running */ if (glfwGetKey( window, GLFW_KEY_ESCAPE )) - running = GL_FALSE; + break; + if (glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + break; } - while( running ); glfwTerminate(); exit( EXIT_SUCCESS ); diff --git a/examples/splitview.c b/examples/splitview.c index e3f01806..2f53f45d 100644 --- a/examples/splitview.c +++ b/examples/splitview.c @@ -42,9 +42,6 @@ static int rot_x = 0, rot_y = 0, rot_z = 0; // Do redraw? static int do_redraw = 1; -// Keep running? -static GLboolean running = GL_TRUE; - //======================================================================== // Draw a solid torus (use a display list for the model) @@ -438,17 +435,6 @@ static void mouseButtonFun(GLFWwindow window, int button, int action) } -//======================================================================== -// Window close callback function -//======================================================================== - -static int windowCloseFun(GLFWwindow window) -{ - running = GL_FALSE; - return GL_TRUE; -} - - //======================================================================== // main //======================================================================== @@ -466,7 +452,6 @@ int main(void) } // Set callback functions - glfwSetWindowCloseCallback(windowCloseFun); glfwSetWindowSizeCallback(windowSizeFun); glfwSetWindowRefreshCallback(windowRefreshFun); glfwSetCursorPosCallback(cursorPosFun); @@ -495,7 +480,7 @@ int main(void) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL); // Main loop - do + for (;;) { // Only redraw if we need to if (do_redraw) @@ -512,11 +497,12 @@ int main(void) // Wait for new events glfwWaitEvents(); + // Check if the ESC key was pressed or the window should be closed if (glfwGetKey(window, GLFW_KEY_ESCAPE)) - running = GL_FALSE; - - } // Check if the ESC key was pressed or the window was closed - while (running); + break; + if (glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + break; + } // Close OpenGL window and terminate GLFW glfwTerminate(); diff --git a/examples/triangle.c b/examples/triangle.c index 8a352ca9..9ebfbcac 100644 --- a/examples/triangle.c +++ b/examples/triangle.c @@ -10,14 +10,6 @@ #define GLFW_INCLUDE_GLU #include -static GLboolean running = GL_TRUE; - -static int window_close_callback(GLFWwindow window) -{ - running = GL_FALSE; - return GL_TRUE; -} - int main(void) { int width, height, x; @@ -44,9 +36,7 @@ int main(void) // Enable vertical sync (on cards that support it) glfwSwapInterval(1); - glfwSetWindowCloseCallback(window_close_callback); - - do + for (;;) { double t = glfwGetTime(); glfwGetCursorPos(window, &x, NULL); @@ -92,11 +82,12 @@ int main(void) glfwSwapBuffers(window); glfwPollEvents(); + // Check if the ESC key was pressed or the window should be closed if (glfwGetKey(window, GLFW_KEY_ESCAPE)) - running = GL_FALSE; - - } // Check if the ESC key was pressed or the window was closed - while (running); + break; + if (glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + break; + } // Close OpenGL window and terminate GLFW glfwTerminate(); diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index d5a55fdd..050f0c30 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -388,6 +388,7 @@ extern "C" { /* glfwGetWindowParam tokens */ #define GLFW_ACTIVE 0x00020001 #define GLFW_ICONIFIED 0x00020002 +#define GLFW_CLOSE_REQUESTED 0x00020003 #define GLFW_OPENGL_REVISION 0x00020004 /* glfwWindowHint tokens */ diff --git a/readme.html b/readme.html index b93b7c6b..39d0739b 100644 --- a/readme.html +++ b/readme.html @@ -307,6 +307,7 @@ version of GLFW.

  • Replaced mouse wheel interface with two-dimensional, floating point scrolling interface
  • Replaced glfwEnable and glfwDisable with glfwGetInputMode and glfwSetInputMode
  • Replaced joystick test with graphical version
  • +
  • Replaced automatic closing of windows with GLFW_CLOSE_REQUESTED window parameter
  • Made Unicode character input unaffected by GLFW_KEY_REPEAT
  • Removed event auto-polling and the GLFW_AUTO_POLL_EVENTS window enable
  • Removed the Win32 port .def files
  • diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 0d8ec67d..446d718e 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -59,8 +59,7 @@ - (BOOL)windowShouldClose:(id)sender { - window->closeRequested = GL_TRUE; - + _glfwInputWindowCloseRequest(window); return NO; } @@ -127,7 +126,7 @@ _GLFWwindow* window; for (window = _glfwLibrary.windowListHead; window; window = window->next) - window->closeRequested = GL_TRUE; + _glfwInputWindowCloseRequest(window); return NSTerminateCancel; } diff --git a/src/internal.h b/src/internal.h index 982d868c..89ac48b2 100644 --- a/src/internal.h +++ b/src/internal.h @@ -337,6 +337,7 @@ void _glfwInputWindowPos(_GLFWwindow* window, int x, int y); void _glfwInputWindowSize(_GLFWwindow* window, int width, int height); void _glfwInputWindowIconify(_GLFWwindow* window, int iconified); void _glfwInputWindowDamage(_GLFWwindow* window); +void _glfwInputWindowCloseRequest(_GLFWwindow* window); // Input event notification (input.c) void _glfwInputKey(_GLFWwindow* window, int key, int action); diff --git a/src/win32_window.c b/src/win32_window.c index 62a53c03..00fb269d 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -532,8 +532,7 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_CLOSE: { - // Flag this window for closing (handled in glfwPollEvents) - window->closeRequested = GL_TRUE; + _glfwInputWindowCloseRequest(window); return 0; } @@ -1249,7 +1248,7 @@ void _glfwPlatformPollEvents(void) window = _glfwLibrary.windowListHead; while (window) { - window->closeRequested = GL_TRUE; + _glfwInputWindowCloseRequest(window); window = window->next; } diff --git a/src/window.c b/src/window.c index 5fc2b32f..81b525bd 100644 --- a/src/window.c +++ b/src/window.c @@ -44,31 +44,6 @@ static int Max(int a, int b) } -//======================================================================== -// Close all GLFW windows with the closed flag set -//======================================================================== - -static void closeFlaggedWindows(void) -{ - _GLFWwindow* window; - - for (window = _glfwLibrary.windowListHead; window; ) - { - if (window->closeRequested && _glfwLibrary.windowCloseCallback) - window->closeRequested = _glfwLibrary.windowCloseCallback(window); - - if (window->closeRequested) - { - _GLFWwindow* next = window->next; - glfwDestroyWindow(window); - window = next; - } - else - window = window->next; - } -} - - //======================================================================== // Clear scroll offsets for all windows //======================================================================== @@ -206,6 +181,19 @@ void _glfwInputWindowDamage(_GLFWwindow* window) } +//======================================================================== +// Register window close request events +//======================================================================== + +void _glfwInputWindowCloseRequest(_GLFWwindow* window) +{ + if (_glfwLibrary.windowCloseCallback) + window->closeRequested = _glfwLibrary.windowCloseCallback(window); + else + window->closeRequested = GL_TRUE; +} + + ////////////////////////////////////////////////////////////////////////// ////// GLFW public API ////// ////////////////////////////////////////////////////////////////////////// @@ -662,6 +650,8 @@ GLFWAPI int glfwGetWindowParam(GLFWwindow handle, int param) return window == _glfwLibrary.activeWindow; case GLFW_ICONIFIED: return window->iconified; + case GLFW_CLOSE_REQUESTED: + return window->closeRequested; case GLFW_REFRESH_RATE: return window->refreshRate; case GLFW_WINDOW_RESIZABLE: @@ -818,8 +808,6 @@ GLFWAPI void glfwPollEvents(void) clearScrollOffsets(); _glfwPlatformPollEvents(); - - closeFlaggedWindows(); } @@ -838,7 +826,5 @@ GLFWAPI void glfwWaitEvents(void) clearScrollOffsets(); _glfwPlatformWaitEvents(); - - closeFlaggedWindows(); } diff --git a/src/x11_window.c b/src/x11_window.c index e90790f7..05d1be83 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -711,7 +711,7 @@ static void processSingleEvent(void) // The window manager was asked to close the window, for example by // the user pressing a 'close' window decoration button - window->closeRequested = GL_TRUE; + _glfwInputWindowCloseRequest(window); } else if (_glfwLibrary.X11.wmPing != None && (Atom) event.xclient.data.l[0] == _glfwLibrary.X11.wmPing) diff --git a/tests/accuracy.c b/tests/accuracy.c index 079a88b8..acf9be2f 100644 --- a/tests/accuracy.c +++ b/tests/accuracy.c @@ -106,7 +106,7 @@ int main(void) set_swap_interval(window, swap_interval); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/clipboard.c b/tests/clipboard.c index 8ab90dbb..654de5f9 100644 --- a/tests/clipboard.c +++ b/tests/clipboard.c @@ -136,7 +136,7 @@ int main(int argc, char** argv) glClearColor(0.5f, 0.5f, 0.5f, 0); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/events.c b/tests/events.c index bd8b1546..20051a24 100644 --- a/tests/events.c +++ b/tests/events.c @@ -48,9 +48,6 @@ static GLboolean closeable = GL_TRUE; // Event index static unsigned int counter = 0; -// Should we keep running? -static GLboolean running = GL_TRUE; - static const char* get_key_name(int key) { switch (key) @@ -238,9 +235,6 @@ static int window_close_callback(GLFWwindow window) { printf("%08x at %0.3f: Window close\n", counter++, glfwGetTime()); - if (closeable) - running = GL_FALSE; - return closeable; } @@ -399,7 +393,7 @@ int main(void) printf("Main loop starting\n"); - while (running) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) glfwWaitEvents(); glfwTerminate(); diff --git a/tests/fsaa.c b/tests/fsaa.c index 93377b59..2a9ccfb5 100644 --- a/tests/fsaa.c +++ b/tests/fsaa.c @@ -127,7 +127,7 @@ int main(int argc, char** argv) gluOrtho2D(0.f, 1.f, 0.f, 0.5f); glMatrixMode(GL_MODELVIEW); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { GLfloat time = (GLfloat) glfwGetTime(); diff --git a/tests/gamma.c b/tests/gamma.c index 1f3ca5e1..b2f1f2b8 100644 --- a/tests/gamma.c +++ b/tests/gamma.c @@ -151,7 +151,7 @@ int main(int argc, char** argv) glClearColor(0.5f, 0.5f, 0.5f, 0); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/iconify.c b/tests/iconify.c index 4f1c7d22..221d558f 100644 --- a/tests/iconify.c +++ b/tests/iconify.c @@ -126,7 +126,7 @@ int main(int argc, char** argv) glEnable(GL_SCISSOR_TEST); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { int width, height; diff --git a/tests/joysticks.c b/tests/joysticks.c index 36952038..374ec82d 100644 --- a/tests/joysticks.c +++ b/tests/joysticks.c @@ -198,7 +198,7 @@ int main(void) glfwSetWindowSizeCallback(window_size_callback); glfwSwapInterval(1); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/peter.c b/tests/peter.c index 7e0be921..be772e94 100644 --- a/tests/peter.c +++ b/tests/peter.c @@ -127,7 +127,7 @@ int main(void) glClearColor(0.f, 0.f, 0.f, 0.f); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window_handle, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/tearing.c b/tests/tearing.c index 19ffe531..51e110ac 100644 --- a/tests/tearing.c +++ b/tests/tearing.c @@ -90,7 +90,7 @@ int main(void) glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/title.c b/tests/title.c index 918d7eed..1370b338 100644 --- a/tests/title.c +++ b/tests/title.c @@ -58,7 +58,7 @@ int main(void) glfwSetWindowSizeCallback(window_size_callback); - while (glfwGetCurrentContext()) + while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) { glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); diff --git a/tests/windows.c b/tests/windows.c index 27c1bd30..ce0609af 100644 --- a/tests/windows.c +++ b/tests/windows.c @@ -32,14 +32,6 @@ #include #include -static GLboolean running = GL_TRUE; - -static int window_close_callback(GLFWwindow window) -{ - running = GL_FALSE; - return GL_TRUE; -} - static const char* titles[] = { "Foo", @@ -51,6 +43,7 @@ static const char* titles[] = int main(void) { int i; + GLboolean running = GL_TRUE; GLFWwindow windows[4]; if (!glfwInit()) @@ -60,8 +53,6 @@ int main(void) exit(EXIT_FAILURE); } - glfwSetWindowCloseCallback(window_close_callback); - for (i = 0; i < 4; i++) { windows[i] = glfwCreateWindow(200, 200, GLFW_WINDOWED, titles[i], NULL); @@ -88,6 +79,9 @@ int main(void) glfwMakeContextCurrent(windows[i]); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(windows[i]); + + if (glfwGetWindowParam(windows[i], GLFW_CLOSE_REQUESTED)) + running = GL_FALSE; } glfwPollEvents();