1 //========================================================================
2 // Cursor & input mode tests
3 // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
4 //
5 // This software is provided 'as-is', without any express or implied
6 // warranty. In no event will the authors be held liable for any damages
7 // arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it
11 // freely, subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented; you must not
14 //    claim that you wrote the original software. If you use this software
15 //    in a product, an acknowledgment in the product documentation would
16 //    be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such, and must not
19 //    be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source
22 //    distribution.
23 //
24 //========================================================================
25 //
26 // This test provides an interface to the cursor image and cursor mode
27 // parts of the API.
28 //
29 // Custom cursor image generation by urraka.
30 //
31 //========================================================================
32 
33 #include <glad/glad.h>
34 #include <GLFW/glfw3.h>
35 
36 #if defined(_MSC_VER)
37  // Make MS math.h define M_PI
38  #define _USE_MATH_DEFINES
39 #endif
40 
41 #include <math.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 
45 #include "linmath.h"
46 
47 #define CURSOR_FRAME_COUNT 60
48 
49 static const char* vertex_shader_text =
50 "#version 110\n"
51 "uniform mat4 MVP;\n"
52 "attribute vec2 vPos;\n"
53 "void main()\n"
54 "{\n"
55 "    gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
56 "}\n";
57 
58 static const char* fragment_shader_text =
59 "#version 110\n"
60 "void main()\n"
61 "{\n"
62 "    gl_FragColor = vec4(1.0);\n"
63 "}\n";
64 
65 static double cursor_x;
66 static double cursor_y;
67 static int swap_interval = 1;
68 static int wait_events = GLFW_TRUE;
69 static int animate_cursor = GLFW_FALSE;
70 static int track_cursor = GLFW_FALSE;
71 static GLFWcursor* standard_cursors[6];
72 
error_callback(int error,const char * description)73 static void error_callback(int error, const char* description)
74 {
75     fprintf(stderr, "Error: %s\n", description);
76 }
77 
star(int x,int y,float t)78 static float star(int x, int y, float t)
79 {
80     const float c = 64 / 2.f;
81 
82     const float i = (0.25f * (float) sin(2.f * M_PI * t) + 0.75f);
83     const float k = 64 * 0.046875f * i;
84 
85     const float dist = (float) sqrt((x - c) * (x - c) + (y - c) * (y - c));
86 
87     const float salpha = 1.f - dist / c;
88     const float xalpha = (float) x == c ? c : k / (float) fabs(x - c);
89     const float yalpha = (float) y == c ? c : k / (float) fabs(y - c);
90 
91     return (float) fmax(0.f, fmin(1.f, i * salpha * 0.2f + salpha * xalpha * yalpha));
92 }
93 
create_cursor_frame(float t)94 static GLFWcursor* create_cursor_frame(float t)
95 {
96     int i = 0, x, y;
97     unsigned char buffer[64 * 64 * 4];
98     const GLFWimage image = { 64, 64, buffer };
99 
100     for (y = 0;  y < image.width;  y++)
101     {
102         for (x = 0;  x < image.height;  x++)
103         {
104             buffer[i++] = 255;
105             buffer[i++] = 255;
106             buffer[i++] = 255;
107             buffer[i++] = (unsigned char) (255 * star(x, y, t));
108         }
109     }
110 
111     return glfwCreateCursor(&image, image.width / 2, image.height / 2);
112 }
113 
cursor_position_callback(GLFWwindow * window,double x,double y)114 static void cursor_position_callback(GLFWwindow* window, double x, double y)
115 {
116     printf("%0.3f: Cursor position: %f %f (%+f %+f)\n",
117            glfwGetTime(),
118            x, y, x - cursor_x, y - cursor_y);
119 
120     cursor_x = x;
121     cursor_y = y;
122 }
123 
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)124 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
125 {
126     if (action != GLFW_PRESS)
127         return;
128 
129     switch (key)
130     {
131         case GLFW_KEY_A:
132         {
133             animate_cursor = !animate_cursor;
134             if (!animate_cursor)
135                 glfwSetCursor(window, NULL);
136 
137             break;
138         }
139 
140         case GLFW_KEY_ESCAPE:
141         {
142             if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED)
143             {
144                 glfwSetWindowShouldClose(window, GLFW_TRUE);
145                 break;
146             }
147 
148             /* FALLTHROUGH */
149         }
150 
151         case GLFW_KEY_N:
152             glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
153             printf("(( cursor is normal ))\n");
154             break;
155 
156         case GLFW_KEY_D:
157             glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
158             printf("(( cursor is disabled ))\n");
159             break;
160 
161         case GLFW_KEY_H:
162             glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
163             printf("(( cursor is hidden ))\n");
164             break;
165 
166         case GLFW_KEY_SPACE:
167             swap_interval = 1 - swap_interval;
168             printf("(( swap interval: %i ))\n", swap_interval);
169             glfwSwapInterval(swap_interval);
170             break;
171 
172         case GLFW_KEY_W:
173             wait_events = !wait_events;
174             printf("(( %sing for events ))\n", wait_events ? "wait" : "poll");
175             break;
176 
177         case GLFW_KEY_T:
178             track_cursor = !track_cursor;
179             break;
180 
181         case GLFW_KEY_0:
182             glfwSetCursor(window, NULL);
183             break;
184 
185         case GLFW_KEY_1:
186             glfwSetCursor(window, standard_cursors[0]);
187             break;
188 
189         case GLFW_KEY_2:
190             glfwSetCursor(window, standard_cursors[1]);
191             break;
192 
193         case GLFW_KEY_3:
194             glfwSetCursor(window, standard_cursors[2]);
195             break;
196 
197         case GLFW_KEY_4:
198             glfwSetCursor(window, standard_cursors[3]);
199             break;
200 
201         case GLFW_KEY_5:
202             glfwSetCursor(window, standard_cursors[4]);
203             break;
204 
205         case GLFW_KEY_6:
206             glfwSetCursor(window, standard_cursors[5]);
207             break;
208     }
209 }
210 
main(void)211 int main(void)
212 {
213     int i;
214     GLFWwindow* window;
215     GLFWcursor* star_cursors[CURSOR_FRAME_COUNT];
216     GLFWcursor* current_frame = NULL;
217     GLuint vertex_buffer, vertex_shader, fragment_shader, program;
218     GLint mvp_location, vpos_location;
219 
220     glfwSetErrorCallback(error_callback);
221 
222     if (!glfwInit())
223         exit(EXIT_FAILURE);
224 
225     for (i = 0;  i < CURSOR_FRAME_COUNT;  i++)
226     {
227         star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);
228         if (!star_cursors[i])
229         {
230             glfwTerminate();
231             exit(EXIT_FAILURE);
232         }
233     }
234 
235     for (i = 0;  i < sizeof(standard_cursors) / sizeof(standard_cursors[0]);  i++)
236     {
237         const int shapes[] = {
238             GLFW_ARROW_CURSOR,
239             GLFW_IBEAM_CURSOR,
240             GLFW_CROSSHAIR_CURSOR,
241             GLFW_HAND_CURSOR,
242             GLFW_HRESIZE_CURSOR,
243             GLFW_VRESIZE_CURSOR
244         };
245 
246         standard_cursors[i] = glfwCreateStandardCursor(shapes[i]);
247         if (!standard_cursors[i])
248         {
249             glfwTerminate();
250             exit(EXIT_FAILURE);
251         }
252     }
253 
254     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
255     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
256 
257     window = glfwCreateWindow(640, 480, "Cursor Test", NULL, NULL);
258     if (!window)
259     {
260         glfwTerminate();
261         exit(EXIT_FAILURE);
262     }
263 
264     glfwMakeContextCurrent(window);
265     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
266 
267     glGenBuffers(1, &vertex_buffer);
268     glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
269 
270     vertex_shader = glCreateShader(GL_VERTEX_SHADER);
271     glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
272     glCompileShader(vertex_shader);
273 
274     fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
275     glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
276     glCompileShader(fragment_shader);
277 
278     program = glCreateProgram();
279     glAttachShader(program, vertex_shader);
280     glAttachShader(program, fragment_shader);
281     glLinkProgram(program);
282 
283     mvp_location = glGetUniformLocation(program, "MVP");
284     vpos_location = glGetAttribLocation(program, "vPos");
285 
286     glEnableVertexAttribArray(vpos_location);
287     glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
288                           sizeof(vec2), (void*) 0);
289     glUseProgram(program);
290 
291     glfwGetCursorPos(window, &cursor_x, &cursor_y);
292     printf("Cursor position: %f %f\n", cursor_x, cursor_y);
293 
294     glfwSetCursorPosCallback(window, cursor_position_callback);
295     glfwSetKeyCallback(window, key_callback);
296 
297     while (!glfwWindowShouldClose(window))
298     {
299         glClear(GL_COLOR_BUFFER_BIT);
300 
301         if (track_cursor)
302         {
303             int wnd_width, wnd_height, fb_width, fb_height;
304             float scale;
305             vec2 vertices[4];
306             mat4x4 mvp;
307 
308             glfwGetWindowSize(window, &wnd_width, &wnd_height);
309             glfwGetFramebufferSize(window, &fb_width, &fb_height);
310 
311             glViewport(0, 0, fb_width, fb_height);
312 
313             scale = (float) fb_width / (float) wnd_width;
314             vertices[0][0] = 0.f;
315             vertices[0][1] = (float) (fb_height - cursor_y * scale);
316             vertices[1][0] = (float) fb_width;
317             vertices[1][1] = (float) (fb_height - cursor_y * scale);
318             vertices[2][0] = (float) (cursor_x * scale);
319             vertices[2][1] = 0.f;
320             vertices[3][0] = (float) (cursor_x * scale);
321             vertices[3][1] = (float) fb_height;
322 
323             glBufferData(GL_ARRAY_BUFFER,
324                          sizeof(vertices),
325                          vertices,
326                          GL_STREAM_DRAW);
327 
328             mat4x4_ortho(mvp, 0.f, (float) fb_width, 0.f, (float) fb_height, 0.f, 1.f);
329             glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
330 
331             glDrawArrays(GL_LINES, 0, 4);
332         }
333 
334         glfwSwapBuffers(window);
335 
336         if (animate_cursor)
337         {
338             const int i = (int) (glfwGetTime() * 30.0) % CURSOR_FRAME_COUNT;
339             if (current_frame != star_cursors[i])
340             {
341                 glfwSetCursor(window, star_cursors[i]);
342                 current_frame = star_cursors[i];
343             }
344         }
345         else
346             current_frame = NULL;
347 
348         if (wait_events)
349         {
350             if (animate_cursor)
351                 glfwWaitEventsTimeout(1.0 / 30.0);
352             else
353                 glfwWaitEvents();
354         }
355         else
356             glfwPollEvents();
357 
358         // Workaround for an issue with msvcrt and mintty
359         fflush(stdout);
360     }
361 
362     glfwDestroyWindow(window);
363 
364     for (i = 0;  i < CURSOR_FRAME_COUNT;  i++)
365         glfwDestroyCursor(star_cursors[i]);
366 
367     for (i = 0;  i < sizeof(standard_cursors) / sizeof(standard_cursors[0]);  i++)
368         glfwDestroyCursor(standard_cursors[i]);
369 
370     glfwTerminate();
371     exit(EXIT_SUCCESS);
372 }
373 
374