1
0

testasyncio.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. #define SDL_MAIN_USE_CALLBACKS 1
  11. #include <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test.h>
  13. #include <SDL3/SDL_test_common.h>
  14. static SDL_Renderer *renderer = NULL;
  15. static SDL_Texture *texture = NULL;
  16. static SDL_AsyncIOQueue *queue = NULL;
  17. static SDLTest_CommonState *state = NULL;
  18. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  19. {
  20. const char *base = NULL;
  21. SDL_AsyncIO *asyncio = NULL;
  22. char **bmps = NULL;
  23. int bmpcount = 0;
  24. int i;
  25. SDL_srand(0);
  26. /* Initialize test framework */
  27. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  28. if (!state) {
  29. return SDL_APP_FAILURE;
  30. }
  31. /* Enable standard application logging */
  32. SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  33. /* Parse commandline */
  34. for (i = 1; i < argc;) {
  35. int consumed = SDLTest_CommonArg(state, i);
  36. if (consumed <= 0) {
  37. static const char *options[] = {
  38. NULL,
  39. };
  40. SDLTest_CommonLogUsage(state, argv[0], options);
  41. SDL_Quit();
  42. SDLTest_CommonDestroyState(state);
  43. return 1;
  44. }
  45. i += consumed;
  46. }
  47. state->num_windows = 1;
  48. /* Load the SDL library */
  49. if (!SDLTest_CommonInit(state)) {
  50. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
  51. return SDL_APP_FAILURE;
  52. }
  53. SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
  54. renderer = state->renderers[0];
  55. if (!renderer) {
  56. /* SDL_Log("Couldn't create renderer: %s", SDL_GetError()); */
  57. return SDL_APP_FAILURE;
  58. }
  59. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, 512, 512);
  60. if (!texture) {
  61. SDL_Log("Couldn't create texture: %s", SDL_GetError());
  62. return SDL_APP_FAILURE;
  63. } else {
  64. static const Uint32 blank[512 * 512];
  65. const SDL_Rect rect = { 0, 0, 512, 512 };
  66. SDL_UpdateTexture(texture, &rect, blank, 512 * sizeof (Uint32));
  67. }
  68. queue = SDL_CreateAsyncIOQueue();
  69. if (!queue) {
  70. SDL_Log("Couldn't create async i/o queue: %s", SDL_GetError());
  71. return SDL_APP_FAILURE;
  72. }
  73. base = SDL_GetBasePath();
  74. bmps = SDL_GlobDirectory(base, "*.bmp", SDL_GLOB_CASEINSENSITIVE, &bmpcount);
  75. if (!bmps || (bmpcount == 0)) {
  76. SDL_Log("No BMP files found.");
  77. return SDL_APP_FAILURE;
  78. }
  79. for (i = 0; i < bmpcount; i++) {
  80. char *path = NULL;
  81. if (SDL_asprintf(&path, "%s%s", base, bmps[i]) < 0) {
  82. SDL_free(path);
  83. } else {
  84. SDL_Log("Loading %s...", path);
  85. SDL_LoadFileAsync(path, queue, path);
  86. }
  87. }
  88. SDL_free(bmps);
  89. SDL_Log("Opening asyncio.tmp...");
  90. asyncio = SDL_AsyncIOFromFile("asyncio.tmp", "w");
  91. if (!asyncio) {
  92. SDL_Log("Failed!");
  93. return SDL_APP_FAILURE;
  94. }
  95. SDL_WriteAsyncIO(asyncio, "hello", 0, 5, queue, "asyncio.tmp (write)");
  96. SDL_CloseAsyncIO(asyncio, true, queue, "asyncio.tmp (flush/close)");
  97. return SDL_APP_CONTINUE;
  98. }
  99. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  100. {
  101. switch (event->type) {
  102. case SDL_EVENT_QUIT:
  103. return SDL_APP_SUCCESS;
  104. default:
  105. break;
  106. }
  107. return SDLTest_CommonEventMainCallbacks(state, event);
  108. }
  109. static void async_io_task_complete(const SDL_AsyncIOOutcome *outcome)
  110. {
  111. const char *fname = (const char *) outcome->userdata;
  112. const char *resultstr = "[unknown result]";
  113. switch (outcome->result) {
  114. #define RESCASE(x) case x: resultstr = #x; break
  115. RESCASE(SDL_ASYNCIO_COMPLETE);
  116. RESCASE(SDL_ASYNCIO_FAILURE);
  117. RESCASE(SDL_ASYNCIO_CANCELED);
  118. #undef RESCASE
  119. }
  120. SDL_Log("File '%s' async results: %s", fname, resultstr);
  121. if (SDL_strncmp(fname, "asyncio.tmp", 11) == 0) {
  122. return;
  123. }
  124. if (outcome->result == SDL_ASYNCIO_COMPLETE) {
  125. SDL_Surface *surface = SDL_LoadBMP_IO(SDL_IOFromConstMem(outcome->buffer, (size_t) outcome->bytes_transferred), true);
  126. if (surface) {
  127. SDL_Surface *converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_RGBA8888);
  128. SDL_DestroySurface(surface);
  129. if (converted) {
  130. const SDL_Rect rect = { 50 + SDL_rand(512 - 100), 50 + SDL_rand(512 - 100), converted->w, converted->h };
  131. SDL_UpdateTexture(texture, &rect, converted->pixels, converted->pitch);
  132. SDL_DestroySurface(converted);
  133. }
  134. }
  135. }
  136. SDL_free(outcome->userdata);
  137. SDL_free(outcome->buffer);
  138. }
  139. SDL_AppResult SDL_AppIterate(void *appstate)
  140. {
  141. SDL_AsyncIOOutcome outcome;
  142. if (SDL_GetAsyncIOResult(queue, &outcome)) {
  143. async_io_task_complete(&outcome);
  144. }
  145. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  146. SDL_RenderClear(renderer);
  147. SDL_RenderTexture(renderer, texture, NULL, NULL);
  148. SDL_RenderPresent(renderer);
  149. return SDL_APP_CONTINUE;
  150. }
  151. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  152. {
  153. SDL_DestroyAsyncIOQueue(queue);
  154. SDL_DestroyTexture(texture);
  155. SDL_RemovePath("asyncio.tmp");
  156. SDLTest_CommonQuit(state);
  157. }