2
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
13
/* Simple program: draw as many random objects on the screen as possible */
16
#include <SDL3/SDL_main.h>
17
#include <SDL3/SDL_test.h>
19
#ifdef SDL_PLATFORM_EMSCRIPTEN
20
#include <emscripten/emscripten.h>
23
#define NUM_OBJECTS 100
25
static SDLTest_CommonState *state;
26
static int num_objects;
27
static SDL_bool cycle_color;
28
static SDL_bool cycle_alpha;
29
static int cycle_direction = 1;
30
static int current_alpha = 255;
31
static int current_color = 255;
32
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
33
static Uint64 next_fps_check;
35
static const int fps_check_delay = 5000;
39
static void DrawPoints(SDL_Renderer *renderer)
46
SDL_GetRenderViewport(renderer, &viewport);
48
for (i = 0; i < num_objects * 4; ++i) {
49
/* Cycle the color and alpha, if desired */
51
current_color += cycle_direction;
52
if (current_color < 0) {
54
cycle_direction = -cycle_direction;
56
if (current_color > 255) {
58
cycle_direction = -cycle_direction;
62
current_alpha += cycle_direction;
63
if (current_alpha < 0) {
65
cycle_direction = -cycle_direction;
67
if (current_alpha > 255) {
69
cycle_direction = -cycle_direction;
72
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
73
(Uint8)current_color, (Uint8)current_alpha);
75
x = (float)SDL_rand(viewport.w);
76
y = (float)SDL_rand(viewport.h);
77
SDL_RenderPoint(renderer, x, y);
81
static void DrawLines(SDL_Renderer *renderer)
88
SDL_GetRenderViewport(renderer, &viewport);
90
for (i = 0; i < num_objects; ++i) {
91
/* Cycle the color and alpha, if desired */
93
current_color += cycle_direction;
94
if (current_color < 0) {
96
cycle_direction = -cycle_direction;
98
if (current_color > 255) {
100
cycle_direction = -cycle_direction;
104
current_alpha += cycle_direction;
105
if (current_alpha < 0) {
107
cycle_direction = -cycle_direction;
109
if (current_alpha > 255) {
111
cycle_direction = -cycle_direction;
114
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
115
(Uint8)current_color, (Uint8)current_alpha);
118
SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
119
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h - 1), (float)(viewport.w - 1), 0.0f);
120
SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
121
SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
123
x1 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
124
x2 = (float)(SDL_rand(viewport.w * 2) - viewport.w);
125
y1 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
126
y2 = (float)(SDL_rand(viewport.h * 2) - viewport.h);
127
SDL_RenderLine(renderer, x1, y1, x2, y2);
132
static void DrawRects(SDL_Renderer *renderer)
138
/* Query the sizes */
139
SDL_GetRenderViewport(renderer, &viewport);
141
for (i = 0; i < num_objects / 4; ++i) {
142
/* Cycle the color and alpha, if desired */
144
current_color += cycle_direction;
145
if (current_color < 0) {
147
cycle_direction = -cycle_direction;
149
if (current_color > 255) {
151
cycle_direction = -cycle_direction;
155
current_alpha += cycle_direction;
156
if (current_alpha < 0) {
158
cycle_direction = -cycle_direction;
160
if (current_alpha > 255) {
162
cycle_direction = -cycle_direction;
165
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
166
(Uint8)current_color, (Uint8)current_alpha);
168
rect.w = (float)SDL_rand(viewport.h / 2);
169
rect.h = (float)SDL_rand(viewport.h / 2);
170
rect.x = (float)((SDL_rand(viewport.w * 2) - viewport.w) - (rect.w / 2));
171
rect.y = (float)((SDL_rand(viewport.h * 2) - viewport.h) - (rect.h / 2));
172
SDL_RenderFillRect(renderer, &rect);
176
static void loop(void)
182
/* Check for events */
183
while (SDL_PollEvent(&event)) {
184
SDLTest_CommonEvent(state, &event, &done);
186
for (i = 0; i < state->num_windows; ++i) {
187
SDL_Renderer *renderer = state->renderers[i];
188
if (state->windows[i] == NULL) {
191
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
192
SDL_RenderClear(renderer);
196
DrawPoints(renderer);
198
SDL_RenderPresent(renderer);
200
#ifdef SDL_PLATFORM_EMSCRIPTEN
202
emscripten_cancel_main_loop();
206
now = SDL_GetTicks();
207
if (now >= next_fps_check) {
208
/* Print out some timing information */
209
const Uint64 then = next_fps_check - fps_check_delay;
210
const double fps = ((double)frames * 1000) / (now - then);
211
SDL_Log("%2.2f frames per second\n", fps);
212
next_fps_check = now + fps_check_delay;
217
int main(int argc, char *argv[])
220
/* Initialize parameters */
221
num_objects = NUM_OBJECTS;
223
/* Initialize test framework */
224
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
229
/* Enable standard application logging */
230
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
232
for (i = 1; i < argc;) {
235
consumed = SDLTest_CommonArg(state, i);
238
if (SDL_strcasecmp(argv[i], "--blend") == 0) {
240
if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
241
blendMode = SDL_BLENDMODE_NONE;
243
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
244
blendMode = SDL_BLENDMODE_BLEND;
246
} else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) {
247
blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
249
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
250
blendMode = SDL_BLENDMODE_ADD;
252
} else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) {
253
blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
255
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
256
blendMode = SDL_BLENDMODE_MOD;
258
} else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
259
blendMode = SDL_BLENDMODE_MUL;
263
} else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
264
cycle_color = SDL_TRUE;
266
} else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
267
cycle_alpha = SDL_TRUE;
269
} else if (SDL_isdigit(*argv[i])) {
270
num_objects = SDL_atoi(argv[i]);
275
static const char *options[] = {
276
"[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]",
282
SDLTest_CommonLogUsage(state, argv[0], options);
287
if (!SDLTest_CommonInit(state)) {
291
/* Create the windows and initialize the renderers */
292
for (i = 0; i < state->num_windows; ++i) {
293
SDL_Renderer *renderer = state->renderers[i];
294
SDL_SetRenderDrawBlendMode(renderer, blendMode);
295
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
296
SDL_RenderClear(renderer);
299
/* Main render loop */
301
next_fps_check = SDL_GetTicks() + fps_check_delay;
304
#ifdef SDL_PLATFORM_EMSCRIPTEN
305
emscripten_set_main_loop(loop, 0, 1);
312
SDLTest_CleanupTextDrawing();
313
SDLTest_CommonQuit(state);