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