15
#include <SDL3/SDL_main.h>
16
#include <SDL3/SDL_test_common.h>
18
#ifdef SDL_PLATFORM_EMSCRIPTEN
19
#include <emscripten/emscripten.h>
22
#define SWAP(typ, a, b) \
28
#define NUM_OBJECTS 100
30
static SDLTest_CommonState *state;
31
static int num_objects;
32
static SDL_bool cycle_color;
33
static SDL_bool cycle_alpha;
34
static int cycle_direction = 1;
35
static int current_alpha = 255;
36
static int current_color = 255;
37
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
39
static float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
41
static void DrawPoints(SDL_Renderer *renderer)
48
SDL_GetRenderViewport(renderer, &viewport);
50
for (i = 0; i < num_objects * 4; ++i) {
53
current_color += cycle_direction;
54
if (current_color < 0) {
56
cycle_direction = -cycle_direction;
58
if (current_color > 255) {
60
cycle_direction = -cycle_direction;
64
current_alpha += cycle_direction;
65
if (current_alpha < 0) {
67
cycle_direction = -cycle_direction;
69
if (current_alpha > 255) {
71
cycle_direction = -cycle_direction;
74
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
75
(Uint8)current_color, (Uint8)current_alpha);
77
x = (float)SDL_rand(viewport.w);
78
y = (float)SDL_rand(viewport.h);
79
SDL_RenderPoint(renderer, x, y);
84
static int num_lines = 0;
85
static SDL_FRect lines[MAX_LINES];
86
static int add_line(float x1, float y1, float x2, float y2)
88
if (num_lines >= MAX_LINES) {
91
if ((x1 == x2) && (y1 == y2)) {
95
SDL_Log("adding line (%g, %g), (%g, %g)\n", x1, y1, x2, y2);
96
lines[num_lines].x = x1;
97
lines[num_lines].y = y1;
98
lines[num_lines].w = x2;
99
lines[num_lines].h = y2;
104
static void DrawLines(SDL_Renderer *renderer)
110
SDL_GetRenderViewport(renderer, &viewport);
112
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
114
for (i = 0; i < num_lines; ++i) {
116
SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
117
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h - 1), (float)(viewport.w - 1), 0.0f);
118
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
119
SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
121
SDL_RenderLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
127
static int num_rects = 0;
128
static SDL_FRect rects[MAX_RECTS];
129
static int add_rect(float x1, float y1, float x2, float y2)
131
if (num_rects >= MAX_RECTS) {
134
if ((x1 == x2) || (y1 == y2)) {
145
SDL_Log("adding rect (%g, %g), (%g, %g) [%gx%g]\n", x1, y1, x2, y2,
148
rects[num_rects].x = x1;
149
rects[num_rects].y = y1;
150
rects[num_rects].w = x2 - x1;
151
rects[num_rects].h = y2 - y1;
157
DrawRects(SDL_Renderer *renderer)
159
SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
160
SDL_RenderFillRects(renderer, rects, num_rects);
164
DrawRectLineIntersections(SDL_Renderer *renderer)
168
SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
170
for (i = 0; i < num_rects; i++) {
171
for (j = 0; j < num_lines; j++) {
172
float x1, y1, x2, y2;
181
if (SDL_GetRectAndLineIntersectionFloat(&r, &x1, &y1, &x2, &y2)) {
182
SDL_RenderLine(renderer, x1, y1, x2, y2);
189
DrawRectRectIntersections(SDL_Renderer *renderer)
193
SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
195
for (i = 0; i < num_rects; i++) {
196
for (j = i + 1; j < num_rects; j++) {
198
if (SDL_GetRectIntersectionFloat(&rects[i], &rects[j], &r)) {
199
SDL_RenderFillRect(renderer, &r);
205
static void loop(void *arg)
209
int *done = (int *)arg;
212
while (SDL_PollEvent(&event)) {
213
SDLTest_CommonEvent(state, &event, done);
214
switch (event.type) {
215
case SDL_EVENT_MOUSE_BUTTON_DOWN:
216
mouse_begin_x = event.button.x;
217
mouse_begin_y = event.button.y;
219
case SDL_EVENT_MOUSE_BUTTON_UP:
220
if (event.button.button == 3) {
221
add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
223
if (event.button.button == 1) {
224
add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
227
case SDL_EVENT_KEY_DOWN:
228
switch (event.key.key) {
230
if (event.key.mod & SDL_KMOD_SHIFT) {
234
(float)SDL_rand(640),
235
(float)SDL_rand(480),
236
(float)SDL_rand(640),
237
(float)SDL_rand(480));
241
if (event.key.mod & SDL_KMOD_SHIFT) {
245
(float)SDL_rand(640),
246
(float)SDL_rand(480),
247
(float)SDL_rand(640),
248
(float)SDL_rand(480));
259
for (i = 0; i < state->num_windows; ++i) {
260
SDL_Renderer *renderer = state->renderers[i];
261
if (state->windows[i] == NULL) {
264
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
265
SDL_RenderClear(renderer);
268
DrawPoints(renderer);
269
DrawRectRectIntersections(renderer);
271
DrawRectLineIntersections(renderer);
273
SDL_RenderPresent(renderer);
275
#ifdef SDL_PLATFORM_EMSCRIPTEN
277
emscripten_cancel_main_loop();
282
int main(int argc, char *argv[])
293
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
299
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
301
for (i = 1; i < argc;) {
304
consumed = SDLTest_CommonArg(state, i);
307
if (SDL_strcasecmp(argv[i], "--blend") == 0) {
309
if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
310
blendMode = SDL_BLENDMODE_NONE;
312
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
313
blendMode = SDL_BLENDMODE_BLEND;
315
} else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) {
316
blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
318
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
319
blendMode = SDL_BLENDMODE_ADD;
321
} else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) {
322
blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
324
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
325
blendMode = SDL_BLENDMODE_MOD;
327
} else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
328
blendMode = SDL_BLENDMODE_MUL;
332
} else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
333
cycle_color = SDL_TRUE;
335
} else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
336
cycle_alpha = SDL_TRUE;
338
} else if (num_objects < 0 && SDL_isdigit(*argv[i])) {
340
num_objects = (int)SDL_strtol(argv[i], &endptr, 0);
341
if (endptr != argv[i] && *endptr == '\0' && num_objects >= 0) {
347
static const char *options[] = { "[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]", "[--cyclecolor]", "[--cyclealpha]", "[count]", NULL };
348
SDLTest_CommonLogUsage(state, argv[0], options);
353
if (!SDLTest_CommonInit(state)) {
357
if (num_objects < 0) {
358
num_objects = NUM_OBJECTS;
362
for (i = 0; i < state->num_windows; ++i) {
363
SDL_Renderer *renderer = state->renderers[i];
364
SDL_SetRenderDrawBlendMode(renderer, blendMode);
365
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
366
SDL_RenderClear(renderer);
371
then = SDL_GetTicks();
374
#ifdef SDL_PLATFORM_EMSCRIPTEN
375
emscripten_set_main_loop_arg(loop, &done, 0, 1);
384
now = SDL_GetTicks();
386
SDLTest_CommonQuit(state);
389
double fps = ((double)frames * 1000) / (now - then);
390
SDL_Log("%2.2f frames per second\n", fps);