load-bitmaps.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 bmps[TOTAL_TEXTURES] = { "sample.bmp", "gamepad_front.bmp", "speaker.bmp", "icon2x.bmp" };
  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, 0, &window, &renderer)) {
  31. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  32. return SDL_APP_FAILURE;
  33. }
  34. queue = SDL_CreateAsyncIOQueue();
  35. if (!queue) {
  36. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create async i/o queue!", SDL_GetError(), NULL);
  37. return SDL_APP_FAILURE;
  38. }
  39. /* Load some .bmp files asynchronously from wherever the app is being run from, put them in the same queue. */
  40. for (i = 0; i < SDL_arraysize(bmps); i++) {
  41. char *path = NULL;
  42. SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), bmps[i]); /* allocate a string of the full file path */
  43. /* you _should) check for failure, but we'll just go on without files here. */
  44. SDL_LoadFileAsync(path, queue, (void *) bmps[i]); /* attach the filename as app-specific data, so we can see it later. */
  45. SDL_free(path);
  46. }
  47. return SDL_APP_CONTINUE; /* carry on with the program! */
  48. }
  49. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  50. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  51. {
  52. if (event->type == SDL_EVENT_QUIT) {
  53. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  54. }
  55. return SDL_APP_CONTINUE; /* carry on with the program! */
  56. }
  57. /* This function runs once per frame, and is the heart of the program. */
  58. SDL_AppResult SDL_AppIterate(void *appstate)
  59. {
  60. SDL_AsyncIOOutcome outcome;
  61. int i;
  62. if (SDL_GetAsyncIOResult(queue, &outcome)) { /* a .bmp file load has finished? */
  63. if (outcome.result == SDL_ASYNCIO_COMPLETE) {
  64. /* this might be _any_ of the bmps; they might finish loading in any order. */
  65. for (i = 0; i < SDL_arraysize(bmps); i++) {
  66. /* this doesn't need a strcmp because we gave the pointer from this array to SDL_LoadFileAsync */
  67. if (outcome.userdata == bmps[i]) {
  68. break;
  69. }
  70. }
  71. if (i < SDL_arraysize(bmps)) { /* (just in case.) */
  72. SDL_Surface *surface = SDL_LoadBMP_IO(SDL_IOFromConstMem(outcome.buffer, (size_t) outcome.bytes_transferred), true);
  73. if (surface) { /* the renderer is not multithreaded, so create the texture here once the data loads. */
  74. textures[i] = SDL_CreateTextureFromSurface(renderer, surface);
  75. if (!textures[i]) {
  76. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create texture!", SDL_GetError(), NULL);
  77. return SDL_APP_FAILURE;
  78. }
  79. SDL_DestroySurface(surface);
  80. }
  81. }
  82. }
  83. SDL_free(outcome.buffer);
  84. }
  85. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  86. SDL_RenderClear(renderer);
  87. for (i = 0; i < SDL_arraysize(textures); i++) {
  88. SDL_RenderTexture(renderer, textures[i], NULL, &texture_rects[i]);
  89. }
  90. SDL_RenderPresent(renderer);
  91. return SDL_APP_CONTINUE; /* carry on with the program! */
  92. }
  93. /* This function runs once at shutdown. */
  94. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  95. {
  96. int i;
  97. SDL_DestroyAsyncIOQueue(queue);
  98. for (i = 0; i < SDL_arraysize(textures); i++) {
  99. SDL_DestroyTexture(textures[i]);
  100. }
  101. /* SDL will clean up the window/renderer for us. */
  102. }