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