test/testintersections.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5184 d976b67150c5
child 5535 96594ac5fd1a
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

Fixed API for updating window rects for consistency with other APIs - the order is pointer to array followed by count in array.
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@5297
    23
DrawPoints(SDL_Renderer * renderer)
slouken@2994
    24
{
slouken@2994
    25
    int i;
slouken@2994
    26
    int x, y;
slouken@5297
    27
    SDL_Rect viewport;
slouken@2994
    28
slouken@2994
    29
    /* Query the sizes */
slouken@5297
    30
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2994
    31
slouken@2994
    32
    for (i = 0; i < num_objects * 4; ++i) {
slouken@2994
    33
        /* Cycle the color and alpha, if desired */
slouken@2994
    34
        if (cycle_color) {
slouken@2994
    35
            current_color += cycle_direction;
slouken@2994
    36
            if (current_color < 0) {
slouken@2994
    37
                current_color = 0;
slouken@2994
    38
                cycle_direction = -cycle_direction;
slouken@2994
    39
            }
slouken@2994
    40
            if (current_color > 255) {
slouken@2994
    41
                current_color = 255;
slouken@2994
    42
                cycle_direction = -cycle_direction;
slouken@2994
    43
            }
slouken@2994
    44
        }
slouken@2994
    45
        if (cycle_alpha) {
slouken@2994
    46
            current_alpha += cycle_direction;
slouken@2994
    47
            if (current_alpha < 0) {
slouken@2994
    48
                current_alpha = 0;
slouken@2994
    49
                cycle_direction = -cycle_direction;
slouken@2994
    50
            }
slouken@2994
    51
            if (current_alpha > 255) {
slouken@2994
    52
                current_alpha = 255;
slouken@2994
    53
                cycle_direction = -cycle_direction;
slouken@2994
    54
            }
slouken@2994
    55
        }
slouken@5147
    56
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2994
    57
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2994
    58
slouken@5297
    59
        x = rand() % viewport.w;
slouken@5297
    60
        y = rand() % viewport.h;
slouken@5147
    61
        SDL_RenderDrawPoint(renderer, x, y);
slouken@2994
    62
    }
slouken@2994
    63
}
slouken@2994
    64
slouken@2994
    65
#define MAX_LINES 16
slouken@2994
    66
int num_lines = 0;
slouken@2994
    67
SDL_Rect lines[MAX_LINES];
slouken@2997
    68
static int
slouken@2997
    69
add_line(int x1, int y1, int x2, int y2)
slouken@2997
    70
{
slouken@2997
    71
    if (num_lines >= MAX_LINES)
slouken@2997
    72
        return 0;
slouken@2997
    73
    if ((x1 == x2) && (y1 == y2))
slouken@2997
    74
        return 0;
slouken@2994
    75
slouken@2994
    76
    printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
slouken@2994
    77
    lines[num_lines].x = x1;
slouken@2994
    78
    lines[num_lines].y = y1;
slouken@2994
    79
    lines[num_lines].w = x2;
slouken@2994
    80
    lines[num_lines].h = y2;
slouken@2994
    81
slouken@2994
    82
    return ++num_lines;
slouken@2994
    83
}
slouken@2994
    84
slouken@2994
    85
slouken@2994
    86
void
slouken@5297
    87
DrawLines(SDL_Renderer * renderer)
slouken@2994
    88
{
slouken@2994
    89
    int i;
slouken@2994
    90
    int x1, y1, x2, y2;
slouken@5297
    91
    SDL_Rect viewport;
slouken@2994
    92
slouken@2994
    93
    /* Query the sizes */
slouken@5297
    94
    SDL_RenderGetViewport(renderer, &viewport);
slouken@5297
    95
slouken@5297
    96
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
slouken@2994
    97
slouken@2994
    98
    for (i = 0; i < num_lines; ++i) {
slouken@2994
    99
        if (i == -1) {
slouken@5297
   100
            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
slouken@5297
   101
            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
slouken@5297
   102
            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
slouken@5297
   103
            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
slouken@2994
   104
        } else {
slouken@5147
   105
            SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
slouken@2994
   106
        }
slouken@2994
   107
    }
slouken@2994
   108
}
slouken@2994
   109
slouken@2994
   110
#define MAX_RECTS 16
slouken@2994
   111
int num_rects = 0;
slouken@2994
   112
SDL_Rect rects[MAX_RECTS];
slouken@2997
   113
static int
slouken@2997
   114
add_rect(int x1, int y1, int x2, int y2)
slouken@2997
   115
{
slouken@2997
   116
    if (num_rects >= MAX_RECTS)
slouken@2997
   117
        return 0;
slouken@2997
   118
    if ((x1 == x2) || (y1 == y2))
slouken@2997
   119
        return 0;
slouken@2994
   120
slouken@2997
   121
    if (x1 > x2)
slouken@2997
   122
        SWAP(int, x1, x2);
slouken@2997
   123
    if (y1 > y2)
slouken@2997
   124
        SWAP(int, y1, y2);
slouken@2994
   125
slouken@2997
   126
    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
slouken@2997
   127
           x2 - x1, y2 - y1);
slouken@2994
   128
slouken@2994
   129
    rects[num_rects].x = x1;
slouken@2994
   130
    rects[num_rects].y = y1;
slouken@2994
   131
    rects[num_rects].w = x2 - x1;
slouken@2994
   132
    rects[num_rects].h = y2 - y1;
slouken@2994
   133
slouken@2994
   134
    return ++num_rects;
slouken@2994
   135
}
slouken@2994
   136
slouken@2994
   137
static void
slouken@5297
   138
DrawRects(SDL_Renderer * renderer)
slouken@2994
   139
{
slouken@5297
   140
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
slouken@5297
   141
    SDL_RenderFillRects(renderer, rects, num_rects);
slouken@2994
   142
}
slouken@2994
   143
slouken@2994
   144
static void
slouken@5297
   145
DrawRectLineIntersections(SDL_Renderer * renderer)
slouken@2994
   146
{
slouken@5297
   147
    int i, j;
slouken@2994
   148
slouken@5297
   149
    SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
slouken@2994
   150
slouken@2994
   151
    for (i = 0; i < num_rects; i++)
slouken@2997
   152
        for (j = 0; j < num_lines; j++) {
slouken@2997
   153
            int x1, y1, x2, y2;
slouken@2997
   154
            SDL_Rect r;
slouken@2994
   155
slouken@2997
   156
            r = rects[i];
slouken@2997
   157
            x1 = lines[j].x;
slouken@2997
   158
            y1 = lines[j].y;
slouken@2997
   159
            x2 = lines[j].w;
slouken@2997
   160
            y2 = lines[j].h;
slouken@2997
   161
slouken@2997
   162
            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
slouken@5147
   163
                SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
slouken@2997
   164
            }
slouken@2994
   165
        }
slouken@2994
   166
}
slouken@2994
   167
slouken@2994
   168
static void
slouken@5297
   169
DrawRectRectIntersections(SDL_Renderer * renderer)
slouken@2994
   170
{
slouken@2994
   171
    int i, j;
slouken@2994
   172
slouken@5297
   173
    SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
slouken@5297
   174
slouken@2994
   175
    for (i = 0; i < num_rects; i++)
slouken@2997
   176
        for (j = i + 1; j < num_rects; j++) {
slouken@2997
   177
            SDL_Rect r;
slouken@2997
   178
            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
slouken@5147
   179
                SDL_RenderFillRect(renderer, &r);
slouken@2997
   180
            }
slouken@2994
   181
        }
slouken@2994
   182
}
slouken@2994
   183
slouken@2994
   184
int
slouken@2994
   185
main(int argc, char *argv[])
slouken@2994
   186
{
slouken@2994
   187
    int mouse_begin_x = -1, mouse_begin_y = -1;
slouken@2994
   188
    int i, done;
slouken@2994
   189
    SDL_Event event;
slouken@2994
   190
    Uint32 then, now, frames;
slouken@2994
   191
slouken@2994
   192
    /* Initialize parameters */
slouken@2994
   193
    num_objects = NUM_OBJECTS;
slouken@2994
   194
slouken@2994
   195
    /* Initialize test framework */
slouken@2994
   196
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2994
   197
    if (!state) {
slouken@2994
   198
        return 1;
slouken@2994
   199
    }
slouken@2994
   200
    for (i = 1; i < argc;) {
slouken@2994
   201
        int consumed;
slouken@2994
   202
slouken@2994
   203
        consumed = CommonArg(state, i);
slouken@2994
   204
        if (consumed == 0) {
slouken@2994
   205
            consumed = -1;
slouken@2994
   206
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2994
   207
                if (argv[i + 1]) {
slouken@2994
   208
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2994
   209
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2994
   210
                        consumed = 2;
slouken@2994
   211
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2994
   212
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2994
   213
                        consumed = 2;
slouken@2994
   214
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2994
   215
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2994
   216
                        consumed = 2;
slouken@5184
   217
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   218
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   219
                        consumed = 2;
slouken@2994
   220
                    }
slouken@2994
   221
                }
slouken@2994
   222
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2994
   223
                cycle_color = SDL_TRUE;
slouken@2994
   224
                consumed = 1;
slouken@2994
   225
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2994
   226
                cycle_alpha = SDL_TRUE;
slouken@2994
   227
                consumed = 1;
slouken@2994
   228
            } else if (SDL_isdigit(*argv[i])) {
slouken@2994
   229
                num_objects = SDL_atoi(argv[i]);
slouken@2994
   230
                consumed = 1;
slouken@2994
   231
            }
slouken@2994
   232
        }
slouken@2994
   233
        if (consumed < 0) {
slouken@2994
   234
            fprintf(stderr,
slouken@5184
   235
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2994
   236
                    argv[0], CommonUsage(state));
slouken@2994
   237
            return 1;
slouken@2994
   238
        }
slouken@2994
   239
        i += consumed;
slouken@2994
   240
    }
slouken@2994
   241
    if (!CommonInit(state)) {
slouken@2994
   242
        return 2;
slouken@2994
   243
    }
slouken@2994
   244
slouken@2994
   245
    /* Create the windows and initialize the renderers */
slouken@2994
   246
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   247
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   248
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   249
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   250
        SDL_RenderClear(renderer);
slouken@2994
   251
    }
slouken@2994
   252
slouken@2994
   253
    srand(time(NULL));
slouken@2994
   254
slouken@2994
   255
    /* Main render loop */
slouken@2994
   256
    frames = 0;
slouken@2994
   257
    then = SDL_GetTicks();
slouken@2994
   258
    done = 0;
slouken@2994
   259
    while (!done) {
slouken@2994
   260
        /* Check for events */
slouken@2994
   261
        ++frames;
slouken@2994
   262
        while (SDL_PollEvent(&event)) {
slouken@2994
   263
            CommonEvent(state, &event, &done);
slouken@2994
   264
            switch (event.type) {
slouken@2994
   265
            case SDL_MOUSEBUTTONDOWN:
slouken@4465
   266
                mouse_begin_x = event.button.x;
slouken@4465
   267
                mouse_begin_y = event.button.y;
slouken@2994
   268
                break;
slouken@2994
   269
            case SDL_MOUSEBUTTONUP:
slouken@4465
   270
                if (event.button.button == 3)
slouken@4465
   271
                    add_line(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   272
                             event.button.y);
slouken@4465
   273
                if (event.button.button == 1)
slouken@4465
   274
                    add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   275
                             event.button.y);
slouken@2994
   276
                break;
slouken@2994
   277
            case SDL_KEYDOWN:
slouken@2994
   278
                switch (event.key.keysym.sym) {
slouken@2997
   279
                case 'l':
slouken@2997
   280
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   281
                        num_lines = 0;
slouken@2997
   282
                    else
slouken@2997
   283
                        add_line(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   284
                                 rand() % 480);
slouken@2997
   285
                    break;
slouken@2997
   286
                case 'r':
slouken@2997
   287
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   288
                        num_rects = 0;
slouken@2997
   289
                    else
slouken@2997
   290
                        add_rect(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   291
                                 rand() % 480);
slouken@2997
   292
                    break;
slouken@2994
   293
                }
slouken@2994
   294
                break;
slouken@2994
   295
            default:
slouken@2994
   296
                break;
slouken@2994
   297
            }
slouken@2994
   298
        }
slouken@2994
   299
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   300
            SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   301
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   302
            SDL_RenderClear(renderer);
slouken@2994
   303
slouken@5297
   304
            DrawRects(renderer);
slouken@5297
   305
            DrawPoints(renderer);
slouken@5297
   306
            DrawRectRectIntersections(renderer);
slouken@5297
   307
            DrawLines(renderer);
slouken@5297
   308
            DrawRectLineIntersections(renderer);
slouken@2994
   309
slouken@5147
   310
            SDL_RenderPresent(renderer);
slouken@2994
   311
        }
slouken@2994
   312
    }
slouken@2994
   313
lestat@3371
   314
    CommonQuit(state);
lestat@3371
   315
slouken@2994
   316
    /* Print out some timing information */
slouken@2994
   317
    now = SDL_GetTicks();
slouken@2994
   318
    if (now > then) {
slouken@2994
   319
        double fps = ((double) frames * 1000) / (now - then);
slouken@2994
   320
        printf("%2.2f frames per second\n", fps);
slouken@2994
   321
    }
slouken@2994
   322
    return 0;
slouken@2994
   323
}
slouken@2994
   324
slouken@2994
   325
/* vi: set ts=4 sw=4 expandtab: */