diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/test/testdropfile.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/test/testdropfile.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testdropfile.c | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testdropfile.c b/contrib/SDL-3.2.8/test/testdropfile.c new file mode 100644 index 0000000..de5b573 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testdropfile.c | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 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 | #define SDL_MAIN_USE_CALLBACKS 1 | ||
| 14 | #include <SDL3/SDL_test_common.h> | ||
| 15 | #include <SDL3/SDL_main.h> | ||
| 16 | |||
| 17 | typedef struct { | ||
| 18 | SDLTest_CommonState *state; | ||
| 19 | bool is_hover; | ||
| 20 | float x; | ||
| 21 | float y; | ||
| 22 | unsigned int windowID; | ||
| 23 | } dropfile_dialog; | ||
| 24 | |||
| 25 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 26 | { | ||
| 27 | int i; | ||
| 28 | dropfile_dialog *dialog; | ||
| 29 | SDLTest_CommonState *state; | ||
| 30 | |||
| 31 | /* Initialize test framework */ | ||
| 32 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
| 33 | if (!state) { | ||
| 34 | return SDL_APP_FAILURE; | ||
| 35 | } | ||
| 36 | |||
| 37 | for (i = 1; i < argc;) { | ||
| 38 | int consumed; | ||
| 39 | |||
| 40 | consumed = SDLTest_CommonArg(state, i); | ||
| 41 | /* needed voodoo to allow app to launch via macOS Finder */ | ||
| 42 | if (SDL_strncmp(argv[i], "-psn", 4) == 0) { | ||
| 43 | consumed = 1; | ||
| 44 | } | ||
| 45 | if (consumed == 0) { | ||
| 46 | consumed = -1; | ||
| 47 | } | ||
| 48 | if (consumed < 0) { | ||
| 49 | SDLTest_CommonLogUsage(state, argv[0], NULL); | ||
| 50 | goto onerror; | ||
| 51 | } | ||
| 52 | i += consumed; | ||
| 53 | } | ||
| 54 | if (!SDLTest_CommonInit(state)) { | ||
| 55 | goto onerror; | ||
| 56 | } | ||
| 57 | dialog = SDL_calloc(1, sizeof(dropfile_dialog)); | ||
| 58 | if (!dialog) { | ||
| 59 | goto onerror; | ||
| 60 | } | ||
| 61 | *appstate = dialog; | ||
| 62 | |||
| 63 | dialog->state = state; | ||
| 64 | return SDL_APP_CONTINUE; | ||
| 65 | onerror: | ||
| 66 | SDLTest_CommonQuit(state); | ||
| 67 | return SDL_APP_FAILURE; | ||
| 68 | } | ||
| 69 | |||
| 70 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 71 | { | ||
| 72 | dropfile_dialog *dialog = appstate; | ||
| 73 | if (event->type == SDL_EVENT_DROP_BEGIN) { | ||
| 74 | SDL_Log("Drop beginning on window %u at (%f, %f)", (unsigned int)event->drop.windowID, event->drop.x, event->drop.y); | ||
| 75 | } else if (event->type == SDL_EVENT_DROP_COMPLETE) { | ||
| 76 | dialog->is_hover = false; | ||
| 77 | SDL_Log("Drop complete on window %u at (%f, %f)", (unsigned int)event->drop.windowID, event->drop.x, event->drop.y); | ||
| 78 | } else if ((event->type == SDL_EVENT_DROP_FILE) || (event->type == SDL_EVENT_DROP_TEXT)) { | ||
| 79 | const char *typestr = (event->type == SDL_EVENT_DROP_FILE) ? "File" : "Text"; | ||
| 80 | SDL_Log("%s dropped on window %u: %s at (%f, %f)", typestr, (unsigned int)event->drop.windowID, event->drop.data, event->drop.x, event->drop.y); | ||
| 81 | } else if (event->type == SDL_EVENT_DROP_POSITION) { | ||
| 82 | const float w_x = event->drop.x; | ||
| 83 | const float w_y = event->drop.y; | ||
| 84 | SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(event)), event); | ||
| 85 | dialog->is_hover = true; | ||
| 86 | dialog->x = event->drop.x; | ||
| 87 | dialog->y = event->drop.y; | ||
| 88 | dialog->windowID = event->drop.windowID; | ||
| 89 | SDL_Log("Drop position on window %u at (%f, %f) data = %s", (unsigned int)event->drop.windowID, w_x, w_y, event->drop.data); | ||
| 90 | } | ||
| 91 | |||
| 92 | return SDLTest_CommonEventMainCallbacks(dialog->state, event); | ||
| 93 | } | ||
| 94 | |||
| 95 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 96 | { | ||
| 97 | dropfile_dialog *dialog = appstate; | ||
| 98 | int i; | ||
| 99 | |||
| 100 | for (i = 0; i < dialog->state->num_windows; ++i) { | ||
| 101 | SDL_Renderer *renderer = dialog->state->renderers[i]; | ||
| 102 | SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); | ||
| 103 | SDL_RenderClear(renderer); | ||
| 104 | if (dialog->is_hover) { | ||
| 105 | if (dialog->windowID == SDL_GetWindowID(SDL_GetRenderWindow(renderer))) { | ||
| 106 | int len = 2000; | ||
| 107 | SDL_SetRenderDrawColor(renderer, 0x0A, 0x0A, 0x0A, 0xFF); | ||
| 108 | SDL_RenderLine(renderer, dialog->x, dialog->y - len, dialog->x, dialog->y + len); | ||
| 109 | SDL_RenderLine(renderer, dialog->x - len, dialog->y, dialog->x + len, dialog->y); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | SDL_RenderPresent(renderer); | ||
| 113 | } | ||
| 114 | return SDL_APP_CONTINUE; | ||
| 115 | } | ||
| 116 | |||
| 117 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 118 | { | ||
| 119 | dropfile_dialog *dialog = appstate; | ||
| 120 | if (dialog) { | ||
| 121 | SDLTest_CommonState *state = dialog->state; | ||
| 122 | SDL_free(dialog); | ||
| 123 | SDLTest_CommonQuit(state); | ||
| 124 | } | ||
| 125 | } | ||
