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

View File

@ -247,6 +247,8 @@ GLFW.
changes in the set of available monitors changes in the set of available monitors
* Added `GLFWwindow` and updated window-related functions and callbacks to take * Added `GLFWwindow` and updated window-related functions and callbacks to take
a window handle a window handle
* Added `glfwSetWindowShouldClose` and `glfwWindowShouldClose` for setting and
retrieving the window close flag
* Added `glfwGetWindowPos` for retrieving the position of a window * Added `glfwGetWindowPos` for retrieving the position of a window
* Added `glfwDefaultWindowHints` for resetting all window hints to their * Added `glfwDefaultWindowHints` for resetting all window hints to their
default values default values
@ -324,8 +326,7 @@ GLFW.
* Replaced `glfwEnable` and `glfwDisable` with `glfwGetInputMode` and * Replaced `glfwEnable` and `glfwDisable` with `glfwGetInputMode` and
`glfwSetInputMode` `glfwSetInputMode`
* Replaced `joystick` test with graphical version * Replaced `joystick` test with graphical version
* Replaced automatic closing of windows with `GLFW_SHOULD_CLOSE` window * Replaced automatic closing of windows with the window close flag
parameter
* Removed the `GLFW_KEY_REPEAT` input option * Removed the `GLFW_KEY_REPEAT` input option
* Removed event auto-polling and the `GLFW_AUTO_POLL_EVENTS` window enable * Removed event auto-polling and the `GLFW_AUTO_POLL_EVENTS` window enable
* Removed the Win32 port .def files * Removed the Win32 port .def files

View File

@ -163,21 +163,32 @@ glfwMakeContextCurrent(window);
@endcode @endcode
@section quick_window_params Retrieving window parameters @section quick_window_params Checking the window close flag
Each window provides a number of parameters that can be queried with @ref Each window has a flag indicating whether the window should be closed. This can
glfwGetWindowParam. Some are related to the window itself and others to the be checked with @ref glfwWindowShouldClose.
OpenGL context. For example, to find out if the user is attempting to close the
window, either by pressing the close widget in the title bar or using a key When the user attempts to close the window, either by pressing the close widget
combination like Alt+F4, check the @c GLFW_SHOULD_CLOSE parameter. in the title bar or using a key combination like Alt+F4, this flag is set to 1.
Note that <b>the window isn't actually closed</b>, so you are expected to
monitor this flag and either destroy the window or give some kind of feedback to
the user.
@code @code
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
// Keep running // Keep running
} }
@endcode @endcode
You can intercept the setting of the close flag by setting a close callback with
@ref glfwSetWindowCloseCallback. The return value of the close callback becomes
the new value of the close flag.
You can also set it yourself with @ref glfwSetWindowShouldClose. This can be
useful if you want to interpret other kinds of input as closing the window, like
for example pressing the escape key.
@section quick_render Rendering with OpenGL @section quick_render Rendering with OpenGL

View File

@ -43,6 +43,7 @@
void init( void ); void init( void );
void display( void ); void display( void );
void reshape( GLFWwindow* window, int w, int h ); void reshape( GLFWwindow* window, int w, int h );
void key_callback( GLFWwindow* window, int key, int action );
void DrawBoingBall( void ); void DrawBoingBall( void );
void BounceBall( double dt ); void BounceBall( double dt );
void DrawBoingBallBand( GLfloat long_lo, GLfloat long_hi ); void DrawBoingBallBand( GLfloat long_lo, GLfloat long_hi );
@ -244,6 +245,11 @@ void reshape( GLFWwindow* window, int w, int h )
0.0, -1.0, 0.0 ); /* up vector */ 0.0, -1.0, 0.0 ); /* up vector */
} }
void key_callback( GLFWwindow* window, int key, int action )
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
/***************************************************************************** /*****************************************************************************
* Draw the Boing ball. * Draw the Boing ball.
@ -584,6 +590,7 @@ int main( void )
} }
glfwSetWindowSizeCallback(window, reshape); glfwSetWindowSizeCallback(window, reshape);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSwapInterval( 1 ); glfwSwapInterval( 1 );
@ -591,7 +598,6 @@ int main( void )
glfwGetWindowSize(window, &width, &height); glfwGetWindowSize(window, &width, &height);
reshape(window, width, height); reshape(window, width, height);
glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
glfwSetTime( 0.0 ); glfwSetTime( 0.0 );
init(); init();
@ -612,9 +618,7 @@ int main( void )
glfwPollEvents(); glfwPollEvents();
/* Check if we are still running */ /* Check if we are still running */
if (glfwGetKey( window, GLFW_KEY_ESCAPE )) if (glfwWindowShouldClose(window))
break;
if (glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
break; break;
} }

View File

@ -32,10 +32,6 @@
#define M_PI 3.141592654 #define M_PI 3.141592654
#endif #endif
/* The program exits when this is zero.
*/
static int running = 1;
/* If non-zero, the program exits after that many seconds /* If non-zero, the program exits after that many seconds
*/ */
static int autoexit = 0; static int autoexit = 0;
@ -227,7 +223,7 @@ void key( GLFWwindow* window, int k, int action )
view_rotz += 5.0; view_rotz += 5.0;
break; break;
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
running = 0; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
case GLFW_KEY_UP: case GLFW_KEY_UP:
view_rotx += 5.0; view_rotx += 5.0;
@ -267,14 +263,6 @@ void reshape( GLFWwindow* window, int width, int height )
} }
/* close callback */
static int window_close_callback(GLFWwindow* window)
{
running = 0;
return GL_TRUE;
}
/* program & OpenGL initialization */ /* program & OpenGL initialization */
static void init(int argc, char *argv[]) static void init(int argc, char *argv[])
{ {
@ -349,7 +337,6 @@ int main(int argc, char *argv[])
} }
// Set callback functions // Set callback functions
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowSizeCallback(window, reshape); glfwSetWindowSizeCallback(window, reshape);
glfwSetKeyCallback(window, key); glfwSetKeyCallback(window, key);
@ -363,7 +350,7 @@ int main(int argc, char *argv[])
init(argc, argv); init(argc, argv);
// Main loop // Main loop
while( running ) while( !glfwWindowShouldClose(window) )
{ {
// Draw gears // Draw gears
draw(); draw();

View File

@ -477,27 +477,13 @@ static void update_mesh(void)
* GLFW callback functions * GLFW callback functions
*********************************************************************/ *********************************************************************/
/* The program runs as long as this is GL_TRUE
*/
static GLboolean running = GL_TRUE;
/* GLFW Window management functions */
static int window_close_callback(GLFWwindow* window)
{
running = GL_FALSE;
/* Disallow window closing
* The window will be closed when the main loop terminates */
return 0;
}
static void key_callback(GLFWwindow* window, int key, int action) static void key_callback(GLFWwindow* window, int key, int action)
{ {
switch(key) switch(key)
{ {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
/* Exit program on Escape */ /* Exit program on Escape */
running = GL_FALSE; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
} }
} }
@ -599,7 +585,6 @@ int main(int argc, char** argv)
} }
/* Register events callback */ /* Register events callback */
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
@ -661,7 +646,7 @@ int main(int argc, char** argv)
iter = 0; iter = 0;
dt = last_update_time = glfwGetTime(); dt = last_update_time = glfwGetTime();
while (running) while (!glfwWindowShouldClose(window))
{ {
++frame; ++frame;
/* render the next frame */ /* render the next frame */

View File

@ -52,7 +52,7 @@ int main(void)
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
float ratio; float ratio;
int width, height; int width, height;

View File

@ -434,6 +434,12 @@ static void mouseButtonFun(GLFWwindow* window, int button, int action)
do_redraw = 1; do_redraw = 1;
} }
static void key_callback(GLFWwindow* window, int key, int action)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
//======================================================================== //========================================================================
// main // main
@ -450,8 +456,6 @@ int main(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
glfwWindowHint(GLFW_DEPTH_BITS, 16);
// Open OpenGL window // Open OpenGL window
window = glfwCreateWindow(500, 500, "Split view demo", NULL, NULL); window = glfwCreateWindow(500, 500, "Split view demo", NULL, NULL);
if (!window) if (!window)
@ -467,6 +471,7 @@ int main(void)
glfwSetWindowRefreshCallback(window, windowRefreshFun); glfwSetWindowRefreshCallback(window, windowRefreshFun);
glfwSetCursorPosCallback(window, cursorPosFun); glfwSetCursorPosCallback(window, cursorPosFun);
glfwSetMouseButtonCallback(window, mouseButtonFun); glfwSetMouseButtonCallback(window, mouseButtonFun);
glfwSetKeyCallback(window, key_callback);
// Enable vsync // Enable vsync
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
@ -475,12 +480,6 @@ int main(void)
glfwGetWindowSize(window, &width, &height); glfwGetWindowSize(window, &width, &height);
windowSizeFun(window, width, height); windowSizeFun(window, width, height);
// Enable sticky keys
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Enable mouse cursor (only needed for fullscreen mode)
glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_NORMAL);
// Main loop // Main loop
for (;;) for (;;)
{ {
@ -499,10 +498,8 @@ int main(void)
// Wait for new events // Wait for new events
glfwWaitEvents(); glfwWaitEvents();
// Check if the ESC key was pressed or the window should be closed // Check if the window should be closed
if (glfwGetKey(window, GLFW_KEY_ESCAPE)) if (glfwWindowShouldClose(window))
break;
if (glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
break; break;
} }

View File

@ -28,7 +28,6 @@
GLfloat alpha = 210.f, beta = -70.f; GLfloat alpha = 210.f, beta = -70.f;
GLfloat zoom = 2.f; GLfloat zoom = 2.f;
GLboolean running = GL_TRUE;
GLboolean locked = GL_FALSE; GLboolean locked = GL_FALSE;
int cursorX; int cursorX;
@ -279,7 +278,7 @@ void key_callback(GLFWwindow* window, int key, int action)
switch (key) switch (key)
{ {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
running = 0; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
case GLFW_KEY_SPACE: case GLFW_KEY_SPACE:
init_grid(); init_grid();
@ -382,17 +381,6 @@ void window_size_callback(GLFWwindow* window, int width, int height)
} }
//========================================================================
// Callback function for window close events
//========================================================================
static int window_close_callback(GLFWwindow* window)
{
running = GL_FALSE;
return GL_TRUE;
}
//======================================================================== //========================================================================
// main // main
//======================================================================== //========================================================================
@ -416,7 +404,6 @@ int main(int argc, char* argv[])
} }
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowSizeCallback(window, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
@ -439,7 +426,7 @@ int main(int argc, char* argv[])
// Initialize timer // Initialize timer
t_old = glfwGetTime() - 0.01; t_old = glfwGetTime() - 0.01;
while (running) while (!glfwWindowShouldClose(window))
{ {
t = glfwGetTime(); t = glfwGetTime();
dt_total = t - t_old; dt_total = t - t_old;

View File

@ -483,7 +483,6 @@ extern "C" {
#define GLFW_FOCUSED 0x00020001 #define GLFW_FOCUSED 0x00020001
#define GLFW_ICONIFIED 0x00020002 #define GLFW_ICONIFIED 0x00020002
#define GLFW_SHOULD_CLOSE 0x00020003
#define GLFW_RESIZABLE 0x00022007 #define GLFW_RESIZABLE 0x00022007
#define GLFW_VISIBLE 0x00022008 #define GLFW_VISIBLE 0x00022008
@ -595,8 +594,8 @@ typedef void (* GLFWwindowsizefun)(GLFWwindow*,int,int);
* @return One of @c GL_TRUE or @c GL_FALSE. * @return One of @c GL_TRUE or @c GL_FALSE.
* @ingroup window * @ingroup window
* *
* The return value of the close callback becomes the new value of the @c * The return value of the close callback becomes the new value returned by
* GLFW_SHOULD_CLOSE window parameter. * @ref glfwWindowShouldClose.
* *
* @sa glfwSetWindowCloseCallback * @sa glfwSetWindowCloseCallback
*/ */
@ -1122,6 +1121,23 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, const char* title, G
*/ */
GLFWAPI void glfwDestroyWindow(GLFWwindow* window); GLFWAPI void glfwDestroyWindow(GLFWwindow* window);
/*! @brief Checks whether the specified window has been requested to close.
* @param[in] window The window to query.
* @return @c GL_TRUE if the window should close, or @c GL_FALSE otherwise.
* @ingroup window
*/
GLFWAPI int glfwWindowShouldClose(GLFWwindow* window);
/*! @brief Sets whether the specified window should close.
* @param[in] window The window whose value to change.
* @param[in] value The new value.
* @ingroup window
*
* @note Calling this from the close callback will have no effect, as whatever
* value you set will be overwritten by the return value of the close callback.
*/
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value);
/*! @brief Sets the title of the specified window. /*! @brief Sets the title of the specified window.
* @param[in] window The window whose title to change. * @param[in] window The window whose title to change.
* @param[in] title The UTF-8 encoded window title. * @param[in] title The UTF-8 encoded window title.
@ -1281,9 +1297,6 @@ GLFWAPI GLFWmonitor* glfwGetWindowMonitor(GLFWwindow* window);
* The @c GLFW_RESIZABLE parameter indicates whether the window is resizable * The @c GLFW_RESIZABLE parameter indicates whether the window is resizable
* by the user. * by the user.
* *
* The @c GLFW_SHOULD_CLOSE parameter indicates whether the window has been
* requested by the user to close.
*
* @par Context parameters * @par Context parameters
* *
* The @c GLFW_CLIENT_API parameter indicates the client API provided by the * The @c GLFW_CLIENT_API parameter indicates the client API provided by the
@ -1357,9 +1370,8 @@ GLFWAPI void glfwSetWindowSizeCallback(GLFWwindow* window, GLFWwindowsizefun cbf
* clicks the window's close widget. Calling @ref glfwDestroyWindow does not * clicks the window's close widget. Calling @ref glfwDestroyWindow does not
* cause this callback to be called. * cause this callback to be called.
* *
* The return value of the close callback becomes the new value of the @c * The return value of the close callback becomes the new value returned by
* GLFW_SHOULD_CLOSE window parameter, which you can query with @ref * @ref glfwWindowShouldClose.
* glfwGetWindowParam.
* *
* @remarks <b>Mac OS X:</b> Selecting Quit from the application menu will * @remarks <b>Mac OS X:</b> Selecting Quit from the application menu will
* trigger the close callback for all windows. * trigger the close callback for all windows.

View File

@ -406,6 +406,20 @@ GLFWAPI void glfwDestroyWindow(GLFWwindow* handle)
free(window); free(window);
} }
GLFWAPI int glfwWindowShouldClose(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(0);
return window->closed;
}
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFW_REQUIRE_INIT();
window->closed = value;
}
GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title) GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title)
{ {
_GLFWwindow* window = (_GLFWwindow*) handle; _GLFWwindow* window = (_GLFWwindow*) handle;
@ -524,8 +538,6 @@ GLFWAPI int glfwGetWindowParam(GLFWwindow* handle, int param)
return window == _glfw.focusedWindow; return window == _glfw.focusedWindow;
case GLFW_ICONIFIED: case GLFW_ICONIFIED:
return window->iconified; return window->iconified;
case GLFW_SHOULD_CLOSE:
return window->closed;
case GLFW_RESIZABLE: case GLFW_RESIZABLE:
return window->resizable; return window->resizable;
case GLFW_VISIBLE: case GLFW_VISIBLE:

View File

@ -108,7 +108,7 @@ int main(void)
set_swap_interval(window, swap_interval); set_swap_interval(window, swap_interval);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

View File

@ -34,8 +34,6 @@
#include "getopt.h" #include "getopt.h"
static GLboolean closed = GL_FALSE;
static void usage(void) static void usage(void)
{ {
printf("Usage: clipboard [-h]\n"); printf("Usage: clipboard [-h]\n");
@ -52,12 +50,6 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", 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) static void key_callback(GLFWwindow* window, int key, int action)
{ {
if (action != GLFW_PRESS) if (action != GLFW_PRESS)
@ -66,7 +58,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
switch (key) switch (key)
{ {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
closed = GL_TRUE; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
case GLFW_KEY_V: case GLFW_KEY_V:
@ -139,7 +131,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); 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); glClearColor(0.5f, 0.5f, 0.5f, 0);
while (!closed) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

View File

@ -416,7 +416,7 @@ int main(void)
printf("Main loop starting\n"); printf("Main loop starting\n");
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
glfwWaitEvents(); glfwWaitEvents();
glfwTerminate(); glfwTerminate();

View File

@ -128,7 +128,7 @@ int main(int argc, char** argv)
gluOrtho2D(0.f, 1.f, 0.f, 0.5f); gluOrtho2D(0.f, 1.f, 0.f, 0.5f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
GLfloat time = (GLfloat) glfwGetTime(); GLfloat time = (GLfloat) glfwGetTime();

View File

@ -33,8 +33,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static GLboolean running = GL_TRUE;
static void error_callback(int error, const char* description) static void error_callback(int error, const char* description)
{ {
fprintf(stderr, "Error: %s\n", 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: case GLFW_KEY_ESCAPE:
{ {
printf("%0.3f: User pressed Escape\n", glfwGetTime()); printf("%0.3f: User pressed Escape\n", glfwGetTime());
running = GL_FALSE; glfwSetWindowShouldClose(window, GL_TRUE);
break; 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) int main(void)
{ {
GLFWwindow* window; GLFWwindow* window;
@ -100,9 +91,8 @@ int main(void)
glfwSetWindowFocusCallback(window, window_focus_callback); glfwSetWindowFocusCallback(window, window_focus_callback);
glfwSetKeyCallback(window, window_key_callback); glfwSetKeyCallback(window, window_key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
while (running) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -37,7 +37,6 @@
#define STEP_SIZE 0.1f #define STEP_SIZE 0.1f
static GLboolean closed = GL_FALSE;
static GLfloat gamma_value = 1.0f; static GLfloat gamma_value = 1.0f;
static void usage(void) static void usage(void)
@ -61,12 +60,6 @@ static void error_callback(int error, const char* description)
fprintf(stderr, "Error: %s\n", 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) static void key_callback(GLFWwindow* window, int key, int action)
{ {
if (action != GLFW_PRESS) if (action != GLFW_PRESS)
@ -76,7 +69,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
{ {
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
{ {
closed = GL_TRUE; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
} }
@ -157,7 +150,6 @@ int main(int argc, char** argv)
glfwSwapInterval(1); glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowSizeCallback(window, size_callback); glfwSetWindowSizeCallback(window, size_callback);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
@ -166,7 +158,7 @@ int main(int argc, char** argv)
glClearColor(0.5f, 0.5f, 0.5f, 0); glClearColor(0.5f, 0.5f, 0.5f, 0);
while (!closed) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

View File

@ -35,8 +35,6 @@
#include "getopt.h" #include "getopt.h"
static GLboolean closed = GL_FALSE;
static void usage(void) static void usage(void)
{ {
printf("Usage: iconify [-h] [-f]\n"); 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); 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) static void key_callback(GLFWwindow* window, int key, int action)
{ {
printf("%0.2f Key %s\n", printf("%0.2f Key %s\n",
@ -68,7 +60,7 @@ static void key_callback(GLFWwindow* window, int key, int action)
glfwIconifyWindow(window); glfwIconifyWindow(window);
break; break;
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
closed = GL_TRUE; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
} }
} }
@ -147,7 +139,6 @@ int main(int argc, char** argv)
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
glfwSetWindowSizeCallback(window, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetWindowFocusCallback(window, window_focus_callback); glfwSetWindowFocusCallback(window, window_focus_callback);
glfwSetWindowIconifyCallback(window, window_iconify_callback); glfwSetWindowIconifyCallback(window, window_iconify_callback);
@ -157,7 +148,7 @@ int main(int argc, char** argv)
glEnable(GL_SCISSOR_TEST); glEnable(GL_SCISSOR_TEST);
while (!closed) while (!glfwWindowShouldClose(window))
{ {
glfwGetWindowSize(window, &width, &height); glfwGetWindowSize(window, &width, &height);

View File

@ -211,7 +211,7 @@ int main(void)
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSwapInterval(1); glfwSwapInterval(1);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

View File

@ -35,8 +35,6 @@
#include "getopt.h" #include "getopt.h"
static GLFWwindow* window_handle = NULL;
enum Mode enum Mode
{ {
LIST_MODE, LIST_MODE,
@ -75,19 +73,10 @@ static void window_size_callback(GLFWwindow* window, int width, int height)
glViewport(0, 0, width, 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) static void key_callback(GLFWwindow* window, int key, int action)
{ {
if (key == GLFW_KEY_ESCAPE) if (key == GLFW_KEY_ESCAPE)
{ glfwSetWindowShouldClose(window, GL_TRUE);
glfwDestroyWindow(window);
window_handle = NULL;
}
} }
static void list_modes(GLFWmonitor* monitor) static void list_modes(GLFWmonitor* monitor)
@ -124,6 +113,7 @@ static void list_modes(GLFWmonitor* monitor)
static void test_modes(GLFWmonitor* monitor) static void test_modes(GLFWmonitor* monitor)
{ {
int i, count; int i, count;
GLFWwindow* window;
const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
@ -140,11 +130,11 @@ static void test_modes(GLFWmonitor* monitor)
glfwGetMonitorName(monitor), glfwGetMonitorName(monitor),
format_mode(mode)); format_mode(mode));
window_handle = glfwCreateWindow(mode->width, mode->height, window = glfwCreateWindow(mode->width, mode->height,
"Video Mode Test", "Video Mode Test",
glfwGetPrimaryMonitor(), glfwGetPrimaryMonitor(),
NULL); NULL);
if (!window_handle) if (!window)
{ {
printf("Failed to enter mode %u: %s\n", printf("Failed to enter mode %u: %s\n",
(unsigned int) i, (unsigned int) i,
@ -152,11 +142,10 @@ static void test_modes(GLFWmonitor* monitor)
continue; continue;
} }
glfwSetWindowSizeCallback(window_handle, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window_handle, window_close_callback); glfwSetKeyCallback(window, key_callback);
glfwSetKeyCallback(window_handle, key_callback);
glfwMakeContextCurrent(window_handle); glfwMakeContextCurrent(window);
glfwSwapInterval(1); glfwSwapInterval(1);
glfwSetTime(0.0); glfwSetTime(0.0);
@ -164,10 +153,10 @@ static void test_modes(GLFWmonitor* monitor)
while (glfwGetTime() < 5.0) while (glfwGetTime() < 5.0)
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window_handle); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
if (!window_handle) if (glfwWindowShouldClose(window))
{ {
printf("User terminated program\n"); printf("User terminated program\n");
@ -180,7 +169,7 @@ static void test_modes(GLFWmonitor* monitor)
glGetIntegerv(GL_GREEN_BITS, &current.greenBits); glGetIntegerv(GL_GREEN_BITS, &current.greenBits);
glGetIntegerv(GL_BLUE_BITS, &current.blueBits); glGetIntegerv(GL_BLUE_BITS, &current.blueBits);
glfwGetWindowSize(window_handle, &current.width, &current.height); glfwGetWindowSize(window, &current.width, &current.height);
if (current.redBits != mode->redBits || if (current.redBits != mode->redBits ||
current.greenBits != mode->greenBits || current.greenBits != mode->greenBits ||
@ -200,8 +189,9 @@ static void test_modes(GLFWmonitor* monitor)
printf("Closing window\n"); printf("Closing window\n");
glfwDestroyWindow(window_handle); glfwDestroyWindow(window);
window_handle = NULL; window = NULL;
glfwPollEvents(); glfwPollEvents();
} }
} }

View File

@ -36,12 +36,9 @@
#include <stdlib.h> #include <stdlib.h>
static GLboolean reopen = GL_FALSE; static GLboolean reopen = GL_FALSE;
static GLFWwindow* window_handle = NULL;
static int cursor_x; static int cursor_x;
static int cursor_y; static int cursor_y;
static GLboolean open_window(void);
static void toggle_cursor(GLFWwindow* window) static void toggle_cursor(GLFWwindow* window)
{ {
if (glfwGetInputMode(window, GLFW_CURSOR_MODE) == GLFW_CURSOR_CAPTURED) 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); glViewport(0, 0, width, height);
} }
static GLboolean open_window(void) static GLFWwindow* open_window(void)
{ {
window_handle = glfwCreateWindow(640, 480, "Peter Detector", NULL, NULL); GLFWwindow* window = glfwCreateWindow(640, 480, "Peter Detector", NULL, NULL);
if (!window_handle) if (!window)
return GL_FALSE; return NULL;
glfwMakeContextCurrent(window_handle); glfwMakeContextCurrent(window);
glfwSwapInterval(1); 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); printf("Cursor position: %i %i\n", cursor_x, cursor_y);
glfwSetWindowSizeCallback(window_handle, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetCursorPosCallback(window_handle, cursor_position_callback); glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetKeyCallback(window_handle, key_callback); glfwSetKeyCallback(window, key_callback);
return GL_TRUE; return window;
} }
int main(void) int main(void)
{ {
GLFWwindow* window;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (!open_window()) window = open_window();
if (!window)
{ {
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -129,17 +129,18 @@ int main(void)
glClearColor(0.f, 0.f, 0.f, 0.f); glClearColor(0.f, 0.f, 0.f, 0.f);
while (!glfwGetWindowParam(window_handle, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window_handle); glfwSwapBuffers(window);
glfwWaitEvents(); glfwWaitEvents();
if (reopen) if (reopen)
{ {
glfwDestroyWindow(window_handle); glfwDestroyWindow(window);
if (!open_window()) window = open_window();
if (!window)
{ {
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View File

@ -38,9 +38,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static GLFWwindow* window_handle = NULL;
static GLboolean closed = GL_FALSE;
static void error_callback(int error, const char* description) static void error_callback(int error, const char* description)
{ {
fprintf(stderr, "Error: %s\n", 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) static int window_close_callback(GLFWwindow* window)
{ {
printf("Close callback triggered\n"); printf("Close callback triggered\n");
closed = GL_TRUE; return GL_TRUE;
return 0;
} }
static void key_callback(GLFWwindow* window, int key, int action) 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_Q:
case GLFW_KEY_ESCAPE: case GLFW_KEY_ESCAPE:
closed = GL_TRUE; glfwSetWindowShouldClose(window, GL_TRUE);
break; break;
} }
} }
static GLboolean open_window(int width, int height, GLFWmonitor* monitor) static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
{ {
double base; double base;
GLFWwindow* window;
base = glfwGetTime(); base = glfwGetTime();
window_handle = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
if (!window_handle) if (!window)
return GL_FALSE; return NULL;
glfwMakeContextCurrent(window_handle); glfwMakeContextCurrent(window);
glfwSwapInterval(1); glfwSwapInterval(1);
glfwSetWindowSizeCallback(window_handle, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
glfwSetWindowCloseCallback(window_handle, window_close_callback); glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window_handle, key_callback); glfwSetKeyCallback(window, key_callback);
printf("Opening %s mode window took %0.3f seconds\n", printf("Opening %s mode window took %0.3f seconds\n",
monitor ? "fullscreen" : "windowed", monitor ? "fullscreen" : "windowed",
glfwGetTime() - base); glfwGetTime() - base);
return GL_TRUE; return window;
} }
static void close_window(void) static void close_window(GLFWwindow* window)
{ {
double base = glfwGetTime(); double base = glfwGetTime();
glfwDestroyWindow(window);
glfwDestroyWindow(window_handle);
window_handle = NULL;
printf("Closing window took %0.3f seconds\n", glfwGetTime() - base); printf("Closing window took %0.3f seconds\n", glfwGetTime() - base);
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int count = 0; int count = 0;
GLFWwindow* window;
glfwSetErrorCallback(error_callback); glfwSetErrorCallback(error_callback);
@ -126,7 +121,8 @@ int main(int argc, char** argv)
monitor = monitors[rand() % monitorCount]; monitor = monitors[rand() % monitorCount];
} }
if (!open_window(640, 480, monitor)) window = open_window(640, 480, monitor);
if (!window)
{ {
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -147,12 +143,12 @@ int main(int argc, char** argv)
glRectf(-0.5f, -0.5f, 1.f, 1.f); glRectf(-0.5f, -0.5f, 1.f, 1.f);
glPopMatrix(); glPopMatrix();
glfwSwapBuffers(window_handle); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
if (closed) if (glfwWindowShouldClose(window))
{ {
close_window(); close_window(window);
printf("User closed window\n"); printf("User closed window\n");
glfwTerminate(); glfwTerminate();
@ -161,7 +157,7 @@ int main(int argc, char** argv)
} }
printf("Closing window\n"); printf("Closing window\n");
close_window(); close_window(window);
count++; count++;
} }

View File

@ -38,7 +38,6 @@
#define OFFSET 50 #define OFFSET 50
static GLFWwindow* windows[2]; static GLFWwindow* windows[2];
static GLboolean closed = GL_FALSE;
static void error_callback(int error, const char* description) 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) static void key_callback(GLFWwindow* window, int key, int action)
{ {
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
closed = GL_TRUE; glfwSetWindowShouldClose(window, GL_TRUE);
}
static int window_close_callback(GLFWwindow* window)
{
closed = GL_TRUE;
return GL_FALSE;
} }
static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, int posY) 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); glfwSetWindowPos(window, posX, posY);
glfwShowWindow(window); glfwShowWindow(window);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback); glfwSetKeyCallback(window, key_callback);
return window; return window;
@ -172,7 +164,8 @@ int main(int argc, char** argv)
glfwMakeContextCurrent(windows[0]); glfwMakeContextCurrent(windows[0]);
while (!closed) while (!glfwWindowShouldClose(windows[0]) &&
!glfwWindowShouldClose(windows[1]))
{ {
glfwMakeContextCurrent(windows[0]); glfwMakeContextCurrent(windows[0]);
draw_quad(texture); draw_quad(texture);

View File

@ -91,7 +91,7 @@ int main(void)
glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);

View File

@ -124,7 +124,7 @@ int main(void)
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
if (glfwGetWindowParam(threads[i].window, GLFW_SHOULD_CLOSE)) if (glfwWindowShouldClose(threads[i].window))
running = GL_FALSE; running = GL_FALSE;
} }
} }

View File

@ -63,7 +63,7 @@ int main(void)
glfwSetWindowSizeCallback(window, window_size_callback); glfwSetWindowSizeCallback(window, window_size_callback);
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE)) while (!glfwWindowShouldClose(window))
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window); glfwSwapBuffers(window);

View File

@ -85,7 +85,7 @@ int main(void)
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(windows[i]); glfwSwapBuffers(windows[i]);
if (glfwGetWindowParam(windows[i], GLFW_SHOULD_CLOSE)) if (glfwWindowShouldClose(windows[i]))
running = GL_FALSE; running = GL_FALSE;
} }