19
#include <SDL3/SDL_main.h>
20
#include <SDL3/SDL_test_common.h>
22
#ifdef SDL_PLATFORM_EMSCRIPTEN
23
#include <emscripten/emscripten.h>
26
static SDLTest_CommonState *state;
27
static SDL_bool use_texture = SDL_FALSE;
28
static SDL_Texture **sprites;
29
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
30
static float angle = 0.0f;
31
static int sprite_w, sprite_h;
32
static int translate_cx = 0;
33
static int translate_cy = 0;
42
SDLTest_CommonQuit(state);
49
static int LoadSprite(const char *file)
53
for (i = 0; i < state->num_windows; ++i) {
55
sprites[i] = LoadTexture(state->renderers[i], file, SDL_TRUE, &sprite_w, &sprite_h);
59
if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
60
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
61
SDL_DestroyTexture(sprites[i]);
76
while (SDL_PollEvent(&event)) {
78
if (event.type == SDL_EVENT_MOUSE_MOTION) {
79
if (event.motion.state) {
81
int window_w, window_h;
82
SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID);
83
SDL_GetWindowSize(window, &window_w, &window_h);
84
xrel = event.motion.xrel;
85
yrel = event.motion.yrel;
86
if (event.motion.y < (float)window_h / 2.0f) {
91
if (event.motion.x < (float)window_w / 2.0f) {
97
} else if (event.type == SDL_EVENT_KEY_DOWN) {
98
if (event.key.key == SDLK_LEFT) {
100
} else if (event.key.key == SDLK_RIGHT) {
102
} else if (event.key.key == SDLK_UP) {
104
} else if (event.key.key == SDLK_DOWN) {
107
SDLTest_CommonEvent(state, &event, &done);
110
SDLTest_CommonEvent(state, &event, &done);
114
for (i = 0; i < state->num_windows; ++i) {
115
SDL_Renderer *renderer = state->renderers[i];
116
if (state->windows[i] == NULL) {
119
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
120
SDL_RenderClear(renderer);
130
SDL_GetRenderViewport(renderer, &viewport);
132
cx = viewport.x + viewport.w / 2;
133
cy = viewport.y + viewport.h / 2;
134
d = (viewport.w + viewport.h) / 5.f;
139
a = (angle * SDL_PI_F) / 180.0f;
140
verts[0].position.x = cx + d * SDL_cosf(a);
141
verts[0].position.y = cy + d * SDL_sinf(a);
142
verts[0].color.r = 1.0f;
143
verts[0].color.g = 0;
144
verts[0].color.b = 0;
145
verts[0].color.a = 1.0f;
147
a = ((angle + 120) * SDL_PI_F) / 180.0f;
148
verts[1].position.x = cx + d * SDL_cosf(a);
149
verts[1].position.y = cy + d * SDL_sinf(a);
150
verts[1].color.r = 0;
151
verts[1].color.g = 1.0f;
152
verts[1].color.b = 0;
153
verts[1].color.a = 1.0f;
155
a = ((angle + 240) * SDL_PI_F) / 180.0f;
156
verts[2].position.x = cx + d * SDL_cosf(a);
157
verts[2].position.y = cy + d * SDL_sinf(a);
158
verts[2].color.r = 0;
159
verts[2].color.g = 0;
160
verts[2].color.b = 1.0f;
161
verts[2].color.a = 1.0f;
164
verts[0].tex_coord.x = 0.5f;
165
verts[0].tex_coord.y = 0.0f;
166
verts[1].tex_coord.x = 1.0f;
167
verts[1].tex_coord.y = 1.0f;
168
verts[2].tex_coord.x = 0.0f;
169
verts[2].tex_coord.y = 1.0f;
172
SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0);
175
SDL_RenderPresent(renderer);
177
#ifdef SDL_PLATFORM_EMSCRIPTEN
179
emscripten_cancel_main_loop();
184
int main(int argc, char *argv[])
187
const char *icon = "icon.bmp";
192
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
198
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
200
for (i = 1; i < argc;) {
203
consumed = SDLTest_CommonArg(state, i);
206
if (SDL_strcasecmp(argv[i], "--blend") == 0) {
208
if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
209
blendMode = SDL_BLENDMODE_NONE;
211
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
212
blendMode = SDL_BLENDMODE_BLEND;
214
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
215
blendMode = SDL_BLENDMODE_ADD;
217
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
218
blendMode = SDL_BLENDMODE_MOD;
220
} else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
221
blendMode = SDL_BLENDMODE_MUL;
225
} else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) {
226
use_texture = SDL_TRUE;
231
static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--use-texture]", NULL };
232
SDLTest_CommonLogUsage(state, argv[0], options);
237
if (!SDLTest_CommonInit(state)) {
243
(SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites));
245
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
249
for (i = 0; i < state->num_windows; ++i) {
250
SDL_Renderer *renderer = state->renderers[i];
251
SDL_SetRenderDrawBlendMode(renderer, blendMode);
252
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
253
SDL_RenderClear(renderer);
257
if (LoadSprite(icon) < 0) {
264
then = SDL_GetTicks();
267
#ifdef SDL_PLATFORM_EMSCRIPTEN
268
emscripten_set_main_loop(loop, 0, 1);
277
now = SDL_GetTicks();
279
double fps = ((double)frames * 1000) / (now - then);
280
SDL_Log("%2.2f frames per second\n", fps);