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