test/testintersections.c
changeset 2994 7563b99e9a49
child 2997 e4f025078c1c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testintersections.c	Sun Jan 04 19:33:21 2009 +0000
     1.3 @@ -0,0 +1,342 @@
     1.4 +
     1.5 +/* Simple program:  draw as many random objects on the screen as possible */
     1.6 +
     1.7 +#include <stdlib.h>
     1.8 +#include <stdio.h>
     1.9 +#include <time.h>
    1.10 +
    1.11 +#include "common.h"
    1.12 +
    1.13 +#define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
    1.14 +#define NUM_OBJECTS	100
    1.15 +
    1.16 +static CommonState *state;
    1.17 +static int num_objects;
    1.18 +static SDL_bool cycle_color;
    1.19 +static SDL_bool cycle_alpha;
    1.20 +static int cycle_direction = 1;
    1.21 +static int current_alpha = 255;
    1.22 +static int current_color = 255;
    1.23 +static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    1.24 +
    1.25 +void
    1.26 +DrawPoints(SDL_WindowID window)
    1.27 +{
    1.28 +    int i;
    1.29 +    int x, y;
    1.30 +    int window_w, window_h;
    1.31 +
    1.32 +    /* Query the sizes */
    1.33 +    SDL_GetWindowSize(window, &window_w, &window_h);
    1.34 +
    1.35 +    SDL_SetRenderDrawBlendMode(blendMode);
    1.36 +    for (i = 0; i < num_objects * 4; ++i) {
    1.37 +        /* Cycle the color and alpha, if desired */
    1.38 +        if (cycle_color) {
    1.39 +            current_color += cycle_direction;
    1.40 +            if (current_color < 0) {
    1.41 +                current_color = 0;
    1.42 +                cycle_direction = -cycle_direction;
    1.43 +            }
    1.44 +            if (current_color > 255) {
    1.45 +                current_color = 255;
    1.46 +                cycle_direction = -cycle_direction;
    1.47 +            }
    1.48 +        }
    1.49 +        if (cycle_alpha) {
    1.50 +            current_alpha += cycle_direction;
    1.51 +            if (current_alpha < 0) {
    1.52 +                current_alpha = 0;
    1.53 +                cycle_direction = -cycle_direction;
    1.54 +            }
    1.55 +            if (current_alpha > 255) {
    1.56 +                current_alpha = 255;
    1.57 +                cycle_direction = -cycle_direction;
    1.58 +            }
    1.59 +        }
    1.60 +        SDL_SetRenderDrawColor(255, (Uint8) current_color,
    1.61 +                               (Uint8) current_color, (Uint8) current_alpha);
    1.62 +
    1.63 +        x = rand() % window_w;
    1.64 +        y = rand() % window_h;
    1.65 +        SDL_RenderPoint(x, y);
    1.66 +    }
    1.67 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
    1.68 +}
    1.69 +
    1.70 +#define MAX_LINES 16
    1.71 +int num_lines = 0;
    1.72 +SDL_Rect lines[MAX_LINES];
    1.73 +static int add_line(int x1, int y1, int x2, int y2) {
    1.74 +    if (num_lines >= MAX_LINES) return 0;
    1.75 +    if ((x1 == x2) && (y1 == y2)) return 0;
    1.76 +
    1.77 +    printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
    1.78 +    lines[num_lines].x = x1;
    1.79 +    lines[num_lines].y = y1;
    1.80 +    lines[num_lines].w = x2;
    1.81 +    lines[num_lines].h = y2;
    1.82 +
    1.83 +    return ++num_lines;
    1.84 +}
    1.85 +
    1.86 +
    1.87 +void
    1.88 +DrawLines(SDL_WindowID window)
    1.89 +{
    1.90 +    int i;
    1.91 +    int x1, y1, x2, y2;
    1.92 +    int window_w, window_h;
    1.93 +
    1.94 +    /* Query the sizes */
    1.95 +    SDL_GetWindowSize(window, &window_w, &window_h);
    1.96 +
    1.97 +    SDL_SetRenderDrawBlendMode(blendMode);
    1.98 +    for (i = 0; i < num_lines; ++i) {
    1.99 +        SDL_SetRenderDrawColor(255, 255, 255, 255);
   1.100 +
   1.101 +        if (i == -1) {
   1.102 +            SDL_RenderLine(0, 0, window_w - 1, window_h - 1);
   1.103 +            SDL_RenderLine(0, window_h - 1, window_w - 1, 0);
   1.104 +            SDL_RenderLine(0, window_h / 2, window_w - 1, window_h / 2);
   1.105 +            SDL_RenderLine(window_w / 2, 0, window_w / 2, window_h - 1);
   1.106 +        } else {
   1.107 +            SDL_RenderLine(lines[i].x, lines[i].y, lines[i].w, lines[i].h);
   1.108 +        }
   1.109 +    }
   1.110 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.111 +}
   1.112 +
   1.113 +#define MAX_RECTS 16
   1.114 +int num_rects = 0;
   1.115 +SDL_Rect rects[MAX_RECTS];
   1.116 +static int add_rect(int x1, int y1, int x2, int y2) {
   1.117 +    if (num_rects >= MAX_RECTS) return 0;
   1.118 +    if ((x1 == x2) || (y1 == y2)) return 0;
   1.119 +
   1.120 +    if (x1 > x2) SWAP(int, x1, x2);
   1.121 +    if (y1 > y2) SWAP(int, y1, y2);
   1.122 +
   1.123 +    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2, x2-x1, y2-y1);
   1.124 +
   1.125 +    rects[num_rects].x = x1;
   1.126 +    rects[num_rects].y = y1;
   1.127 +    rects[num_rects].w = x2 - x1;
   1.128 +    rects[num_rects].h = y2 - y1;
   1.129 +
   1.130 +    return ++num_rects;
   1.131 +}
   1.132 +
   1.133 +static void
   1.134 +DrawRects(SDL_WindowID window)
   1.135 +{
   1.136 +    int i;
   1.137 +    int window_w, window_h;
   1.138 +
   1.139 +    /* Query the sizes */
   1.140 +    SDL_GetWindowSize(window, &window_w, &window_h);
   1.141 +
   1.142 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.143 +    for (i = 0; i < num_rects; ++i) {
   1.144 +        SDL_SetRenderDrawColor(255, 127, 0, 255);
   1.145 +        SDL_RenderFill(&rects[i]);
   1.146 +    }
   1.147 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.148 +}
   1.149 +
   1.150 +static void
   1.151 +DrawRectLineIntersections(SDL_WindowID window)
   1.152 +{
   1.153 +    int i, j, window_w, window_h;
   1.154 +
   1.155 +    /* Query the sizes */
   1.156 +    SDL_GetWindowSize(window, &window_w, &window_h);
   1.157 +
   1.158 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.159 +
   1.160 +    for (i = 0; i < num_rects; i++)
   1.161 +    for (j = 0; j < num_lines; j++) {
   1.162 +        int x1, y1, x2, y2;
   1.163 +        SDL_Rect r;
   1.164 +        
   1.165 +        r = rects[i];
   1.166 +        x1 = lines[j].x;
   1.167 +        y1 = lines[j].y;
   1.168 +        x2 = lines[j].w;
   1.169 +        y2 = lines[j].h;
   1.170 +
   1.171 +        if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
   1.172 +            SDL_SetRenderDrawColor(0, 255, 55, 255);
   1.173 +            SDL_RenderLine(x1, y1, x2, y2);
   1.174 +        }
   1.175 +    }
   1.176 +
   1.177 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.178 +}
   1.179 +
   1.180 +static void
   1.181 +DrawRectRectIntersections(SDL_WindowID window)
   1.182 +{
   1.183 +    int i, j;
   1.184 +
   1.185 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.186 +
   1.187 +    for (i = 0; i < num_rects; i++)
   1.188 +    for (j = i+1; j < num_rects; j++) {
   1.189 +        SDL_Rect r;
   1.190 +        if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
   1.191 +            SDL_SetRenderDrawColor(255, 200, 0, 255);
   1.192 +            SDL_RenderFill(&r);
   1.193 +        }
   1.194 +    }
   1.195 +
   1.196 +    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
   1.197 +}
   1.198 +
   1.199 +int
   1.200 +main(int argc, char *argv[])
   1.201 +{
   1.202 +    int mouse_begin_x = -1, mouse_begin_y = -1;
   1.203 +    int i, done;
   1.204 +    SDL_Event event;
   1.205 +    Uint32 then, now, frames;
   1.206 +
   1.207 +    /* Initialize parameters */
   1.208 +    num_objects = NUM_OBJECTS;
   1.209 +
   1.210 +    /* Initialize test framework */
   1.211 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   1.212 +    if (!state) {
   1.213 +        return 1;
   1.214 +    }
   1.215 +    for (i = 1; i < argc;) {
   1.216 +        int consumed;
   1.217 +
   1.218 +        consumed = CommonArg(state, i);
   1.219 +        if (consumed == 0) {
   1.220 +            consumed = -1;
   1.221 +            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
   1.222 +                if (argv[i + 1]) {
   1.223 +                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
   1.224 +                        blendMode = SDL_BLENDMODE_NONE;
   1.225 +                        consumed = 2;
   1.226 +                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
   1.227 +                        blendMode = SDL_BLENDMODE_MASK;
   1.228 +                        consumed = 2;
   1.229 +                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
   1.230 +                        blendMode = SDL_BLENDMODE_BLEND;
   1.231 +                        consumed = 2;
   1.232 +                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
   1.233 +                        blendMode = SDL_BLENDMODE_ADD;
   1.234 +                        consumed = 2;
   1.235 +                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
   1.236 +                        blendMode = SDL_BLENDMODE_MOD;
   1.237 +                        consumed = 2;
   1.238 +                    }
   1.239 +                }
   1.240 +            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
   1.241 +                cycle_color = SDL_TRUE;
   1.242 +                consumed = 1;
   1.243 +            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
   1.244 +                cycle_alpha = SDL_TRUE;
   1.245 +                consumed = 1;
   1.246 +            } else if (SDL_isdigit(*argv[i])) {
   1.247 +                num_objects = SDL_atoi(argv[i]);
   1.248 +                consumed = 1;
   1.249 +            }
   1.250 +        }
   1.251 +        if (consumed < 0) {
   1.252 +            fprintf(stderr,
   1.253 +                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
   1.254 +                    argv[0], CommonUsage(state));
   1.255 +            return 1;
   1.256 +        }
   1.257 +        i += consumed;
   1.258 +    }
   1.259 +    if (!CommonInit(state)) {
   1.260 +        return 2;
   1.261 +    }
   1.262 +
   1.263 +    /* Create the windows and initialize the renderers */
   1.264 +    for (i = 0; i < state->num_windows; ++i) {
   1.265 +        SDL_SelectRenderer(state->windows[i]);
   1.266 +        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.267 +        SDL_RenderFill(NULL);
   1.268 +    }
   1.269 +
   1.270 +    srand(time(NULL));
   1.271 +
   1.272 +    /* Main render loop */
   1.273 +    frames = 0;
   1.274 +    then = SDL_GetTicks();
   1.275 +    done = 0;
   1.276 +    while (!done) {
   1.277 +        /* Check for events */
   1.278 +        ++frames;
   1.279 +        while (SDL_PollEvent(&event)) {
   1.280 +            CommonEvent(state, &event, &done);
   1.281 +            switch (event.type) {
   1.282 +            case SDL_MOUSEBUTTONDOWN:
   1.283 +                if (event.button.which == 0) {
   1.284 +                    mouse_begin_x = event.button.x;
   1.285 +                    mouse_begin_y = event.button.y;
   1.286 +                }
   1.287 +                break;
   1.288 +            case SDL_MOUSEBUTTONUP:
   1.289 +                if (event.button.which == 0) {
   1.290 +                    if (event.button.button == 3)
   1.291 +                        add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
   1.292 +                    if (event.button.button == 1)
   1.293 +                        add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
   1.294 +                }
   1.295 +                break;
   1.296 +            case SDL_KEYDOWN:
   1.297 +                switch (event.key.keysym.sym) {
   1.298 +                    case 'l':
   1.299 +                        if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0;
   1.300 +                        else add_line(rand()%640, rand()%480, rand()%640, rand()%480);
   1.301 +                        break;
   1.302 +                    case 'r':
   1.303 +                        if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0;
   1.304 +                        else add_rect(rand()%640, rand()%480, rand()%640, rand()%480);
   1.305 +                        break;
   1.306 +                }
   1.307 +                break;
   1.308 +            case SDL_WINDOWEVENT:
   1.309 +                switch (event.window.event) {
   1.310 +                case SDL_WINDOWEVENT_EXPOSED:
   1.311 +                    SDL_SelectRenderer(event.window.windowID);
   1.312 +                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.313 +                    SDL_RenderFill(NULL);
   1.314 +                    break;
   1.315 +                }
   1.316 +                break;
   1.317 +            default:
   1.318 +                break;
   1.319 +            }
   1.320 +        }
   1.321 +        for (i = 0; i < state->num_windows; ++i) {
   1.322 +            SDL_SelectRenderer(state->windows[i]);
   1.323 +            SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.324 +            SDL_RenderFill(NULL);
   1.325 +
   1.326 +            DrawRects(state->windows[i]);
   1.327 +            DrawPoints(state->windows[i]);
   1.328 +            DrawRectRectIntersections(state->windows[i]);
   1.329 +            DrawLines(state->windows[i]);
   1.330 +            DrawRectLineIntersections(state->windows[i]);
   1.331 +
   1.332 +            SDL_RenderPresent();
   1.333 +        }
   1.334 +    }
   1.335 +
   1.336 +    /* Print out some timing information */
   1.337 +    now = SDL_GetTicks();
   1.338 +    if (now > then) {
   1.339 +        double fps = ((double) frames * 1000) / (now - then);
   1.340 +        printf("%2.2f frames per second\n", fps);
   1.341 +    }
   1.342 +    return 0;
   1.343 +}
   1.344 +
   1.345 +/* vi: set ts=4 sw=4 expandtab: */