From 33c7c28dde77a9d68e7159c646eef1230f64f5b4 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Wed, 15 Jul 2015 12:13:42 +0200 Subject: [PATCH] Replaced the various cursor tests with a sane one. --- .gitignore | 3 - tests/CMakeLists.txt | 11 +- tests/accuracy.c | 124 -------------- tests/cursor.c | 400 ++++++++++++++++++++++--------------------- tests/cursoranim.c | 135 --------------- tests/peter.c | 151 ---------------- 6 files changed, 207 insertions(+), 617 deletions(-) delete mode 100644 tests/accuracy.c delete mode 100644 tests/cursoranim.c delete mode 100644 tests/peter.c diff --git a/.gitignore b/.gitignore index 2001ee87..52cb5f19 100644 --- a/.gitignore +++ b/.gitignore @@ -55,10 +55,8 @@ examples/simple examples/wave tests/*.app tests/*.exe -tests/accuracy tests/clipboard tests/cursor -tests/cursoranim tests/empty tests/events tests/gamma @@ -67,7 +65,6 @@ tests/iconify tests/joysticks tests/monitors tests/msaa -tests/peter tests/reopen tests/sharing tests/tearing diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index de886a50..bfc67d83 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,16 +28,9 @@ add_executable(glfwinfo glfwinfo.c ${GETOPT}) add_executable(iconify iconify.c ${GETOPT}) add_executable(joysticks joysticks.c) add_executable(monitors monitors.c ${GETOPT}) -add_executable(peter peter.c) add_executable(reopen reopen.c) add_executable(cursor cursor.c) -add_executable(cursoranim WIN32 MACOSX_BUNDLE cursoranim.c) -set_target_properties(cursoranim PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Cursor animation") - -add_executable(accuracy WIN32 MACOSX_BUNDLE accuracy.c) -set_target_properties(accuracy PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Accuracy") - add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD}) set_target_properties(empty PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Empty Event") @@ -59,9 +52,9 @@ set_target_properties(windows PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Windows") target_link_libraries(empty "${CMAKE_THREAD_LIBS_INIT}" "${RT_LIBRARY}") target_link_libraries(threads "${CMAKE_THREAD_LIBS_INIT}" "${RT_LIBRARY}") -set(WINDOWS_BINARIES accuracy empty sharing tearing threads title windows cursoranim) +set(WINDOWS_BINARIES empty sharing tearing threads title windows) set(CONSOLE_BINARIES clipboard events msaa gamma glfwinfo - iconify joysticks monitors peter reopen cursor) + iconify joysticks monitors reopen cursor) set_target_properties(${WINDOWS_BINARIES} ${CONSOLE_BINARIES} PROPERTIES FOLDER "GLFW3/Tests") diff --git a/tests/accuracy.c b/tests/accuracy.c deleted file mode 100644 index 5ec3a446..00000000 --- a/tests/accuracy.c +++ /dev/null @@ -1,124 +0,0 @@ -//======================================================================== -// Mouse cursor accuracy test -// Copyright (c) Camilla Berglund -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would -// be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, and must not -// be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source -// distribution. -// -//======================================================================== -// -// This test came about as the result of bug #1867804 -// -// No sign of said bug has so far been detected -// -//======================================================================== - -#include - -#include -#include - -static double cursor_x = 0.0, cursor_y = 0.0; -static int swap_interval = 1; - -static void set_swap_interval(GLFWwindow* window, int interval) -{ - char title[256]; - - swap_interval = interval; - glfwSwapInterval(swap_interval); - - sprintf(title, "Cursor Inaccuracy Detector (interval %i)", swap_interval); - - glfwSetWindowTitle(window, title); -} - -static void error_callback(int error, const char* description) -{ - fprintf(stderr, "Error: %s\n", description); -} - -static void cursor_position_callback(GLFWwindow* window, double x, double y) -{ - cursor_x = x; - cursor_y = y; -} - -static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) -{ - if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) - set_swap_interval(window, 1 - swap_interval); -} - -int main(void) -{ - GLFWwindow* window; - - glfwSetErrorCallback(error_callback); - - if (!glfwInit()) - exit(EXIT_FAILURE); - - window = glfwCreateWindow(640, 480, "", NULL, NULL); - if (!window) - { - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glfwSetCursorPosCallback(window, cursor_position_callback); - glfwSetKeyCallback(window, key_callback); - - glfwMakeContextCurrent(window); - - set_swap_interval(window, swap_interval); - - while (!glfwWindowShouldClose(window)) - { - int wnd_width, wnd_height, fb_width, fb_height; - float scale; - - glfwGetWindowSize(window, &wnd_width, &wnd_height); - glfwGetFramebufferSize(window, &fb_width, &fb_height); - - scale = (float) fb_width / (float) wnd_width; - - glClear(GL_COLOR_BUFFER_BIT); - - glViewport(0, 0, fb_width, fb_height); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0.f, fb_width, 0.f, fb_height, 0.f, 1.f); - - glBegin(GL_LINES); - glVertex2f(0.f, (GLfloat) (fb_height - cursor_y * scale)); - glVertex2f((GLfloat) fb_width, (GLfloat) (fb_height - cursor_y * scale)); - glVertex2f((GLfloat) cursor_x * scale, 0.f); - glVertex2f((GLfloat) cursor_x * scale, (GLfloat) fb_height); - glEnd(); - - glfwSwapBuffers(window); - glfwPollEvents(); - } - - glfwTerminate(); - exit(EXIT_SUCCESS); -} - diff --git a/tests/cursor.c b/tests/cursor.c index fc65c4da..a994388c 100644 --- a/tests/cursor.c +++ b/tests/cursor.c @@ -23,158 +23,78 @@ // //======================================================================== // -// System cursors and input modes tests. +// This test provides an interface to the cursor image and cursor mode +// parts of the API. +// +// Custom cursor image generation by urraka. // //======================================================================== #include +#include #include #include -static int W = 640; -static int H = 480; -static int delay = 0; +#define CURSOR_FRAME_COUNT 60 -static GLFWwindow* windows[2] = { NULL, NULL }; -static GLFWwindow* activeWindow = NULL; -static GLFWcursor* cursor = NULL; - -static struct -{ - int key; - double time; -} commands[] = { - {GLFW_KEY_H, 0}, - {GLFW_KEY_C, 0}, - {GLFW_KEY_D, 0}, - {GLFW_KEY_S, 0}, - {GLFW_KEY_N, 0}, - {GLFW_KEY_1, 0}, - {GLFW_KEY_2, 0}, - {GLFW_KEY_3, 0} -}; - -static int CommandCount = sizeof(commands) / sizeof(commands[0]); - -static struct -{ - int w, h; -} cursorSize[] = { - { 24, 24 }, { 13, 37 }, { 5, 53 }, { 43, 64 }, { 300, 300 } -}; - -static int SizeCount = sizeof(cursorSize) / sizeof(cursorSize[0]); -static int currentSize = 0; - -static void command_callback(int key) -{ - switch (key) - { - case GLFW_KEY_H: - { - printf("H: show this help\n"); - printf("C: call glfwCreateCursor()\n"); - printf("D: call glfwDestroyCursor()\n"); - printf("S: call glfwSetCursor()\n"); - printf("N: call glfwSetCursor() with NULL\n"); - printf("1: set GLFW_CURSOR_NORMAL\n"); - printf("2: set GLFW_CURSOR_HIDDEN\n"); - printf("3: set GLFW_CURSOR_DISABLED\n"); - printf("T: enable 3s delay for all previous commands\n"); - } - break; - - case GLFW_KEY_C: - { - int x, y; - GLFWimage image; - unsigned char* pixels; - - if (cursor) - break; - - image.width = cursorSize[currentSize].w; - image.height = cursorSize[currentSize].h; - - pixels = malloc(4 * image.width * image.height); - image.pixels = pixels; - - for (y = 0; y < image.height; y++) - { - for (x = 0; x < image.width; x++) - { - *pixels++ = 0xff; - *pixels++ = 0; - *pixels++ = 255 * y / image.height; - *pixels++ = 255 * x / image.width; - } - } - - cursor = glfwCreateCursor(&image, image.width / 2, image.height / 2); - currentSize = (currentSize + 1) % SizeCount; - free(image.pixels); - break; - } - - case GLFW_KEY_D: - { - if (cursor != NULL) - { - glfwDestroyCursor(cursor); - cursor = NULL; - } - - break; - } - - case GLFW_KEY_S: - { - if (cursor != NULL) - glfwSetCursor(activeWindow, cursor); - else - printf("The cursor is not created\n"); - - break; - } - - case GLFW_KEY_N: - glfwSetCursor(activeWindow, NULL); - break; - - case GLFW_KEY_1: - glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - break; - - case GLFW_KEY_2: - glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - break; - - case GLFW_KEY_3: - glfwSetInputMode(activeWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - break; - } -} +static double cursor_x; +static double cursor_y; +static int swap_interval = 1; +static GLboolean wait_events = GL_FALSE; +static GLboolean animate_cursor = GL_FALSE; +static GLboolean track_cursor = GL_FALSE; +static GLFWcursor* standard_cursors[6]; static void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); } -static void framebuffer_size_callback(GLFWwindow* window, int width, int height) +static float star(int x, int y, float t) { - W = width; - H = height; + const float c = 64 / 2.f; - glViewport(0, 0, W, H); + const float i = (0.25f * (float) sin(2.f * M_PI * t) + 0.75f); + const float k = 64 * 0.046875f * i; + + const float dist = (float) sqrt((x - c) * (x - c) + (y - c) * (y - c)); + + const float salpha = 1.f - dist / c; + const float xalpha = (float) x == c ? c : k / (float) fabs(x - c); + const float yalpha = (float) y == c ? c : k / (float) fabs(y - c); + + return fmax(0.f, fmin(1.f, i * salpha * 0.2f + salpha * xalpha * yalpha)); } -static void refresh_callback(GLFWwindow* window) +static GLFWcursor* create_cursor_frame(float t) { - glfwMakeContextCurrent(window); - glClearColor(0.0f, window == activeWindow ? 0.8f : 0.0f, 0.0f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(window); + int i = 0, x, y; + unsigned char buffer[64 * 64 * 4]; + const GLFWimage image = { 64, 64, buffer }; + + for (y = 0; y < image.width; y++) + { + for (x = 0; x < image.height; x++) + { + buffer[i++] = 255; + buffer[i++] = 255; + buffer[i++] = 255; + buffer[i++] = (unsigned char) (255 * star(x, y, t)); + } + } + + return glfwCreateCursor(&image, image.width / 2, image.height / 2); +} + +static void cursor_position_callback(GLFWwindow* window, double x, double y) +{ + printf("%0.3f: Cursor position: %f %f (%+f %+f)\n", + glfwGetTime(), + x, y, x - cursor_x, y - cursor_y); + + cursor_x = x; + cursor_y = y; } static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) @@ -184,104 +104,194 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, switch (key) { + case GLFW_KEY_A: + { + animate_cursor = !animate_cursor; + if (!animate_cursor) + glfwSetCursor(window, NULL); + + break; + } + case GLFW_KEY_ESCAPE: - glfwSetWindowShouldClose(window, GL_TRUE); + { + if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED) + { + glfwSetWindowShouldClose(window, GL_TRUE); + break; + } + + /* FALLTHROUGH */ + } + + case GLFW_KEY_N: + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + printf("(( cursor is normal ))\n"); + break; + + case GLFW_KEY_D: + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + printf("(( cursor is disabled ))\n"); + break; + + case GLFW_KEY_H: + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + printf("(( cursor is hidden ))\n"); + break; + + case GLFW_KEY_SPACE: + swap_interval = 1 - swap_interval; + printf("(( swap interval: %i ))\n", swap_interval); + glfwSwapInterval(swap_interval); + break; + + case GLFW_KEY_W: + wait_events = !wait_events; + printf("(( %sing for events ))\n", wait_events ? "poll" : "wait"); break; case GLFW_KEY_T: - delay = !delay; - printf("Delay %s.\n", delay ? "enabled" : "disabled"); + track_cursor = !track_cursor; break; - default: - { - if (delay) - { - int i = 0; + case GLFW_KEY_0: + glfwSetCursor(window, NULL); + break; - while (i < CommandCount && commands[i].key != key) - i++; + case GLFW_KEY_1: + glfwSetCursor(window, standard_cursors[0]); + break; - if (i < CommandCount) - commands[i].time = glfwGetTime(); - } - else - { - command_callback(key); - } - } - break; - } -} + case GLFW_KEY_2: + glfwSetCursor(window, standard_cursors[1]); + break; -static void focus_callback(GLFWwindow* window, int focused) -{ - if (focused) - { - activeWindow = window; - refresh_callback(windows[0]); - refresh_callback(windows[1]); + case GLFW_KEY_3: + glfwSetCursor(window, standard_cursors[2]); + break; + + case GLFW_KEY_4: + glfwSetCursor(window, standard_cursors[3]); + break; + + case GLFW_KEY_5: + glfwSetCursor(window, standard_cursors[4]); + break; + + case GLFW_KEY_6: + glfwSetCursor(window, standard_cursors[5]); + break; } } int main(void) { int i; - GLboolean running = GL_TRUE; + GLFWwindow* window; + GLFWcursor* star_cursors[CURSOR_FRAME_COUNT]; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); - for (i = 0; i < 2; i++) + for (i = 0; i < CURSOR_FRAME_COUNT; i++) { - windows[i] = glfwCreateWindow(W, H, "Cursor testing", NULL, NULL); - - if (!windows[i]) + star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT); + if (!star_cursors[i]) { glfwTerminate(); exit(EXIT_FAILURE); } - - glfwSetWindowPos(windows[i], 100 + (i & 1) * (W + 50), 100); - - glfwSetWindowRefreshCallback(windows[i], refresh_callback); - glfwSetFramebufferSizeCallback(windows[i], framebuffer_size_callback); - glfwSetKeyCallback(windows[i], key_callback); - glfwSetWindowFocusCallback(windows[i], focus_callback); - - glfwMakeContextCurrent(windows[i]); - glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(windows[i]); } - activeWindow = windows[0]; - - key_callback(NULL, GLFW_KEY_H, 0, GLFW_PRESS, 0); - - while (running) + for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++) { - if (delay) - { - int i; - double t = glfwGetTime(); + const int shapes[] = { + GLFW_ARROW_CURSOR, + GLFW_IBEAM_CURSOR, + GLFW_CROSSHAIR_CURSOR, + GLFW_HAND_CURSOR, + GLFW_HRESIZE_CURSOR, + GLFW_VRESIZE_CURSOR + }; - for (i = 0; i < CommandCount; i++) - { - if (commands[i].time != 0 && t - commands[i].time >= 3.0) - { - command_callback(commands[i].key); - commands[i].time = 0; - } - } + standard_cursors[i] = glfwCreateStandardCursor(shapes[i]); + if (!standard_cursors[i]) + { + glfwTerminate(); + exit(EXIT_FAILURE); + } + } + + window = glfwCreateWindow(640, 480, "Cursor Test", NULL, NULL); + if (!window) + { + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glfwMakeContextCurrent(window); + + glfwGetCursorPos(window, &cursor_x, &cursor_y); + printf("Cursor position: %f %f\n", cursor_x, cursor_y); + + glfwSetCursorPosCallback(window, cursor_position_callback); + glfwSetKeyCallback(window, key_callback); + + while (!glfwWindowShouldClose(window)) + { + glClear(GL_COLOR_BUFFER_BIT); + + if (track_cursor) + { + int wnd_width, wnd_height, fb_width, fb_height; + float scale; + + glfwGetWindowSize(window, &wnd_width, &wnd_height); + glfwGetFramebufferSize(window, &fb_width, &fb_height); + + scale = (float) fb_width / (float) wnd_width; + + glViewport(0, 0, fb_width, fb_height); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0.f, fb_width, 0.f, fb_height, 0.f, 1.f); + + glBegin(GL_LINES); + glVertex2f(0.f, (GLfloat) (fb_height - cursor_y * scale)); + glVertex2f((GLfloat) fb_width, (GLfloat) (fb_height - cursor_y * scale)); + glVertex2f((GLfloat) cursor_x * scale, 0.f); + glVertex2f((GLfloat) cursor_x * scale, (GLfloat) fb_height); + glEnd(); } - running = !(glfwWindowShouldClose(windows[0]) || glfwWindowShouldClose(windows[1])); + glfwSwapBuffers(window); - glfwPollEvents(); + if (animate_cursor) + { + const int i = (int) (glfwGetTime() * 30.0) % CURSOR_FRAME_COUNT; + glfwSetCursor(window, star_cursors[i]); + } + + if (wait_events) + glfwWaitEvents(); + else + glfwPollEvents(); + + // Workaround for an issue with msvcrt and mintty + fflush(stdout); } + glfwDestroyWindow(window); + + for (i = 0; i < CURSOR_FRAME_COUNT; i++) + glfwDestroyCursor(star_cursors[i]); + + for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++) + glfwDestroyCursor(standard_cursors[i]); + glfwTerminate(); exit(EXIT_SUCCESS); } diff --git a/tests/cursoranim.c b/tests/cursoranim.c deleted file mode 100644 index 39a229c8..00000000 --- a/tests/cursoranim.c +++ /dev/null @@ -1,135 +0,0 @@ -//======================================================================== -// Cursor animation -// Copyright (c) Camilla Berglund -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would -// be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, and must not -// be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source -// distribution. -// -//======================================================================== -// -// Cursor animation test. -// -//======================================================================== - -#include - -#include -#include - -#ifdef min - #undef min -#endif -#ifdef max - #undef max -#endif - -#define SIZE 64 // cursor size (width & height) -#define N 60 // number of frames - -unsigned char buffer[4 * SIZE * SIZE]; - -static float max(float a, float b) { return a > b ? a : b; } -static float min(float a, float b) { return a < b ? a : b; } - -static float star(int x, int y, float t) -{ - float c = SIZE / 2.0f; - - float i = (0.25f * (float)sin(2.0f * 3.1415926f * t) + 0.75f); - float k = SIZE * 0.046875f * i; - - float dist = (float)sqrt((x - c) * (x - c) + (y - c) * (y - c)); - - float salpha = 1.0f - dist / c; - float xalpha = (float)x == c ? c : k / (float)fabs(x - c); - float yalpha = (float)y == c ? c : k / (float)fabs(y - c); - - return max(0.0f, min(1.0f, i * salpha * 0.2f + salpha * xalpha * yalpha)); -} - -static GLFWcursor* load_frame(float t) -{ - int i = 0, x, y; - const GLFWimage image = { SIZE, SIZE, buffer }; - - for (y = 0; y < image.width; y++) - { - for (x = 0; x < image.height; x++) - { - buffer[i++] = 255; - buffer[i++] = 255; - buffer[i++] = 255; - buffer[i++] = (unsigned char)(255 * star(x, y, t)); - } - } - - return glfwCreateCursor(&image, image.width / 2, image.height / 2); -} - -int main(void) -{ - int i; - double t0, t1, frameTime = 0.0; - - GLFWwindow* window; - GLFWcursor* frames[N]; - - if (!glfwInit()) - exit(EXIT_FAILURE); - - window = glfwCreateWindow(640, 480, "Cursor animation", NULL, NULL); - - if (!window) - { - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glfwMakeContextCurrent(window); - glfwSwapInterval(1); - - for (i = 0; i < N; i++) - frames[i] = load_frame(i / (float)N); - - i = 0; - - t0 = glfwGetTime(); - - while (!glfwWindowShouldClose(window)) - { - glClear(GL_COLOR_BUFFER_BIT); - glfwSetCursor(window, frames[i]); - glfwSwapBuffers(window); - glfwPollEvents(); - - t1 = glfwGetTime(); - frameTime += t1 - t0; - t0 = t1; - - while (frameTime > 1.0 / (double)N) - { - i = (i + 1) % N; - frameTime -= 1.0 / (double)N; - } - } - - glfwTerminate(); - exit(EXIT_SUCCESS); -} - diff --git a/tests/peter.c b/tests/peter.c deleted file mode 100644 index 723167ad..00000000 --- a/tests/peter.c +++ /dev/null @@ -1,151 +0,0 @@ -//======================================================================== -// Cursor mode test -// Copyright (c) Camilla Berglund -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would -// be appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, and must not -// be misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source -// distribution. -// -//======================================================================== -// -// This test allows you to switch between the various cursor modes -// -//======================================================================== - -#include - -#include -#include - -static GLboolean reopen = GL_FALSE; -static double cursor_x; -static double cursor_y; - -static void error_callback(int error, const char* description) -{ - fprintf(stderr, "Error: %s\n", description); -} - -static void cursor_position_callback(GLFWwindow* window, double x, double y) -{ - printf("%0.3f: Cursor position: %f %f (%f %f)\n", - glfwGetTime(), - x, y, x - cursor_x, y - cursor_y); - - cursor_x = x; - cursor_y = y; -} - -static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) -{ - if (action != GLFW_PRESS) - return; - - switch (key) - { - case GLFW_KEY_D: - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); - printf("(( cursor is disabled ))\n"); - break; - - case GLFW_KEY_H: - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - printf("(( cursor is hidden ))\n"); - break; - - case GLFW_KEY_N: - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - printf("(( cursor is normal ))\n"); - break; - - case GLFW_KEY_R: - reopen = GL_TRUE; - break; - } -} - -static void framebuffer_size_callback(GLFWwindow* window, int width, int height) -{ - glViewport(0, 0, width, height); -} - -static GLFWwindow* open_window(void) -{ - GLFWwindow* window = glfwCreateWindow(640, 480, "Peter Detector", NULL, NULL); - if (!window) - return NULL; - - glfwMakeContextCurrent(window); - glfwSwapInterval(1); - - glfwGetCursorPos(window, &cursor_x, &cursor_y); - printf("Cursor position: %f %f\n", cursor_x, cursor_y); - - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - glfwSetCursorPosCallback(window, cursor_position_callback); - glfwSetKeyCallback(window, key_callback); - - return window; -} - -int main(void) -{ - GLFWwindow* window; - - glfwSetErrorCallback(error_callback); - - if (!glfwInit()) - exit(EXIT_FAILURE); - - window = open_window(); - if (!window) - { - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glClearColor(0.f, 0.f, 0.f, 0.f); - - while (!glfwWindowShouldClose(window)) - { - glClear(GL_COLOR_BUFFER_BIT); - - glfwSwapBuffers(window); - glfwWaitEvents(); - - if (reopen) - { - glfwDestroyWindow(window); - window = open_window(); - if (!window) - { - glfwTerminate(); - exit(EXIT_FAILURE); - } - - reopen = GL_FALSE; - } - - // Workaround for an issue with msvcrt and mintty - fflush(stdout); - } - - glfwTerminate(); - exit(EXIT_SUCCESS); -} -