test/testdraw2.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 11 Mar 2015 21:14:21 +0100
changeset 9380 07b7c1005a23
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Updated internal documentation comments.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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@2901
    12
slouken@2901
    13
/* Simple program:  draw as many random objects on the screen as possible */
slouken@2901
    14
slouken@2901
    15
#include <stdlib.h>
slouken@2901
    16
#include <stdio.h>
slouken@2901
    17
#include <time.h>
slouken@2901
    18
icculus@9278
    19
#ifdef __EMSCRIPTEN__
icculus@9278
    20
#include <emscripten/emscripten.h>
icculus@9278
    21
#endif
icculus@9278
    22
slouken@6785
    23
#include "SDL_test_common.h"
slouken@2901
    24
slouken@7191
    25
#define NUM_OBJECTS 100
slouken@2901
    26
slouken@6785
    27
static SDLTest_CommonState *state;
slouken@2901
    28
static int num_objects;
slouken@2901
    29
static SDL_bool cycle_color;
slouken@2901
    30
static SDL_bool cycle_alpha;
slouken@2901
    31
static int cycle_direction = 1;
slouken@2901
    32
static int current_alpha = 255;
slouken@2901
    33
static int current_color = 255;
slouken@2901
    34
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
slouken@2901
    35
icculus@9278
    36
int done;
icculus@9278
    37
slouken@2901
    38
void
slouken@5297
    39
DrawPoints(SDL_Renderer * renderer)
slouken@2901
    40
{
slouken@2901
    41
    int i;
slouken@2901
    42
    int x, y;
slouken@5297
    43
    SDL_Rect viewport;
slouken@2901
    44
slouken@2901
    45
    /* Query the sizes */
slouken@5297
    46
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
    47
slouken@2902
    48
    for (i = 0; i < num_objects * 4; ++i) {
slouken@2901
    49
        /* Cycle the color and alpha, if desired */
slouken@2901
    50
        if (cycle_color) {
slouken@2901
    51
            current_color += cycle_direction;
slouken@2901
    52
            if (current_color < 0) {
slouken@2901
    53
                current_color = 0;
slouken@2901
    54
                cycle_direction = -cycle_direction;
slouken@2901
    55
            }
slouken@2901
    56
            if (current_color > 255) {
slouken@2901
    57
                current_color = 255;
slouken@2901
    58
                cycle_direction = -cycle_direction;
slouken@2901
    59
            }
slouken@2901
    60
        }
slouken@2901
    61
        if (cycle_alpha) {
slouken@2901
    62
            current_alpha += cycle_direction;
slouken@2901
    63
            if (current_alpha < 0) {
slouken@2901
    64
                current_alpha = 0;
slouken@2901
    65
                cycle_direction = -cycle_direction;
slouken@2901
    66
            }
slouken@2901
    67
            if (current_alpha > 255) {
slouken@2901
    68
                current_alpha = 255;
slouken@2901
    69
                cycle_direction = -cycle_direction;
slouken@2901
    70
            }
slouken@2901
    71
        }
slouken@5147
    72
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
    73
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
    74
slouken@5297
    75
        x = rand() % viewport.w;
slouken@5297
    76
        y = rand() % viewport.h;
slouken@5147
    77
        SDL_RenderDrawPoint(renderer, x, y);
slouken@2901
    78
    }
slouken@2901
    79
}
slouken@2901
    80
slouken@2901
    81
void
slouken@5297
    82
DrawLines(SDL_Renderer * renderer)
slouken@2901
    83
{
slouken@2901
    84
    int i;
slouken@2901
    85
    int x1, y1, x2, y2;
slouken@5297
    86
    SDL_Rect viewport;
slouken@2901
    87
slouken@2901
    88
    /* Query the sizes */
slouken@5297
    89
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
    90
slouken@2901
    91
    for (i = 0; i < num_objects; ++i) {
slouken@2901
    92
        /* Cycle the color and alpha, if desired */
slouken@2901
    93
        if (cycle_color) {
slouken@2901
    94
            current_color += cycle_direction;
slouken@2901
    95
            if (current_color < 0) {
slouken@2901
    96
                current_color = 0;
slouken@2901
    97
                cycle_direction = -cycle_direction;
slouken@2901
    98
            }
slouken@2901
    99
            if (current_color > 255) {
slouken@2901
   100
                current_color = 255;
slouken@2901
   101
                cycle_direction = -cycle_direction;
slouken@2901
   102
            }
slouken@2901
   103
        }
slouken@2901
   104
        if (cycle_alpha) {
slouken@2901
   105
            current_alpha += cycle_direction;
slouken@2901
   106
            if (current_alpha < 0) {
slouken@2901
   107
                current_alpha = 0;
slouken@2901
   108
                cycle_direction = -cycle_direction;
slouken@2901
   109
            }
slouken@2901
   110
            if (current_alpha > 255) {
slouken@2901
   111
                current_alpha = 255;
slouken@2901
   112
                cycle_direction = -cycle_direction;
slouken@2901
   113
            }
slouken@2901
   114
        }
slouken@5147
   115
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
   116
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   117
slouken@2901
   118
        if (i == 0) {
slouken@5297
   119
            SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
slouken@5297
   120
            SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
slouken@5297
   121
            SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
slouken@5297
   122
            SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
slouken@2901
   123
        } else {
slouken@5297
   124
            x1 = (rand() % (viewport.w*2)) - viewport.w;
slouken@5297
   125
            x2 = (rand() % (viewport.w*2)) - viewport.w;
slouken@5297
   126
            y1 = (rand() % (viewport.h*2)) - viewport.h;
slouken@5297
   127
            y2 = (rand() % (viewport.h*2)) - viewport.h;
slouken@5147
   128
            SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
slouken@2901
   129
        }
slouken@2901
   130
    }
slouken@2901
   131
}
slouken@2901
   132
slouken@2901
   133
void
slouken@5297
   134
DrawRects(SDL_Renderer * renderer)
slouken@2901
   135
{
slouken@2901
   136
    int i;
slouken@2901
   137
    SDL_Rect rect;
slouken@5297
   138
    SDL_Rect viewport;
slouken@2901
   139
slouken@2901
   140
    /* Query the sizes */
slouken@5297
   141
    SDL_RenderGetViewport(renderer, &viewport);
slouken@2901
   142
slouken@2902
   143
    for (i = 0; i < num_objects / 4; ++i) {
slouken@2901
   144
        /* Cycle the color and alpha, if desired */
slouken@2901
   145
        if (cycle_color) {
slouken@2901
   146
            current_color += cycle_direction;
slouken@2901
   147
            if (current_color < 0) {
slouken@2901
   148
                current_color = 0;
slouken@2901
   149
                cycle_direction = -cycle_direction;
slouken@2901
   150
            }
slouken@2901
   151
            if (current_color > 255) {
slouken@2901
   152
                current_color = 255;
slouken@2901
   153
                cycle_direction = -cycle_direction;
slouken@2901
   154
            }
slouken@2901
   155
        }
slouken@2901
   156
        if (cycle_alpha) {
slouken@2901
   157
            current_alpha += cycle_direction;
slouken@2901
   158
            if (current_alpha < 0) {
slouken@2901
   159
                current_alpha = 0;
slouken@2901
   160
                cycle_direction = -cycle_direction;
slouken@2901
   161
            }
slouken@2901
   162
            if (current_alpha > 255) {
slouken@2901
   163
                current_alpha = 255;
slouken@2901
   164
                cycle_direction = -cycle_direction;
slouken@2901
   165
            }
slouken@2901
   166
        }
slouken@5147
   167
        SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
slouken@2901
   168
                               (Uint8) current_color, (Uint8) current_alpha);
slouken@2901
   169
slouken@5297
   170
        rect.w = rand() % (viewport.h / 2);
slouken@5297
   171
        rect.h = rand() % (viewport.h / 2);
slouken@5297
   172
        rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
slouken@5297
   173
        rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
slouken@5147
   174
        SDL_RenderFillRect(renderer, &rect);
slouken@2901
   175
    }
slouken@2901
   176
}
slouken@2901
   177
icculus@9278
   178
void
icculus@9278
   179
loop()
icculus@9278
   180
{
icculus@9278
   181
    int i;
icculus@9278
   182
    SDL_Event event;
icculus@9278
   183
icculus@9278
   184
    /* Check for events */
icculus@9278
   185
    while (SDL_PollEvent(&event)) {
icculus@9278
   186
        SDLTest_CommonEvent(state, &event, &done);
icculus@9278
   187
    }
icculus@9278
   188
    for (i = 0; i < state->num_windows; ++i) {
icculus@9278
   189
        SDL_Renderer *renderer = state->renderers[i];
icculus@9278
   190
        if (state->windows[i] == NULL)
icculus@9278
   191
            continue;
icculus@9278
   192
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
icculus@9278
   193
        SDL_RenderClear(renderer);
icculus@9278
   194
icculus@9278
   195
        DrawRects(renderer);
icculus@9278
   196
        DrawLines(renderer);
icculus@9278
   197
        DrawPoints(renderer);
icculus@9278
   198
icculus@9278
   199
        SDL_RenderPresent(renderer);
icculus@9278
   200
    }
icculus@9278
   201
}
icculus@9278
   202
slouken@2901
   203
int
slouken@2901
   204
main(int argc, char *argv[])
slouken@2901
   205
{
icculus@9278
   206
    int i;
slouken@2901
   207
    Uint32 then, now, frames;
slouken@2901
   208
aschiffler@7639
   209
	/* Enable standard application logging */
aschiffler@7639
   210
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   211
slouken@2901
   212
    /* Initialize parameters */
slouken@2901
   213
    num_objects = NUM_OBJECTS;
slouken@2901
   214
slouken@2901
   215
    /* Initialize test framework */
slouken@6785
   216
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2901
   217
    if (!state) {
slouken@2901
   218
        return 1;
slouken@2901
   219
    }
slouken@2901
   220
    for (i = 1; i < argc;) {
slouken@2901
   221
        int consumed;
slouken@2901
   222
slouken@6785
   223
        consumed = SDLTest_CommonArg(state, i);
slouken@2901
   224
        if (consumed == 0) {
slouken@2901
   225
            consumed = -1;
slouken@2901
   226
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2901
   227
                if (argv[i + 1]) {
slouken@2901
   228
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2901
   229
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2901
   230
                        consumed = 2;
slouken@2901
   231
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2901
   232
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2901
   233
                        consumed = 2;
slouken@2901
   234
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2901
   235
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2901
   236
                        consumed = 2;
slouken@5184
   237
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   238
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   239
                        consumed = 2;
slouken@2901
   240
                    }
slouken@2901
   241
                }
slouken@2901
   242
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2901
   243
                cycle_color = SDL_TRUE;
slouken@2901
   244
                consumed = 1;
slouken@2901
   245
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2901
   246
                cycle_alpha = SDL_TRUE;
slouken@2901
   247
                consumed = 1;
slouken@2901
   248
            } else if (SDL_isdigit(*argv[i])) {
slouken@2901
   249
                num_objects = SDL_atoi(argv[i]);
slouken@2901
   250
                consumed = 1;
slouken@2901
   251
            }
slouken@2901
   252
        }
slouken@2901
   253
        if (consumed < 0) {
aschiffler@7639
   254
            SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@6785
   255
                    argv[0], SDLTest_CommonUsage(state));
slouken@2901
   256
            return 1;
slouken@2901
   257
        }
slouken@2901
   258
        i += consumed;
slouken@2901
   259
    }
slouken@6785
   260
    if (!SDLTest_CommonInit(state)) {
slouken@2901
   261
        return 2;
slouken@2901
   262
    }
slouken@2901
   263
slouken@2901
   264
    /* Create the windows and initialize the renderers */
slouken@2901
   265
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   266
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   267
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   268
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   269
        SDL_RenderClear(renderer);
slouken@2901
   270
    }
slouken@2901
   271
aschiffler@4884
   272
    srand((unsigned int)time(NULL));
slouken@2901
   273
slouken@2901
   274
    /* Main render loop */
slouken@2901
   275
    frames = 0;
slouken@2901
   276
    then = SDL_GetTicks();
slouken@2901
   277
    done = 0;
icculus@9278
   278
icculus@9278
   279
#ifdef __EMSCRIPTEN__
icculus@9278
   280
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   281
#else
slouken@2901
   282
    while (!done) {
slouken@2901
   283
        ++frames;
icculus@9278
   284
        loop();
slouken@2901
   285
        }
icculus@9278
   286
#endif
slouken@2901
   287
slouken@2901
   288
slouken@6785
   289
    SDLTest_CommonQuit(state);
lestat@3371
   290
slouken@2901
   291
    /* Print out some timing information */
slouken@2901
   292
    now = SDL_GetTicks();
slouken@2901
   293
    if (now > then) {
slouken@2901
   294
        double fps = ((double) frames * 1000) / (now - then);
aschiffler@7639
   295
        SDL_Log("%2.2f frames per second\n", fps);
slouken@2901
   296
    }
slouken@2901
   297
    return 0;
slouken@2901
   298
}
slouken@2901
   299
slouken@2901
   300
/* vi: set ts=4 sw=4 expandtab: */