test/testdraw2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147 ad50b3db78bd
parent 5140 e743b9c3f6d6
child 5184 d976b67150c5
permissions -rw-r--r--
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
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@5147
    22
DrawPoints(SDL_Window * window, SDL_Renderer * renderer)
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@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@2901
    58
        x = rand() % window_w;
slouken@2901
    59
        y = rand() % window_h;
slouken@5147
    60
        SDL_RenderDrawPoint(renderer, x, y);
slouken@2901
    61
    }
slouken@2901
    62
}
slouken@2901
    63
slouken@2901
    64
void
slouken@5147
    65
DrawLines(SDL_Window * window, SDL_Renderer * renderer)
slouken@2901
    66
{
slouken@2901
    67
    int i;
slouken@2901
    68
    int x1, y1, x2, y2;
slouken@2901
    69
    int window_w, window_h;
slouken@2901
    70
slouken@2901
    71
    /* Query the sizes */
slouken@2901
    72
    SDL_GetWindowSize(window, &window_w, &window_h);
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@5147
   102
            SDL_RenderDrawLine(renderer, 0, 0, window_w - 1, window_h - 1);
slouken@5147
   103
            SDL_RenderDrawLine(renderer, 0, window_h - 1, window_w - 1, 0);
slouken@5147
   104
            SDL_RenderDrawLine(renderer, 0, window_h / 2, window_w - 1, window_h / 2);
slouken@5147
   105
            SDL_RenderDrawLine(renderer, window_w / 2, 0, window_w / 2, window_h - 1);
slouken@2901
   106
        } else {
slouken@3546
   107
            x1 = (rand() % (window_w*2)) - window_w;
slouken@3546
   108
            x2 = (rand() % (window_w*2)) - window_w;
slouken@3546
   109
            y1 = (rand() % (window_h*2)) - window_h;
slouken@3546
   110
            y2 = (rand() % (window_h*2)) - window_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@5147
   117
DrawRects(SDL_Window * window, SDL_Renderer * renderer)
slouken@2901
   118
{
slouken@2901
   119
    int i;
slouken@2901
   120
    SDL_Rect rect;
slouken@2901
   121
    int window_w, window_h;
slouken@2901
   122
slouken@2901
   123
    /* Query the sizes */
slouken@2901
   124
    SDL_GetWindowSize(window, &window_w, &window_h);
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@2901
   153
        rect.w = rand() % (window_h / 2);
slouken@2901
   154
        rect.h = rand() % (window_h / 2);
slouken@3546
   155
        rect.x = (rand() % (window_w*2) - window_w) - (rect.w / 2);
slouken@3546
   156
        rect.y = (rand() % (window_h*2) - window_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@2901
   193
                    }
slouken@2901
   194
                }
slouken@2901
   195
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2901
   196
                cycle_color = SDL_TRUE;
slouken@2901
   197
                consumed = 1;
slouken@2901
   198
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2901
   199
                cycle_alpha = SDL_TRUE;
slouken@2901
   200
                consumed = 1;
slouken@2901
   201
            } else if (SDL_isdigit(*argv[i])) {
slouken@2901
   202
                num_objects = SDL_atoi(argv[i]);
slouken@2901
   203
                consumed = 1;
slouken@2901
   204
            }
slouken@2901
   205
        }
slouken@2901
   206
        if (consumed < 0) {
slouken@2901
   207
            fprintf(stderr,
slouken@5140
   208
                    "Usage: %s %s [--blend none|blend|add] [--cyclecolor] [--cyclealpha]\n",
slouken@2901
   209
                    argv[0], CommonUsage(state));
slouken@2901
   210
            return 1;
slouken@2901
   211
        }
slouken@2901
   212
        i += consumed;
slouken@2901
   213
    }
slouken@2901
   214
    if (!CommonInit(state)) {
slouken@2901
   215
        return 2;
slouken@2901
   216
    }
slouken@2901
   217
slouken@2901
   218
    /* Create the windows and initialize the renderers */
slouken@2901
   219
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   220
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   221
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   222
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   223
        SDL_RenderClear(renderer);
slouken@2901
   224
    }
slouken@2901
   225
aschiffler@4884
   226
    srand((unsigned int)time(NULL));
slouken@2901
   227
slouken@2901
   228
    /* Main render loop */
slouken@2901
   229
    frames = 0;
slouken@2901
   230
    then = SDL_GetTicks();
slouken@2901
   231
    done = 0;
slouken@2901
   232
    while (!done) {
slouken@2901
   233
        /* Check for events */
slouken@2901
   234
        ++frames;
slouken@2901
   235
        while (SDL_PollEvent(&event)) {
slouken@2901
   236
            CommonEvent(state, &event, &done);
slouken@2901
   237
        }
slouken@2901
   238
        for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   239
            SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   240
            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   241
            SDL_RenderClear(renderer);
slouken@2901
   242
slouken@5147
   243
            DrawRects(state->windows[i], renderer);
slouken@5147
   244
            DrawLines(state->windows[i], renderer);
slouken@5147
   245
            DrawPoints(state->windows[i], renderer);
slouken@2901
   246
slouken@5147
   247
            SDL_RenderPresent(renderer);
slouken@2901
   248
        }
slouken@2901
   249
    }
slouken@2901
   250
lestat@3371
   251
    CommonQuit(state);
lestat@3371
   252
slouken@2901
   253
    /* Print out some timing information */
slouken@2901
   254
    now = SDL_GetTicks();
slouken@2901
   255
    if (now > then) {
slouken@2901
   256
        double fps = ((double) frames * 1000) / (now - then);
slouken@2901
   257
        printf("%2.2f frames per second\n", fps);
slouken@2901
   258
    }
slouken@2901
   259
    return 0;
slouken@2901
   260
}
slouken@2901
   261
slouken@2901
   262
/* vi: set ts=4 sw=4 expandtab: */