test/testsprite2.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 07 Jan 2017 22:24:45 -0500
changeset 10782 1de6d4633ee6
parent 10737 3406a0f8b041
child 11282 180e8906dc3c
permissions -rw-r--r--
Fixed a bunch of compiler warnings in the test code.
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@1895
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@1895
    13
slouken@1895
    14
#include <stdlib.h>
slouken@1907
    15
#include <stdio.h>
slouken@1895
    16
#include <time.h>
slouken@1895
    17
icculus@9278
    18
#ifdef __EMSCRIPTEN__
icculus@9278
    19
#include <emscripten/emscripten.h>
icculus@9278
    20
#endif
icculus@9278
    21
aschiffler@7315
    22
#include "SDL_test.h"
slouken@6785
    23
#include "SDL_test_common.h"
slouken@1895
    24
slouken@5147
    25
#define NUM_SPRITES    100
slouken@5147
    26
#define MAX_SPEED     1
slouken@1895
    27
slouken@6785
    28
static SDLTest_CommonState *state;
slouken@1895
    29
static int num_sprites;
slouken@3685
    30
static SDL_Texture **sprites;
slouken@1985
    31
static SDL_bool cycle_color;
slouken@1985
    32
static SDL_bool cycle_alpha;
slouken@1985
    33
static int cycle_direction = 1;
slouken@1985
    34
static int current_alpha = 0;
slouken@1985
    35
static int current_color = 0;
slouken@1895
    36
static SDL_Rect *positions;
slouken@1895
    37
static SDL_Rect *velocities;
slouken@1895
    38
static int sprite_w, sprite_h;
slouken@5140
    39
static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
slouken@1895
    40
aschiffler@7315
    41
/* Number of iterations to move sprites - used for visual tests. */
aschiffler@7315
    42
/* -1: infinite random moves (default); >=0: enables N deterministic moves */
aschiffler@7315
    43
static int iterations = -1;
aschiffler@7315
    44
icculus@9278
    45
int done;
icculus@9278
    46
slouken@1895
    47
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    48
static void
slouken@1895
    49
quit(int rc)
slouken@1895
    50
{
slouken@7719
    51
    SDL_free(sprites);
slouken@7719
    52
    SDL_free(positions);
slouken@7719
    53
    SDL_free(velocities);
slouken@6785
    54
    SDLTest_CommonQuit(state);
slouken@1895
    55
    exit(rc);
slouken@1895
    56
}
slouken@1895
    57
slouken@1895
    58
int
slouken@7502
    59
LoadSprite(const char *file)
slouken@1895
    60
{
slouken@1895
    61
    int i;
slouken@1895
    62
    SDL_Surface *temp;
slouken@1895
    63
slouken@1895
    64
    /* Load the sprite image */
slouken@1895
    65
    temp = SDL_LoadBMP(file);
slouken@1895
    66
    if (temp == NULL) {
aschiffler@7639
    67
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    68
        return (-1);
slouken@1895
    69
    }
slouken@1895
    70
    sprite_w = temp->w;
slouken@1895
    71
    sprite_h = temp->h;
slouken@1895
    72
slouken@1895
    73
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    74
    if (temp->format->palette) {
slouken@3560
    75
        SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
lestat@3183
    76
    } else {
lestat@3183
    77
        switch (temp->format->BitsPerPixel) {
lestat@3183
    78
        case 15:
slouken@3560
    79
            SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
lestat@3183
    80
            break;
lestat@3183
    81
        case 16:
slouken@3560
    82
            SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
lestat@3183
    83
            break;
lestat@3183
    84
        case 24:
slouken@3560
    85
            SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
lestat@3183
    86
            break;
lestat@3183
    87
        case 32:
slouken@3560
    88
            SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
lestat@3183
    89
            break;
lestat@3183
    90
        }
slouken@1895
    91
    }
slouken@1895
    92
slouken@1895
    93
    /* Create textures from the image */
slouken@1914
    94
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
    95
        SDL_Renderer *renderer = state->renderers[i];
slouken@5158
    96
        sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
slouken@3285
    97
        if (!sprites[i]) {
aschiffler@7639
    98
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    99
            SDL_FreeSurface(temp);
slouken@1895
   100
            return (-1);
slouken@1895
   101
        }
slouken@1985
   102
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1895
   103
    }
slouken@1895
   104
    SDL_FreeSurface(temp);
slouken@1895
   105
slouken@1895
   106
    /* We're ready to roll. :) */
slouken@1895
   107
    return (0);
slouken@1895
   108
}
slouken@1895
   109
slouken@1895
   110
void
slouken@5297
   111
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
slouken@1895
   112
{
aschiffler@7315
   113
    int i;
slouken@5297
   114
    SDL_Rect viewport, temp;
slouken@2783
   115
    SDL_Rect *position, *velocity;
slouken@1895
   116
slouken@1895
   117
    /* Query the sizes */
slouken@5297
   118
    SDL_RenderGetViewport(renderer, &viewport);
slouken@1895
   119
slouken@1985
   120
    /* Cycle the color and alpha, if desired */
slouken@1985
   121
    if (cycle_color) {
slouken@1985
   122
        current_color += cycle_direction;
slouken@1985
   123
        if (current_color < 0) {
slouken@1985
   124
            current_color = 0;
slouken@1985
   125
            cycle_direction = -cycle_direction;
slouken@1985
   126
        }
slouken@1985
   127
        if (current_color > 255) {
slouken@1985
   128
            current_color = 255;
slouken@1985
   129
            cycle_direction = -cycle_direction;
slouken@1985
   130
        }
slouken@1985
   131
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   132
                               (Uint8) current_color);
slouken@1985
   133
    }
slouken@1985
   134
    if (cycle_alpha) {
slouken@1985
   135
        current_alpha += cycle_direction;
slouken@1985
   136
        if (current_alpha < 0) {
slouken@1985
   137
            current_alpha = 0;
slouken@1985
   138
            cycle_direction = -cycle_direction;
slouken@1985
   139
        }
slouken@1985
   140
        if (current_alpha > 255) {
slouken@1985
   141
            current_alpha = 255;
slouken@1985
   142
            cycle_direction = -cycle_direction;
slouken@1985
   143
        }
slouken@1985
   144
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   145
    }
slouken@1985
   146
slouken@3264
   147
    /* Draw a gray background */
slouken@5147
   148
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   149
    SDL_RenderClear(renderer);
slouken@3264
   150
slouken@3265
   151
    /* Test points */
slouken@5147
   152
    SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
slouken@5147
   153
    SDL_RenderDrawPoint(renderer, 0, 0);
slouken@5297
   154
    SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
slouken@5297
   155
    SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
slouken@5297
   156
    SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
slouken@3264
   157
slouken@3265
   158
    /* Test horizontal and vertical lines */
slouken@5147
   159
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5297
   160
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
slouken@5297
   161
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
slouken@5297
   162
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
slouken@5297
   163
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
slouken@3265
   164
slouken@3266
   165
    /* Test fill and copy */
slouken@5147
   166
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
slouken@3266
   167
    temp.x = 1;
slouken@3266
   168
    temp.y = 1;
slouken@3266
   169
    temp.w = sprite_w;
slouken@3266
   170
    temp.h = sprite_h;
slouken@5147
   171
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   172
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   173
    temp.x = viewport.w-sprite_w-1;
slouken@3265
   174
    temp.y = 1;
slouken@3265
   175
    temp.w = sprite_w;
slouken@3265
   176
    temp.h = sprite_h;
slouken@5147
   177
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   178
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   179
    temp.x = 1;
slouken@5297
   180
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   181
    temp.w = sprite_w;
slouken@3265
   182
    temp.h = sprite_h;
slouken@5147
   183
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   184
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   185
    temp.x = viewport.w-sprite_w-1;
slouken@5297
   186
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   187
    temp.w = sprite_w;
slouken@3265
   188
    temp.h = sprite_h;
slouken@5147
   189
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   190
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   191
slouken@3265
   192
    /* Test diagonal lines */
slouken@5147
   193
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5147
   194
    SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
slouken@5297
   195
                       viewport.w-sprite_w-2, viewport.h-sprite_h-2);
slouken@5297
   196
    SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
slouken@5297
   197
                       sprite_w, viewport.h-sprite_h-2);
slouken@3265
   198
aschiffler@7315
   199
    /* Conditionally move the sprites, bounce at the wall */
aschiffler@7315
   200
    if (iterations == -1 || iterations > 0) {
aschiffler@7315
   201
        for (i = 0; i < num_sprites; ++i) {
aschiffler@7315
   202
            position = &positions[i];
aschiffler@7315
   203
            velocity = &velocities[i];
aschiffler@7315
   204
            position->x += velocity->x;
aschiffler@7315
   205
            if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
philipp@9922
   206
                velocity->x = -velocity->x;
philipp@9922
   207
                position->x += velocity->x;
aschiffler@7315
   208
            }
aschiffler@7315
   209
            position->y += velocity->y;
aschiffler@7315
   210
            if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
philipp@9922
   211
                velocity->y = -velocity->y;
philipp@9922
   212
                position->y += velocity->y;
aschiffler@7315
   213
            }
aschiffler@7315
   214
aschiffler@7315
   215
        }
aschiffler@7315
   216
        
aschiffler@7315
   217
        /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
aschiffler@7315
   218
        if (iterations > 0) {
aschiffler@7315
   219
            iterations--;
aschiffler@7315
   220
            if (iterations == 0) {
aschiffler@7315
   221
                cycle_alpha = SDL_FALSE;
aschiffler@7315
   222
                cycle_color = SDL_FALSE;
aschiffler@7315
   223
            }
aschiffler@7315
   224
        }
aschiffler@7315
   225
    }
aschiffler@7315
   226
aschiffler@7315
   227
    /* Draw sprites */
slouken@1895
   228
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   229
        position = &positions[i];
philipp@9922
   230
slouken@1895
   231
        /* Blit the sprite onto the screen */
slouken@5147
   232
        SDL_RenderCopy(renderer, sprite, NULL, position);
slouken@1895
   233
    }
slouken@1895
   234
slouken@1895
   235
    /* Update the screen! */
slouken@5147
   236
    SDL_RenderPresent(renderer);
slouken@1895
   237
}
slouken@1895
   238
icculus@9278
   239
void
icculus@9278
   240
loop()
icculus@9278
   241
{
icculus@9278
   242
    int i;
icculus@9278
   243
    SDL_Event event;
icculus@9278
   244
icculus@9278
   245
    /* Check for events */
icculus@9278
   246
    while (SDL_PollEvent(&event)) {
icculus@9278
   247
        SDLTest_CommonEvent(state, &event, &done);
icculus@9278
   248
    }
icculus@9278
   249
    for (i = 0; i < state->num_windows; ++i) {
icculus@9278
   250
        if (state->windows[i] == NULL)
icculus@9278
   251
            continue;
icculus@9278
   252
        MoveSprites(state->renderers[i], sprites[i]);
icculus@9278
   253
    }
philipp@9607
   254
#ifdef __EMSCRIPTEN__
philipp@9607
   255
    if (done) {
philipp@9607
   256
        emscripten_cancel_main_loop();
philipp@9607
   257
    }
philipp@9607
   258
#endif
icculus@9278
   259
}
icculus@9278
   260
slouken@1895
   261
int
slouken@1895
   262
main(int argc, char *argv[])
slouken@1895
   263
{
icculus@9278
   264
    int i;
slouken@1895
   265
    Uint32 then, now, frames;
philipp@9922
   266
    Uint64 seed;
slouken@7502
   267
    const char *icon = "icon.bmp";
slouken@1895
   268
slouken@1914
   269
    /* Initialize parameters */
slouken@1914
   270
    num_sprites = NUM_SPRITES;
slouken@1914
   271
slouken@1914
   272
    /* Initialize test framework */
slouken@6785
   273
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   274
    if (!state) {
slouken@1914
   275
        return 1;
slouken@1895
   276
    }
icculus@5520
   277
slouken@1914
   278
    for (i = 1; i < argc;) {
slouken@1914
   279
        int consumed;
slouken@1895
   280
slouken@6785
   281
        consumed = SDLTest_CommonArg(state, i);
slouken@1915
   282
        if (consumed == 0) {
slouken@1916
   283
            consumed = -1;
slouken@1916
   284
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   285
                if (argv[i + 1]) {
slouken@1916
   286
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   287
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   288
                        consumed = 2;
slouken@1916
   289
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   290
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   291
                        consumed = 2;
slouken@1916
   292
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   293
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   294
                        consumed = 2;
slouken@5184
   295
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   296
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   297
                        consumed = 2;
slouken@1916
   298
                    }
slouken@1916
   299
                }
aschiffler@7315
   300
            } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
aschiffler@7315
   301
                if (argv[i + 1]) {
aschiffler@7315
   302
                    iterations = SDL_atoi(argv[i + 1]);
aschiffler@7315
   303
                    if (iterations < -1) iterations = -1;
aschiffler@7315
   304
                    consumed = 2;
aschiffler@7315
   305
                }
slouken@1985
   306
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   307
                cycle_color = SDL_TRUE;
slouken@1985
   308
                consumed = 1;
slouken@1985
   309
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   310
                cycle_alpha = SDL_TRUE;
slouken@1985
   311
                consumed = 1;
slouken@1916
   312
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   313
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   314
                consumed = 1;
slouken@7502
   315
            } else if (argv[i][0] != '-') {
slouken@7502
   316
                icon = argv[i];
slouken@7502
   317
                consumed = 1;
slouken@1916
   318
            }
slouken@1915
   319
        }
slouken@1914
   320
        if (consumed < 0) {
aschiffler@7639
   321
            SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha] [--iterations N] [num_sprites] [icon.bmp]\n",
slouken@6785
   322
                    argv[0], SDLTest_CommonUsage(state));
slouken@1895
   323
            quit(1);
slouken@1895
   324
        }
slouken@1914
   325
        i += consumed;
slouken@1895
   326
    }
slouken@6785
   327
    if (!SDLTest_CommonInit(state)) {
slouken@1914
   328
        quit(2);
slouken@1895
   329
    }
slouken@1895
   330
slouken@1895
   331
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   332
    sprites =
slouken@3685
   333
        (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   334
    if (!sprites) {
aschiffler@7639
   335
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
slouken@1895
   336
        quit(2);
slouken@1895
   337
    }
slouken@1914
   338
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   339
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   340
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   341
        SDL_RenderClear(renderer);
slouken@1895
   342
    }
slouken@7502
   343
    if (LoadSprite(icon) < 0) {
slouken@1895
   344
        quit(2);
slouken@1895
   345
    }
slouken@1895
   346
slouken@1895
   347
    /* Allocate memory for the sprite info */
slouken@1895
   348
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   349
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   350
    if (!positions || !velocities) {
aschiffler@7639
   351
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
slouken@1895
   352
        quit(2);
slouken@1895
   353
    }
aschiffler@7315
   354
aschiffler@7315
   355
    /* Position sprites and set their velocities using the fuzzer */ 
aschiffler@7315
   356
    if (iterations >= 0) {
aschiffler@7315
   357
        /* Deterministic seed - used for visual tests */
aschiffler@7315
   358
        seed = (Uint64)iterations;
aschiffler@7315
   359
    } else {
aschiffler@7315
   360
        /* Pseudo-random seed generated from the time */
aschiffler@7315
   361
        seed = (Uint64)time(NULL);
aschiffler@7315
   362
    }
aschiffler@7315
   363
    SDLTest_FuzzerInit(seed);
slouken@1895
   364
    for (i = 0; i < num_sprites; ++i) {
aschiffler@7315
   365
        positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
aschiffler@7315
   366
        positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
slouken@1895
   367
        positions[i].w = sprite_w;
slouken@1895
   368
        positions[i].h = sprite_h;
slouken@1895
   369
        velocities[i].x = 0;
slouken@1895
   370
        velocities[i].y = 0;
slouken@1895
   371
        while (!velocities[i].x && !velocities[i].y) {
aschiffler@7315
   372
            velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
aschiffler@7315
   373
            velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
slouken@1895
   374
        }
slouken@1895
   375
    }
slouken@1895
   376
slouken@1915
   377
    /* Main render loop */
slouken@1895
   378
    frames = 0;
slouken@1895
   379
    then = SDL_GetTicks();
slouken@1895
   380
    done = 0;
icculus@9278
   381
icculus@9278
   382
#ifdef __EMSCRIPTEN__
icculus@9278
   383
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   384
#else
slouken@1895
   385
    while (!done) {
slouken@1895
   386
        ++frames;
icculus@9278
   387
        loop();
slouken@1895
   388
    }
icculus@9278
   389
#endif
slouken@1895
   390
slouken@1895
   391
    /* Print out some timing information */
slouken@1895
   392
    now = SDL_GetTicks();
slouken@1895
   393
    if (now > then) {
slouken@2786
   394
        double fps = ((double) frames * 1000) / (now - then);
aschiffler@7639
   395
        SDL_Log("%2.2f frames per second\n", fps);
slouken@1895
   396
    }
slouken@1895
   397
    quit(0);
slouken@5147
   398
    return 0;
slouken@1895
   399
}
slouken@1895
   400
slouken@1895
   401
/* vi: set ts=4 sw=4 expandtab: */