test/testintersections.c
changeset 2997 e4f025078c1c
parent 2994 7563b99e9a49
child 3371 438ba87e9578
     1.1 --- a/test/testintersections.c	Sun Jan 04 23:36:53 2009 +0000
     1.2 +++ b/test/testintersections.c	Sun Jan 04 23:41:09 2009 +0000
     1.3 @@ -67,9 +67,13 @@
     1.4  #define MAX_LINES 16
     1.5  int num_lines = 0;
     1.6  SDL_Rect lines[MAX_LINES];
     1.7 -static int add_line(int x1, int y1, int x2, int y2) {
     1.8 -    if (num_lines >= MAX_LINES) return 0;
     1.9 -    if ((x1 == x2) && (y1 == y2)) return 0;
    1.10 +static int
    1.11 +add_line(int x1, int y1, int x2, int y2)
    1.12 +{
    1.13 +    if (num_lines >= MAX_LINES)
    1.14 +        return 0;
    1.15 +    if ((x1 == x2) && (y1 == y2))
    1.16 +        return 0;
    1.17  
    1.18      printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
    1.19      lines[num_lines].x = x1;
    1.20 @@ -110,14 +114,21 @@
    1.21  #define MAX_RECTS 16
    1.22  int num_rects = 0;
    1.23  SDL_Rect rects[MAX_RECTS];
    1.24 -static int add_rect(int x1, int y1, int x2, int y2) {
    1.25 -    if (num_rects >= MAX_RECTS) return 0;
    1.26 -    if ((x1 == x2) || (y1 == y2)) return 0;
    1.27 +static int
    1.28 +add_rect(int x1, int y1, int x2, int y2)
    1.29 +{
    1.30 +    if (num_rects >= MAX_RECTS)
    1.31 +        return 0;
    1.32 +    if ((x1 == x2) || (y1 == y2))
    1.33 +        return 0;
    1.34  
    1.35 -    if (x1 > x2) SWAP(int, x1, x2);
    1.36 -    if (y1 > y2) SWAP(int, y1, y2);
    1.37 +    if (x1 > x2)
    1.38 +        SWAP(int, x1, x2);
    1.39 +    if (y1 > y2)
    1.40 +        SWAP(int, y1, y2);
    1.41  
    1.42 -    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2, x2-x1, y2-y1);
    1.43 +    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
    1.44 +           x2 - x1, y2 - y1);
    1.45  
    1.46      rects[num_rects].x = x1;
    1.47      rects[num_rects].y = y1;
    1.48 @@ -155,21 +166,21 @@
    1.49      SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    1.50  
    1.51      for (i = 0; i < num_rects; i++)
    1.52 -    for (j = 0; j < num_lines; j++) {
    1.53 -        int x1, y1, x2, y2;
    1.54 -        SDL_Rect r;
    1.55 -        
    1.56 -        r = rects[i];
    1.57 -        x1 = lines[j].x;
    1.58 -        y1 = lines[j].y;
    1.59 -        x2 = lines[j].w;
    1.60 -        y2 = lines[j].h;
    1.61 +        for (j = 0; j < num_lines; j++) {
    1.62 +            int x1, y1, x2, y2;
    1.63 +            SDL_Rect r;
    1.64  
    1.65 -        if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
    1.66 -            SDL_SetRenderDrawColor(0, 255, 55, 255);
    1.67 -            SDL_RenderLine(x1, y1, x2, y2);
    1.68 +            r = rects[i];
    1.69 +            x1 = lines[j].x;
    1.70 +            y1 = lines[j].y;
    1.71 +            x2 = lines[j].w;
    1.72 +            y2 = lines[j].h;
    1.73 +
    1.74 +            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
    1.75 +                SDL_SetRenderDrawColor(0, 255, 55, 255);
    1.76 +                SDL_RenderLine(x1, y1, x2, y2);
    1.77 +            }
    1.78          }
    1.79 -    }
    1.80  
    1.81      SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    1.82  }
    1.83 @@ -182,13 +193,13 @@
    1.84      SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    1.85  
    1.86      for (i = 0; i < num_rects; i++)
    1.87 -    for (j = i+1; j < num_rects; j++) {
    1.88 -        SDL_Rect r;
    1.89 -        if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
    1.90 -            SDL_SetRenderDrawColor(255, 200, 0, 255);
    1.91 -            SDL_RenderFill(&r);
    1.92 +        for (j = i + 1; j < num_rects; j++) {
    1.93 +            SDL_Rect r;
    1.94 +            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
    1.95 +                SDL_SetRenderDrawColor(255, 200, 0, 255);
    1.96 +                SDL_RenderFill(&r);
    1.97 +            }
    1.98          }
    1.99 -    }
   1.100  
   1.101      SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.102  }
   1.103 @@ -285,21 +296,29 @@
   1.104              case SDL_MOUSEBUTTONUP:
   1.105                  if (event.button.which == 0) {
   1.106                      if (event.button.button == 3)
   1.107 -                        add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
   1.108 +                        add_line(mouse_begin_x, mouse_begin_y, event.button.x,
   1.109 +                                 event.button.y);
   1.110                      if (event.button.button == 1)
   1.111 -                        add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
   1.112 +                        add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
   1.113 +                                 event.button.y);
   1.114                  }
   1.115                  break;
   1.116              case SDL_KEYDOWN:
   1.117                  switch (event.key.keysym.sym) {
   1.118 -                    case 'l':
   1.119 -                        if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0;
   1.120 -                        else add_line(rand()%640, rand()%480, rand()%640, rand()%480);
   1.121 -                        break;
   1.122 -                    case 'r':
   1.123 -                        if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0;
   1.124 -                        else add_rect(rand()%640, rand()%480, rand()%640, rand()%480);
   1.125 -                        break;
   1.126 +                case 'l':
   1.127 +                    if (event.key.keysym.mod & KMOD_SHIFT)
   1.128 +                        num_lines = 0;
   1.129 +                    else
   1.130 +                        add_line(rand() % 640, rand() % 480, rand() % 640,
   1.131 +                                 rand() % 480);
   1.132 +                    break;
   1.133 +                case 'r':
   1.134 +                    if (event.key.keysym.mod & KMOD_SHIFT)
   1.135 +                        num_rects = 0;
   1.136 +                    else
   1.137 +                        add_rect(rand() % 640, rand() % 480, rand() % 640,
   1.138 +                                 rand() % 480);
   1.139 +                    break;
   1.140                  }
   1.141                  break;
   1.142              case SDL_WINDOWEVENT: