diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testasyncio.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testasyncio.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testasyncio.c b/contrib/SDL-3.2.8/test/testasyncio.c new file mode 100644 index 0000000..5a9f719 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testasyncio.c | |||
| @@ -0,0 +1,191 @@ | |||
| 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_main.h> | ||
| 15 | #include <SDL3/SDL_test.h> | ||
| 16 | #include <SDL3/SDL_test_common.h> | ||
| 17 | |||
| 18 | static SDL_Renderer *renderer = NULL; | ||
| 19 | static SDL_Texture *texture = NULL; | ||
| 20 | static SDL_AsyncIOQueue *queue = NULL; | ||
| 21 | static SDLTest_CommonState *state = NULL; | ||
| 22 | |||
| 23 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 24 | { | ||
| 25 | const char *base = NULL; | ||
| 26 | SDL_AsyncIO *asyncio = NULL; | ||
| 27 | char **bmps = NULL; | ||
| 28 | int bmpcount = 0; | ||
| 29 | int i; | ||
| 30 | |||
| 31 | SDL_srand(0); | ||
| 32 | |||
| 33 | /* Initialize test framework */ | ||
| 34 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
| 35 | if (!state) { | ||
| 36 | return SDL_APP_FAILURE; | ||
| 37 | } | ||
| 38 | |||
| 39 | /* Enable standard application logging */ | ||
| 40 | SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); | ||
| 41 | |||
| 42 | /* Parse commandline */ | ||
| 43 | for (i = 1; i < argc;) { | ||
| 44 | int consumed = SDLTest_CommonArg(state, i); | ||
| 45 | if (consumed <= 0) { | ||
| 46 | static const char *options[] = { | ||
| 47 | NULL, | ||
| 48 | }; | ||
| 49 | SDLTest_CommonLogUsage(state, argv[0], options); | ||
| 50 | SDL_Quit(); | ||
| 51 | SDLTest_CommonDestroyState(state); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | i += consumed; | ||
| 55 | } | ||
| 56 | |||
| 57 | state->num_windows = 1; | ||
| 58 | |||
| 59 | /* Load the SDL library */ | ||
| 60 | if (!SDLTest_CommonInit(state)) { | ||
| 61 | SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 62 | return SDL_APP_FAILURE; | ||
| 63 | } | ||
| 64 | |||
| 65 | SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE); | ||
| 66 | |||
| 67 | renderer = state->renderers[0]; | ||
| 68 | if (!renderer) { | ||
| 69 | /* SDL_Log("Couldn't create renderer: %s", SDL_GetError()); */ | ||
| 70 | return SDL_APP_FAILURE; | ||
| 71 | } | ||
| 72 | |||
| 73 | texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, 512, 512); | ||
| 74 | if (!texture) { | ||
| 75 | SDL_Log("Couldn't create texture: %s", SDL_GetError()); | ||
| 76 | return SDL_APP_FAILURE; | ||
| 77 | } else { | ||
| 78 | static const Uint32 blank[512 * 512]; | ||
| 79 | const SDL_Rect rect = { 0, 0, 512, 512 }; | ||
| 80 | SDL_UpdateTexture(texture, &rect, blank, 512 * sizeof (Uint32)); | ||
| 81 | } | ||
| 82 | |||
| 83 | queue = SDL_CreateAsyncIOQueue(); | ||
| 84 | if (!queue) { | ||
| 85 | SDL_Log("Couldn't create async i/o queue: %s", SDL_GetError()); | ||
| 86 | return SDL_APP_FAILURE; | ||
| 87 | } | ||
| 88 | |||
| 89 | base = SDL_GetBasePath(); | ||
| 90 | bmps = SDL_GlobDirectory(base, "*.bmp", SDL_GLOB_CASEINSENSITIVE, &bmpcount); | ||
| 91 | if (!bmps || (bmpcount == 0)) { | ||
| 92 | SDL_Log("No BMP files found."); | ||
| 93 | return SDL_APP_FAILURE; | ||
| 94 | } | ||
| 95 | |||
| 96 | for (i = 0; i < bmpcount; i++) { | ||
| 97 | char *path = NULL; | ||
| 98 | if (SDL_asprintf(&path, "%s%s", base, bmps[i]) < 0) { | ||
| 99 | SDL_free(path); | ||
| 100 | } else { | ||
| 101 | SDL_Log("Loading %s...", path); | ||
| 102 | SDL_LoadFileAsync(path, queue, path); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | SDL_free(bmps); | ||
| 107 | |||
| 108 | SDL_Log("Opening asyncio.tmp..."); | ||
| 109 | asyncio = SDL_AsyncIOFromFile("asyncio.tmp", "w"); | ||
| 110 | if (!asyncio) { | ||
| 111 | SDL_Log("Failed!"); | ||
| 112 | return SDL_APP_FAILURE; | ||
| 113 | } | ||
| 114 | SDL_WriteAsyncIO(asyncio, "hello", 0, 5, queue, "asyncio.tmp (write)"); | ||
| 115 | SDL_CloseAsyncIO(asyncio, true, queue, "asyncio.tmp (flush/close)"); | ||
| 116 | |||
| 117 | return SDL_APP_CONTINUE; | ||
| 118 | } | ||
| 119 | |||
| 120 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 121 | { | ||
| 122 | switch (event->type) { | ||
| 123 | case SDL_EVENT_QUIT: | ||
| 124 | return SDL_APP_SUCCESS; | ||
| 125 | |||
| 126 | default: | ||
| 127 | break; | ||
| 128 | } | ||
| 129 | |||
| 130 | return SDLTest_CommonEventMainCallbacks(state, event); | ||
| 131 | } | ||
| 132 | |||
| 133 | static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome) | ||
| 134 | { | ||
| 135 | const char *fname = (const char *) outcome->userdata; | ||
| 136 | const char *resultstr = "[unknown result]"; | ||
| 137 | |||
| 138 | switch (outcome->result) { | ||
| 139 | #define RESCASE(x) case x: resultstr = #x; break | ||
| 140 | RESCASE(SDL_ASYNCIO_COMPLETE); | ||
| 141 | RESCASE(SDL_ASYNCIO_FAILURE); | ||
| 142 | RESCASE(SDL_ASYNCIO_CANCELED); | ||
| 143 | #undef RESCASE | ||
| 144 | } | ||
| 145 | |||
| 146 | SDL_Log("File '%s' async results: %s", fname, resultstr); | ||
| 147 | |||
| 148 | if (SDL_strncmp(fname, "asyncio.tmp", 11) == 0) { | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (outcome->result == SDL_ASYNCIO_COMPLETE) { | ||
| 153 | SDL_Surface *surface = SDL_LoadBMP_IO(SDL_IOFromConstMem(outcome->buffer, (size_t) outcome->bytes_transferred), true); | ||
| 154 | if (surface) { | ||
| 155 | SDL_Surface *converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888); | ||
| 156 | SDL_DestroySurface(surface); | ||
| 157 | if (converted) { | ||
| 158 | const SDL_Rect rect = { 50 + SDL_rand(512 - 100), 50 + SDL_rand(512 - 100), converted->w, converted->h }; | ||
| 159 | SDL_UpdateTexture(texture, &rect, converted->pixels, converted->pitch); | ||
| 160 | SDL_DestroySurface(converted); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | SDL_free(outcome->userdata); | ||
| 166 | SDL_free(outcome->buffer); | ||
| 167 | } | ||
| 168 | |||
| 169 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 170 | { | ||
| 171 | SDL_AsyncIOOutcome outcome; | ||
| 172 | if (SDL_GetAsyncIOResult(queue, &outcome)) { | ||
| 173 | async_io_task_complete(&outcome); | ||
| 174 | } | ||
| 175 | |||
| 176 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | ||
| 177 | SDL_RenderClear(renderer); | ||
| 178 | SDL_RenderTexture(renderer, texture, NULL, NULL); | ||
| 179 | SDL_RenderPresent(renderer); | ||
| 180 | |||
| 181 | return SDL_APP_CONTINUE; | ||
| 182 | } | ||
| 183 | |||
| 184 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 185 | { | ||
| 186 | SDL_DestroyAsyncIOQueue(queue); | ||
| 187 | SDL_DestroyTexture(texture); | ||
| 188 | SDL_RemovePath("asyncio.tmp"); | ||
| 189 | SDLTest_CommonQuit(state); | ||
| 190 | } | ||
| 191 | |||
