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