| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
#ifdef __EMSCRIPTEN__ |
| 17 |
#include <emscripten/emscripten.h> |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "SDL_test_common.h" |
| 21 |
|
| 22 |
/* Stolen from the mailing list */ |
| 23 |
/* Creates a new mouse cursor from an XPM */ |
| 24 |
|
| 25 |
/* XPM */ |
| 26 |
static const char *arrow[] = { |
| 27 |
/* width height num_colors chars_per_pixel */ |
| 28 |
" 32 32 3 1", |
| 29 |
/* colors */ |
| 30 |
"X c #000000", |
| 31 |
". c #ffffff", |
| 32 |
" c None", |
| 33 |
/* pixels */ |
| 34 |
"X ", |
| 35 |
"XX ", |
| 36 |
"X.X ", |
| 37 |
"X..X ", |
| 38 |
"X...X ", |
| 39 |
"X....X ", |
| 40 |
"X.....X ", |
| 41 |
"X......X ", |
| 42 |
"X.......X ", |
| 43 |
"X........X ", |
| 44 |
"X.....XXXXX ", |
| 45 |
"X..X..X ", |
| 46 |
"X.X X..X ", |
| 47 |
"XX X..X ", |
| 48 |
"X X..X ", |
| 49 |
" X..X ", |
| 50 |
" X..X ", |
| 51 |
" X..X ", |
| 52 |
" XX ", |
| 53 |
" ", |
| 54 |
" ", |
| 55 |
" ", |
| 56 |
" ", |
| 57 |
" ", |
| 58 |
" ", |
| 59 |
" ", |
| 60 |
" ", |
| 61 |
" ", |
| 62 |
" ", |
| 63 |
" ", |
| 64 |
" ", |
| 65 |
" ", |
| 66 |
"0,0" |
| 67 |
}; |
| 68 |
|
| 69 |
static SDL_Cursor * |
| 70 |
init_color_cursor(const char *file) |
| 71 |
{ |
| 72 |
SDL_Cursor *cursor = NULL; |
| 73 |
SDL_Surface *surface = SDL_LoadBMP(file); |
| 74 |
if (surface) { |
| 75 |
if (surface->format->palette) { |
| 76 |
SDL_SetColorKey(surface, 1, *(Uint8 *)surface->pixels); |
| 77 |
} else { |
| 78 |
switch (surface->format->BitsPerPixel) { |
| 79 |
case 15: |
| 80 |
SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF); |
| 81 |
break; |
| 82 |
case 16: |
| 83 |
SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels); |
| 84 |
break; |
| 85 |
case 24: |
| 86 |
SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF); |
| 87 |
break; |
| 88 |
case 32: |
| 89 |
SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels); |
| 90 |
break; |
| 91 |
} |
| 92 |
} |
| 93 |
cursor = SDL_CreateColorCursor(surface, 0, 0); |
| 94 |
SDL_FreeSurface(surface); |
| 95 |
} |
| 96 |
return cursor; |
| 97 |
} |
| 98 |
|
| 99 |
static SDL_Cursor * |
| 100 |
init_system_cursor(const char *image[]) |
| 101 |
{ |
| 102 |
int i, row, col; |
| 103 |
Uint8 data[4 * 32]; |
| 104 |
Uint8 mask[4 * 32]; |
| 105 |
int hot_x = 0; |
| 106 |
int hot_y = 0; |
| 107 |
|
| 108 |
i = -1; |
| 109 |
for (row = 0; row < 32; ++row) { |
| 110 |
for (col = 0; col < 32; ++col) { |
| 111 |
if (col % 8) { |
| 112 |
data[i] <<= 1; |
| 113 |
mask[i] <<= 1; |
| 114 |
} else { |
| 115 |
++i; |
| 116 |
data[i] = mask[i] = 0; |
| 117 |
} |
| 118 |
switch (image[4 + row][col]) { |
| 119 |
case 'X': |
| 120 |
data[i] |= 0x01; |
| 121 |
mask[i] |= 0x01; |
| 122 |
break; |
| 123 |
case '.': |
| 124 |
mask[i] |= 0x01; |
| 125 |
break; |
| 126 |
case ' ': |
| 127 |
break; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
(void)SDL_sscanf(image[4 + row], "%d,%d", &hot_x, &hot_y); |
| 132 |
return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y); |
| 133 |
} |
| 134 |
|
| 135 |
static SDLTest_CommonState *state; |
| 136 |
int done; |
| 137 |
static SDL_Cursor *cursors[1 + SDL_NUM_SYSTEM_CURSORS]; |
| 138 |
static SDL_SystemCursor cursor_types[1 + SDL_NUM_SYSTEM_CURSORS]; |
| 139 |
static int num_cursors; |
| 140 |
static int current_cursor; |
| 141 |
static int show_cursor; |
| 142 |
|
| 143 |
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
| 144 |
static void |
| 145 |
quit(int rc) |
| 146 |
{ |
| 147 |
SDLTest_CommonQuit(state); |
| 148 |
exit(rc); |
| 149 |
} |
| 150 |
|
| 151 |
void loop(void) |
| 152 |
{ |
| 153 |
int i; |
| 154 |
SDL_Event event; |
| 155 |
/* Check for events */ |
| 156 |
while (SDL_PollEvent(&event)) { |
| 157 |
SDLTest_CommonEvent(state, &event, &done); |
| 158 |
if (event.type == SDL_MOUSEBUTTONDOWN) { |
| 159 |
if (event.button.button == SDL_BUTTON_LEFT) { |
| 160 |
if (num_cursors == 0) { |
| 161 |
continue; |
| 162 |
} |
| 163 |
|
| 164 |
++current_cursor; |
| 165 |
if (current_cursor == num_cursors) { |
| 166 |
current_cursor = 0; |
| 167 |
} |
| 168 |
|
| 169 |
SDL_SetCursor(cursors[current_cursor]); |
| 170 |
|
| 171 |
switch ((int)cursor_types[current_cursor]) { |
| 172 |
case (SDL_SystemCursor)-1: |
| 173 |
SDL_Log("Custom cursor"); |
| 174 |
break; |
| 175 |
case SDL_SYSTEM_CURSOR_ARROW: |
| 176 |
SDL_Log("Arrow"); |
| 177 |
break; |
| 178 |
case SDL_SYSTEM_CURSOR_IBEAM: |
| 179 |
SDL_Log("I-beam"); |
| 180 |
break; |
| 181 |
case SDL_SYSTEM_CURSOR_WAIT: |
| 182 |
SDL_Log("Wait"); |
| 183 |
break; |
| 184 |
case SDL_SYSTEM_CURSOR_CROSSHAIR: |
| 185 |
SDL_Log("Crosshair"); |
| 186 |
break; |
| 187 |
case SDL_SYSTEM_CURSOR_WAITARROW: |
| 188 |
SDL_Log("Small wait cursor (or Wait if not available)"); |
| 189 |
break; |
| 190 |
case SDL_SYSTEM_CURSOR_SIZENWSE: |
| 191 |
SDL_Log("Double arrow pointing northwest and southeast"); |
| 192 |
break; |
| 193 |
case SDL_SYSTEM_CURSOR_SIZENESW: |
| 194 |
SDL_Log("Double arrow pointing northeast and southwest"); |
| 195 |
break; |
| 196 |
case SDL_SYSTEM_CURSOR_SIZEWE: |
| 197 |
SDL_Log("Double arrow pointing west and east"); |
| 198 |
break; |
| 199 |
case SDL_SYSTEM_CURSOR_SIZENS: |
| 200 |
SDL_Log("Double arrow pointing north and south"); |
| 201 |
break; |
| 202 |
case SDL_SYSTEM_CURSOR_SIZEALL: |
| 203 |
SDL_Log("Four pointed arrow pointing north, south, east, and west"); |
| 204 |
break; |
| 205 |
case SDL_SYSTEM_CURSOR_NO: |
| 206 |
SDL_Log("Slashed circle or crossbones"); |
| 207 |
break; |
| 208 |
case SDL_SYSTEM_CURSOR_HAND: |
| 209 |
SDL_Log("Hand"); |
| 210 |
break; |
| 211 |
default: |
| 212 |
SDL_Log("UNKNOWN CURSOR TYPE, FIX THIS PROGRAM."); |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
} else { |
| 217 |
show_cursor = !show_cursor; |
| 218 |
SDL_ShowCursor(show_cursor); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
for (i = 0; i < state->num_windows; ++i) { |
| 224 |
SDL_Renderer *renderer = state->renderers[i]; |
| 225 |
SDL_RenderClear(renderer); |
| 226 |
SDL_RenderPresent(renderer); |
| 227 |
} |
| 228 |
#ifdef __EMSCRIPTEN__ |
| 229 |
if (done) { |
| 230 |
emscripten_cancel_main_loop(); |
| 231 |
} |
| 232 |
#endif |
| 233 |
} |
| 234 |
|
| 235 |
int main(int argc, char *argv[]) |
| 236 |
{ |
| 237 |
int i; |
| 238 |
const char *color_cursor = NULL; |
| 239 |
|
| 240 |
/* Enable standard application logging */ |
| 241 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 242 |
|
| 243 |
/* Initialize test framework */ |
| 244 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 245 |
if (!state) { |
| 246 |
return 1; |
| 247 |
} |
| 248 |
for (i = 1; i < argc;) { |
| 249 |
int consumed; |
| 250 |
|
| 251 |
consumed = SDLTest_CommonArg(state, i); |
| 252 |
if (consumed == 0) { |
| 253 |
color_cursor = argv[i]; |
| 254 |
break; |
| 255 |
} |
| 256 |
if (consumed < 0) { |
| 257 |
SDLTest_CommonLogUsage(state, argv[0], NULL); |
| 258 |
quit(1); |
| 259 |
} |
| 260 |
i += consumed; |
| 261 |
} |
| 262 |
|
| 263 |
if (!SDLTest_CommonInit(state)) { |
| 264 |
quit(2); |
| 265 |
} |
| 266 |
|
| 267 |
for (i = 0; i < state->num_windows; ++i) { |
| 268 |
SDL_Renderer *renderer = state->renderers[i]; |
| 269 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 270 |
SDL_RenderClear(renderer); |
| 271 |
} |
| 272 |
|
| 273 |
num_cursors = 0; |
| 274 |
|
| 275 |
if (color_cursor) { |
| 276 |
SDL_Cursor *cursor = init_color_cursor(color_cursor); |
| 277 |
if (cursor) { |
| 278 |
cursors[num_cursors] = cursor; |
| 279 |
cursor_types[num_cursors] = (SDL_SystemCursor)-1; |
| 280 |
num_cursors++; |
| 281 |
} |
| 282 |
} else { |
| 283 |
SDL_Cursor *cursor = init_system_cursor(arrow); |
| 284 |
if (cursor) { |
| 285 |
cursors[num_cursors] = cursor; |
| 286 |
cursor_types[num_cursors] = (SDL_SystemCursor)-1; |
| 287 |
num_cursors++; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) { |
| 292 |
SDL_Cursor *cursor = SDL_CreateSystemCursor((SDL_SystemCursor)i); |
| 293 |
if (cursor) { |
| 294 |
cursors[num_cursors] = cursor; |
| 295 |
cursor_types[num_cursors] = i; |
| 296 |
num_cursors++; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (num_cursors > 0) { |
| 301 |
SDL_SetCursor(cursors[0]); |
| 302 |
} |
| 303 |
|
| 304 |
show_cursor = SDL_ShowCursor(SDL_QUERY); |
| 305 |
|
| 306 |
/* Main render loop */ |
| 307 |
done = 0; |
| 308 |
#ifdef __EMSCRIPTEN__ |
| 309 |
emscripten_set_main_loop(loop, 0, 1); |
| 310 |
#else |
| 311 |
while (!done) { |
| 312 |
loop(); |
| 313 |
} |
| 314 |
#endif |
| 315 |
|
| 316 |
for (i = 0; i < num_cursors; ++i) { |
| 317 |
SDL_FreeCursor(cursors[i]); |
| 318 |
} |
| 319 |
quit(0); |
| 320 |
|
| 321 |
/* keep the compiler happy ... */ |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
/* vi: set ts=4 sw=4 expandtab: */ |