test/testdraw2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 21 Dec 2008 17:39:41 +0000
changeset 2901 133601e3b255
child 2902 83c3a4b0e421
permissions -rw-r--r--
Added RenderPiont() API
Merged the drawing tests into a single test program
slouken@2901
     1
slouken@2901
     2
/* Simple program:  draw as many random objects on the screen as possible */
slouken@2901
     3
slouken@2901
     4
#include <stdlib.h>
slouken@2901
     5
#include <stdio.h>
slouken@2901
     6
#include <time.h>
slouken@2901
     7
slouken@2901
     8
#include "common.h"
slouken@2901
     9
slouken@2901
    10
#define NUM_OBJECTS	100
slouken@2901
    11
slouken@2901
    12
static CommonState *state;
slouken@2901
    13
static int num_objects;
slouken@2901
    14
static SDL_bool cycle_color;
slouken@2901
    15
static SDL_bool cycle_alpha;
slouken@2901
    16
static int cycle_direction = 1;
slouken@2901
    17
static int current_alpha = 255;
slouken@2901
    18
static int current_color = 255;
slouken@2901
    19
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
slouken@2901
    20
slouken@2901
    21
void
slouken@2901
    22
DrawPoints(SDL_WindowID window)
slouken@2901
    23
{
slouken@2901
    24
    int i;
slouken@2901
    25
    int x, y;
slouken@2901
    26
    int window_w, window_h;
slouken@2901
    27
slouken@2901
    28
    /* Query the sizes */
slouken@2901
    29
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2901
    30
slouken@2901
    31
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2901
    32
    for (i = 0; i < num_objects; ++i) {
slouken@2901
    33
        /* Cycle the color and alpha, if desired */
slouken@2901
    34
        if (cycle_color) {
slouken@2901
    35
            current_color += cycle_direction;
slouken@2901
    36
            if (current_color < 0) {
slouken@2901
    37
                current_color = 0;
slouken@2901
    38
                cycle_direction = -cycle_direction;
slouken@2901
    39
            }
slouken@2901
    40
            if (current_color > 255) {
slouken@2901
    41
                current_color = 255;
slouken@2901
    42
                cycle_direction = -cycle_direction;
slouken@2901
    43
            }
slouken@2901
    44
        }
slouken@2901
    45
        if (cycle_alpha) {
slouken@2901
    46
            current_alpha += cycle_direction;
slouken@2901
    47
            if (current_alpha < 0) {
slouken@2901
    48
                current_alpha = 0;
slouken@2901
    49
                cycle_direction = -cycle_direction;
slouken@2901
    50
            }
slouken@2901
    51
            if (current_alpha > 255) {
slouken@2901
    52
                current_alpha = 255;
slouken@2901
    53
                cycle_direction = -cycle_direction;
slouken@2901
    54
            }
slouken@2901
    55
        }
slouken@2901
    56
        SDL_SetRenderDrawColor(255, (Uint8) current_color,
slouken@2901
    57
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
    58
slouken@2901
    59
        x = rand() % window_w;
slouken@2901
    60
        y = rand() % window_h;
slouken@2901
    61
        SDL_RenderPoint(x, y);
slouken@2901
    62
    }
slouken@2901
    63
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2901
    64
}
slouken@2901
    65
slouken@2901
    66
void
slouken@2901
    67
DrawLines(SDL_WindowID window)
slouken@2901
    68
{
slouken@2901
    69
    int i;
slouken@2901
    70
    int x1, y1, x2, y2;
slouken@2901
    71
    int window_w, window_h;
slouken@2901
    72
slouken@2901
    73
    /* Query the sizes */
slouken@2901
    74
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2901
    75
slouken@2901
    76
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2901
    77
    for (i = 0; i < num_objects; ++i) {
slouken@2901
    78
        /* Cycle the color and alpha, if desired */
slouken@2901
    79
        if (cycle_color) {
slouken@2901
    80
            current_color += cycle_direction;
slouken@2901
    81
            if (current_color < 0) {
slouken@2901
    82
                current_color = 0;
slouken@2901
    83
                cycle_direction = -cycle_direction;
slouken@2901
    84
            }
slouken@2901
    85
            if (current_color > 255) {
slouken@2901
    86
                current_color = 255;
slouken@2901
    87
                cycle_direction = -cycle_direction;
slouken@2901
    88
            }
slouken@2901
    89
        }
slouken@2901
    90
        if (cycle_alpha) {
slouken@2901
    91
            current_alpha += cycle_direction;
slouken@2901
    92
            if (current_alpha < 0) {
slouken@2901
    93
                current_alpha = 0;
slouken@2901
    94
                cycle_direction = -cycle_direction;
slouken@2901
    95
            }
slouken@2901
    96
            if (current_alpha > 255) {
slouken@2901
    97
                current_alpha = 255;
slouken@2901
    98
                cycle_direction = -cycle_direction;
slouken@2901
    99
            }
slouken@2901
   100
        }
slouken@2901
   101
        SDL_SetRenderDrawColor(255, (Uint8) current_color,
slouken@2901
   102
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   103
slouken@2901
   104
        if (i == 0) {
slouken@2901
   105
            SDL_RenderLine(0, 0, window_w, window_h);
slouken@2901
   106
            SDL_RenderLine(0, window_h, window_w, 0);
slouken@2901
   107
            SDL_RenderLine(0, window_h / 2, window_w, window_h / 2);
slouken@2901
   108
            SDL_RenderLine(window_w / 2, 0, window_w / 2, window_h);
slouken@2901
   109
        } else {
slouken@2901
   110
            x1 = rand() % window_w;
slouken@2901
   111
            x2 = rand() % window_w;
slouken@2901
   112
            y1 = rand() % window_h;
slouken@2901
   113
            y2 = rand() % window_h;
slouken@2901
   114
            SDL_RenderLine(x1, y1, x2, y2);
slouken@2901
   115
        }
slouken@2901
   116
    }
slouken@2901
   117
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2901
   118
}
slouken@2901
   119
slouken@2901
   120
void
slouken@2901
   121
DrawRects(SDL_WindowID window)
slouken@2901
   122
{
slouken@2901
   123
    int i;
slouken@2901
   124
    SDL_Rect rect;
slouken@2901
   125
    int window_w, window_h;
slouken@2901
   126
slouken@2901
   127
    /* Query the sizes */
slouken@2901
   128
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@2901
   129
slouken@2901
   130
    SDL_SetRenderDrawBlendMode(blendMode);
slouken@2901
   131
    for (i = 0; i < num_objects/4; ++i) {
slouken@2901
   132
        /* Cycle the color and alpha, if desired */
slouken@2901
   133
        if (cycle_color) {
slouken@2901
   134
            current_color += cycle_direction;
slouken@2901
   135
            if (current_color < 0) {
slouken@2901
   136
                current_color = 0;
slouken@2901
   137
                cycle_direction = -cycle_direction;
slouken@2901
   138
            }
slouken@2901
   139
            if (current_color > 255) {
slouken@2901
   140
                current_color = 255;
slouken@2901
   141
                cycle_direction = -cycle_direction;
slouken@2901
   142
            }
slouken@2901
   143
        }
slouken@2901
   144
        if (cycle_alpha) {
slouken@2901
   145
            current_alpha += cycle_direction;
slouken@2901
   146
            if (current_alpha < 0) {
slouken@2901
   147
                current_alpha = 0;
slouken@2901
   148
                cycle_direction = -cycle_direction;
slouken@2901
   149
            }
slouken@2901
   150
            if (current_alpha > 255) {
slouken@2901
   151
                current_alpha = 255;
slouken@2901
   152
                cycle_direction = -cycle_direction;
slouken@2901
   153
            }
slouken@2901
   154
        }
slouken@2901
   155
        SDL_SetRenderDrawColor(255, (Uint8) current_color,
slouken@2901
   156
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   157
slouken@2901
   158
        rect.w = rand() % (window_h / 2);
slouken@2901
   159
        rect.h = rand() % (window_h / 2);
slouken@2901
   160
        rect.x = (rand() % window_w) - (rect.w / 2);
slouken@2901
   161
        rect.y = (rand() % window_w) - (rect.h / 2);
slouken@2901
   162
        SDL_RenderFill(&rect);
slouken@2901
   163
    }
slouken@2901
   164
    SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
slouken@2901
   165
}
slouken@2901
   166
slouken@2901
   167
int
slouken@2901
   168
main(int argc, char *argv[])
slouken@2901
   169
{
slouken@2901
   170
    int i, done;
slouken@2901
   171
    SDL_Event event;
slouken@2901
   172
    Uint32 then, now, frames;
slouken@2901
   173
slouken@2901
   174
    /* Initialize parameters */
slouken@2901
   175
    num_objects = NUM_OBJECTS;
slouken@2901
   176
slouken@2901
   177
    /* Initialize test framework */
slouken@2901
   178
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2901
   179
    if (!state) {
slouken@2901
   180
        return 1;
slouken@2901
   181
    }
slouken@2901
   182
    for (i = 1; i < argc;) {
slouken@2901
   183
        int consumed;
slouken@2901
   184
slouken@2901
   185
        consumed = CommonArg(state, i);
slouken@2901
   186
        if (consumed == 0) {
slouken@2901
   187
            consumed = -1;
slouken@2901
   188
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2901
   189
                if (argv[i + 1]) {
slouken@2901
   190
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2901
   191
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2901
   192
                        consumed = 2;
slouken@2901
   193
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2901
   194
                        blendMode = SDL_BLENDMODE_MASK;
slouken@2901
   195
                        consumed = 2;
slouken@2901
   196
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2901
   197
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2901
   198
                        consumed = 2;
slouken@2901
   199
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2901
   200
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2901
   201
                        consumed = 2;
slouken@2901
   202
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2901
   203
                        blendMode = SDL_BLENDMODE_MOD;
slouken@2901
   204
                        consumed = 2;
slouken@2901
   205
                    }
slouken@2901
   206
                }
slouken@2901
   207
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2901
   208
                cycle_color = SDL_TRUE;
slouken@2901
   209
                consumed = 1;
slouken@2901
   210
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2901
   211
                cycle_alpha = SDL_TRUE;
slouken@2901
   212
                consumed = 1;
slouken@2901
   213
            } else if (SDL_isdigit(*argv[i])) {
slouken@2901
   214
                num_objects = SDL_atoi(argv[i]);
slouken@2901
   215
                consumed = 1;
slouken@2901
   216
            }
slouken@2901
   217
        }
slouken@2901
   218
        if (consumed < 0) {
slouken@2901
   219
            fprintf(stderr,
slouken@2901
   220
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2901
   221
                    argv[0], CommonUsage(state));
slouken@2901
   222
            return 1;
slouken@2901
   223
        }
slouken@2901
   224
        i += consumed;
slouken@2901
   225
    }
slouken@2901
   226
    if (!CommonInit(state)) {
slouken@2901
   227
        return 2;
slouken@2901
   228
    }
slouken@2901
   229
slouken@2901
   230
    /* Create the windows and initialize the renderers */
slouken@2901
   231
    for (i = 0; i < state->num_windows; ++i) {
slouken@2901
   232
        SDL_SelectRenderer(state->windows[i]);
slouken@2901
   233
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2901
   234
        SDL_RenderFill(NULL);
slouken@2901
   235
    }
slouken@2901
   236
slouken@2901
   237
    srand(time(NULL));
slouken@2901
   238
slouken@2901
   239
    /* Main render loop */
slouken@2901
   240
    frames = 0;
slouken@2901
   241
    then = SDL_GetTicks();
slouken@2901
   242
    done = 0;
slouken@2901
   243
    while (!done) {
slouken@2901
   244
        /* Check for events */
slouken@2901
   245
        ++frames;
slouken@2901
   246
        while (SDL_PollEvent(&event)) {
slouken@2901
   247
            CommonEvent(state, &event, &done);
slouken@2901
   248
            switch (event.type) {
slouken@2901
   249
            case SDL_WINDOWEVENT:
slouken@2901
   250
                switch (event.window.event) {
slouken@2901
   251
                case SDL_WINDOWEVENT_EXPOSED:
slouken@2901
   252
                    SDL_SelectRenderer(event.window.windowID);
slouken@2901
   253
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2901
   254
                    SDL_RenderFill(NULL);
slouken@2901
   255
                    break;
slouken@2901
   256
                }
slouken@2901
   257
                break;
slouken@2901
   258
            default:
slouken@2901
   259
                break;
slouken@2901
   260
            }
slouken@2901
   261
        }
slouken@2901
   262
        for (i = 0; i < state->num_windows; ++i) {
slouken@2901
   263
            SDL_SelectRenderer(state->windows[i]);
slouken@2901
   264
            SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2901
   265
            SDL_RenderFill(NULL);
slouken@2901
   266
slouken@2901
   267
            DrawRects(state->windows[i]);
slouken@2901
   268
            DrawLines(state->windows[i]);
slouken@2901
   269
            DrawPoints(state->windows[i]);
slouken@2901
   270
slouken@2901
   271
            SDL_RenderPresent();
slouken@2901
   272
        }
slouken@2901
   273
    }
slouken@2901
   274
slouken@2901
   275
    /* Print out some timing information */
slouken@2901
   276
    now = SDL_GetTicks();
slouken@2901
   277
    if (now > then) {
slouken@2901
   278
        double fps = ((double) frames * 1000) / (now - then);
slouken@2901
   279
        printf("%2.2f frames per second\n", fps);
slouken@2901
   280
    }
slouken@2901
   281
    return 0;
slouken@2901
   282
}
slouken@2901
   283
slouken@2901
   284
/* vi: set ts=4 sw=4 expandtab: */