| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 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. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* Simple program: draw as many random objects on the screen as possible */ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <time.h> |
| 18 |
|
| 19 |
#ifdef __EMSCRIPTEN__ |
| 20 |
#include <emscripten/emscripten.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include "SDL_test_common.h" |
| 24 |
|
| 25 |
#define SWAP(typ, a, b) \ |
| 26 |
do { \ |
| 27 |
typ t = a; \ |
| 28 |
a = b; \ |
| 29 |
b = t; \ |
| 30 |
} while (0) |
| 31 |
#define NUM_OBJECTS 100 |
| 32 |
|
| 33 |
static SDLTest_CommonState *state; |
| 34 |
static int num_objects; |
| 35 |
static SDL_bool cycle_color; |
| 36 |
static SDL_bool cycle_alpha; |
| 37 |
static int cycle_direction = 1; |
| 38 |
static int current_alpha = 255; |
| 39 |
static int current_color = 255; |
| 40 |
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; |
| 41 |
|
| 42 |
int mouse_begin_x = -1, mouse_begin_y = -1; |
| 43 |
int done; |
| 44 |
|
| 45 |
void DrawPoints(SDL_Renderer *renderer) |
| 46 |
{ |
| 47 |
int i; |
| 48 |
int x, y; |
| 49 |
SDL_Rect viewport; |
| 50 |
|
| 51 |
/* Query the sizes */ |
| 52 |
SDL_RenderGetViewport(renderer, &viewport); |
| 53 |
|
| 54 |
for (i = 0; i < num_objects * 4; ++i) { |
| 55 |
/* Cycle the color and alpha, if desired */ |
| 56 |
if (cycle_color) { |
| 57 |
current_color += cycle_direction; |
| 58 |
if (current_color < 0) { |
| 59 |
current_color = 0; |
| 60 |
cycle_direction = -cycle_direction; |
| 61 |
} |
| 62 |
if (current_color > 255) { |
| 63 |
current_color = 255; |
| 64 |
cycle_direction = -cycle_direction; |
| 65 |
} |
| 66 |
} |
| 67 |
if (cycle_alpha) { |
| 68 |
current_alpha += cycle_direction; |
| 69 |
if (current_alpha < 0) { |
| 70 |
current_alpha = 0; |
| 71 |
cycle_direction = -cycle_direction; |
| 72 |
} |
| 73 |
if (current_alpha > 255) { |
| 74 |
current_alpha = 255; |
| 75 |
cycle_direction = -cycle_direction; |
| 76 |
} |
| 77 |
} |
| 78 |
SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, |
| 79 |
(Uint8)current_color, (Uint8)current_alpha); |
| 80 |
|
| 81 |
x = rand() % viewport.w; |
| 82 |
y = rand() % viewport.h; |
| 83 |
SDL_RenderDrawPoint(renderer, x, y); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#define MAX_LINES 16 |
| 88 |
int num_lines = 0; |
| 89 |
SDL_Rect lines[MAX_LINES]; |
| 90 |
static int |
| 91 |
add_line(int x1, int y1, int x2, int y2) |
| 92 |
{ |
| 93 |
if (num_lines >= MAX_LINES) { |
| 94 |
return 0; |
| 95 |
} |
| 96 |
if ((x1 == x2) && (y1 == y2)) { |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2); |
| 101 |
lines[num_lines].x = x1; |
| 102 |
lines[num_lines].y = y1; |
| 103 |
lines[num_lines].w = x2; |
| 104 |
lines[num_lines].h = y2; |
| 105 |
|
| 106 |
return ++num_lines; |
| 107 |
} |
| 108 |
|
| 109 |
void DrawLines(SDL_Renderer *renderer) |
| 110 |
{ |
| 111 |
int i; |
| 112 |
SDL_Rect viewport; |
| 113 |
|
| 114 |
/* Query the sizes */ |
| 115 |
SDL_RenderGetViewport(renderer, &viewport); |
| 116 |
|
| 117 |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 118 |
|
| 119 |
for (i = 0; i < num_lines; ++i) { |
| 120 |
if (i == -1) { |
| 121 |
SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1); |
| 122 |
SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0); |
| 123 |
SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2); |
| 124 |
SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1); |
| 125 |
} else { |
| 126 |
SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#define MAX_RECTS 16 |
| 132 |
int num_rects = 0; |
| 133 |
SDL_Rect rects[MAX_RECTS]; |
| 134 |
static int |
| 135 |
add_rect(int x1, int y1, int x2, int y2) |
| 136 |
{ |
| 137 |
if (num_rects >= MAX_RECTS) { |
| 138 |
return 0; |
| 139 |
} |
| 140 |
if ((x1 == x2) || (y1 == y2)) { |
| 141 |
return 0; |
| 142 |
} |
| 143 |
|
| 144 |
if (x1 > x2) { |
| 145 |
SWAP(int, x1, x2); |
| 146 |
} |
| 147 |
if (y1 > y2) { |
| 148 |
SWAP(int, y1, y2); |
| 149 |
} |
| 150 |
|
| 151 |
SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2, |
| 152 |
x2 - x1, y2 - y1); |
| 153 |
|
| 154 |
rects[num_rects].x = x1; |
| 155 |
rects[num_rects].y = y1; |
| 156 |
rects[num_rects].w = x2 - x1; |
| 157 |
rects[num_rects].h = y2 - y1; |
| 158 |
|
| 159 |
return ++num_rects; |
| 160 |
} |
| 161 |
|
| 162 |
static void |
| 163 |
DrawRects(SDL_Renderer *renderer) |
| 164 |
{ |
| 165 |
SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255); |
| 166 |
SDL_RenderFillRects(renderer, rects, num_rects); |
| 167 |
} |
| 168 |
|
| 169 |
static void |
| 170 |
DrawRectLineIntersections(SDL_Renderer *renderer) |
| 171 |
{ |
| 172 |
int i, j; |
| 173 |
|
| 174 |
SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255); |
| 175 |
|
| 176 |
for (i = 0; i < num_rects; i++) { |
| 177 |
for (j = 0; j < num_lines; j++) { |
| 178 |
int x1, y1, x2, y2; |
| 179 |
SDL_Rect r; |
| 180 |
|
| 181 |
r = rects[i]; |
| 182 |
x1 = lines[j].x; |
| 183 |
y1 = lines[j].y; |
| 184 |
x2 = lines[j].w; |
| 185 |
y2 = lines[j].h; |
| 186 |
|
| 187 |
if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) { |
| 188 |
SDL_RenderDrawLine(renderer, x1, y1, x2, y2); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
static void |
| 195 |
DrawRectRectIntersections(SDL_Renderer *renderer) |
| 196 |
{ |
| 197 |
int i, j; |
| 198 |
|
| 199 |
SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255); |
| 200 |
|
| 201 |
for (i = 0; i < num_rects; i++) { |
| 202 |
for (j = i + 1; j < num_rects; j++) { |
| 203 |
SDL_Rect r; |
| 204 |
if (SDL_IntersectRect(&rects[i], &rects[j], &r)) { |
| 205 |
SDL_RenderFillRect(renderer, &r); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void loop(void) |
| 212 |
{ |
| 213 |
int i; |
| 214 |
SDL_Event event; |
| 215 |
|
| 216 |
/* Check for events */ |
| 217 |
while (SDL_PollEvent(&event)) { |
| 218 |
SDLTest_CommonEvent(state, &event, &done); |
| 219 |
switch (event.type) { |
| 220 |
case SDL_MOUSEBUTTONDOWN: |
| 221 |
mouse_begin_x = event.button.x; |
| 222 |
mouse_begin_y = event.button.y; |
| 223 |
break; |
| 224 |
case SDL_MOUSEBUTTONUP: |
| 225 |
if (event.button.button == 3) { |
| 226 |
add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); |
| 227 |
} |
| 228 |
if (event.button.button == 1) { |
| 229 |
add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); |
| 230 |
} |
| 231 |
break; |
| 232 |
case SDL_KEYDOWN: |
| 233 |
switch (event.key.keysym.sym) { |
| 234 |
case 'l': |
| 235 |
if (event.key.keysym.mod & KMOD_SHIFT) { |
| 236 |
num_lines = 0; |
| 237 |
} else { |
| 238 |
add_line(rand() % 640, rand() % 480, rand() % 640, |
| 239 |
rand() % 480); |
| 240 |
} |
| 241 |
break; |
| 242 |
case 'r': |
| 243 |
if (event.key.keysym.mod & KMOD_SHIFT) { |
| 244 |
num_rects = 0; |
| 245 |
} else { |
| 246 |
add_rect(rand() % 640, rand() % 480, rand() % 640, |
| 247 |
rand() % 480); |
| 248 |
} |
| 249 |
break; |
| 250 |
} |
| 251 |
break; |
| 252 |
default: |
| 253 |
break; |
| 254 |
} |
| 255 |
} |
| 256 |
for (i = 0; i < state->num_windows; ++i) { |
| 257 |
SDL_Renderer *renderer = state->renderers[i]; |
| 258 |
if (state->windows[i] == NULL) { |
| 259 |
continue; |
| 260 |
} |
| 261 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 262 |
SDL_RenderClear(renderer); |
| 263 |
|
| 264 |
DrawRects(renderer); |
| 265 |
DrawPoints(renderer); |
| 266 |
DrawRectRectIntersections(renderer); |
| 267 |
DrawLines(renderer); |
| 268 |
DrawRectLineIntersections(renderer); |
| 269 |
|
| 270 |
SDL_RenderPresent(renderer); |
| 271 |
} |
| 272 |
#ifdef __EMSCRIPTEN__ |
| 273 |
if (done) { |
| 274 |
emscripten_cancel_main_loop(); |
| 275 |
} |
| 276 |
#endif |
| 277 |
} |
| 278 |
|
| 279 |
int main(int argc, char *argv[]) |
| 280 |
{ |
| 281 |
int i; |
| 282 |
Uint32 then, now, frames; |
| 283 |
|
| 284 |
/* Enable standard application logging */ |
| 285 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 286 |
|
| 287 |
/* Initialize parameters */ |
| 288 |
num_objects = NUM_OBJECTS; |
| 289 |
|
| 290 |
/* Initialize test framework */ |
| 291 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 292 |
if (!state) { |
| 293 |
return 1; |
| 294 |
} |
| 295 |
for (i = 1; i < argc;) { |
| 296 |
int consumed; |
| 297 |
|
| 298 |
consumed = SDLTest_CommonArg(state, i); |
| 299 |
if (consumed == 0) { |
| 300 |
consumed = -1; |
| 301 |
if (SDL_strcasecmp(argv[i], "--blend") == 0) { |
| 302 |
if (argv[i + 1]) { |
| 303 |
if (SDL_strcasecmp(argv[i + 1], "none") == 0) { |
| 304 |
blendMode = SDL_BLENDMODE_NONE; |
| 305 |
consumed = 2; |
| 306 |
} else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { |
| 307 |
blendMode = SDL_BLENDMODE_BLEND; |
| 308 |
consumed = 2; |
| 309 |
} else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { |
| 310 |
blendMode = SDL_BLENDMODE_ADD; |
| 311 |
consumed = 2; |
| 312 |
} else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { |
| 313 |
blendMode = SDL_BLENDMODE_MOD; |
| 314 |
consumed = 2; |
| 315 |
} |
| 316 |
} |
| 317 |
} else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { |
| 318 |
cycle_color = SDL_TRUE; |
| 319 |
consumed = 1; |
| 320 |
} else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { |
| 321 |
cycle_alpha = SDL_TRUE; |
| 322 |
consumed = 1; |
| 323 |
} else if (SDL_isdigit(*argv[i])) { |
| 324 |
num_objects = SDL_atoi(argv[i]); |
| 325 |
consumed = 1; |
| 326 |
} |
| 327 |
} |
| 328 |
if (consumed < 0) { |
| 329 |
static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", NULL }; |
| 330 |
SDLTest_CommonLogUsage(state, argv[0], options); |
| 331 |
return 1; |
| 332 |
} |
| 333 |
i += consumed; |
| 334 |
} |
| 335 |
if (!SDLTest_CommonInit(state)) { |
| 336 |
return 2; |
| 337 |
} |
| 338 |
|
| 339 |
/* Create the windows and initialize the renderers */ |
| 340 |
for (i = 0; i < state->num_windows; ++i) { |
| 341 |
SDL_Renderer *renderer = state->renderers[i]; |
| 342 |
SDL_SetRenderDrawBlendMode(renderer, blendMode); |
| 343 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 344 |
SDL_RenderClear(renderer); |
| 345 |
} |
| 346 |
|
| 347 |
srand((unsigned int)time(NULL)); |
| 348 |
|
| 349 |
/* Main render loop */ |
| 350 |
frames = 0; |
| 351 |
then = SDL_GetTicks(); |
| 352 |
done = 0; |
| 353 |
|
| 354 |
#ifdef __EMSCRIPTEN__ |
| 355 |
emscripten_set_main_loop(loop, 0, 1); |
| 356 |
#else |
| 357 |
while (!done) { |
| 358 |
++frames; |
| 359 |
loop(); |
| 360 |
} |
| 361 |
#endif |
| 362 |
|
| 363 |
SDLTest_CommonQuit(state); |
| 364 |
|
| 365 |
/* Print out some timing information */ |
| 366 |
now = SDL_GetTicks(); |
| 367 |
if (now > then) { |
| 368 |
double fps = ((double)frames * 1000) / (now - then); |
| 369 |
SDL_Log("%2.2f frames per second\n", fps); |
| 370 |
} |
| 371 |
return 0; |
| 372 |
} |
| 373 |
|
| 374 |
/* vi: set ts=4 sw=4 expandtab: */ |