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
* Added `GLFWwindow` and updated window-related functions and callbacks to take
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 `glfwDefaultWindowHints` for resetting all window hints to their
default values
@ -324,8 +326,7 @@ GLFW.
* Replaced `glfwEnable` and `glfwDisable` with `glfwGetInputMode` and
`glfwSetInputMode`
* Replaced `joystick` test with graphical version
* Replaced automatic closing of windows with `GLFW_SHOULD_CLOSE` window
parameter
* Replaced automatic closing of windows with the window close flag
* Removed the `GLFW_KEY_REPEAT` input option
* Removed event auto-polling and the `GLFW_AUTO_POLL_EVENTS` window enable
* Removed the Win32 port .def files

View File

@ -163,21 +163,32 @@ glfwMakeContextCurrent(window);
@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
glfwGetWindowParam. Some are related to the window itself and others to the
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
combination like Alt+F4, check the @c GLFW_SHOULD_CLOSE parameter.
Each window has a flag indicating whether the window should be closed. This can
be checked with @ref glfwWindowShouldClose.
When the user attempts to close the window, either by pressing the close widget
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
while (!glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
while (!glfwWindowShouldClose(window))
{
// Keep running
}
@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

View File

@ -43,6 +43,7 @@
void init( void );
void display( void );
void reshape( GLFWwindow* window, int w, int h );
void key_callback( GLFWwindow* window, int key, int action );
void DrawBoingBall( void );
void BounceBall( double dt );
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 */
}
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.
@ -584,6 +590,7 @@ int main( void )
}
glfwSetWindowSizeCallback(window, reshape);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
glfwSwapInterval( 1 );
@ -591,7 +598,6 @@ int main( void )
glfwGetWindowSize(window, &width, &height);
reshape(window, width, height);
glfwSetInputMode( window, GLFW_STICKY_KEYS, GL_TRUE );
glfwSetTime( 0.0 );
init();
@ -612,9 +618,7 @@ int main( void )
glfwPollEvents();
/* Check if we are still running */
if (glfwGetKey( window, GLFW_KEY_ESCAPE ))
break;
if (glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
if (glfwWindowShouldClose(window))
break;
}

View File

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

View File

@ -477,27 +477,13 @@ static void update_mesh(void)
* 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)
{
switch(key)
{
case GLFW_KEY_ESCAPE:
/* Exit program on Escape */
running = GL_FALSE;
glfwSetWindowShouldClose(window, GL_TRUE);
break;
}
}
@ -599,7 +585,6 @@ int main(int argc, char** argv)
}
/* Register events callback */
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
@ -661,7 +646,7 @@ int main(int argc, char** argv)
iter = 0;
dt = last_update_time = glfwGetTime();
while (running)
while (!glfwWindowShouldClose(window))
{
++frame;
/* render the next frame */

View File

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

View File

@ -434,6 +434,12 @@ static void mouseButtonFun(GLFWwindow* window, int button, int action)
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
@ -450,8 +456,6 @@ int main(void)
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_DEPTH_BITS, 16);
// Open OpenGL window
window = glfwCreateWindow(500, 500, "Split view demo", NULL, NULL);
if (!window)
@ -467,6 +471,7 @@ int main(void)
glfwSetWindowRefreshCallback(window, windowRefreshFun);
glfwSetCursorPosCallback(window, cursorPosFun);
glfwSetMouseButtonCallback(window, mouseButtonFun);
glfwSetKeyCallback(window, key_callback);
// Enable vsync
glfwMakeContextCurrent(window);
@ -475,12 +480,6 @@ int main(void)
glfwGetWindowSize(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
for (;;)
{
@ -499,10 +498,8 @@ 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))
break;
if (glfwGetWindowParam(window, GLFW_SHOULD_CLOSE))
// Check if the window should be closed
if (glfwWindowShouldClose(window))
break;
}

View File

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

View File

@ -483,7 +483,6 @@ extern "C" {
#define GLFW_FOCUSED 0x00020001
#define GLFW_ICONIFIED 0x00020002
#define GLFW_SHOULD_CLOSE 0x00020003
#define GLFW_RESIZABLE 0x00022007
#define GLFW_VISIBLE 0x00022008
@ -595,8 +594,8 @@ typedef void (* GLFWwindowsizefun)(GLFWwindow*,int,int);
* @return One of @c GL_TRUE or @c GL_FALSE.
* @ingroup window
*
* The return value of the close callback becomes the new value of the @c
* GLFW_SHOULD_CLOSE window parameter.
* The return value of the close callback becomes the new value returned by
* @ref glfwWindowShouldClose.
*
* @sa glfwSetWindowCloseCallback
*/
@ -1122,6 +1121,23 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, const char* title, G
*/
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.
* @param[in] window The window whose title to change.
* @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
* by the user.
*
* The @c GLFW_SHOULD_CLOSE parameter indicates whether the window has been
* requested by the user to close.
*
* @par Context parameters
*
* 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
* cause this callback to be called.
*
* The return value of the close callback becomes the new value of the @c
* GLFW_SHOULD_CLOSE window parameter, which you can query with @ref
* glfwGetWindowParam.
* The return value of the close callback becomes the new value returned by
* @ref glfwWindowShouldClose.
*
* @remarks <b>Mac OS X:</b> Selecting Quit from the application menu will
* trigger the close callback for all windows.

View File

@ -406,6 +406,20 @@ GLFWAPI void glfwDestroyWindow(GLFWwindow* handle)
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)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
@ -524,8 +538,6 @@ GLFWAPI int glfwGetWindowParam(GLFWwindow* handle, int param)
return window == _glfw.focusedWindow;
case GLFW_ICONIFIED:
return window->iconified;
case GLFW_SHOULD_CLOSE:
return window->closed;
case GLFW_RESIZABLE:
return window->resizable;
case GLFW_VISIBLE:

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);

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);

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();

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();

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);

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);

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);

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);

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();
}
}

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);

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++;
}

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);

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);

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;
}
}

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);

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;
}