1 //========================================================================
2 // Cursor & input mode tests
3 // Copyright (c) Camilla Berglund <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 #define CURSOR_FRAME_COUNT 60
46
47 static double cursor_x;
48 static double cursor_y;
49 static int swap_interval = 1;
50 static int wait_events = GLFW_TRUE;
51 static int animate_cursor = GLFW_FALSE;
52 static int track_cursor = GLFW_FALSE;
53 static GLFWcursor* standard_cursors[6];
54
error_callback(int error,const char * description)55 static void error_callback(int error, const char* description)
56 {
57 fprintf(stderr, "Error: %s\n", description);
58 }
59
star(int x,int y,float t)60 static float star(int x, int y, float t)
61 {
62 const float c = 64 / 2.f;
63
64 const float i = (0.25f * (float) sin(2.f * M_PI * t) + 0.75f);
65 const float k = 64 * 0.046875f * i;
66
67 const float dist = (float) sqrt((x - c) * (x - c) + (y - c) * (y - c));
68
69 const float salpha = 1.f - dist / c;
70 const float xalpha = (float) x == c ? c : k / (float) fabs(x - c);
71 const float yalpha = (float) y == c ? c : k / (float) fabs(y - c);
72
73 return (float) fmax(0.f, fmin(1.f, i * salpha * 0.2f + salpha * xalpha * yalpha));
74 }
75
create_cursor_frame(float t)76 static GLFWcursor* create_cursor_frame(float t)
77 {
78 int i = 0, x, y;
79 unsigned char buffer[64 * 64 * 4];
80 const GLFWimage image = { 64, 64, buffer };
81
82 for (y = 0; y < image.width; y++)
83 {
84 for (x = 0; x < image.height; x++)
85 {
86 buffer[i++] = 255;
87 buffer[i++] = 255;
88 buffer[i++] = 255;
89 buffer[i++] = (unsigned char) (255 * star(x, y, t));
90 }
91 }
92
93 return glfwCreateCursor(&image, image.width / 2, image.height / 2);
94 }
95
cursor_position_callback(GLFWwindow * window,double x,double y)96 static void cursor_position_callback(GLFWwindow* window, double x, double y)
97 {
98 printf("%0.3f: Cursor position: %f %f (%+f %+f)\n",
99 glfwGetTime(),
100 x, y, x - cursor_x, y - cursor_y);
101
102 cursor_x = x;
103 cursor_y = y;
104 }
105
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)106 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
107 {
108 if (action != GLFW_PRESS)
109 return;
110
111 switch (key)
112 {
113 case GLFW_KEY_A:
114 {
115 animate_cursor = !animate_cursor;
116 if (!animate_cursor)
117 glfwSetCursor(window, NULL);
118
119 break;
120 }
121
122 case GLFW_KEY_ESCAPE:
123 {
124 if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED)
125 {
126 glfwSetWindowShouldClose(window, GLFW_TRUE);
127 break;
128 }
129
130 /* FALLTHROUGH */
131 }
132
133 case GLFW_KEY_N:
134 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
135 printf("(( cursor is normal ))\n");
136 break;
137
138 case GLFW_KEY_D:
139 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
140 printf("(( cursor is disabled ))\n");
141 break;
142
143 case GLFW_KEY_H:
144 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
145 printf("(( cursor is hidden ))\n");
146 break;
147
148 case GLFW_KEY_SPACE:
149 swap_interval = 1 - swap_interval;
150 printf("(( swap interval: %i ))\n", swap_interval);
151 glfwSwapInterval(swap_interval);
152 break;
153
154 case GLFW_KEY_W:
155 wait_events = !wait_events;
156 printf("(( %sing for events ))\n", wait_events ? "wait" : "poll");
157 break;
158
159 case GLFW_KEY_T:
160 track_cursor = !track_cursor;
161 break;
162
163 case GLFW_KEY_0:
164 glfwSetCursor(window, NULL);
165 break;
166
167 case GLFW_KEY_1:
168 glfwSetCursor(window, standard_cursors[0]);
169 break;
170
171 case GLFW_KEY_2:
172 glfwSetCursor(window, standard_cursors[1]);
173 break;
174
175 case GLFW_KEY_3:
176 glfwSetCursor(window, standard_cursors[2]);
177 break;
178
179 case GLFW_KEY_4:
180 glfwSetCursor(window, standard_cursors[3]);
181 break;
182
183 case GLFW_KEY_5:
184 glfwSetCursor(window, standard_cursors[4]);
185 break;
186
187 case GLFW_KEY_6:
188 glfwSetCursor(window, standard_cursors[5]);
189 break;
190 }
191 }
192
main(void)193 int main(void)
194 {
195 int i;
196 GLFWwindow* window;
197 GLFWcursor* star_cursors[CURSOR_FRAME_COUNT];
198 GLFWcursor* current_frame = NULL;
199
200 glfwSetErrorCallback(error_callback);
201
202 if (!glfwInit())
203 exit(EXIT_FAILURE);
204
205 for (i = 0; i < CURSOR_FRAME_COUNT; i++)
206 {
207 star_cursors[i] = create_cursor_frame(i / (float) CURSOR_FRAME_COUNT);
208 if (!star_cursors[i])
209 {
210 glfwTerminate();
211 exit(EXIT_FAILURE);
212 }
213 }
214
215 for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
216 {
217 const int shapes[] = {
218 GLFW_ARROW_CURSOR,
219 GLFW_IBEAM_CURSOR,
220 GLFW_CROSSHAIR_CURSOR,
221 GLFW_HAND_CURSOR,
222 GLFW_HRESIZE_CURSOR,
223 GLFW_VRESIZE_CURSOR
224 };
225
226 standard_cursors[i] = glfwCreateStandardCursor(shapes[i]);
227 if (!standard_cursors[i])
228 {
229 glfwTerminate();
230 exit(EXIT_FAILURE);
231 }
232 }
233
234 window = glfwCreateWindow(640, 480, "Cursor Test", NULL, NULL);
235 if (!window)
236 {
237 glfwTerminate();
238 exit(EXIT_FAILURE);
239 }
240
241 glfwMakeContextCurrent(window);
242 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
243
244 glfwGetCursorPos(window, &cursor_x, &cursor_y);
245 printf("Cursor position: %f %f\n", cursor_x, cursor_y);
246
247 glfwSetCursorPosCallback(window, cursor_position_callback);
248 glfwSetKeyCallback(window, key_callback);
249
250 while (!glfwWindowShouldClose(window))
251 {
252 glClear(GL_COLOR_BUFFER_BIT);
253
254 if (track_cursor)
255 {
256 int wnd_width, wnd_height, fb_width, fb_height;
257 float scale;
258
259 glfwGetWindowSize(window, &wnd_width, &wnd_height);
260 glfwGetFramebufferSize(window, &fb_width, &fb_height);
261
262 scale = (float) fb_width / (float) wnd_width;
263
264 glViewport(0, 0, fb_width, fb_height);
265
266 glMatrixMode(GL_PROJECTION);
267 glLoadIdentity();
268 glOrtho(0.f, fb_width, 0.f, fb_height, 0.f, 1.f);
269
270 glBegin(GL_LINES);
271 glVertex2f(0.f, (GLfloat) (fb_height - cursor_y * scale));
272 glVertex2f((GLfloat) fb_width, (GLfloat) (fb_height - cursor_y * scale));
273 glVertex2f((GLfloat) cursor_x * scale, 0.f);
274 glVertex2f((GLfloat) cursor_x * scale, (GLfloat) fb_height);
275 glEnd();
276 }
277
278 glfwSwapBuffers(window);
279
280 if (animate_cursor)
281 {
282 const int i = (int) (glfwGetTime() * 30.0) % CURSOR_FRAME_COUNT;
283 if (current_frame != star_cursors[i])
284 {
285 glfwSetCursor(window, star_cursors[i]);
286 current_frame = star_cursors[i];
287 }
288 }
289 else
290 current_frame = NULL;
291
292 if (wait_events)
293 {
294 if (animate_cursor)
295 glfwWaitEventsTimeout(1.0 / 30.0);
296 else
297 glfwWaitEvents();
298 }
299 else
300 glfwPollEvents();
301
302 // Workaround for an issue with msvcrt and mintty
303 fflush(stdout);
304 }
305
306 glfwDestroyWindow(window);
307
308 for (i = 0; i < CURSOR_FRAME_COUNT; i++)
309 glfwDestroyCursor(star_cursors[i]);
310
311 for (i = 0; i < sizeof(standard_cursors) / sizeof(standard_cursors[0]); i++)
312 glfwDestroyCursor(standard_cursors[i]);
313
314 glfwTerminate();
315 exit(EXIT_SUCCESS);
316 }
317
318