test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7502 6ff02ff3cf06
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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
aschiffler@7315
    18
#include "SDL_test.h"
slouken@6785
    19
#include "SDL_test_common.h"
slouken@1895
    20
slouken@5147
    21
#define NUM_SPRITES    100
slouken@5147
    22
#define MAX_SPEED     1
slouken@1895
    23
slouken@6785
    24
static SDLTest_CommonState *state;
slouken@1895
    25
static int num_sprites;
slouken@3685
    26
static SDL_Texture **sprites;
slouken@1985
    27
static SDL_bool cycle_color;
slouken@1985
    28
static SDL_bool cycle_alpha;
slouken@1985
    29
static int cycle_direction = 1;
slouken@1985
    30
static int current_alpha = 0;
slouken@1985
    31
static int current_color = 0;
slouken@1895
    32
static SDL_Rect *positions;
slouken@1895
    33
static SDL_Rect *velocities;
slouken@1895
    34
static int sprite_w, sprite_h;
slouken@5140
    35
static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
slouken@1895
    36
aschiffler@7315
    37
/* Number of iterations to move sprites - used for visual tests. */
aschiffler@7315
    38
/* -1: infinite random moves (default); >=0: enables N deterministic moves */
aschiffler@7315
    39
static int iterations = -1;
aschiffler@7315
    40
slouken@1895
    41
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    42
static void
slouken@1895
    43
quit(int rc)
slouken@1895
    44
{
slouken@1895
    45
    if (sprites) {
slouken@1895
    46
        SDL_free(sprites);
slouken@1895
    47
    }
slouken@1895
    48
    if (positions) {
slouken@1895
    49
        SDL_free(positions);
slouken@1895
    50
    }
slouken@1895
    51
    if (velocities) {
slouken@1895
    52
        SDL_free(velocities);
slouken@1895
    53
    }
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) {
slouken@1895
    67
        fprintf(stderr, "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]) {
slouken@1895
    98
            fprintf(stderr, "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))) {
aschiffler@7315
   206
            	velocity->x = -velocity->x;
aschiffler@7315
   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))) {
aschiffler@7315
   211
            	velocity->y = -velocity->y;
aschiffler@7315
   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];
aschiffler@7315
   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
slouken@1895
   239
int
slouken@1895
   240
main(int argc, char *argv[])
slouken@1895
   241
{
slouken@1895
   242
    int i, done;
slouken@1895
   243
    SDL_Event event;
slouken@1895
   244
    Uint32 then, now, frames;
aschiffler@7315
   245
	Uint64 seed;
slouken@7502
   246
    const char *icon = "icon.bmp";
slouken@1895
   247
slouken@1914
   248
    /* Initialize parameters */
slouken@1914
   249
    num_sprites = NUM_SPRITES;
slouken@1914
   250
slouken@1914
   251
    /* Initialize test framework */
slouken@6785
   252
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   253
    if (!state) {
slouken@1914
   254
        return 1;
slouken@1895
   255
    }
icculus@5520
   256
icculus@5520
   257
    state->window_flags |= SDL_WINDOW_RESIZABLE;
icculus@5520
   258
slouken@1914
   259
    for (i = 1; i < argc;) {
slouken@1914
   260
        int consumed;
slouken@1895
   261
slouken@6785
   262
        consumed = SDLTest_CommonArg(state, i);
slouken@1915
   263
        if (consumed == 0) {
slouken@1916
   264
            consumed = -1;
slouken@1916
   265
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   266
                if (argv[i + 1]) {
slouken@1916
   267
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   268
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   269
                        consumed = 2;
slouken@1916
   270
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   271
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   272
                        consumed = 2;
slouken@1916
   273
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   274
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   275
                        consumed = 2;
slouken@5184
   276
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   277
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   278
                        consumed = 2;
slouken@1916
   279
                    }
slouken@1916
   280
                }
aschiffler@7315
   281
            } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
aschiffler@7315
   282
                if (argv[i + 1]) {
aschiffler@7315
   283
                    iterations = SDL_atoi(argv[i + 1]);
aschiffler@7315
   284
                    if (iterations < -1) iterations = -1;
aschiffler@7315
   285
                    consumed = 2;
aschiffler@7315
   286
                }
slouken@1985
   287
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   288
                cycle_color = SDL_TRUE;
slouken@1985
   289
                consumed = 1;
slouken@1985
   290
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   291
                cycle_alpha = SDL_TRUE;
slouken@1985
   292
                consumed = 1;
slouken@1916
   293
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   294
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   295
                consumed = 1;
slouken@7502
   296
            } else if (argv[i][0] != '-') {
slouken@7502
   297
                icon = argv[i];
slouken@7502
   298
                consumed = 1;
slouken@1916
   299
            }
slouken@1915
   300
        }
slouken@1914
   301
        if (consumed < 0) {
slouken@1917
   302
            fprintf(stderr,
slouken@7502
   303
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha] [--iterations N] [num_sprites] [icon.bmp]\n",
slouken@6785
   304
                    argv[0], SDLTest_CommonUsage(state));
slouken@1895
   305
            quit(1);
slouken@1895
   306
        }
slouken@1914
   307
        i += consumed;
slouken@1895
   308
    }
slouken@6785
   309
    if (!SDLTest_CommonInit(state)) {
slouken@1914
   310
        quit(2);
slouken@1895
   311
    }
slouken@1895
   312
slouken@1895
   313
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   314
    sprites =
slouken@3685
   315
        (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   316
    if (!sprites) {
slouken@1895
   317
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   318
        quit(2);
slouken@1895
   319
    }
slouken@1914
   320
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   321
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   322
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   323
        SDL_RenderClear(renderer);
slouken@1895
   324
    }
slouken@7502
   325
    if (LoadSprite(icon) < 0) {
slouken@1895
   326
        quit(2);
slouken@1895
   327
    }
slouken@1895
   328
slouken@1895
   329
    /* Allocate memory for the sprite info */
slouken@1895
   330
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   331
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   332
    if (!positions || !velocities) {
slouken@1895
   333
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   334
        quit(2);
slouken@1895
   335
    }
aschiffler@7315
   336
aschiffler@7315
   337
    /* Position sprites and set their velocities using the fuzzer */ 
aschiffler@7315
   338
    if (iterations >= 0) {
aschiffler@7315
   339
        /* Deterministic seed - used for visual tests */
aschiffler@7315
   340
        seed = (Uint64)iterations;
aschiffler@7315
   341
    } else {
aschiffler@7315
   342
        /* Pseudo-random seed generated from the time */
aschiffler@7315
   343
        seed = (Uint64)time(NULL);
aschiffler@7315
   344
    }
aschiffler@7315
   345
    SDLTest_FuzzerInit(seed);
slouken@1895
   346
    for (i = 0; i < num_sprites; ++i) {
aschiffler@7315
   347
        positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
aschiffler@7315
   348
        positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
slouken@1895
   349
        positions[i].w = sprite_w;
slouken@1895
   350
        positions[i].h = sprite_h;
slouken@1895
   351
        velocities[i].x = 0;
slouken@1895
   352
        velocities[i].y = 0;
slouken@1895
   353
        while (!velocities[i].x && !velocities[i].y) {
aschiffler@7315
   354
            velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
aschiffler@7315
   355
            velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
slouken@1895
   356
        }
slouken@1895
   357
    }
slouken@1895
   358
slouken@1915
   359
    /* Main render loop */
slouken@1895
   360
    frames = 0;
slouken@1895
   361
    then = SDL_GetTicks();
slouken@1895
   362
    done = 0;
slouken@1895
   363
    while (!done) {
slouken@1895
   364
        /* Check for events */
slouken@1895
   365
        ++frames;
slouken@1895
   366
        while (SDL_PollEvent(&event)) {
slouken@6785
   367
            SDLTest_CommonEvent(state, &event, &done);
slouken@1895
   368
        }
slouken@1914
   369
        for (i = 0; i < state->num_windows; ++i) {
slouken@5297
   370
            MoveSprites(state->renderers[i], sprites[i]);
slouken@1895
   371
        }
slouken@1895
   372
    }
slouken@1895
   373
slouken@1895
   374
    /* Print out some timing information */
slouken@1895
   375
    now = SDL_GetTicks();
slouken@1895
   376
    if (now > then) {
slouken@2786
   377
        double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   378
        printf("%2.2f frames per second\n", fps);
slouken@1895
   379
    }
slouken@1895
   380
    quit(0);
slouken@5147
   381
    return 0;
slouken@1895
   382
}
slouken@1895
   383
slouken@1895
   384
/* vi: set ts=4 sw=4 expandtab: */