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