1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
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.
*/
#include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h>
/* Stolen from the mailing list */
/* Creates a new mouse cursor from an XPM */
/* XPM */
static const char *arrow[] = {
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"X c #000000",
". c #ffffff",
" c None",
/* pixels */
"X ",
"XX ",
"X.X ",
"X..X ",
"X...X ",
"X....X ",
"X.....X ",
"X......X ",
"X.......X ",
"X........X ",
"X.....XXXXX ",
"X..X..X ",
"X.X X..X ",
"XX X..X ",
"X X..X ",
" X..X ",
" X..X ",
" X..X ",
" XX ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
"0,0"
};
static const char *cross[] = {
/* width height num_colors chars_per_pixel */
" 32 32 3 1",
/* colors */
"o c #ffffff",
". c #000000",
" c None",
/* pixels */
" ",
" ",
" ",
" ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oooooooooooooooooooooooo ",
" oooooooooooooooooooooooo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" oo ",
" ",
" ",
" ",
" ",
"0,0"
};
static SDLTest_CommonState *state;
static int done;
#define PROP_ARROW_CURSOR_TEXTURE "arrow_cursor_texture"
#define PROP_CROSS_CURSOR_TEXTURE "cross_cursor_texture"
#define MAX_MICE 3
#define MAX_KEYBOARDS 3
#define CURSOR_SIZE 48.0f
#define MAX_TRAIL 500
#define TRAIL_SIZE 8.0f
static SDL_Color colors[] = {
{ 0, 255, 255, 255 }, /* mouse 1, cyan */
{ 255, 0, 255, 255 }, /* mouse 2, magenta */
{ 255, 255, 0, 255 }, /* mouse 3, yellow */
};
SDL_COMPILE_TIME_ASSERT(mouse_colors, SDL_arraysize(colors) == MAX_MICE);
SDL_COMPILE_TIME_ASSERT(keyboard_colors, SDL_arraysize(colors) == MAX_KEYBOARDS);
typedef struct
{
SDL_MouseID instance_id;
bool active;
Uint8 button_state;
SDL_FPoint position;
int trail_head;
int trail_length;
SDL_FPoint trail[MAX_TRAIL];
} MouseState;
static MouseState mice[MAX_MICE];
typedef struct
{
SDL_KeyboardID instance_id;
bool active;
Uint8 button_state;
SDL_FPoint position;
} KeyboardState;
static KeyboardState keyboards[MAX_KEYBOARDS];
static SDL_Texture *CreateTexture(const char *image[], SDL_Renderer *renderer)
{
SDL_Surface *surface;
SDL_Palette *palette;
SDL_Texture *texture;
int row;
surface = SDL_CreateSurface(32, 32, SDL_PIXELFORMAT_INDEX8);
for (row = 0; row < surface->h; ++row) {
SDL_memcpy((Uint8 *)surface->pixels + row * surface->pitch, image[4 + row], surface->w);
}
palette = SDL_CreateSurfacePalette(surface);
if (!palette) {
SDL_DestroySurface(surface);
return NULL;
}
palette->colors['.'].r = 0xFF;
palette->colors['.'].g = 0xFF;
palette->colors['.'].b = 0xFF;
palette->colors['o'].r = 0xFF;
palette->colors['o'].g = 0xFF;
palette->colors['o'].b = 0xFF;
palette->colors['X'].r = 0x00;
palette->colors['X'].g = 0x00;
palette->colors['X'].b = 0x00;
SDL_SetSurfaceColorKey(surface, true, ' ');
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
return texture;
}
static void HandleMouseAdded(SDL_MouseID instance_id)
{
SDL_Window *window = state->windows[0];
int i, w = 0, h = 0;
SDL_GetWindowSizeInPixels(window, &w, &h);
for (i = 0; i < SDL_arraysize(mice); ++i) {
MouseState *mouse_state = &mice[i];
if (!mouse_state->active) {
mouse_state->instance_id = instance_id;
mouse_state->active = true;
mouse_state->position.x = w * 0.5f;
mouse_state->position.y = h * 0.5f;
return;
}
}
}
static void HandleMouseRemoved(SDL_MouseID instance_id)
{
int i;
for (i = 0; i < SDL_arraysize(mice); ++i) {
MouseState *mouse_state = &mice[i];
if (instance_id == mouse_state->instance_id) {
SDL_zerop(mouse_state);
return;
}
}
}
static void ActivateMouse(SDL_MouseID instance_id)
{
int i;
for (i = 0; i < SDL_arraysize(mice); ++i) {
MouseState *mouse_state = &mice[i];
if (mouse_state->active && instance_id == mouse_state->instance_id) {
return;
}
}
HandleMouseAdded(instance_id);
}
static void HandleMouseMotion(SDL_MouseMotionEvent *event)
{
SDL_Window *window = state->windows[0];
int i, w = 0, h = 0;
if (event->which == 0) {
/* The system pointer, not a distinct mouse device */
return;
}
ActivateMouse(event->which);
SDL_GetWindowSizeInPixels(window, &w, &h);
for (i = 0; i < SDL_arraysize(mice); ++i) {
MouseState *mouse_state = &mice[i];
if (!mouse_state->active) {
continue;
}
if (event->which == mouse_state->instance_id) {
float x = (mouse_state->position.x + event->xrel);
float y = (mouse_state->position.y + event->yrel);
x = SDL_clamp(x, 0.0f, (float)w);
y = SDL_clamp(y, 0.0f, (float)h);
mouse_state->position.x = x;
mouse_state->position.y = y;
if (mouse_state->button_state) {
/* Add a spot to the mouse trail */
SDL_FPoint *spot = &mouse_state->trail[mouse_state->trail_head];
spot->x = x - TRAIL_SIZE * 0.5f;
spot->y = y - TRAIL_SIZE * 0.5f;
if (mouse_state->trail_length < MAX_TRAIL) {
++mouse_state->trail_length;
}
mouse_state->trail_head = (mouse_state->trail_head + 1) % MAX_TRAIL;
}
}
}
}
static void HandleMouseButton(SDL_MouseButtonEvent *event)
{
int i;
if (event->which == 0) {
/* The system pointer, not a distinct mouse device */
return;
}
ActivateMouse(event->which);
for (i = 0; i < SDL_arraysize(mice); ++i) {
MouseState *mouse_state = &mice[i];
if (!mouse_state->active) {
continue;
}
if (event->which == mouse_state->instance_id) {
if (event->down) {
mouse_state->button_state |= SDL_BUTTON_MASK(event->button);
} else {
mouse_state->button_state &= ~SDL_BUTTON_MASK(event->button);
}
}
}
}
static void DrawMouseState(SDL_Window *window, SDL_Renderer *renderer, MouseState *mouse_state, SDL_Texture *cursor, SDL_Color *color)
{
SDL_FRect rect;
if (!mouse_state->active) {
return;
}
if (mouse_state->trail_length > 0) {
int i;
int spot = mouse_state->trail_head - mouse_state->trail_length;
if (spot < 0) {
spot += MAX_TRAIL;
}
rect.w = TRAIL_SIZE;
rect.h = TRAIL_SIZE;
SDL_SetRenderDrawColor(renderer, color->r, color->g, color->b, color->a);
for (i = 0; i < mouse_state->trail_length; ++i) {
rect.x = mouse_state->trail[spot].x;
rect.y = mouse_state->trail[spot].y;
SDL_RenderFillRect(renderer, &rect);
spot = (spot + 1) % MAX_TRAIL;
}
}
rect.x = mouse_state->position.x;
rect.y = mouse_state->position.y;
rect.w = CURSOR_SIZE;
rect.h = CURSOR_SIZE;
SDL_SetTextureColorMod(cursor, color->r, color->g, color->b);
SDL_RenderTexture(renderer, cursor, NULL, &rect);
}
static void HandleKeyboardAdded(SDL_KeyboardID instance_id)
{
SDL_Window *window = state->windows[0];
int i, w = 0, h = 0;
SDL_GetWindowSize(window, &w, &h);
for (i = 0; i < SDL_arraysize(keyboards); ++i) {
KeyboardState *keyboard_state = &keyboards[i];
if (!keyboard_state->active) {
keyboard_state->instance_id = instance_id;
keyboard_state->active = true;
keyboard_state->position.x = w * 0.5f;
keyboard_state->position.y = h * 0.5f;
return;
}
}
}
static void HandleKeyboardRemoved(SDL_KeyboardID instance_id)
{
int i;
for (i = 0; i < SDL_arraysize(keyboards); ++i) {
KeyboardState *keyboard_state = &keyboards[i];
if (instance_id == keyboard_state->instance_id) {
SDL_zerop(keyboard_state);
return;
}
}
}
static void ActivateKeyboard(SDL_KeyboardID instance_id)
{
int i;
for (i = 0; i < SDL_arraysize(keyboards); ++i) {
KeyboardState *keyboard_state = &keyboards[i];
if (keyboard_state->active && instance_id == keyboard_state->instance_id) {
return;
}
}
HandleKeyboardAdded(instance_id);
}
static void HandleKeyboardKeyDown(SDL_KeyboardEvent *event)
{
SDL_Window *window = state->windows[0];
int i, w = 0, h = 0;
SDL_GetWindowSize(window, &w, &h);
ActivateKeyboard(event->which);
for (i = 0; i < SDL_arraysize(keyboards); ++i) {
KeyboardState *keyboard_state = &keyboards[i];
if (!keyboard_state->active) {
continue;
}
if (event->which == keyboard_state->instance_id) {
switch (event->key) {
case SDLK_LEFT:
keyboard_state->position.x -= CURSOR_SIZE;
if (keyboard_state->position.x < 0.0f) {
keyboard_state->position.x = 0.0f;
}
break;
case SDLK_RIGHT:
keyboard_state->position.x += CURSOR_SIZE;
if (keyboard_state->position.x > w) {
keyboard_state->position.x = (float)w;
}
break;
case SDLK_UP:
keyboard_state->position.y -= CURSOR_SIZE;
if (keyboard_state->position.y < 0.0f) {
keyboard_state->position.y = 0.0f;
}
break;
case SDLK_DOWN:
keyboard_state->position.y += CURSOR_SIZE;
if (keyboard_state->position.y > h) {
keyboard_state->position.y = (float)h;
}
break;
default:
break;
}
}
}
}
static void DrawKeyboardState(SDL_Window *window, SDL_Renderer *renderer, KeyboardState *keyboard_state, SDL_Texture *cursor, SDL_Color *color)
{
SDL_FRect rect;
if (!keyboard_state->active) {
return;
}
rect.x = keyboard_state->position.x - CURSOR_SIZE / 2;
rect.y = keyboard_state->position.y - CURSOR_SIZE / 2;
rect.w = CURSOR_SIZE;
rect.h = CURSOR_SIZE;
SDL_SetTextureColorMod(cursor, color->r, color->g, color->b);
SDL_RenderTexture(renderer, cursor, NULL, &rect);
}
static void loop(void)
{
int i, j;
SDL_Event event;
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
switch (event.type) {
case SDL_EVENT_KEYBOARD_ADDED:
/* Wait for events before activating this keyboard */
break;
case SDL_EVENT_KEYBOARD_REMOVED:
HandleKeyboardRemoved(event.kdevice.which);
break;
case SDL_EVENT_KEY_DOWN:
HandleKeyboardKeyDown(&event.key);
break;
case SDL_EVENT_MOUSE_ADDED:
/* Wait for events before activating this mouse */
break;
case SDL_EVENT_MOUSE_REMOVED:
HandleMouseRemoved(event.mdevice.which);
break;
case SDL_EVENT_MOUSE_MOTION:
HandleMouseMotion(&event.motion);
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
HandleMouseButton(&event.button);
break;
default:
break;
}
}
for (i = 0; i < state->num_windows; ++i) {
SDL_Window *window = state->windows[i];
SDL_Renderer *renderer = state->renderers[i];
SDL_Texture *arrow_cursor = (SDL_Texture *)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), PROP_ARROW_CURSOR_TEXTURE, NULL);
SDL_Texture *cross_cursor = (SDL_Texture *)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), PROP_CROSS_CURSOR_TEXTURE, NULL);
SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
SDL_RenderClear(renderer);
for (j = 0; j < SDL_arraysize(mice); ++j) {
DrawMouseState(window, renderer, &mice[j], arrow_cursor, &colors[j]);
}
for (j = 0; j < SDL_arraysize(keyboards); ++j) {
DrawKeyboardState(window, renderer, &keyboards[j], cross_cursor, &colors[j]);
}
SDL_RenderPresent(renderer);
}
}
int main(int argc, char *argv[])
{
int i;
/* Log all events, including mouse motion */
SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2");
/* Support for multiple keyboards requires raw keyboard events on Windows */
SDL_SetHint(SDL_HINT_WINDOWS_RAW_KEYBOARD, "1");
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;
consumed = SDLTest_CommonArg(state, i);
if (consumed < 0) {
SDLTest_CommonLogUsage(state, argv[0], NULL);
SDLTest_CommonQuit(state);
return 1;
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
SDLTest_CommonQuit(state);
return 2;
}
/* Create the cursor textures */
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_Texture *cursor_arrow = CreateTexture(arrow, renderer);
SDL_Texture *cursor_cross = CreateTexture(cross, renderer);
SDL_SetPointerProperty(SDL_GetRendererProperties(renderer), PROP_ARROW_CURSOR_TEXTURE, cursor_arrow);
SDL_SetPointerProperty(SDL_GetRendererProperties(renderer), PROP_CROSS_CURSOR_TEXTURE, cursor_cross);
/* We only get mouse motion for distinct devices when relative mode is enabled */
SDL_SetWindowRelativeMouseMode(state->windows[i], true);
}
/* Main render loop */
done = 0;
while (!done) {
loop();
}
SDLTest_CommonQuit(state);
return 0;
}
|