test/testintersections.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
     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 "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 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     int x1, y1, x2, y2;
   102     SDL_Rect viewport;
   103 
   104     /* Query the sizes */
   105     SDL_RenderGetViewport(renderer, &viewport);
   106 
   107     SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
   108 
   109     for (i = 0; i < num_lines; ++i) {
   110         if (i == -1) {
   111             SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
   112             SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
   113             SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
   114             SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
   115         } else {
   116             SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
   117         }
   118     }
   119 }
   120 
   121 #define MAX_RECTS 16
   122 int num_rects = 0;
   123 SDL_Rect rects[MAX_RECTS];
   124 static int
   125 add_rect(int x1, int y1, int x2, int y2)
   126 {
   127     if (num_rects >= MAX_RECTS)
   128         return 0;
   129     if ((x1 == x2) || (y1 == y2))
   130         return 0;
   131 
   132     if (x1 > x2)
   133         SWAP(int, x1, x2);
   134     if (y1 > y2)
   135         SWAP(int, y1, y2);
   136 
   137     printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
   138            x2 - x1, y2 - y1);
   139 
   140     rects[num_rects].x = x1;
   141     rects[num_rects].y = y1;
   142     rects[num_rects].w = x2 - x1;
   143     rects[num_rects].h = y2 - y1;
   144 
   145     return ++num_rects;
   146 }
   147 
   148 static void
   149 DrawRects(SDL_Renderer * renderer)
   150 {
   151     SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
   152     SDL_RenderFillRects(renderer, rects, num_rects);
   153 }
   154 
   155 static void
   156 DrawRectLineIntersections(SDL_Renderer * renderer)
   157 {
   158     int i, j;
   159 
   160     SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
   161 
   162     for (i = 0; i < num_rects; i++)
   163         for (j = 0; j < num_lines; j++) {
   164             int x1, y1, x2, y2;
   165             SDL_Rect r;
   166 
   167             r = rects[i];
   168             x1 = lines[j].x;
   169             y1 = lines[j].y;
   170             x2 = lines[j].w;
   171             y2 = lines[j].h;
   172 
   173             if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
   174                 SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
   175             }
   176         }
   177 }
   178 
   179 static void
   180 DrawRectRectIntersections(SDL_Renderer * renderer)
   181 {
   182     int i, j;
   183 
   184     SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
   185 
   186     for (i = 0; i < num_rects; i++)
   187         for (j = i + 1; j < num_rects; j++) {
   188             SDL_Rect r;
   189             if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
   190                 SDL_RenderFillRect(renderer, &r);
   191             }
   192         }
   193 }
   194 
   195 int
   196 main(int argc, char *argv[])
   197 {
   198     int mouse_begin_x = -1, mouse_begin_y = -1;
   199     int i, done;
   200     SDL_Event event;
   201     Uint32 then, now, frames;
   202 
   203     /* Initialize parameters */
   204     num_objects = NUM_OBJECTS;
   205 
   206     /* Initialize test framework */
   207     state = CommonCreateState(argv, SDL_INIT_VIDEO);
   208     if (!state) {
   209         return 1;
   210     }
   211     for (i = 1; i < argc;) {
   212         int consumed;
   213 
   214         consumed = CommonArg(state, i);
   215         if (consumed == 0) {
   216             consumed = -1;
   217             if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   218                 if (argv[i + 1]) {
   219                     if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   220                         blendMode = SDL_BLENDMODE_NONE;
   221                         consumed = 2;
   222                     } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   223                         blendMode = SDL_BLENDMODE_BLEND;
   224                         consumed = 2;
   225                     } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   226                         blendMode = SDL_BLENDMODE_ADD;
   227                         consumed = 2;
   228                     } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   229                         blendMode = SDL_BLENDMODE_MOD;
   230                         consumed = 2;
   231                     }
   232                 }
   233             } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   234                 cycle_color = SDL_TRUE;
   235                 consumed = 1;
   236             } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   237                 cycle_alpha = SDL_TRUE;
   238                 consumed = 1;
   239             } else if (SDL_isdigit(*argv[i])) {
   240                 num_objects = SDL_atoi(argv[i]);
   241                 consumed = 1;
   242             }
   243         }
   244         if (consumed < 0) {
   245             fprintf(stderr,
   246                     "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   247                     argv[0], CommonUsage(state));
   248             return 1;
   249         }
   250         i += consumed;
   251     }
   252     if (!CommonInit(state)) {
   253         return 2;
   254     }
   255 
   256     /* Create the windows and initialize the renderers */
   257     for (i = 0; i < state->num_windows; ++i) {
   258         SDL_Renderer *renderer = state->renderers[i];
   259         SDL_SetRenderDrawBlendMode(renderer, blendMode);
   260         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   261         SDL_RenderClear(renderer);
   262     }
   263 
   264     srand(time(NULL));
   265 
   266     /* Main render loop */
   267     frames = 0;
   268     then = SDL_GetTicks();
   269     done = 0;
   270     while (!done) {
   271         /* Check for events */
   272         ++frames;
   273         while (SDL_PollEvent(&event)) {
   274             CommonEvent(state, &event, &done);
   275             switch (event.type) {
   276             case SDL_MOUSEBUTTONDOWN:
   277                 mouse_begin_x = event.button.x;
   278                 mouse_begin_y = event.button.y;
   279                 break;
   280             case SDL_MOUSEBUTTONUP:
   281                 if (event.button.button == 3)
   282                     add_line(mouse_begin_x, mouse_begin_y, event.button.x,
   283                              event.button.y);
   284                 if (event.button.button == 1)
   285                     add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
   286                              event.button.y);
   287                 break;
   288             case SDL_KEYDOWN:
   289                 switch (event.key.keysym.sym) {
   290                 case 'l':
   291                     if (event.key.keysym.mod & KMOD_SHIFT)
   292                         num_lines = 0;
   293                     else
   294                         add_line(rand() % 640, rand() % 480, rand() % 640,
   295                                  rand() % 480);
   296                     break;
   297                 case 'r':
   298                     if (event.key.keysym.mod & KMOD_SHIFT)
   299                         num_rects = 0;
   300                     else
   301                         add_rect(rand() % 640, rand() % 480, rand() % 640,
   302                                  rand() % 480);
   303                     break;
   304                 }
   305                 break;
   306             default:
   307                 break;
   308             }
   309         }
   310         for (i = 0; i < state->num_windows; ++i) {
   311             SDL_Renderer *renderer = state->renderers[i];
   312             SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   313             SDL_RenderClear(renderer);
   314 
   315             DrawRects(renderer);
   316             DrawPoints(renderer);
   317             DrawRectRectIntersections(renderer);
   318             DrawLines(renderer);
   319             DrawRectLineIntersections(renderer);
   320 
   321             SDL_RenderPresent(renderer);
   322         }
   323     }
   324 
   325     CommonQuit(state);
   326 
   327     /* Print out some timing information */
   328     now = SDL_GetTicks();
   329     if (now > then) {
   330         double fps = ((double) frames * 1000) / (now - then);
   331         printf("%2.2f frames per second\n", fps);
   332     }
   333     return 0;
   334 }
   335 
   336 /* vi: set ts=4 sw=4 expandtab: */