13
#include <SDL3/SDL_test_common.h>
14
#include <SDL3/SDL_main.h>
16
#ifdef SDL_PLATFORM_EMSCRIPTEN
17
#include <emscripten/emscripten.h>
26
static const char *arrow[] = {
69
static const char *cross[] = {
92
" oooooooooooooooooooooooo ",
93
" oooooooooooooooooooooooo ",
112
static SDL_Surface *load_image_file(const char *file)
114
SDL_Surface *surface = SDL_LoadBMP(file);
116
if (SDL_GetSurfacePalette(surface)) {
117
const Uint8 bpp = SDL_BITSPERPIXEL(surface->format);
118
const Uint8 mask = (1 << bpp) - 1;
119
if (SDL_PIXELORDER(surface->format) == SDL_BITMAPORDER_4321)
120
SDL_SetSurfaceColorKey(surface, 1, (*(Uint8 *)surface->pixels) & mask);
122
SDL_SetSurfaceColorKey(surface, 1, ((*(Uint8 *)surface->pixels) >> (8 - bpp)) & mask);
124
switch (SDL_BITSPERPIXEL(surface->format)) {
126
SDL_SetSurfaceColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
129
SDL_SetSurfaceColorKey(surface, 1, *(Uint16 *)surface->pixels);
132
SDL_SetSurfaceColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
135
SDL_SetSurfaceColorKey(surface, 1, *(Uint32 *)surface->pixels);
143
static SDL_Surface *load_image(const char *file)
145
SDL_Surface *surface = load_image_file(file);
148
SDL_Surface *surface2x = NULL;
149
const char *ext = SDL_strrchr(file, '.');
150
size_t len = SDL_strlen(file) + 2 + 1;
151
char *file2x = (char *)SDL_malloc(len);
153
SDL_strlcpy(file2x, file, len);
155
SDL_memcpy(file2x + (ext - file), "2x", 3);
156
SDL_strlcat(file2x, ext, len);
158
SDL_strlcat(file2x, "2x", len);
160
surface2x = load_image_file(file2x);
164
SDL_AddSurfaceAlternateImage(surface, surface2x);
165
SDL_DestroySurface(surface2x);
171
static SDL_Cursor *init_color_cursor(const char *file)
173
SDL_Cursor *cursor = NULL;
174
SDL_Surface *surface = load_image(file);
176
cursor = SDL_CreateColorCursor(surface, 0, 0);
177
SDL_DestroySurface(surface);
182
static SDL_Cursor *init_system_cursor(const char *image[])
191
for (row = 0; row < 32; ++row) {
192
for (col = 0; col < 32; ++col) {
198
data[i] = mask[i] = 0;
200
switch (image[4 + row][col]) {
216
(void)SDL_sscanf(image[4 + row], "%d,%d", &hot_x, &hot_y);
217
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
220
static SDLTest_CommonState *state;
222
static SDL_Cursor *cursors[3 + SDL_NUM_SYSTEM_CURSORS];
223
static SDL_SystemCursor cursor_types[3 + SDL_NUM_SYSTEM_CURSORS];
224
static int num_cursors;
225
static int current_cursor;
226
static SDL_bool show_cursor;
232
SDLTest_CommonQuit(state);
239
static void loop(void)
244
while (SDL_PollEvent(&event)) {
245
SDLTest_CommonEvent(state, &event, &done);
246
if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
247
if (event.button.button == SDL_BUTTON_LEFT) {
248
if (num_cursors == 0) {
253
if (current_cursor == num_cursors) {
257
SDL_SetCursor(cursors[current_cursor]);
259
switch ((int)cursor_types[current_cursor]) {
260
case (SDL_SystemCursor)-1:
261
SDL_Log("Custom cursor");
263
case SDL_SYSTEM_CURSOR_DEFAULT:
266
case SDL_SYSTEM_CURSOR_TEXT:
269
case SDL_SYSTEM_CURSOR_WAIT:
272
case SDL_SYSTEM_CURSOR_CROSSHAIR:
273
SDL_Log("Crosshair");
275
case SDL_SYSTEM_CURSOR_PROGRESS:
276
SDL_Log("Progress: Small wait cursor (or Wait if not available)");
278
case SDL_SYSTEM_CURSOR_NWSE_RESIZE:
279
SDL_Log("Double arrow pointing northwest and southeast");
281
case SDL_SYSTEM_CURSOR_NESW_RESIZE:
282
SDL_Log("Double arrow pointing northeast and southwest");
284
case SDL_SYSTEM_CURSOR_EW_RESIZE:
285
SDL_Log("Double arrow pointing west and east");
287
case SDL_SYSTEM_CURSOR_NS_RESIZE:
288
SDL_Log("Double arrow pointing north and south");
290
case SDL_SYSTEM_CURSOR_MOVE:
291
SDL_Log("Move: Four pointed arrow pointing north, south, east, and west");
293
case SDL_SYSTEM_CURSOR_NOT_ALLOWED:
294
SDL_Log("Not Allowed: Slashed circle or crossbones");
296
case SDL_SYSTEM_CURSOR_POINTER:
297
SDL_Log("Pointer: Hand");
299
case SDL_SYSTEM_CURSOR_NW_RESIZE:
300
SDL_Log("Window resize top-left");
302
case SDL_SYSTEM_CURSOR_N_RESIZE:
303
SDL_Log("Window resize top");
305
case SDL_SYSTEM_CURSOR_NE_RESIZE:
306
SDL_Log("Window resize top-right");
308
case SDL_SYSTEM_CURSOR_E_RESIZE:
309
SDL_Log("Window resize right");
311
case SDL_SYSTEM_CURSOR_SE_RESIZE:
312
SDL_Log("Window resize bottom-right");
314
case SDL_SYSTEM_CURSOR_S_RESIZE:
315
SDL_Log("Window resize bottom");
317
case SDL_SYSTEM_CURSOR_SW_RESIZE:
318
SDL_Log("Window resize bottom-left");
320
case SDL_SYSTEM_CURSOR_W_RESIZE:
321
SDL_Log("Window resize left");
324
SDL_Log("UNKNOWN CURSOR TYPE, FIX THIS PROGRAM.");
329
show_cursor = !show_cursor;
339
for (i = 0; i < state->num_windows; ++i) {
340
SDL_Renderer *renderer = state->renderers[i];
343
int window_w = 0, window_h = 0;
345
SDL_GetWindowSize(state->windows[i], &window_w, &window_h);
348
for (y = 0, row = 0; y < window_h; y += (int)rect.h, ++row) {
349
SDL_bool black = ((row % 2) == 0) ? SDL_TRUE : SDL_FALSE;
350
for (x = 0; x < window_w; x += (int)rect.w) {
355
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
357
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
359
SDL_RenderFillRect(renderer, &rect);
364
SDL_RenderPresent(renderer);
366
#ifdef SDL_PLATFORM_EMSCRIPTEN
368
emscripten_cancel_main_loop();
373
int main(int argc, char *argv[])
376
const char *color_cursor = NULL;
380
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
386
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
388
for (i = 1; i < argc;) {
391
consumed = SDLTest_CommonArg(state, i);
393
color_cursor = argv[i];
397
SDLTest_CommonLogUsage(state, argv[0], NULL);
403
if (!SDLTest_CommonInit(state)) {
410
SDL_Surface *icon = load_image(color_cursor);
412
for (i = 0; i < state->num_windows; ++i) {
413
SDL_SetWindowIcon(state->windows[i], icon);
415
SDL_DestroySurface(icon);
418
cursor = init_color_cursor(color_cursor);
420
cursors[num_cursors] = cursor;
421
cursor_types[num_cursors] = (SDL_SystemCursor)-1;
426
cursor = init_system_cursor(arrow);
428
cursors[num_cursors] = cursor;
429
cursor_types[num_cursors] = (SDL_SystemCursor)-1;
433
cursor = init_system_cursor(cross);
435
cursors[num_cursors] = cursor;
436
cursor_types[num_cursors] = (SDL_SystemCursor)-1;
440
for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
441
cursor = SDL_CreateSystemCursor((SDL_SystemCursor)i);
443
cursors[num_cursors] = cursor;
444
cursor_types[num_cursors] = i;
449
if (num_cursors > 0) {
450
SDL_SetCursor(cursors[0]);
453
show_cursor = SDL_CursorVisible();
457
#ifdef SDL_PLATFORM_EMSCRIPTEN
458
emscripten_set_main_loop(loop, 0, 1);
465
for (i = 0; i < num_cursors; ++i) {
466
SDL_DestroyCursor(cursors[i]);