From 89018331f76d257d48aab9691ef872c2bba70aaa Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 13 Sep 2012 17:29:07 +0200 Subject: [PATCH 1/4] Fixed invalid uses of glfwDestroyWindow. --- tests/clipboard.c | 17 +++++++++++++---- tests/gamma.c | 12 ++++++++++-- tests/iconify.c | 17 +++++++++++++---- tests/modes.c | 32 ++++++++++++++++---------------- tests/peter.c | 20 ++++++++++++++------ tests/sharing.c | 19 +++++-------------- 6 files changed, 71 insertions(+), 46 deletions(-) diff --git a/tests/clipboard.c b/tests/clipboard.c index fcd1c307..f83bbfea 100644 --- a/tests/clipboard.c +++ b/tests/clipboard.c @@ -34,6 +34,8 @@ #include "getopt.h" +static GLboolean closed = GL_FALSE; + static void usage(void) { printf("Usage: clipboard [-h]\n"); @@ -45,6 +47,12 @@ static GLboolean control_is_down(GLFWwindow window) glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL); } +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) @@ -53,7 +61,7 @@ static void key_callback(GLFWwindow window, int key, int action) switch (key) { case GLFW_KEY_ESCAPE: - glfwDestroyWindow(window); + closed = GL_TRUE; break; case GLFW_KEY_V: @@ -80,7 +88,7 @@ static void key_callback(GLFWwindow window, int key, int action) } } -static void size_callback(GLFWwindow window, int width, int height) +static void window_size_callback(GLFWwindow window, int width, int height) { glViewport(0, 0, width, height); } @@ -130,7 +138,8 @@ int main(int argc, char** argv) glfwSwapInterval(1); glfwSetKeyCallback(key_callback); - glfwSetWindowSizeCallback(size_callback); + glfwSetWindowSizeCallback(window_size_callback); + glfwSetWindowCloseCallback(window_close_callback); glMatrixMode(GL_PROJECTION); glOrtho(-1.f, 1.f, -1.f, 1.f, -1.f, 1.f); @@ -138,7 +147,7 @@ int main(int argc, char** argv) glClearColor(0.5f, 0.5f, 0.5f, 0); - while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + while (!closed) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/gamma.c b/tests/gamma.c index e74d5b43..c30dd53c 100644 --- a/tests/gamma.c +++ b/tests/gamma.c @@ -37,6 +37,7 @@ #define STEP_SIZE 0.1f +static GLboolean closed = GL_FALSE; static GLfloat gamma_value = 1.0f; static void usage(void) @@ -51,6 +52,12 @@ static void set_gamma(float value) glfwSetGamma(gamma_value); } +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) @@ -60,7 +67,7 @@ static void key_callback(GLFWwindow window, int key, int action) { case GLFW_KEY_ESCAPE: { - glfwDestroyWindow(window); + closed = GL_TRUE; break; } @@ -145,6 +152,7 @@ int main(int argc, char** argv) glfwSwapInterval(1); glfwSetKeyCallback(key_callback); + glfwSetWindowCloseCallback(window_close_callback); glfwSetWindowSizeCallback(size_callback); glMatrixMode(GL_PROJECTION); @@ -153,7 +161,7 @@ int main(int argc, char** argv) glClearColor(0.5f, 0.5f, 0.5f, 0); - while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + while (!closed) { glClear(GL_COLOR_BUFFER_BIT); diff --git a/tests/iconify.c b/tests/iconify.c index 33b6f058..77815358 100644 --- a/tests/iconify.c +++ b/tests/iconify.c @@ -35,11 +35,19 @@ #include "getopt.h" +static GLboolean closed = GL_FALSE; + static void usage(void) { printf("Usage: iconify [-h] [-f]\n"); } +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", @@ -55,12 +63,12 @@ static void key_callback(GLFWwindow window, int key, int action) glfwIconifyWindow(window); break; case GLFW_KEY_ESCAPE: - glfwDestroyWindow(window); + closed = GL_TRUE; break; } } -static void size_callback(GLFWwindow window, int width, int height) +static void window_size_callback(GLFWwindow window, int width, int height) { printf("%0.2f Size %ix%i\n", glfwGetTime(), width, height); @@ -124,11 +132,12 @@ int main(int argc, char** argv) glfwSwapInterval(1); glfwSetKeyCallback(key_callback); - glfwSetWindowSizeCallback(size_callback); + glfwSetWindowSizeCallback(window_size_callback); + glfwSetWindowCloseCallback(window_close_callback); glEnable(GL_SCISSOR_TEST); - while (!glfwGetWindowParam(window, GLFW_CLOSE_REQUESTED)) + while (!closed) { if (iconified != glfwGetWindowParam(window, GLFW_ICONIFIED) || active != glfwGetWindowParam(window, GLFW_ACTIVE)) diff --git a/tests/modes.c b/tests/modes.c index 5ce65abb..ef1db71c 100644 --- a/tests/modes.c +++ b/tests/modes.c @@ -35,7 +35,7 @@ #include "getopt.h" -static GLFWwindow window = NULL; +static GLFWwindow window_handle = NULL; enum Mode { @@ -68,25 +68,25 @@ static void error_callback(int error, const char* description) fprintf(stderr, "Error: %s\n", description); } -static void window_size_callback(GLFWwindow in_window, int width, int height) +static void window_size_callback(GLFWwindow window, int width, int height) { printf("Window resized to %ix%i\n", width, height); glViewport(0, 0, width, height); } -static int window_close_callback(GLFWwindow dummy) +static int window_close_callback(GLFWwindow window) { - window = NULL; + window_handle = NULL; return GL_TRUE; } -static void key_callback(GLFWwindow dummy, int key, int action) +static void key_callback(GLFWwindow window, int key, int action) { if (key == GLFW_KEY_ESCAPE) { glfwDestroyWindow(window); - window = NULL; + window_handle = NULL; } } @@ -132,10 +132,10 @@ static void test_modes(void) printf("Testing mode %u: %s", (unsigned int) i, format_mode(mode)); - window = glfwCreateWindow(mode->width, mode->height, - GLFW_FULLSCREEN, "Video Mode Test", - NULL); - if (!window) + window_handle = glfwCreateWindow(mode->width, mode->height, + GLFW_FULLSCREEN, "Video Mode Test", + NULL); + if (!window_handle) { printf("Failed to enter mode %u: %s\n", (unsigned int) i, @@ -143,7 +143,7 @@ static void test_modes(void) continue; } - glfwMakeContextCurrent(window); + glfwMakeContextCurrent(window_handle); glfwSwapInterval(1); glfwSetTime(0.0); @@ -151,10 +151,10 @@ static void test_modes(void) while (glfwGetTime() < 5.0) { glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(window); + glfwSwapBuffers(window_handle); glfwPollEvents(); - if (!window) + if (!window_handle) { printf("User terminated program\n"); exit(EXIT_SUCCESS); @@ -165,7 +165,7 @@ static void test_modes(void) glGetIntegerv(GL_GREEN_BITS, ¤t.greenBits); glGetIntegerv(GL_BLUE_BITS, ¤t.blueBits); - glfwGetWindowSize(window, ¤t.width, ¤t.height); + glfwGetWindowSize(window_handle, ¤t.width, ¤t.height); if (current.redBits != mode->redBits || current.greenBits != mode->greenBits || @@ -185,9 +185,9 @@ static void test_modes(void) printf("Closing window\n"); - glfwDestroyWindow(window); + glfwDestroyWindow(window_handle); + window_handle = NULL; glfwPollEvents(); - window = NULL; } } diff --git a/tests/peter.c b/tests/peter.c index ae6e4b7d..59c917e9 100644 --- a/tests/peter.c +++ b/tests/peter.c @@ -35,6 +35,7 @@ #include #include +static GLboolean reopen = GL_FALSE; static GLFWwindow window_handle = NULL; static int cursor_x; static int cursor_y; @@ -77,10 +78,7 @@ static void key_callback(GLFWwindow window, int key, int action) case GLFW_KEY_R: { if (action == GLFW_PRESS) - { - glfwDestroyWindow(window); - open_window(); - } + reopen = GL_TRUE; break; } @@ -121,8 +119,6 @@ int main(void) if (!open_window()) { - glfwTerminate(); - fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); exit(EXIT_FAILURE); } @@ -135,6 +131,18 @@ int main(void) glfwSwapBuffers(window_handle); glfwWaitEvents(); + + if (reopen) + { + glfwDestroyWindow(window_handle); + if (!open_window()) + { + fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); + exit(EXIT_FAILURE); + } + + reopen = GL_FALSE; + } } glfwTerminate(); diff --git a/tests/sharing.c b/tests/sharing.c index 95f21342..64e61740 100644 --- a/tests/sharing.c +++ b/tests/sharing.c @@ -37,27 +37,18 @@ #define HEIGHT 400 static GLFWwindow windows[2]; +static GLboolean closed = GL_FALSE; static void key_callback(GLFWwindow window, int key, int action) { if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) - glfwDestroyWindow(window); + closed = GL_TRUE; } static int window_close_callback(GLFWwindow window) { - int i; - - for (i = 0; i < 2; i++) - { - if (windows[i] == window) - { - windows[i] = NULL; - break; - } - } - - return GL_TRUE; + closed = GL_TRUE; + return GL_FALSE; } static GLFWwindow open_window(const char* title, GLFWwindow share) @@ -170,7 +161,7 @@ int main(int argc, char** argv) glfwGetWindowPos(windows[0], &x, &y); glfwSetWindowPos(windows[1], x + WIDTH + 50, y); - while (windows[0] && windows[1]) + while (!closed) { glfwMakeContextCurrent(windows[0]); draw_quad(texture); From dbd4d1657ea38ecbb18c66175fd58d365714f6de Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 13 Sep 2012 21:57:42 +0200 Subject: [PATCH 2/4] Formatting. --- src/win32_window.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/win32_window.c b/src/win32_window.c index 735f9b97..84829d61 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1130,6 +1130,7 @@ void _glfwPlatformHideWindow(_GLFWwindow* window) ShowWindow(window->Win32.handle, SW_HIDE); } + //======================================================================== // Write back window parameters into GLFW window structure //======================================================================== From d6d5fb26845ebab3383afde878b721839cd8798d Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Wed, 19 Sep 2012 13:17:53 +0200 Subject: [PATCH 3/4] Simplified character input. --- src/win32_window.c | 71 ++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 53 deletions(-) diff --git a/src/win32_window.c b/src/win32_window.c index 84829d61..636fab07 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -305,37 +305,6 @@ static int translateKey(WPARAM wParam, LPARAM lParam) } -//======================================================================== -// Translates a Windows key to Unicode -//======================================================================== - -static void translateChar(_GLFWwindow* window, DWORD wParam, DWORD lParam) -{ - BYTE keyboard_state[256]; - WCHAR unicode_buf[10]; - UINT scan_code; - int i, num_chars; - - GetKeyboardState(keyboard_state); - - // Derive scan code from lParam and action - scan_code = (lParam & 0x01ff0000) >> 16; - - num_chars = ToUnicode( - wParam, // virtual-key code - scan_code, // scan code - keyboard_state, // key-state array - unicode_buf, // buffer for translated key - 10, // size of translated key buffer - 0 // active-menu flag - ); - - // Report characters - for (i = 0; i < num_chars; i++) - _glfwInputChar(window, (int) unicode_buf[i]); -} - - //======================================================================== // Window callback function (handles window events) //======================================================================== @@ -459,13 +428,15 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, case WM_SYSKEYDOWN: { _glfwInputKey(window, translateKey(wParam, lParam), GLFW_PRESS); - - if (_glfwLibrary.charCallback) - translateChar(window, (DWORD) wParam, (DWORD) lParam); - break; } + case WM_CHAR: + { + _glfwInputChar(window, wParam); + return 0; + } + case WM_KEYUP: case WM_SYSKEYUP: { @@ -1177,28 +1148,22 @@ void _glfwPlatformPollEvents(void) while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { - switch (msg.message) + if (msg.message == WM_QUIT) { - case WM_QUIT: + // Treat WM_QUIT as a close on all windows + + window = _glfwLibrary.windowListHead; + while (window) { - // Treat WM_QUIT as a close on all windows - - window = _glfwLibrary.windowListHead; - while (window) - { - _glfwInputWindowCloseRequest(window); - window = window->next; - } - - break; - } - - default: - { - DispatchMessage(&msg); - break; + _glfwInputWindowCloseRequest(window); + window = window->next; } } + else + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } } // LSHIFT/RSHIFT fixup (keys tend to "stick" without this fix) From 718af531a332ca876b9438ece39ccab1cdbf11f2 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Sun, 23 Sep 2012 14:08:36 +0200 Subject: [PATCH 4/4] Fixed order of operations. --- src/window.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/window.c b/src/window.c index 817691de..383662af 100644 --- a/src/window.c +++ b/src/window.c @@ -336,8 +336,9 @@ GLFWAPI GLFWwindow glfwCreateWindow(int width, int height, // Cache the actual (as opposed to requested) window parameters _glfwPlatformRefreshWindowParams(window); - // Cache the actual (as opposed to requested) context parameters glfwMakeContextCurrent(window); + + // Cache the actual (as opposed to requested) context parameters if (!_glfwRefreshContextParams()) { glfwDestroyWindow(window); @@ -353,6 +354,11 @@ GLFWAPI GLFWwindow glfwCreateWindow(int width, int height, return GL_FALSE; } + // Clearing the front buffer to black to avoid garbage pixels left over + // from previous uses of our bit of VRAM + glClear(GL_COLOR_BUFFER_BIT); + _glfwPlatformSwapBuffers(window); + // Restore the previously current context (or NULL) glfwMakeContextCurrent(previous); @@ -361,12 +367,7 @@ GLFWAPI GLFWwindow glfwCreateWindow(int width, int height, if (mode == GLFW_FULLSCREEN) glfwSetInputMode(window, GLFW_CURSOR_MODE, GLFW_CURSOR_CAPTURED); - // Clearing the front buffer to black to avoid garbage pixels left over - // from previous uses of our bit of VRAM - glClear(GL_COLOR_BUFFER_BIT); - _glfwPlatformSwapBuffers(window); - - if (wndconfig.visible || mode == GLFW_FULLSCREEN) + if (mode == GLFW_FULLSCREEN || wndconfig.visible) glfwShowWindow(window); return window;