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