test/testintersections.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 23 Dec 2009 01:55:00 +0000
changeset 3596 f638ded38b8a
parent 3536 0267b8b1595c
child 3685 64ce267332c6
permissions -rw-r--r--
Added SDL_RenderClear() as a fast method of clearing the screen to the drawing color.
Renamed SDL_RenderPoint() and SDL_RenderLine() to SDL_RenderDrawPoint() and SDL_RenderDrawLine().
Added API for rectangle drawing (as opposed to filling)
Added placeholder API functions for circles and ellipses ... I'm not sure whether these will stay.
Optimized software line drawing quite a bit.
Added support for Wu's anti-aliased line drawing, currently disabled by default.
slouken@2994
     1
slouken@2994
     2
/* Simple program:  draw as many random objects on the screen as possible */
slouken@2994
     3
slouken@2994
     4
#include <stdlib.h>
slouken@2994
     5
#include <stdio.h>
slouken@2994
     6
#include <time.h>
slouken@2994
     7
slouken@2994
     8
#include "common.h"
slouken@2994
     9
slouken@2994
    10
#define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
slouken@2994
    11
#define NUM_OBJECTS	100
slouken@2994
    12
slouken@2994
    13
static CommonState *state;
slouken@2994
    14
static int num_objects;
slouken@2994
    15
static SDL_bool cycle_color;
slouken@2994
    16
static SDL_bool cycle_alpha;
slouken@2994
    17
static int cycle_direction = 1;
slouken@2994
    18
static int current_alpha = 255;
slouken@2994
    19
static int current_color = 255;
slouken@2994
    20
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
slouken@2994
    21
slouken@2994
    22
void
slouken@2994
    23
DrawPoints(SDL_WindowID window)
slouken@2994
    24
{
slouken@2994
    25
    int i;
slouken@2994
    26
    int x, y;
slouken@2994
    27
    int window_w, window_h;
slouken@2994
    28
slouken@2994
    29
    /* Query the sizes */
slouken@2994
    30
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
    31
slouken@2994
    32
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2994
    33
    for (i = 0; i < num_objects * 4; ++i) {
slouken@2994
    34
        /* Cycle the color and alpha, if desired */
slouken@2994
    35
        if (cycle_color) {
slouken@2994
    36
            current_color += cycle_direction;
slouken@2994
    37
            if (current_color < 0) {
slouken@2994
    38
                current_color = 0;
slouken@2994
    39
                cycle_direction = -cycle_direction;
slouken@2994
    40
            }
slouken@2994
    41
            if (current_color > 255) {
slouken@2994
    42
                current_color = 255;
slouken@2994
    43
                cycle_direction = -cycle_direction;
slouken@2994
    44
            }
slouken@2994
    45
        }
slouken@2994
    46
        if (cycle_alpha) {
slouken@2994
    47
            current_alpha += cycle_direction;
slouken@2994
    48
            if (current_alpha < 0) {
slouken@2994
    49
                current_alpha = 0;
slouken@2994
    50
                cycle_direction = -cycle_direction;
slouken@2994
    51
            }
slouken@2994
    52
            if (current_alpha > 255) {
slouken@2994
    53
                current_alpha = 255;
slouken@2994
    54
                cycle_direction = -cycle_direction;
slouken@2994
    55
            }
slouken@2994
    56
        }
slouken@2994
    57
        SDL_SetRenderDrawColor(255, (Uint8) current_color,
slouken@2994
    58
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2994
    59
slouken@2994
    60
        x = rand() % window_w;
slouken@2994
    61
        y = rand() % window_h;
slouken@3596
    62
        SDL_RenderDrawPoint(x, y);
slouken@2994
    63
    }
slouken@2994
    64
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
    65
}
slouken@2994
    66
slouken@2994
    67
#define MAX_LINES 16
slouken@2994
    68
int num_lines = 0;
slouken@2994
    69
SDL_Rect lines[MAX_LINES];
slouken@2997
    70
static int
slouken@2997
    71
add_line(int x1, int y1, int x2, int y2)
slouken@2997
    72
{
slouken@2997
    73
    if (num_lines >= MAX_LINES)
slouken@2997
    74
        return 0;
slouken@2997
    75
    if ((x1 == x2) && (y1 == y2))
slouken@2997
    76
        return 0;
slouken@2994
    77
slouken@2994
    78
    printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
slouken@2994
    79
    lines[num_lines].x = x1;
slouken@2994
    80
    lines[num_lines].y = y1;
slouken@2994
    81
    lines[num_lines].w = x2;
slouken@2994
    82
    lines[num_lines].h = y2;
slouken@2994
    83
slouken@2994
    84
    return ++num_lines;
slouken@2994
    85
}
slouken@2994
    86
slouken@2994
    87
slouken@2994
    88
void
slouken@2994
    89
DrawLines(SDL_WindowID window)
slouken@2994
    90
{
slouken@2994
    91
    int i;
slouken@2994
    92
    int x1, y1, x2, y2;
slouken@2994
    93
    int window_w, window_h;
slouken@2994
    94
slouken@2994
    95
    /* Query the sizes */
slouken@2994
    96
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
    97
slouken@2994
    98
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2994
    99
    for (i = 0; i < num_lines; ++i) {
slouken@2994
   100
        SDL_SetRenderDrawColor(255, 255, 255, 255);
slouken@2994
   101
slouken@2994
   102
        if (i == -1) {
slouken@3596
   103
            SDL_RenderDrawLine(0, 0, window_w - 1, window_h - 1);
slouken@3596
   104
            SDL_RenderDrawLine(0, window_h - 1, window_w - 1, 0);
slouken@3596
   105
            SDL_RenderDrawLine(0, window_h / 2, window_w - 1, window_h / 2);
slouken@3596
   106
            SDL_RenderDrawLine(window_w / 2, 0, window_w / 2, window_h - 1);
slouken@2994
   107
        } else {
slouken@3596
   108
            SDL_RenderDrawLine(lines[i].x, lines[i].y, lines[i].w, lines[i].h);
slouken@2994
   109
        }
slouken@2994
   110
    }
slouken@2994
   111
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   112
}
slouken@2994
   113
slouken@2994
   114
#define MAX_RECTS 16
slouken@2994
   115
int num_rects = 0;
slouken@2994
   116
SDL_Rect rects[MAX_RECTS];
slouken@2997
   117
static int
slouken@2997
   118
add_rect(int x1, int y1, int x2, int y2)
slouken@2997
   119
{
slouken@2997
   120
    if (num_rects >= MAX_RECTS)
slouken@2997
   121
        return 0;
slouken@2997
   122
    if ((x1 == x2) || (y1 == y2))
slouken@2997
   123
        return 0;
slouken@2994
   124
slouken@2997
   125
    if (x1 > x2)
slouken@2997
   126
        SWAP(int, x1, x2);
slouken@2997
   127
    if (y1 > y2)
slouken@2997
   128
        SWAP(int, y1, y2);
slouken@2994
   129
slouken@2997
   130
    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
slouken@2997
   131
           x2 - x1, y2 - y1);
slouken@2994
   132
slouken@2994
   133
    rects[num_rects].x = x1;
slouken@2994
   134
    rects[num_rects].y = y1;
slouken@2994
   135
    rects[num_rects].w = x2 - x1;
slouken@2994
   136
    rects[num_rects].h = y2 - y1;
slouken@2994
   137
slouken@2994
   138
    return ++num_rects;
slouken@2994
   139
}
slouken@2994
   140
slouken@2994
   141
static void
slouken@2994
   142
DrawRects(SDL_WindowID window)
slouken@2994
   143
{
slouken@2994
   144
    int i;
slouken@2994
   145
    int window_w, window_h;
slouken@2994
   146
slouken@2994
   147
    /* Query the sizes */
slouken@2994
   148
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
   149
slouken@2994
   150
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   151
    for (i = 0; i < num_rects; ++i) {
slouken@2994
   152
        SDL_SetRenderDrawColor(255, 127, 0, 255);
slouken@3596
   153
        SDL_RenderFillRect(&rects[i]);
slouken@2994
   154
    }
slouken@2994
   155
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   156
}
slouken@2994
   157
slouken@2994
   158
static void
slouken@2994
   159
DrawRectLineIntersections(SDL_WindowID window)
slouken@2994
   160
{
slouken@2994
   161
    int i, j, window_w, window_h;
slouken@2994
   162
slouken@2994
   163
    /* Query the sizes */
slouken@2994
   164
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
   165
slouken@2994
   166
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   167
slouken@2994
   168
    for (i = 0; i < num_rects; i++)
slouken@2997
   169
        for (j = 0; j < num_lines; j++) {
slouken@2997
   170
            int x1, y1, x2, y2;
slouken@2997
   171
            SDL_Rect r;
slouken@2994
   172
slouken@2997
   173
            r = rects[i];
slouken@2997
   174
            x1 = lines[j].x;
slouken@2997
   175
            y1 = lines[j].y;
slouken@2997
   176
            x2 = lines[j].w;
slouken@2997
   177
            y2 = lines[j].h;
slouken@2997
   178
slouken@2997
   179
            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
slouken@2997
   180
                SDL_SetRenderDrawColor(0, 255, 55, 255);
slouken@3596
   181
                SDL_RenderDrawLine(x1, y1, x2, y2);
slouken@2997
   182
            }
slouken@2994
   183
        }
slouken@2994
   184
slouken@2994
   185
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   186
}
slouken@2994
   187
slouken@2994
   188
static void
slouken@2994
   189
DrawRectRectIntersections(SDL_WindowID window)
slouken@2994
   190
{
slouken@2994
   191
    int i, j;
slouken@2994
   192
slouken@2994
   193
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   194
slouken@2994
   195
    for (i = 0; i < num_rects; i++)
slouken@2997
   196
        for (j = i + 1; j < num_rects; j++) {
slouken@2997
   197
            SDL_Rect r;
slouken@2997
   198
            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
slouken@2997
   199
                SDL_SetRenderDrawColor(255, 200, 0, 255);
slouken@3596
   200
                SDL_RenderFillRect(&r);
slouken@2997
   201
            }
slouken@2994
   202
        }
slouken@2994
   203
slouken@2994
   204
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2994
   205
}
slouken@2994
   206
slouken@2994
   207
int
slouken@2994
   208
main(int argc, char *argv[])
slouken@2994
   209
{
slouken@2994
   210
    int mouse_begin_x = -1, mouse_begin_y = -1;
slouken@2994
   211
    int i, done;
slouken@2994
   212
    SDL_Event event;
slouken@2994
   213
    Uint32 then, now, frames;
slouken@2994
   214
slouken@2994
   215
    /* Initialize parameters */
slouken@2994
   216
    num_objects = NUM_OBJECTS;
slouken@2994
   217
slouken@2994
   218
    /* Initialize test framework */
slouken@2994
   219
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2994
   220
    if (!state) {
slouken@2994
   221
        return 1;
slouken@2994
   222
    }
slouken@2994
   223
    for (i = 1; i < argc;) {
slouken@2994
   224
        int consumed;
slouken@2994
   225
slouken@2994
   226
        consumed = CommonArg(state, i);
slouken@2994
   227
        if (consumed == 0) {
slouken@2994
   228
            consumed = -1;
slouken@2994
   229
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2994
   230
                if (argv[i + 1]) {
slouken@2994
   231
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2994
   232
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2994
   233
                        consumed = 2;
slouken@2994
   234
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2994
   235
                        blendMode = SDL_BLENDMODE_MASK;
slouken@2994
   236
                        consumed = 2;
slouken@2994
   237
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2994
   238
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2994
   239
                        consumed = 2;
slouken@2994
   240
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2994
   241
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2994
   242
                        consumed = 2;
slouken@2994
   243
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2994
   244
                        blendMode = SDL_BLENDMODE_MOD;
slouken@2994
   245
                        consumed = 2;
slouken@2994
   246
                    }
slouken@2994
   247
                }
slouken@2994
   248
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2994
   249
                cycle_color = SDL_TRUE;
slouken@2994
   250
                consumed = 1;
slouken@2994
   251
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2994
   252
                cycle_alpha = SDL_TRUE;
slouken@2994
   253
                consumed = 1;
slouken@2994
   254
            } else if (SDL_isdigit(*argv[i])) {
slouken@2994
   255
                num_objects = SDL_atoi(argv[i]);
slouken@2994
   256
                consumed = 1;
slouken@2994
   257
            }
slouken@2994
   258
        }
slouken@2994
   259
        if (consumed < 0) {
slouken@2994
   260
            fprintf(stderr,
slouken@2994
   261
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2994
   262
                    argv[0], CommonUsage(state));
slouken@2994
   263
            return 1;
slouken@2994
   264
        }
slouken@2994
   265
        i += consumed;
slouken@2994
   266
    }
slouken@2994
   267
    if (!CommonInit(state)) {
slouken@2994
   268
        return 2;
slouken@2994
   269
    }
slouken@2994
   270
slouken@2994
   271
    /* Create the windows and initialize the renderers */
slouken@2994
   272
    for (i = 0; i < state->num_windows; ++i) {
slouken@2994
   273
        SDL_SelectRenderer(state->windows[i]);
slouken@2994
   274
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
   275
        SDL_RenderClear();
slouken@2994
   276
    }
slouken@2994
   277
slouken@2994
   278
    srand(time(NULL));
slouken@2994
   279
slouken@2994
   280
    /* Main render loop */
slouken@2994
   281
    frames = 0;
slouken@2994
   282
    then = SDL_GetTicks();
slouken@2994
   283
    done = 0;
slouken@2994
   284
    while (!done) {
slouken@2994
   285
        /* Check for events */
slouken@2994
   286
        ++frames;
slouken@2994
   287
        while (SDL_PollEvent(&event)) {
slouken@2994
   288
            CommonEvent(state, &event, &done);
slouken@2994
   289
            switch (event.type) {
slouken@2994
   290
            case SDL_MOUSEBUTTONDOWN:
slouken@2994
   291
                if (event.button.which == 0) {
slouken@2994
   292
                    mouse_begin_x = event.button.x;
slouken@2994
   293
                    mouse_begin_y = event.button.y;
slouken@2994
   294
                }
slouken@2994
   295
                break;
slouken@2994
   296
            case SDL_MOUSEBUTTONUP:
slouken@2994
   297
                if (event.button.which == 0) {
slouken@2994
   298
                    if (event.button.button == 3)
slouken@2997
   299
                        add_line(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@2997
   300
                                 event.button.y);
slouken@2994
   301
                    if (event.button.button == 1)
slouken@2997
   302
                        add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@2997
   303
                                 event.button.y);
slouken@2994
   304
                }
slouken@2994
   305
                break;
slouken@2994
   306
            case SDL_KEYDOWN:
slouken@2994
   307
                switch (event.key.keysym.sym) {
slouken@2997
   308
                case 'l':
slouken@2997
   309
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   310
                        num_lines = 0;
slouken@2997
   311
                    else
slouken@2997
   312
                        add_line(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   313
                                 rand() % 480);
slouken@2997
   314
                    break;
slouken@2997
   315
                case 'r':
slouken@2997
   316
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   317
                        num_rects = 0;
slouken@2997
   318
                    else
slouken@2997
   319
                        add_rect(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   320
                                 rand() % 480);
slouken@2997
   321
                    break;
slouken@2994
   322
                }
slouken@2994
   323
                break;
slouken@2994
   324
            case SDL_WINDOWEVENT:
slouken@2994
   325
                switch (event.window.event) {
slouken@2994
   326
                case SDL_WINDOWEVENT_EXPOSED:
slouken@2994
   327
                    SDL_SelectRenderer(event.window.windowID);
slouken@2994
   328
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
   329
                    SDL_RenderClear();
slouken@2994
   330
                    break;
slouken@2994
   331
                }
slouken@2994
   332
                break;
slouken@2994
   333
            default:
slouken@2994
   334
                break;
slouken@2994
   335
            }
slouken@2994
   336
        }
slouken@2994
   337
        for (i = 0; i < state->num_windows; ++i) {
slouken@2994
   338
            SDL_SelectRenderer(state->windows[i]);
slouken@2994
   339
            SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3596
   340
            SDL_RenderClear();
slouken@2994
   341
slouken@2994
   342
            DrawRects(state->windows[i]);
slouken@2994
   343
            DrawPoints(state->windows[i]);
slouken@2994
   344
            DrawRectRectIntersections(state->windows[i]);
slouken@2994
   345
            DrawLines(state->windows[i]);
slouken@2994
   346
            DrawRectLineIntersections(state->windows[i]);
slouken@2994
   347
slouken@2994
   348
            SDL_RenderPresent();
slouken@2994
   349
        }
slouken@2994
   350
    }
slouken@2994
   351
lestat@3371
   352
    CommonQuit(state);
lestat@3371
   353
slouken@2994
   354
    /* Print out some timing information */
slouken@2994
   355
    now = SDL_GetTicks();
slouken@2994
   356
    if (now > then) {
slouken@2994
   357
        double fps = ((double) frames * 1000) / (now - then);
slouken@2994
   358
        printf("%2.2f frames per second\n", fps);
slouken@2994
   359
    }
slouken@2994
   360
    return 0;
slouken@2994
   361
}
slouken@2994
   362
slouken@2994
   363
/* vi: set ts=4 sw=4 expandtab: */