test/testintersections.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 26 Dec 2012 22:26:44 -0800
changeset 6773 5ebc5a9e35b4
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
Add surface test suite; minor improvements to render suite; refactor image saving into test lib compare function; fix for Haiku build
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@2994
    12
slouken@2994
    13
/* Simple program:  draw as many random objects on the screen as possible */
slouken@2994
    14
slouken@2994
    15
#include <stdlib.h>
slouken@2994
    16
#include <stdio.h>
slouken@2994
    17
#include <time.h>
slouken@2994
    18
slouken@2994
    19
#include "common.h"
slouken@2994
    20
slouken@2994
    21
#define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
slouken@2994
    22
#define NUM_OBJECTS	100
slouken@2994
    23
slouken@2994
    24
static CommonState *state;
slouken@2994
    25
static int num_objects;
slouken@2994
    26
static SDL_bool cycle_color;
slouken@2994
    27
static SDL_bool cycle_alpha;
slouken@2994
    28
static int cycle_direction = 1;
slouken@2994
    29
static int current_alpha = 255;
slouken@2994
    30
static int current_color = 255;
slouken@2994
    31
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
slouken@2994
    32
slouken@2994
    33
void
slouken@5297
    34
DrawPoints(SDL_Renderer * renderer)
slouken@2994
    35
{
slouken@2994
    36
    int i;
slouken@2994
    37
    int x, y;
slouken@5297
    38
    SDL_Rect viewport;
slouken@2994
    39
slouken@2994
    40
    /* Query the sizes */
slouken@5297
    41
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2994
    42
slouken@2994
    43
    for (i = 0; i < num_objects * 4; ++i) {
slouken@2994
    44
        /* Cycle the color and alpha, if desired */
slouken@2994
    45
        if (cycle_color) {
slouken@2994
    46
            current_color += cycle_direction;
slouken@2994
    47
            if (current_color < 0) {
slouken@2994
    48
                current_color = 0;
slouken@2994
    49
                cycle_direction = -cycle_direction;
slouken@2994
    50
            }
slouken@2994
    51
            if (current_color > 255) {
slouken@2994
    52
                current_color = 255;
slouken@2994
    53
                cycle_direction = -cycle_direction;
slouken@2994
    54
            }
slouken@2994
    55
        }
slouken@2994
    56
        if (cycle_alpha) {
slouken@2994
    57
            current_alpha += cycle_direction;
slouken@2994
    58
            if (current_alpha < 0) {
slouken@2994
    59
                current_alpha = 0;
slouken@2994
    60
                cycle_direction = -cycle_direction;
slouken@2994
    61
            }
slouken@2994
    62
            if (current_alpha > 255) {
slouken@2994
    63
                current_alpha = 255;
slouken@2994
    64
                cycle_direction = -cycle_direction;
slouken@2994
    65
            }
slouken@2994
    66
        }
slouken@5147
    67
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2994
    68
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2994
    69
slouken@5297
    70
        x = rand() % viewport.w;
slouken@5297
    71
        y = rand() % viewport.h;
slouken@5147
    72
        SDL_RenderDrawPoint(renderer, x, y);
slouken@2994
    73
    }
slouken@2994
    74
}
slouken@2994
    75
slouken@2994
    76
#define MAX_LINES 16
slouken@2994
    77
int num_lines = 0;
slouken@2994
    78
SDL_Rect lines[MAX_LINES];
slouken@2997
    79
static int
slouken@2997
    80
add_line(int x1, int y1, int x2, int y2)
slouken@2997
    81
{
slouken@2997
    82
    if (num_lines >= MAX_LINES)
slouken@2997
    83
        return 0;
slouken@2997
    84
    if ((x1 == x2) && (y1 == y2))
slouken@2997
    85
        return 0;
slouken@2994
    86
slouken@2994
    87
    printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
slouken@2994
    88
    lines[num_lines].x = x1;
slouken@2994
    89
    lines[num_lines].y = y1;
slouken@2994
    90
    lines[num_lines].w = x2;
slouken@2994
    91
    lines[num_lines].h = y2;
slouken@2994
    92
slouken@2994
    93
    return ++num_lines;
slouken@2994
    94
}
slouken@2994
    95
slouken@2994
    96
slouken@2994
    97
void
slouken@5297
    98
DrawLines(SDL_Renderer * renderer)
slouken@2994
    99
{
slouken@2994
   100
    int i;
slouken@2994
   101
    int x1, y1, x2, y2;
slouken@5297
   102
    SDL_Rect viewport;
slouken@2994
   103
slouken@2994
   104
    /* Query the sizes */
slouken@5297
   105
    SDL_RenderGetViewport(renderer, &viewport);
slouken@5297
   106
slouken@5297
   107
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
slouken@2994
   108
slouken@2994
   109
    for (i = 0; i < num_lines; ++i) {
slouken@2994
   110
        if (i == -1) {
slouken@5297
   111
            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
slouken@5297
   112
            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
slouken@5297
   113
            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
slouken@5297
   114
            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
slouken@2994
   115
        } else {
slouken@5147
   116
            SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
slouken@2994
   117
        }
slouken@2994
   118
    }
slouken@2994
   119
}
slouken@2994
   120
slouken@2994
   121
#define MAX_RECTS 16
slouken@2994
   122
int num_rects = 0;
slouken@2994
   123
SDL_Rect rects[MAX_RECTS];
slouken@2997
   124
static int
slouken@2997
   125
add_rect(int x1, int y1, int x2, int y2)
slouken@2997
   126
{
slouken@2997
   127
    if (num_rects >= MAX_RECTS)
slouken@2997
   128
        return 0;
slouken@2997
   129
    if ((x1 == x2) || (y1 == y2))
slouken@2997
   130
        return 0;
slouken@2994
   131
slouken@2997
   132
    if (x1 > x2)
slouken@2997
   133
        SWAP(int, x1, x2);
slouken@2997
   134
    if (y1 > y2)
slouken@2997
   135
        SWAP(int, y1, y2);
slouken@2994
   136
slouken@2997
   137
    printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
slouken@2997
   138
           x2 - x1, y2 - y1);
slouken@2994
   139
slouken@2994
   140
    rects[num_rects].x = x1;
slouken@2994
   141
    rects[num_rects].y = y1;
slouken@2994
   142
    rects[num_rects].w = x2 - x1;
slouken@2994
   143
    rects[num_rects].h = y2 - y1;
slouken@2994
   144
slouken@2994
   145
    return ++num_rects;
slouken@2994
   146
}
slouken@2994
   147
slouken@2994
   148
static void
slouken@5297
   149
DrawRects(SDL_Renderer * renderer)
slouken@2994
   150
{
slouken@5297
   151
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
slouken@5297
   152
    SDL_RenderFillRects(renderer, rects, num_rects);
slouken@2994
   153
}
slouken@2994
   154
slouken@2994
   155
static void
slouken@5297
   156
DrawRectLineIntersections(SDL_Renderer * renderer)
slouken@2994
   157
{
slouken@5297
   158
    int i, j;
slouken@2994
   159
slouken@5297
   160
    SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
slouken@2994
   161
slouken@2994
   162
    for (i = 0; i < num_rects; i++)
slouken@2997
   163
        for (j = 0; j < num_lines; j++) {
slouken@2997
   164
            int x1, y1, x2, y2;
slouken@2997
   165
            SDL_Rect r;
slouken@2994
   166
slouken@2997
   167
            r = rects[i];
slouken@2997
   168
            x1 = lines[j].x;
slouken@2997
   169
            y1 = lines[j].y;
slouken@2997
   170
            x2 = lines[j].w;
slouken@2997
   171
            y2 = lines[j].h;
slouken@2997
   172
slouken@2997
   173
            if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
slouken@5147
   174
                SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
slouken@2997
   175
            }
slouken@2994
   176
        }
slouken@2994
   177
}
slouken@2994
   178
slouken@2994
   179
static void
slouken@5297
   180
DrawRectRectIntersections(SDL_Renderer * renderer)
slouken@2994
   181
{
slouken@2994
   182
    int i, j;
slouken@2994
   183
slouken@5297
   184
    SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
slouken@5297
   185
slouken@2994
   186
    for (i = 0; i < num_rects; i++)
slouken@2997
   187
        for (j = i + 1; j < num_rects; j++) {
slouken@2997
   188
            SDL_Rect r;
slouken@2997
   189
            if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
slouken@5147
   190
                SDL_RenderFillRect(renderer, &r);
slouken@2997
   191
            }
slouken@2994
   192
        }
slouken@2994
   193
}
slouken@2994
   194
slouken@2994
   195
int
slouken@2994
   196
main(int argc, char *argv[])
slouken@2994
   197
{
slouken@2994
   198
    int mouse_begin_x = -1, mouse_begin_y = -1;
slouken@2994
   199
    int i, done;
slouken@2994
   200
    SDL_Event event;
slouken@2994
   201
    Uint32 then, now, frames;
slouken@2994
   202
slouken@2994
   203
    /* Initialize parameters */
slouken@2994
   204
    num_objects = NUM_OBJECTS;
slouken@2994
   205
slouken@2994
   206
    /* Initialize test framework */
slouken@2994
   207
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2994
   208
    if (!state) {
slouken@2994
   209
        return 1;
slouken@2994
   210
    }
slouken@2994
   211
    for (i = 1; i < argc;) {
slouken@2994
   212
        int consumed;
slouken@2994
   213
slouken@2994
   214
        consumed = CommonArg(state, i);
slouken@2994
   215
        if (consumed == 0) {
slouken@2994
   216
            consumed = -1;
slouken@2994
   217
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2994
   218
                if (argv[i + 1]) {
slouken@2994
   219
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2994
   220
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2994
   221
                        consumed = 2;
slouken@2994
   222
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2994
   223
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2994
   224
                        consumed = 2;
slouken@2994
   225
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2994
   226
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2994
   227
                        consumed = 2;
slouken@5184
   228
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   229
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   230
                        consumed = 2;
slouken@2994
   231
                    }
slouken@2994
   232
                }
slouken@2994
   233
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2994
   234
                cycle_color = SDL_TRUE;
slouken@2994
   235
                consumed = 1;
slouken@2994
   236
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2994
   237
                cycle_alpha = SDL_TRUE;
slouken@2994
   238
                consumed = 1;
slouken@2994
   239
            } else if (SDL_isdigit(*argv[i])) {
slouken@2994
   240
                num_objects = SDL_atoi(argv[i]);
slouken@2994
   241
                consumed = 1;
slouken@2994
   242
            }
slouken@2994
   243
        }
slouken@2994
   244
        if (consumed < 0) {
slouken@2994
   245
            fprintf(stderr,
slouken@5184
   246
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2994
   247
                    argv[0], CommonUsage(state));
slouken@2994
   248
            return 1;
slouken@2994
   249
        }
slouken@2994
   250
        i += consumed;
slouken@2994
   251
    }
slouken@2994
   252
    if (!CommonInit(state)) {
slouken@2994
   253
        return 2;
slouken@2994
   254
    }
slouken@2994
   255
slouken@2994
   256
    /* Create the windows and initialize the renderers */
slouken@2994
   257
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   258
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   259
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   260
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   261
        SDL_RenderClear(renderer);
slouken@2994
   262
    }
slouken@2994
   263
slouken@2994
   264
    srand(time(NULL));
slouken@2994
   265
slouken@2994
   266
    /* Main render loop */
slouken@2994
   267
    frames = 0;
slouken@2994
   268
    then = SDL_GetTicks();
slouken@2994
   269
    done = 0;
slouken@2994
   270
    while (!done) {
slouken@2994
   271
        /* Check for events */
slouken@2994
   272
        ++frames;
slouken@2994
   273
        while (SDL_PollEvent(&event)) {
slouken@2994
   274
            CommonEvent(state, &event, &done);
slouken@2994
   275
            switch (event.type) {
slouken@2994
   276
            case SDL_MOUSEBUTTONDOWN:
slouken@4465
   277
                mouse_begin_x = event.button.x;
slouken@4465
   278
                mouse_begin_y = event.button.y;
slouken@2994
   279
                break;
slouken@2994
   280
            case SDL_MOUSEBUTTONUP:
slouken@4465
   281
                if (event.button.button == 3)
slouken@4465
   282
                    add_line(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   283
                             event.button.y);
slouken@4465
   284
                if (event.button.button == 1)
slouken@4465
   285
                    add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
slouken@4465
   286
                             event.button.y);
slouken@2994
   287
                break;
slouken@2994
   288
            case SDL_KEYDOWN:
slouken@2994
   289
                switch (event.key.keysym.sym) {
slouken@2997
   290
                case 'l':
slouken@2997
   291
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   292
                        num_lines = 0;
slouken@2997
   293
                    else
slouken@2997
   294
                        add_line(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   295
                                 rand() % 480);
slouken@2997
   296
                    break;
slouken@2997
   297
                case 'r':
slouken@2997
   298
                    if (event.key.keysym.mod & KMOD_SHIFT)
slouken@2997
   299
                        num_rects = 0;
slouken@2997
   300
                    else
slouken@2997
   301
                        add_rect(rand() % 640, rand() % 480, rand() % 640,
slouken@2997
   302
                                 rand() % 480);
slouken@2997
   303
                    break;
slouken@2994
   304
                }
slouken@2994
   305
                break;
slouken@2994
   306
            default:
slouken@2994
   307
                break;
slouken@2994
   308
            }
slouken@2994
   309
        }
slouken@2994
   310
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   311
            SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   312
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   313
            SDL_RenderClear(renderer);
slouken@2994
   314
slouken@5297
   315
            DrawRects(renderer);
slouken@5297
   316
            DrawPoints(renderer);
slouken@5297
   317
            DrawRectRectIntersections(renderer);
slouken@5297
   318
            DrawLines(renderer);
slouken@5297
   319
            DrawRectLineIntersections(renderer);
slouken@2994
   320
slouken@5147
   321
            SDL_RenderPresent(renderer);
slouken@2994
   322
        }
slouken@2994
   323
    }
slouken@2994
   324
lestat@3371
   325
    CommonQuit(state);
lestat@3371
   326
slouken@2994
   327
    /* Print out some timing information */
slouken@2994
   328
    now = SDL_GetTicks();
slouken@2994
   329
    if (now > then) {
slouken@2994
   330
        double fps = ((double) frames * 1000) / (now - then);
slouken@2994
   331
        printf("%2.2f frames per second\n", fps);
slouken@2994
   332
    }
slouken@2994
   333
    return 0;
slouken@2994
   334
}
slouken@2994
   335
slouken@2994
   336
/* vi: set ts=4 sw=4 expandtab: */