test/testintersections.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Feb 2011 19:50:56 -0800
changeset 5184 d976b67150c5
parent 5147 ad50b3db78bd
child 5297 1800dc39b74c
permissions -rw-r--r--
Restored SDL_BLENDMODE_MOD for MAME
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@5147
    23
DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
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
    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@2994
    59
        x = rand() % window_w;
slouken@2994
    60
        y = rand() % window_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@5147
    87
DrawLines(SDL_Window * window, SDL_Renderer * renderer)
slouken@2994
    88
{
slouken@2994
    89
    int i;
slouken@2994
    90
    int x1, y1, x2, y2;
slouken@2994
    91
    int window_w, window_h;
slouken@2994
    92
slouken@2994
    93
    /* Query the sizes */
slouken@2994
    94
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
    95
slouken@2994
    96
    for (i = 0; i < num_lines; ++i) {
slouken@5147
    97
        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
slouken@2994
    98
slouken@2994
    99
        if (i == -1) {
slouken@5147
   100
            SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
slouken@5147
   101
            SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
slouken@5147
   102
            SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
slouken@5147
   103
            SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_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@5147
   138
DrawRects(SDL_Window * window, SDL_Renderer * renderer)
slouken@2994
   139
{
slouken@2994
   140
    int i;
slouken@2994
   141
    int window_w, window_h;
slouken@2994
   142
slouken@2994
   143
    /* Query the sizes */
slouken@2994
   144
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
   145
slouken@2994
   146
    for (i = 0; i < num_rects; ++i) {
slouken@5147
   147
        SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
slouken@5147
   148
        SDL_RenderFillRect(renderer, &rects[i]);
slouken@2994
   149
    }
slouken@2994
   150
}
slouken@2994
   151
slouken@2994
   152
static void
slouken@5147
   153
DrawRectLineIntersections(SDL_Window * window, SDL_Renderer * renderer)
slouken@2994
   154
{
slouken@2994
   155
    int i, j, window_w, window_h;
slouken@2994
   156
slouken@2994
   157
    /* Query the sizes */
slouken@2994
   158
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2994
   159
slouken@2994
   160
    for (i = 0; i < num_rects; i++)
slouken@2997
   161
        for (j = 0; j < num_lines; j++) {
slouken@2997
   162
            int x1, y1, x2, y2;
slouken@2997
   163
            SDL_Rect r;
slouken@2994
   164
slouken@2997
   165
            r = rects[i];
slouken@2997
   166
            x1 = lines[j].x;
slouken@2997
   167
            y1 = lines[j].y;
slouken@2997
   168
            x2 = lines[j].w;
slouken@2997
   169
            y2 = lines[j].h;
slouken@2997
   170
slouken@2997
   171
            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
slouken@5147
   172
                SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
slouken@5147
   173
                SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
slouken@2997
   174
            }
slouken@2994
   175
        }
slouken@2994
   176
}
slouken@2994
   177
slouken@2994
   178
static void
slouken@5147
   179
DrawRectRectIntersections(SDL_Window * window, SDL_Renderer * renderer)
slouken@2994
   180
{
slouken@2994
   181
    int i, j;
slouken@2994
   182
slouken@2994
   183
    for (i = 0; i < num_rects; i++)
slouken@2997
   184
        for (j = i + 1; j < num_rects; j++) {
slouken@2997
   185
            SDL_Rect r;
slouken@2997
   186
            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
slouken@5147
   187
                SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
slouken@5147
   188
                SDL_RenderFillRect(renderer, &r);
slouken@2997
   189
            }
slouken@2994
   190
        }
slouken@2994
   191
}
slouken@2994
   192
slouken@2994
   193
int
slouken@2994
   194
main(int argc, char *argv[])
slouken@2994
   195
{
slouken@2994
   196
    int mouse_begin_x = -1, mouse_begin_y = -1;
slouken@2994
   197
    int i, done;
slouken@2994
   198
    SDL_Event event;
slouken@2994
   199
    Uint32 then, now, frames;
slouken@2994
   200
slouken@2994
   201
    /* Initialize parameters */
slouken@2994
   202
    num_objects = NUM_OBJECTS;
slouken@2994
   203
slouken@2994
   204
    /* Initialize test framework */
slouken@2994
   205
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2994
   206
    if (!state) {
slouken@2994
   207
        return 1;
slouken@2994
   208
    }
slouken@2994
   209
    for (i = 1; i < argc;) {
slouken@2994
   210
        int consumed;
slouken@2994
   211
slouken@2994
   212
        consumed = CommonArg(state, i);
slouken@2994
   213
        if (consumed == 0) {
slouken@2994
   214
            consumed = -1;
slouken@2994
   215
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2994
   216
                if (argv[i + 1]) {
slouken@2994
   217
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2994
   218
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2994
   219
                        consumed = 2;
slouken@2994
   220
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2994
   221
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2994
   222
                        consumed = 2;
slouken@2994
   223
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2994
   224
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2994
   225
                        consumed = 2;
slouken@5184
   226
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   227
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   228
                        consumed = 2;
slouken@2994
   229
                    }
slouken@2994
   230
                }
slouken@2994
   231
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2994
   232
                cycle_color = SDL_TRUE;
slouken@2994
   233
                consumed = 1;
slouken@2994
   234
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2994
   235
                cycle_alpha = SDL_TRUE;
slouken@2994
   236
                consumed = 1;
slouken@2994
   237
            } else if (SDL_isdigit(*argv[i])) {
slouken@2994
   238
                num_objects = SDL_atoi(argv[i]);
slouken@2994
   239
                consumed = 1;
slouken@2994
   240
            }
slouken@2994
   241
        }
slouken@2994
   242
        if (consumed < 0) {
slouken@2994
   243
            fprintf(stderr,
slouken@5184
   244
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2994
   245
                    argv[0], CommonUsage(state));
slouken@2994
   246
            return 1;
slouken@2994
   247
        }
slouken@2994
   248
        i += consumed;
slouken@2994
   249
    }
slouken@2994
   250
    if (!CommonInit(state)) {
slouken@2994
   251
        return 2;
slouken@2994
   252
    }
slouken@2994
   253
slouken@2994
   254
    /* Create the windows and initialize the renderers */
slouken@2994
   255
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   256
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   257
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   258
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   259
        SDL_RenderClear(renderer);
slouken@2994
   260
    }
slouken@2994
   261
slouken@2994
   262
    srand(time(NULL));
slouken@2994
   263
slouken@2994
   264
    /* Main render loop */
slouken@2994
   265
    frames = 0;
slouken@2994
   266
    then = SDL_GetTicks();
slouken@2994
   267
    done = 0;
slouken@2994
   268
    while (!done) {
slouken@2994
   269
        /* Check for events */
slouken@2994
   270
        ++frames;
slouken@2994
   271
        while (SDL_PollEvent(&event)) {
slouken@2994
   272
            CommonEvent(state, &event, &done);
slouken@2994
   273
            switch (event.type) {
slouken@2994
   274
            case SDL_MOUSEBUTTONDOWN:
slouken@4465
   275
                mouse_begin_x = event.button.x;
slouken@4465
   276
                mouse_begin_y = event.button.y;
slouken@2994
   277
                break;
slouken@2994
   278
            case SDL_MOUSEBUTTONUP:
slouken@4465
   279
                if (event.button.button == 3)
slouken@4465
   280
                    add_line(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   281
                             event.button.y);
slouken@4465
   282
                if (event.button.button == 1)
slouken@4465
   283
                    add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   284
                             event.button.y);
slouken@2994
   285
                break;
slouken@2994
   286
            case SDL_KEYDOWN:
slouken@2994
   287
                switch (event.key.keysym.sym) {
slouken@2997
   288
                case 'l':
slouken@2997
   289
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   290
                        num_lines = 0;
slouken@2997
   291
                    else
slouken@2997
   292
                        add_line(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   293
                                 rand() % 480);
slouken@2997
   294
                    break;
slouken@2997
   295
                case 'r':
slouken@2997
   296
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   297
                        num_rects = 0;
slouken@2997
   298
                    else
slouken@2997
   299
                        add_rect(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   300
                                 rand() % 480);
slouken@2997
   301
                    break;
slouken@2994
   302
                }
slouken@2994
   303
                break;
slouken@2994
   304
            default:
slouken@2994
   305
                break;
slouken@2994
   306
            }
slouken@2994
   307
        }
slouken@2994
   308
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   309
            SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   310
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   311
            SDL_RenderClear(renderer);
slouken@2994
   312
slouken@5147
   313
            DrawRects(state->windows[i], renderer);
slouken@5147
   314
            DrawPoints(state->windows[i], renderer);
slouken@5147
   315
            DrawRectRectIntersections(state->windows[i], renderer);
slouken@5147
   316
            DrawLines(state->windows[i], renderer);
slouken@5147
   317
            DrawRectLineIntersections(state->windows[i], renderer);
slouken@2994
   318
slouken@5147
   319
            SDL_RenderPresent(renderer);
slouken@2994
   320
        }
slouken@2994
   321
    }
slouken@2994
   322
lestat@3371
   323
    CommonQuit(state);
lestat@3371
   324
slouken@2994
   325
    /* Print out some timing information */
slouken@2994
   326
    now = SDL_GetTicks();
slouken@2994
   327
    if (now > then) {
slouken@2994
   328
        double fps = ((double) frames * 1000) / (now - then);
slouken@2994
   329
        printf("%2.2f frames per second\n", fps);
slouken@2994
   330
    }
slouken@2994
   331
    return 0;
slouken@2994
   332
}
slouken@2994
   333
slouken@2994
   334
/* vi: set ts=4 sw=4 expandtab: */