load-bitmaps.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This example code loads a bitmap with asynchronous i/o and renders it.
  3. *
  4. * This code is public domain. Feel free to use it for any purpose!
  5. */
  6. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  7. #include <SDL3/SDL.h>
  8. #include <SDL3/SDL_main.h>
  9. /* We will use this renderer to draw into this window every frame. */
  10. static SDL_Window *window = NULL;
  11. static SDL_Renderer *renderer = NULL;
  12. static SDL_AsyncIOQueue *queue = NULL;
  13. #define TOTAL_TEXTURES 4
  14. static const char * const pngs[TOTAL_TEXTURES] = { "sample.png", "gamepad_front.png", "speaker.png", "icon2x.png" };
  15. static SDL_Texture *textures[TOTAL_TEXTURES];
  16. static const SDL_FRect texture_rects[TOTAL_TEXTURES] = {
  17. { 116, 156, 408, 167 },
  18. { 20, 200, 96, 60 },
  19. { 525, 180, 96, 96 },
  20. { 288, 375, 64, 64 }
  21. };
  22. /* This function runs once at startup. */
  23. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  24. {
  25. int i;
  26. if (!SDL_Init(SDL_INIT_VIDEO)) {
  27. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  28. return SDL_APP_FAILURE;
  29. }
  30. if (!SDL_CreateWindowAndRenderer("examples/asyncio/load-bitmaps", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  31. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  32. return SDL_APP_FAILURE;
  33. }
  34. SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  35. queue = SDL_CreateAsyncIOQueue();
  36. if (!queue) {
  37. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create async i/o queue!", SDL_GetError(), NULL);
  38. return SDL_APP_FAILURE;
  39. }
  40. /* Load some .png files asynchronously from wherever the app is being run from, put them in the same queue. */
  41. for (i = 0; i < SDL_arraysize(pngs); i++) {
  42. char *path = NULL;
  43. SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), pngs[i]); /* allocate a string of the full file path */
  44. /* you _should) check for failure, but we'll just go on without files here. */
  45. SDL_LoadFileAsync(path, queue, (void *) pngs[i]); /* attach the filename as app-specific data, so we can see it later. */
  46. SDL_free(path);
  47. }
  48. return SDL_APP_CONTINUE; /* carry on with the program! */
  49. }
  50. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  51. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  52. {
  53. if (event->type == SDL_EVENT_QUIT) {
  54. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  55. }
  56. return SDL_APP_CONTINUE; /* carry on with the program! */
  57. }
  58. /* This function runs once per frame, and is the heart of the program. */
  59. SDL_AppResult SDL_AppIterate(void *appstate)
  60. {
  61. SDL_AsyncIOOutcome outcome;
  62. int i;
  63. if (SDL_GetAsyncIOResult(queue, &outcome)) { /* a .png file load has finished? */
  64. if (outcome.result == SDL_ASYNCIO_COMPLETE) {
  65. /* this might be _any_ of the pngs; they might finish loading in any order. */
  66. for (i = 0; i < SDL_arraysize(pngs); i++) {
  67. /* this doesn't need a strcmp because we gave the pointer from this array to SDL_LoadFileAsync */
  68. if (outcome.userdata == pngs[i]) {
  69. break;
  70. }
  71. }
  72. if (i < SDL_arraysize(pngs)) { /* (just in case.) */
  73. SDL_Surface *surface = SDL_LoadPNG_IO(SDL_IOFromConstMem(outcome.buffer, (size_t) outcome.bytes_transferred), true);
  74. if (surface) { /* the renderer is not multithreaded, so create the texture here once the data loads. */
  75. textures[i] = SDL_CreateTextureFromSurface(renderer, surface);
  76. if (!textures[i]) {
  77. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create texture!", SDL_GetError(), NULL);
  78. return SDL_APP_FAILURE;
  79. }
  80. SDL_DestroySurface(surface);
  81. }
  82. }
  83. }
  84. SDL_free(outcome.buffer);
  85. }
  86. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  87. SDL_RenderClear(renderer);
  88. for (i = 0; i < SDL_arraysize(textures); i++) {
  89. SDL_RenderTexture(renderer, textures[i], NULL, &texture_rects[i]);
  90. }
  91. SDL_RenderPresent(renderer);
  92. return SDL_APP_CONTINUE; /* carry on with the program! */
  93. }
  94. /* This function runs once at shutdown. */
  95. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  96. {
  97. int i;
  98. SDL_DestroyAsyncIOQueue(queue);
  99. for (i = 0; i < SDL_arraysize(textures); i++) {
  100. SDL_DestroyTexture(textures[i]);
  101. }
  102. /* SDL will clean up the window/renderer for us. */
  103. }