test/testintersections.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jan 2009 04:05:28 +0000
changeset 3017 3272431eeee2
parent 2997 e4f025078c1c
child 3371 438ba87e9578
permissions -rw-r--r--
Added testresample.c
     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_WindowID 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_RenderPoint(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_WindowID 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_RenderLine(0, 0, window_w - 1, window_h - 1);
   104             SDL_RenderLine(0, window_h - 1, window_w - 1, 0);
   105             SDL_RenderLine(0, window_h / 2, window_w - 1, window_h / 2);
   106             SDL_RenderLine(window_w / 2, 0, window_w / 2, window_h - 1);
   107         } else {
   108             SDL_RenderLine(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_WindowID 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_RenderFill(&rects[i]);
   154     }
   155     SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   156 }
   157 
   158 static void
   159 DrawRectLineIntersections(SDL_WindowID 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_RenderLine(x1, y1, x2, y2);
   182             }
   183         }
   184 
   185     SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   186 }
   187 
   188 static void
   189 DrawRectRectIntersections(SDL_WindowID 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_RenderFill(&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], "mask") == 0) {
   235                         blendMode = SDL_BLENDMODE_MASK;
   236                         consumed = 2;
   237                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   238                         blendMode = SDL_BLENDMODE_BLEND;
   239                         consumed = 2;
   240                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   241                         blendMode = SDL_BLENDMODE_ADD;
   242                         consumed = 2;
   243                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   244                         blendMode = SDL_BLENDMODE_MOD;
   245                         consumed = 2;
   246                     }
   247                 }
   248             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   249                 cycle_color = SDL_TRUE;
   250                 consumed = 1;
   251             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   252                 cycle_alpha = SDL_TRUE;
   253                 consumed = 1;
   254             } else if (SDL_isdigit(*argv[i])) {
   255                 num_objects = SDL_atoi(argv[i]);
   256                 consumed = 1;
   257             }
   258         }
   259         if (consumed < 0) {
   260             fprintf(stderr,
   261                     "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   262                     argv[0], CommonUsage(state));
   263             return 1;
   264         }
   265         i += consumed;
   266     }
   267     if (!CommonInit(state)) {
   268         return 2;
   269     }
   270 
   271     /* Create the windows and initialize the renderers */
   272     for (i = 0; i < state->num_windows; ++i) {
   273         SDL_SelectRenderer(state->windows[i]);
   274         SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   275         SDL_RenderFill(NULL);
   276     }
   277 
   278     srand(time(NULL));
   279 
   280     /* Main render loop */
   281     frames = 0;
   282     then = SDL_GetTicks();
   283     done = 0;
   284     while (!done) {
   285         /* Check for events */
   286         ++frames;
   287         while (SDL_PollEvent(&event)) {
   288             CommonEvent(state, &event, &done);
   289             switch (event.type) {
   290             case SDL_MOUSEBUTTONDOWN:
   291                 if (event.button.which == 0) {
   292                     mouse_begin_x = event.button.x;
   293                     mouse_begin_y = event.button.y;
   294                 }
   295                 break;
   296             case SDL_MOUSEBUTTONUP:
   297                 if (event.button.which == 0) {
   298                     if (event.button.button == 3)
   299                         add_line(mouse_begin_x, mouse_begin_y, event.button.x,
   300                                  event.button.y);
   301                     if (event.button.button == 1)
   302                         add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
   303                                  event.button.y);
   304                 }
   305                 break;
   306             case SDL_KEYDOWN:
   307                 switch (event.key.keysym.sym) {
   308                 case 'l':
   309                     if (event.key.keysym.mod & KMOD_SHIFT)
   310                         num_lines = 0;
   311                     else
   312                         add_line(rand() % 640, rand() % 480, rand() % 640,
   313                                  rand() % 480);
   314                     break;
   315                 case 'r':
   316                     if (event.key.keysym.mod & KMOD_SHIFT)
   317                         num_rects = 0;
   318                     else
   319                         add_rect(rand() % 640, rand() % 480, rand() % 640,
   320                                  rand() % 480);
   321                     break;
   322                 }
   323                 break;
   324             case SDL_WINDOWEVENT:
   325                 switch (event.window.event) {
   326                 case SDL_WINDOWEVENT_EXPOSED:
   327                     SDL_SelectRenderer(event.window.windowID);
   328                     SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   329                     SDL_RenderFill(NULL);
   330                     break;
   331                 }
   332                 break;
   333             default:
   334                 break;
   335             }
   336         }
   337         for (i = 0; i < state->num_windows; ++i) {
   338             SDL_SelectRenderer(state->windows[i]);
   339             SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   340             SDL_RenderFill(NULL);
   341 
   342             DrawRects(state->windows[i]);
   343             DrawPoints(state->windows[i]);
   344             DrawRectRectIntersections(state->windows[i]);
   345             DrawLines(state->windows[i]);
   346             DrawRectLineIntersections(state->windows[i]);
   347 
   348             SDL_RenderPresent();
   349         }
   350     }
   351 
   352     /* Print out some timing information */
   353     now = SDL_GetTicks();
   354     if (now > then) {
   355         double fps = ((double) frames * 1000) / (now - then);
   356         printf("%2.2f frames per second\n", fps);
   357     }
   358     return 0;
   359 }
   360 
   361 /* vi: set ts=4 sw=4 expandtab: */