test/testintersections.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 13 Jul 2013 21:13:09 +0200
changeset 7437 964e8876ad3b
parent 7191 75360622e65f
child 7517 965d57022c01
permissions -rw-r--r--
Fixed compiler warnings in test programs.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@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     printf("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     printf("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     /* Initialize parameters */
   203     num_objects = NUM_OBJECTS;
   204 
   205     /* Initialize test framework */
   206     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   207     if (!state) {
   208         return 1;
   209     }
   210     for (i = 1; i < argc;) {
   211         int consumed;
   212 
   213         consumed = SDLTest_CommonArg(state, i);
   214         if (consumed == 0) {
   215             consumed = -1;
   216             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   217                 if (argv[i + 1]) {
   218                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   219                         blendMode = SDL_BLENDMODE_NONE;
   220                         consumed = 2;
   221                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   222                         blendMode = SDL_BLENDMODE_BLEND;
   223                         consumed = 2;
   224                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   225                         blendMode = SDL_BLENDMODE_ADD;
   226                         consumed = 2;
   227                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   228                         blendMode = SDL_BLENDMODE_MOD;
   229                         consumed = 2;
   230                     }
   231                 }
   232             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   233                 cycle_color = SDL_TRUE;
   234                 consumed = 1;
   235             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   236                 cycle_alpha = SDL_TRUE;
   237                 consumed = 1;
   238             } else if (SDL_isdigit(*argv[i])) {
   239                 num_objects = SDL_atoi(argv[i]);
   240                 consumed = 1;
   241             }
   242         }
   243         if (consumed < 0) {
   244             fprintf(stderr,
   245                     "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   246                     argv[0], SDLTest_CommonUsage(state));
   247             return 1;
   248         }
   249         i += consumed;
   250     }
   251     if (!SDLTest_CommonInit(state)) {
   252         return 2;
   253     }
   254 
   255     /* Create the windows and initialize the renderers */
   256     for (i = 0; i < state->num_windows; ++i) {
   257         SDL_Renderer *renderer = state->renderers[i];
   258         SDL_SetRenderDrawBlendMode(renderer, blendMode);
   259         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   260         SDL_RenderClear(renderer);
   261     }
   262 
   263     srand(time(NULL));
   264 
   265     /* Main render loop */
   266     frames = 0;
   267     then = SDL_GetTicks();
   268     done = 0;
   269     while (!done) {
   270         /* Check for events */
   271         ++frames;
   272         while (SDL_PollEvent(&event)) {
   273             SDLTest_CommonEvent(state, &event, &done);
   274             switch (event.type) {
   275             case SDL_MOUSEBUTTONDOWN:
   276                 mouse_begin_x = event.button.x;
   277                 mouse_begin_y = event.button.y;
   278                 break;
   279             case SDL_MOUSEBUTTONUP:
   280                 if (event.button.button == 3)
   281                     add_line(mouse_begin_x, mouse_begin_y, event.button.x,
   282                              event.button.y);
   283                 if (event.button.button == 1)
   284                     add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
   285                              event.button.y);
   286                 break;
   287             case SDL_KEYDOWN:
   288                 switch (event.key.keysym.sym) {
   289                 case 'l':
   290                     if (event.key.keysym.mod & KMOD_SHIFT)
   291                         num_lines = 0;
   292                     else
   293                         add_line(rand() % 640, rand() % 480, rand() % 640,
   294                                  rand() % 480);
   295                     break;
   296                 case 'r':
   297                     if (event.key.keysym.mod & KMOD_SHIFT)
   298                         num_rects = 0;
   299                     else
   300                         add_rect(rand() % 640, rand() % 480, rand() % 640,
   301                                  rand() % 480);
   302                     break;
   303                 }
   304                 break;
   305             default:
   306                 break;
   307             }
   308         }
   309         for (i = 0; i < state->num_windows; ++i) {
   310             SDL_Renderer *renderer = state->renderers[i];
   311             SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   312             SDL_RenderClear(renderer);
   313 
   314             DrawRects(renderer);
   315             DrawPoints(renderer);
   316             DrawRectRectIntersections(renderer);
   317             DrawLines(renderer);
   318             DrawRectLineIntersections(renderer);
   319 
   320             SDL_RenderPresent(renderer);
   321         }
   322     }
   323 
   324     SDLTest_CommonQuit(state);
   325 
   326     /* Print out some timing information */
   327     now = SDL_GetTicks();
   328     if (now > then) {
   329         double fps = ((double) frames * 1000) / (now - then);
   330         printf("%2.2f frames per second\n", fps);
   331     }
   332     return 0;
   333 }
   334 
   335 /* vi: set ts=4 sw=4 expandtab: */