Add custom cursor to cursor test tracking mode

Related to #1461.
This commit is contained in:
Camilla Löwy 2019-04-10 19:01:50 +02:00 committed by Camilla Löwy
parent 28f118f4de
commit 718609275d

View File

@ -69,6 +69,7 @@ static int wait_events = GLFW_TRUE;
static int animate_cursor = GLFW_FALSE; static int animate_cursor = GLFW_FALSE;
static int track_cursor = GLFW_FALSE; static int track_cursor = GLFW_FALSE;
static GLFWcursor* standard_cursors[6]; static GLFWcursor* standard_cursors[6];
static GLFWcursor* tracking_cursor = NULL;
static void error_callback(int error, const char* description) 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); 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) static void cursor_position_callback(GLFWwindow* window, double x, double y)
{ {
printf("%0.3f: Cursor position: %f %f (%+f %+f)\n", 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: case GLFW_KEY_T:
track_cursor = !track_cursor; track_cursor = !track_cursor;
if (track_cursor)
glfwSetCursor(window, tracking_cursor);
else
glfwSetCursor(window, NULL);
break; break;
case GLFW_KEY_0: case GLFW_KEY_0:
@ -238,6 +274,13 @@ int main(void)
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
tracking_cursor = create_tracking_cursor();
if (!tracking_cursor)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
for (i = 0; i < CURSOR_FRAME_COUNT; i++) for (i = 0; i < CURSOR_FRAME_COUNT; i++)
{ {
star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT); star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);