Atlas - testdraw.c
Home / ext / SDL / test Lines: 1 | Size: 10109 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2025 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 <SDL3/SDL.h> 16#include <SDL3/SDL_main.h> 17#include <SDL3/SDL_test.h> 18 19#ifdef SDL_PLATFORM_EMSCRIPTEN 20#include <emscripten/emscripten.h> 21#endif 22 23#define NUM_OBJECTS 100 24 25static SDLTest_CommonState *state; 26static int num_objects; 27static bool cycle_color; 28static bool cycle_alpha; 29static int cycle_direction = 1; 30static int current_alpha = 255; 31static int current_color = 255; 32static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; 33static Uint64 next_fps_check; 34static Uint32 frames; 35static const int fps_check_delay = 5000; 36 37static int done; 38 39static void DrawPoints(SDL_Renderer *renderer) 40{ 41 int i; 42 float x, y; 43 SDL_Rect viewport; 44 45 /* Query the sizes */ 46 SDL_GetRenderViewport(renderer, &viewport); 47 48 for (i = 0; i < num_objects * 4; ++i) { 49 /* Cycle the color and alpha, if desired */ 50 if (cycle_color) { 51 current_color += cycle_direction; 52 if (current_color < 0) { 53 current_color = 0; 54 cycle_direction = -cycle_direction; 55 } 56 if (current_color > 255) { 57 current_color = 255; 58 cycle_direction = -cycle_direction; 59 } 60 } 61 if (cycle_alpha) { 62 current_alpha += cycle_direction; 63 if (current_alpha < 0) { 64 current_alpha = 0; 65 cycle_direction = -cycle_direction; 66 } 67 if (current_alpha > 255) { 68 current_alpha = 255; 69 cycle_direction = -cycle_direction; 70 } 71 } 72 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, 73 (Uint8)current_color, (Uint8)current_alpha); 74 75 x = (float)SDL_rand(viewport.w); 76 y = (float)SDL_rand(viewport.h); 77 SDL_RenderPoint(renderer, x, y); 78 } 79} 80 81static void DrawLines(SDL_Renderer *renderer) 82{ 83 int i; 84 float x1, y1, x2, y2; 85 SDL_Rect viewport; 86 87 /* Query the sizes */ 88 SDL_GetRenderViewport(renderer, &viewport); 89 90 for (i = 0; i < num_objects; ++i) { 91 /* Cycle the color and alpha, if desired */ 92 if (cycle_color) { 93 current_color += cycle_direction; 94 if (current_color < 0) { 95 current_color = 0; 96 cycle_direction = -cycle_direction; 97 } 98 if (current_color > 255) { 99 current_color = 255; 100 cycle_direction = -cycle_direction; 101 } 102 } 103 if (cycle_alpha) { 104 current_alpha += cycle_direction; 105 if (current_alpha < 0) { 106 current_alpha = 0; 107 cycle_direction = -cycle_direction; 108 } 109 if (current_alpha > 255) { 110 current_alpha = 255; 111 cycle_direction = -cycle_direction; 112 } 113 } 114 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, 115 (Uint8)current_color, (Uint8)current_alpha); 116 117 if (i == 0) { 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)); 122 } else { 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); 128 } 129 } 130} 131 132static void DrawRects(SDL_Renderer *renderer) 133{ 134 int i; 135 SDL_FRect rect; 136 SDL_Rect viewport; 137 138 /* Query the sizes */ 139 SDL_GetRenderViewport(renderer, &viewport); 140 141 for (i = 0; i < num_objects / 4; ++i) { 142 /* Cycle the color and alpha, if desired */ 143 if (cycle_color) { 144 current_color += cycle_direction; 145 if (current_color < 0) { 146 current_color = 0; 147 cycle_direction = -cycle_direction; 148 } 149 if (current_color > 255) { 150 current_color = 255; 151 cycle_direction = -cycle_direction; 152 } 153 } 154 if (cycle_alpha) { 155 current_alpha += cycle_direction; 156 if (current_alpha < 0) { 157 current_alpha = 0; 158 cycle_direction = -cycle_direction; 159 } 160 if (current_alpha > 255) { 161 current_alpha = 255; 162 cycle_direction = -cycle_direction; 163 } 164 } 165 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color, 166 (Uint8)current_color, (Uint8)current_alpha); 167 168 rect.w = (float)SDL_rand(viewport.h / 2); 169 rect.h = (float)SDL_rand(viewport.h / 2); 170 rect.x = (SDL_rand(viewport.w * 2) - viewport.w) - (rect.w / 2); 171 rect.y = (SDL_rand(viewport.h * 2) - viewport.h) - (rect.h / 2); 172 SDL_RenderFillRect(renderer, &rect); 173 } 174} 175 176static void loop(void) 177{ 178 Uint64 now; 179 int i; 180 SDL_Event event; 181 182 /* Check for events */ 183 while (SDL_PollEvent(&event)) { 184 SDLTest_CommonEvent(state, &event, &done); 185 } 186 for (i = 0; i < state->num_windows; ++i) { 187 SDL_Renderer *renderer = state->renderers[i]; 188 if (state->windows[i] == NULL) { 189 continue; 190 } 191 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 192 SDL_RenderClear(renderer); 193 194 DrawRects(renderer); 195 DrawLines(renderer); 196 DrawPoints(renderer); 197 198 SDL_RenderPresent(renderer); 199 } 200#ifdef SDL_PLATFORM_EMSCRIPTEN 201 if (done) { 202 emscripten_cancel_main_loop(); 203 } 204#endif 205 frames++; 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", fps); 212 next_fps_check = now + fps_check_delay; 213 frames = 0; 214 } 215} 216 217int main(int argc, char *argv[]) 218{ 219 int i; 220 /* Initialize parameters */ 221 num_objects = NUM_OBJECTS; 222 223 /* Initialize test framework */ 224 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 225 if (!state) { 226 return 1; 227 } 228 229 for (i = 1; i < argc;) { 230 int consumed; 231 232 consumed = SDLTest_CommonArg(state, i); 233 if (consumed == 0) { 234 consumed = -1; 235 if (SDL_strcasecmp(argv[i], "--blend") == 0) { 236 if (argv[i + 1]) { 237 if (SDL_strcasecmp(argv[i + 1], "none") == 0) { 238 blendMode = SDL_BLENDMODE_NONE; 239 consumed = 2; 240 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { 241 blendMode = SDL_BLENDMODE_BLEND; 242 consumed = 2; 243 } else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) { 244 blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED; 245 consumed = 2; 246 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { 247 blendMode = SDL_BLENDMODE_ADD; 248 consumed = 2; 249 } else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) { 250 blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED; 251 consumed = 2; 252 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { 253 blendMode = SDL_BLENDMODE_MOD; 254 consumed = 2; 255 } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) { 256 blendMode = SDL_BLENDMODE_MUL; 257 consumed = 2; 258 } 259 } 260 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) { 261 cycle_color = true; 262 consumed = 1; 263 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) { 264 cycle_alpha = true; 265 consumed = 1; 266 } else if (SDL_isdigit(*argv[i])) { 267 num_objects = SDL_atoi(argv[i]); 268 consumed = 1; 269 } 270 } 271 if (consumed < 0) { 272 static const char *options[] = { 273 "[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]", 274 "[--cyclecolor]", 275 "[--cyclealpha]", 276 "[num_objects]", 277 NULL 278 }; 279 SDLTest_CommonLogUsage(state, argv[0], options); 280 return 1; 281 } 282 i += consumed; 283 } 284 if (!SDLTest_CommonInit(state)) { 285 return 2; 286 } 287 288 /* Create the windows and initialize the renderers */ 289 for (i = 0; i < state->num_windows; ++i) { 290 SDL_Renderer *renderer = state->renderers[i]; 291 SDL_SetRenderDrawBlendMode(renderer, blendMode); 292 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 293 SDL_RenderClear(renderer); 294 } 295 296 /* Main render loop */ 297 frames = 0; 298 next_fps_check = SDL_GetTicks() + fps_check_delay; 299 done = 0; 300 301#ifdef SDL_PLATFORM_EMSCRIPTEN 302 emscripten_set_main_loop(loop, 0, 1); 303#else 304 while (!done) { 305 loop(); 306 } 307#endif 308 309 SDLTest_CleanupTextDrawing(); 310 SDLTest_CommonQuit(state); 311 312 return 0; 313} 314[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.