test/testdraw2.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@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@5297
    22
DrawPoints(SDL_Renderer * renderer)
slouken@2901
    23
{
slouken@2901
    24
    int i;
slouken@2901
    25
    int x, y;
slouken@5297
    26
    SDL_Rect viewport;
slouken@2901
    27
slouken@2901
    28
    /* Query the sizes */
slouken@5297
    29
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
    30
slouken@2902
    31
    for (i = 0; i < num_objects * 4; ++i) {
slouken@2901
    32
        /* Cycle the color and alpha, if desired */
slouken@2901
    33
        if (cycle_color) {
slouken@2901
    34
            current_color += cycle_direction;
slouken@2901
    35
            if (current_color < 0) {
slouken@2901
    36
                current_color = 0;
slouken@2901
    37
                cycle_direction = -cycle_direction;
slouken@2901
    38
            }
slouken@2901
    39
            if (current_color > 255) {
slouken@2901
    40
                current_color = 255;
slouken@2901
    41
                cycle_direction = -cycle_direction;
slouken@2901
    42
            }
slouken@2901
    43
        }
slouken@2901
    44
        if (cycle_alpha) {
slouken@2901
    45
            current_alpha += cycle_direction;
slouken@2901
    46
            if (current_alpha < 0) {
slouken@2901
    47
                current_alpha = 0;
slouken@2901
    48
                cycle_direction = -cycle_direction;
slouken@2901
    49
            }
slouken@2901
    50
            if (current_alpha > 255) {
slouken@2901
    51
                current_alpha = 255;
slouken@2901
    52
                cycle_direction = -cycle_direction;
slouken@2901
    53
            }
slouken@2901
    54
        }
slouken@5147
    55
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
    56
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
    57
slouken@5297
    58
        x = rand() % viewport.w;
slouken@5297
    59
        y = rand() % viewport.h;
slouken@5147
    60
        SDL_RenderDrawPoint(renderer, x, y);
slouken@2901
    61
    }
slouken@2901
    62
}
slouken@2901
    63
slouken@2901
    64
void
slouken@5297
    65
DrawLines(SDL_Renderer * renderer)
slouken@2901
    66
{
slouken@2901
    67
    int i;
slouken@2901
    68
    int x1, y1, x2, y2;
slouken@5297
    69
    SDL_Rect viewport;
slouken@2901
    70
slouken@2901
    71
    /* Query the sizes */
slouken@5297
    72
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
    73
slouken@2901
    74
    for (i = 0; i < num_objects; ++i) {
slouken@2901
    75
        /* Cycle the color and alpha, if desired */
slouken@2901
    76
        if (cycle_color) {
slouken@2901
    77
            current_color += cycle_direction;
slouken@2901
    78
            if (current_color < 0) {
slouken@2901
    79
                current_color = 0;
slouken@2901
    80
                cycle_direction = -cycle_direction;
slouken@2901
    81
            }
slouken@2901
    82
            if (current_color > 255) {
slouken@2901
    83
                current_color = 255;
slouken@2901
    84
                cycle_direction = -cycle_direction;
slouken@2901
    85
            }
slouken@2901
    86
        }
slouken@2901
    87
        if (cycle_alpha) {
slouken@2901
    88
            current_alpha += cycle_direction;
slouken@2901
    89
            if (current_alpha < 0) {
slouken@2901
    90
                current_alpha = 0;
slouken@2901
    91
                cycle_direction = -cycle_direction;
slouken@2901
    92
            }
slouken@2901
    93
            if (current_alpha > 255) {
slouken@2901
    94
                current_alpha = 255;
slouken@2901
    95
                cycle_direction = -cycle_direction;
slouken@2901
    96
            }
slouken@2901
    97
        }
slouken@5147
    98
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
    99
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   100
slouken@2901
   101
        if (i == 0) {
slouken@5297
   102
            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
slouken@5297
   103
            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
slouken@5297
   104
            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
slouken@5297
   105
            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
slouken@2901
   106
        } else {
slouken@5297
   107
            x1 = (rand() % (viewport.w*2)) - viewport.w;
slouken@5297
   108
            x2 = (rand() % (viewport.w*2)) - viewport.w;
slouken@5297
   109
            y1 = (rand() % (viewport.h*2)) - viewport.h;
slouken@5297
   110
            y2 = (rand() % (viewport.h*2)) - viewport.h;
slouken@5147
   111
            SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
slouken@2901
   112
        }
slouken@2901
   113
    }
slouken@2901
   114
}
slouken@2901
   115
slouken@2901
   116
void
slouken@5297
   117
DrawRects(SDL_Renderer * renderer)
slouken@2901
   118
{
slouken@2901
   119
    int i;
slouken@2901
   120
    SDL_Rect rect;
slouken@5297
   121
    SDL_Rect viewport;
slouken@2901
   122
slouken@2901
   123
    /* Query the sizes */
slouken@5297
   124
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
   125
slouken@2902
   126
    for (i = 0; i < num_objects / 4; ++i) {
slouken@2901
   127
        /* Cycle the color and alpha, if desired */
slouken@2901
   128
        if (cycle_color) {
slouken@2901
   129
            current_color += cycle_direction;
slouken@2901
   130
            if (current_color < 0) {
slouken@2901
   131
                current_color = 0;
slouken@2901
   132
                cycle_direction = -cycle_direction;
slouken@2901
   133
            }
slouken@2901
   134
            if (current_color > 255) {
slouken@2901
   135
                current_color = 255;
slouken@2901
   136
                cycle_direction = -cycle_direction;
slouken@2901
   137
            }
slouken@2901
   138
        }
slouken@2901
   139
        if (cycle_alpha) {
slouken@2901
   140
            current_alpha += cycle_direction;
slouken@2901
   141
            if (current_alpha < 0) {
slouken@2901
   142
                current_alpha = 0;
slouken@2901
   143
                cycle_direction = -cycle_direction;
slouken@2901
   144
            }
slouken@2901
   145
            if (current_alpha > 255) {
slouken@2901
   146
                current_alpha = 255;
slouken@2901
   147
                cycle_direction = -cycle_direction;
slouken@2901
   148
            }
slouken@2901
   149
        }
slouken@5147
   150
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
   151
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   152
slouken@5297
   153
        rect.w = rand() % (viewport.h / 2);
slouken@5297
   154
        rect.h = rand() % (viewport.h / 2);
slouken@5297
   155
        rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
slouken@5297
   156
        rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
slouken@5147
   157
        SDL_RenderFillRect(renderer, &rect);
slouken@2901
   158
    }
slouken@2901
   159
}
slouken@2901
   160
slouken@2901
   161
int
slouken@2901
   162
main(int argc, char *argv[])
slouken@2901
   163
{
slouken@2901
   164
    int i, done;
slouken@2901
   165
    SDL_Event event;
slouken@2901
   166
    Uint32 then, now, frames;
slouken@2901
   167
slouken@2901
   168
    /* Initialize parameters */
slouken@2901
   169
    num_objects = NUM_OBJECTS;
slouken@2901
   170
slouken@2901
   171
    /* Initialize test framework */
slouken@2901
   172
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2901
   173
    if (!state) {
slouken@2901
   174
        return 1;
slouken@2901
   175
    }
slouken@2901
   176
    for (i = 1; i < argc;) {
slouken@2901
   177
        int consumed;
slouken@2901
   178
slouken@2901
   179
        consumed = CommonArg(state, i);
slouken@2901
   180
        if (consumed == 0) {
slouken@2901
   181
            consumed = -1;
slouken@2901
   182
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2901
   183
                if (argv[i + 1]) {
slouken@2901
   184
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2901
   185
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2901
   186
                        consumed = 2;
slouken@2901
   187
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2901
   188
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2901
   189
                        consumed = 2;
slouken@2901
   190
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2901
   191
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2901
   192
                        consumed = 2;
slouken@5184
   193
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   194
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   195
                        consumed = 2;
slouken@2901
   196
                    }
slouken@2901
   197
                }
slouken@2901
   198
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2901
   199
                cycle_color = SDL_TRUE;
slouken@2901
   200
                consumed = 1;
slouken@2901
   201
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2901
   202
                cycle_alpha = SDL_TRUE;
slouken@2901
   203
                consumed = 1;
slouken@2901
   204
            } else if (SDL_isdigit(*argv[i])) {
slouken@2901
   205
                num_objects = SDL_atoi(argv[i]);
slouken@2901
   206
                consumed = 1;
slouken@2901
   207
            }
slouken@2901
   208
        }
slouken@2901
   209
        if (consumed < 0) {
slouken@2901
   210
            fprintf(stderr,
slouken@5184
   211
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@2901
   212
                    argv[0], CommonUsage(state));
slouken@2901
   213
            return 1;
slouken@2901
   214
        }
slouken@2901
   215
        i += consumed;
slouken@2901
   216
    }
slouken@2901
   217
    if (!CommonInit(state)) {
slouken@2901
   218
        return 2;
slouken@2901
   219
    }
slouken@2901
   220
slouken@2901
   221
    /* Create the windows and initialize the renderers */
slouken@2901
   222
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   223
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   224
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   225
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   226
        SDL_RenderClear(renderer);
slouken@2901
   227
    }
slouken@2901
   228
aschiffler@4884
   229
    srand((unsigned int)time(NULL));
slouken@2901
   230
slouken@2901
   231
    /* Main render loop */
slouken@2901
   232
    frames = 0;
slouken@2901
   233
    then = SDL_GetTicks();
slouken@2901
   234
    done = 0;
slouken@2901
   235
    while (!done) {
slouken@2901
   236
        /* Check for events */
slouken@2901
   237
        ++frames;
slouken@2901
   238
        while (SDL_PollEvent(&event)) {
slouken@2901
   239
            CommonEvent(state, &event, &done);
slouken@2901
   240
        }
slouken@2901
   241
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   242
            SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   243
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   244
            SDL_RenderClear(renderer);
slouken@2901
   245
slouken@5297
   246
            DrawRects(renderer);
slouken@5297
   247
            DrawLines(renderer);
slouken@5297
   248
            DrawPoints(renderer);
slouken@2901
   249
slouken@5147
   250
            SDL_RenderPresent(renderer);
slouken@2901
   251
        }
slouken@2901
   252
    }
slouken@2901
   253
lestat@3371
   254
    CommonQuit(state);
lestat@3371
   255
slouken@2901
   256
    /* Print out some timing information */
slouken@2901
   257
    now = SDL_GetTicks();
slouken@2901
   258
    if (now > then) {
slouken@2901
   259
        double fps = ((double) frames * 1000) / (now - then);
slouken@2901
   260
        printf("%2.2f frames per second\n", fps);
slouken@2901
   261
    }
slouken@2901
   262
    return 0;
slouken@2901
   263
}
slouken@2901
   264
slouken@2901
   265
/* vi: set ts=4 sw=4 expandtab: */