From 718609275dd94e69cd5551274d607099edcbce66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Wed, 10 Apr 2019 19:01:50 +0200 Subject: [PATCH] Add custom cursor to cursor test tracking mode Related to #1461. --- tests/cursor.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/cursor.c b/tests/cursor.c index 85f6660c..18619327 100644 --- a/tests/cursor.c +++ b/tests/cursor.c @@ -69,6 +69,7 @@ static int wait_events = GLFW_TRUE; static int animate_cursor = GLFW_FALSE; static int track_cursor = GLFW_FALSE; static GLFWcursor* standard_cursors[6]; +static GLFWcursor* tracking_cursor = NULL; static void error_callback(int error, const char* description) { @@ -111,6 +112,36 @@ static GLFWcursor* create_cursor_frame(float t) return glfwCreateCursor(&image, image.width / 2, image.height / 2); } +static GLFWcursor* create_tracking_cursor(void) +{ + int i = 0, x, y; + unsigned char buffer[32 * 32 * 4]; + const GLFWimage image = { 32, 32, buffer }; + + for (y = 0; y < image.width; y++) + { + for (x = 0; x < image.height; x++) + { + if (x == 7 || y == 7) + { + buffer[i++] = 255; + buffer[i++] = 0; + buffer[i++] = 0; + buffer[i++] = 255; + } + else + { + buffer[i++] = 0; + buffer[i++] = 0; + buffer[i++] = 0; + buffer[i++] = 0; + } + } + } + + return glfwCreateCursor(&image, 7, 7); +} + static void cursor_position_callback(GLFWwindow* window, double x, double y) { printf("%0.3f: Cursor position: %f %f (%+f %+f)\n", @@ -192,6 +223,11 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, case GLFW_KEY_T: track_cursor = !track_cursor; + if (track_cursor) + glfwSetCursor(window, tracking_cursor); + else + glfwSetCursor(window, NULL); + break; case GLFW_KEY_0: @@ -238,6 +274,13 @@ int main(void) if (!glfwInit()) exit(EXIT_FAILURE); + tracking_cursor = create_tracking_cursor(); + if (!tracking_cursor) + { + glfwTerminate(); + exit(EXIT_FAILURE); + } + for (i = 0; i < CURSOR_FRAME_COUNT; i++) { star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);