test/testdraw2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
    }
philipp@9607
   201
#ifdef __EMSCRIPTEN__
philipp@9607
   202
    if (done) {
philipp@9607
   203
        emscripten_cancel_main_loop();
philipp@9607
   204
    }
philipp@9607
   205
#endif
icculus@9278
   206
}
icculus@9278
   207
slouken@2901
   208
int
slouken@2901
   209
main(int argc, char *argv[])
slouken@2901
   210
{
icculus@9278
   211
    int i;
slouken@2901
   212
    Uint32 then, now, frames;
slouken@2901
   213
philipp@9922
   214
    /* Enable standard application logging */
philipp@9922
   215
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
   216
slouken@2901
   217
    /* Initialize parameters */
slouken@2901
   218
    num_objects = NUM_OBJECTS;
slouken@2901
   219
slouken@2901
   220
    /* Initialize test framework */
slouken@6785
   221
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@2901
   222
    if (!state) {
slouken@2901
   223
        return 1;
slouken@2901
   224
    }
slouken@2901
   225
    for (i = 1; i < argc;) {
slouken@2901
   226
        int consumed;
slouken@2901
   227
slouken@6785
   228
        consumed = SDLTest_CommonArg(state, i);
slouken@2901
   229
        if (consumed == 0) {
slouken@2901
   230
            consumed = -1;
slouken@2901
   231
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@2901
   232
                if (argv[i + 1]) {
slouken@2901
   233
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2901
   234
                        blendMode = SDL_BLENDMODE_NONE;
slouken@2901
   235
                        consumed = 2;
slouken@2901
   236
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2901
   237
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@2901
   238
                        consumed = 2;
slouken@2901
   239
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2901
   240
                        blendMode = SDL_BLENDMODE_ADD;
slouken@2901
   241
                        consumed = 2;
slouken@5184
   242
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   243
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   244
                        consumed = 2;
slouken@2901
   245
                    }
slouken@2901
   246
                }
slouken@2901
   247
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@2901
   248
                cycle_color = SDL_TRUE;
slouken@2901
   249
                consumed = 1;
slouken@2901
   250
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@2901
   251
                cycle_alpha = SDL_TRUE;
slouken@2901
   252
                consumed = 1;
slouken@2901
   253
            } else if (SDL_isdigit(*argv[i])) {
slouken@2901
   254
                num_objects = SDL_atoi(argv[i]);
slouken@2901
   255
                consumed = 1;
slouken@2901
   256
            }
slouken@2901
   257
        }
slouken@2901
   258
        if (consumed < 0) {
aschiffler@7639
   259
            SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@6785
   260
                    argv[0], SDLTest_CommonUsage(state));
slouken@2901
   261
            return 1;
slouken@2901
   262
        }
slouken@2901
   263
        i += consumed;
slouken@2901
   264
    }
slouken@6785
   265
    if (!SDLTest_CommonInit(state)) {
slouken@2901
   266
        return 2;
slouken@2901
   267
    }
slouken@2901
   268
slouken@2901
   269
    /* Create the windows and initialize the renderers */
slouken@2901
   270
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   271
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   272
        SDL_SetRenderDrawBlendMode(renderer, blendMode);
slouken@5147
   273
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   274
        SDL_RenderClear(renderer);
slouken@2901
   275
    }
slouken@2901
   276
aschiffler@4884
   277
    srand((unsigned int)time(NULL));
slouken@2901
   278
slouken@2901
   279
    /* Main render loop */
slouken@2901
   280
    frames = 0;
slouken@2901
   281
    then = SDL_GetTicks();
slouken@2901
   282
    done = 0;
icculus@9278
   283
icculus@9278
   284
#ifdef __EMSCRIPTEN__
icculus@9278
   285
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   286
#else
slouken@2901
   287
    while (!done) {
slouken@2901
   288
        ++frames;
icculus@9278
   289
        loop();
slouken@2901
   290
        }
icculus@9278
   291
#endif
slouken@2901
   292
slouken@2901
   293
slouken@6785
   294
    SDLTest_CommonQuit(state);
lestat@3371
   295
slouken@2901
   296
    /* Print out some timing information */
slouken@2901
   297
    now = SDL_GetTicks();
slouken@2901
   298
    if (now > then) {
slouken@2901
   299
        double fps = ((double) frames * 1000) / (now - then);
aschiffler@7639
   300
        SDL_Log("%2.2f frames per second\n", fps);
slouken@2901
   301
    }
slouken@2901
   302
    return 0;
slouken@2901
   303
}
slouken@2901
   304
slouken@2901
   305
/* vi: set ts=4 sw=4 expandtab: */