test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 20 Sep 2009 23:09:30 +0000
changeset 3285 0d7b20a4a629
parent 3266 7856dfe3a44f
child 3536 0267b8b1595c
permissions -rw-r--r--
Fall back to opaque sprite if no formats with alpha are supported.
slouken@1895
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@1895
     2
slouken@1895
     3
#include <stdlib.h>
slouken@1907
     4
#include <stdio.h>
slouken@1895
     5
#include <time.h>
slouken@1895
     6
slouken@1914
     7
#include "common.h"
slouken@1895
     8
slouken@1895
     9
#define NUM_SPRITES	100
slouken@1895
    10
#define MAX_SPEED 	1
slouken@1895
    11
slouken@1914
    12
static CommonState *state;
slouken@1895
    13
static int num_sprites;
slouken@1895
    14
static SDL_TextureID *sprites;
slouken@1985
    15
static SDL_bool cycle_color;
slouken@1985
    16
static SDL_bool cycle_alpha;
slouken@1985
    17
static int cycle_direction = 1;
slouken@1985
    18
static int current_alpha = 0;
slouken@1985
    19
static int current_color = 0;
slouken@1895
    20
static SDL_Rect *positions;
slouken@1895
    21
static SDL_Rect *velocities;
slouken@1895
    22
static int sprite_w, sprite_h;
slouken@2884
    23
static SDL_BlendMode blendMode = SDL_BLENDMODE_MASK;
slouken@1965
    24
static SDL_TextureScaleMode scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1895
    25
slouken@1895
    26
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    27
static void
slouken@1895
    28
quit(int rc)
slouken@1895
    29
{
slouken@1895
    30
    if (sprites) {
slouken@1895
    31
        SDL_free(sprites);
slouken@1895
    32
    }
slouken@1895
    33
    if (positions) {
slouken@1895
    34
        SDL_free(positions);
slouken@1895
    35
    }
slouken@1895
    36
    if (velocities) {
slouken@1895
    37
        SDL_free(velocities);
slouken@1895
    38
    }
slouken@1914
    39
    CommonQuit(state);
slouken@1895
    40
    exit(rc);
slouken@1895
    41
}
slouken@1895
    42
slouken@1895
    43
int
slouken@1895
    44
LoadSprite(char *file)
slouken@1895
    45
{
slouken@1895
    46
    int i;
slouken@1895
    47
    SDL_Surface *temp;
slouken@1895
    48
slouken@1895
    49
    /* Load the sprite image */
slouken@1895
    50
    temp = SDL_LoadBMP(file);
slouken@1895
    51
    if (temp == NULL) {
slouken@1895
    52
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    53
        return (-1);
slouken@1895
    54
    }
slouken@1895
    55
    sprite_w = temp->w;
slouken@1895
    56
    sprite_h = temp->h;
slouken@1895
    57
slouken@1895
    58
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    59
    if (temp->format->palette) {
slouken@1895
    60
        SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint8 *) temp->pixels);
lestat@3183
    61
    } else {
lestat@3183
    62
        switch (temp->format->BitsPerPixel) {
lestat@3183
    63
        case 15:
slouken@3186
    64
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
slouken@3186
    65
                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
lestat@3183
    66
            break;
lestat@3183
    67
        case 16:
lestat@3183
    68
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint16 *) temp->pixels);
lestat@3183
    69
            break;
lestat@3183
    70
        case 24:
slouken@3186
    71
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
slouken@3186
    72
                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
lestat@3183
    73
            break;
lestat@3183
    74
        case 32:
lestat@3183
    75
            SDL_SetColorKey(temp, SDL_SRCCOLORKEY, *(Uint32 *) temp->pixels);
lestat@3183
    76
            break;
lestat@3183
    77
        }
slouken@1895
    78
    }
slouken@1895
    79
slouken@1895
    80
    /* Create textures from the image */
slouken@1914
    81
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
    82
        SDL_SelectRenderer(state->windows[i]);
slouken@2222
    83
        sprites[i] = SDL_CreateTextureFromSurface(0, temp);
slouken@1895
    84
        if (!sprites[i]) {
slouken@3285
    85
		SDL_SetColorKey(temp, 0, 0);
slouken@3285
    86
		sprites[i] = SDL_CreateTextureFromSurface(0, temp);
slouken@3285
    87
	}
slouken@3285
    88
        if (!sprites[i]) {
slouken@1895
    89
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    90
            SDL_FreeSurface(temp);
slouken@1895
    91
            return (-1);
slouken@1895
    92
        }
slouken@1985
    93
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1985
    94
        SDL_SetTextureScaleMode(sprites[i], scaleMode);
slouken@1895
    95
    }
slouken@1895
    96
    SDL_FreeSurface(temp);
slouken@1895
    97
slouken@1895
    98
    /* We're ready to roll. :) */
slouken@1895
    99
    return (0);
slouken@1895
   100
}
slouken@1895
   101
slouken@1895
   102
void
slouken@1895
   103
MoveSprites(SDL_WindowID window, SDL_TextureID sprite)
slouken@1895
   104
{
slouken@1895
   105
    int i, n;
slouken@1895
   106
    int window_w, window_h;
slouken@3265
   107
    SDL_Rect temp;
slouken@2783
   108
    SDL_Rect *position, *velocity;
slouken@1895
   109
slouken@1895
   110
    SDL_SelectRenderer(window);
slouken@1895
   111
slouken@1895
   112
    /* Query the sizes */
slouken@1895
   113
    SDL_GetWindowSize(window, &window_w, &window_h);
slouken@1895
   114
slouken@1985
   115
    /* Cycle the color and alpha, if desired */
slouken@1985
   116
    if (cycle_color) {
slouken@1985
   117
        current_color += cycle_direction;
slouken@1985
   118
        if (current_color < 0) {
slouken@1985
   119
            current_color = 0;
slouken@1985
   120
            cycle_direction = -cycle_direction;
slouken@1985
   121
        }
slouken@1985
   122
        if (current_color > 255) {
slouken@1985
   123
            current_color = 255;
slouken@1985
   124
            cycle_direction = -cycle_direction;
slouken@1985
   125
        }
slouken@1985
   126
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   127
                               (Uint8) current_color);
slouken@1985
   128
    }
slouken@1985
   129
    if (cycle_alpha) {
slouken@1985
   130
        current_alpha += cycle_direction;
slouken@1985
   131
        if (current_alpha < 0) {
slouken@1985
   132
            current_alpha = 0;
slouken@1985
   133
            cycle_direction = -cycle_direction;
slouken@1985
   134
        }
slouken@1985
   135
        if (current_alpha > 255) {
slouken@1985
   136
            current_alpha = 255;
slouken@1985
   137
            cycle_direction = -cycle_direction;
slouken@1985
   138
        }
slouken@1985
   139
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   140
    }
slouken@1985
   141
slouken@3264
   142
    /* Draw a gray background */
slouken@3264
   143
    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@3264
   144
    SDL_RenderFill(NULL);
slouken@3264
   145
slouken@3265
   146
    /* Test points */
slouken@3264
   147
    SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
slouken@3264
   148
    SDL_RenderPoint(0, 0);
slouken@3264
   149
    SDL_RenderPoint(window_w-1, 0);
slouken@3264
   150
    SDL_RenderPoint(0, window_h-1);
slouken@3264
   151
    SDL_RenderPoint(window_w-1, window_h-1);
slouken@3264
   152
slouken@3265
   153
    /* Test horizontal and vertical lines */
slouken@3265
   154
    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
slouken@3265
   155
    SDL_RenderLine(1, 0, window_w-2, 0);
slouken@3265
   156
    SDL_RenderLine(1, window_h-1, window_w-2, window_h-1);
slouken@3265
   157
    SDL_RenderLine(0, 1, 0, window_h-2);
slouken@3265
   158
    SDL_RenderLine(window_w-1, 1, window_w-1, window_h-2);
slouken@3265
   159
slouken@3266
   160
    /* Test fill and copy */
slouken@3265
   161
    SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
slouken@3266
   162
    temp.x = 1;
slouken@3266
   163
    temp.y = 1;
slouken@3266
   164
    temp.w = sprite_w;
slouken@3266
   165
    temp.h = sprite_h;
slouken@3266
   166
    SDL_RenderFill(&temp);
slouken@3266
   167
    SDL_RenderCopy(sprite, NULL, &temp);
slouken@3265
   168
    temp.x = window_w-sprite_w-1;
slouken@3265
   169
    temp.y = 1;
slouken@3265
   170
    temp.w = sprite_w;
slouken@3265
   171
    temp.h = sprite_h;
slouken@3265
   172
    SDL_RenderFill(&temp);
slouken@3266
   173
    SDL_RenderCopy(sprite, NULL, &temp);
slouken@3265
   174
    temp.x = 1;
slouken@3265
   175
    temp.y = window_h-sprite_h-1;
slouken@3265
   176
    temp.w = sprite_w;
slouken@3265
   177
    temp.h = sprite_h;
slouken@3265
   178
    SDL_RenderFill(&temp);
slouken@3265
   179
    SDL_RenderCopy(sprite, NULL, &temp);
slouken@3265
   180
    temp.x = window_w-sprite_w-1;
slouken@3265
   181
    temp.y = window_h-sprite_h-1;
slouken@3265
   182
    temp.w = sprite_w;
slouken@3265
   183
    temp.h = sprite_h;
slouken@3266
   184
    SDL_RenderFill(&temp);
slouken@3265
   185
    SDL_RenderCopy(sprite, NULL, &temp);
slouken@3265
   186
slouken@3265
   187
    /* Test diagonal lines */
slouken@3265
   188
    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
slouken@3265
   189
    SDL_RenderLine(sprite_w, sprite_h,
slouken@3265
   190
                   window_w-sprite_w-2, window_h-sprite_h-2);
slouken@3265
   191
    SDL_RenderLine(window_w-sprite_w-2, sprite_h,
slouken@3265
   192
                   sprite_w, window_h-sprite_h-2);
slouken@3265
   193
slouken@1895
   194
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
   195
    n = 0;
slouken@1895
   196
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   197
        position = &positions[i];
slouken@1895
   198
        velocity = &velocities[i];
slouken@1895
   199
        position->x += velocity->x;
slouken@1895
   200
        if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
slouken@1895
   201
            velocity->x = -velocity->x;
slouken@1895
   202
            position->x += velocity->x;
slouken@1895
   203
        }
slouken@1895
   204
        position->y += velocity->y;
slouken@2834
   205
        if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
slouken@1895
   206
            velocity->y = -velocity->y;
slouken@1895
   207
            position->y += velocity->y;
slouken@1895
   208
        }
slouken@1895
   209
slouken@1895
   210
        /* Blit the sprite onto the screen */
slouken@1985
   211
        SDL_RenderCopy(sprite, NULL, position);
slouken@1895
   212
    }
slouken@1895
   213
slouken@1895
   214
    /* Update the screen! */
slouken@1895
   215
    SDL_RenderPresent();
slouken@1895
   216
}
slouken@1895
   217
slouken@1895
   218
int
slouken@1895
   219
main(int argc, char *argv[])
slouken@1895
   220
{
slouken@1895
   221
    int i, done;
slouken@1895
   222
    SDL_Event event;
slouken@1895
   223
    Uint32 then, now, frames;
slouken@1895
   224
slouken@1914
   225
    /* Initialize parameters */
slouken@1914
   226
    num_sprites = NUM_SPRITES;
slouken@1914
   227
slouken@1914
   228
    /* Initialize test framework */
slouken@1914
   229
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   230
    if (!state) {
slouken@1914
   231
        return 1;
slouken@1895
   232
    }
slouken@1914
   233
    for (i = 1; i < argc;) {
slouken@1914
   234
        int consumed;
slouken@1895
   235
slouken@1914
   236
        consumed = CommonArg(state, i);
slouken@1915
   237
        if (consumed == 0) {
slouken@1916
   238
            consumed = -1;
slouken@1916
   239
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   240
                if (argv[i + 1]) {
slouken@1916
   241
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   242
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   243
                        consumed = 2;
slouken@1917
   244
                    } else if (SDL_strcasecmp(argv[i + 1], "mask") == 0) {
slouken@2884
   245
                        blendMode = SDL_BLENDMODE_MASK;
slouken@1917
   246
                        consumed = 2;
slouken@1916
   247
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   248
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   249
                        consumed = 2;
slouken@1916
   250
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   251
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   252
                        consumed = 2;
slouken@1916
   253
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@2884
   254
                        blendMode = SDL_BLENDMODE_MOD;
slouken@1916
   255
                        consumed = 2;
slouken@1916
   256
                    }
slouken@1916
   257
                }
slouken@1917
   258
            } else if (SDL_strcasecmp(argv[i], "--scale") == 0) {
slouken@1917
   259
                if (argv[i + 1]) {
slouken@1917
   260
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@1965
   261
                        scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1917
   262
                        consumed = 2;
slouken@1917
   263
                    } else if (SDL_strcasecmp(argv[i + 1], "fast") == 0) {
slouken@1965
   264
                        scaleMode = SDL_TEXTURESCALEMODE_FAST;
slouken@1917
   265
                        consumed = 2;
slouken@1917
   266
                    } else if (SDL_strcasecmp(argv[i + 1], "slow") == 0) {
slouken@1965
   267
                        scaleMode = SDL_TEXTURESCALEMODE_SLOW;
slouken@1917
   268
                        consumed = 2;
slouken@1917
   269
                    } else if (SDL_strcasecmp(argv[i + 1], "best") == 0) {
slouken@1965
   270
                        scaleMode = SDL_TEXTURESCALEMODE_BEST;
slouken@1917
   271
                        consumed = 2;
slouken@1917
   272
                    }
slouken@1917
   273
                }
slouken@1985
   274
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   275
                cycle_color = SDL_TRUE;
slouken@1985
   276
                consumed = 1;
slouken@1985
   277
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   278
                cycle_alpha = SDL_TRUE;
slouken@1985
   279
                consumed = 1;
slouken@1916
   280
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   281
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   282
                consumed = 1;
slouken@1916
   283
            }
slouken@1915
   284
        }
slouken@1914
   285
        if (consumed < 0) {
slouken@1917
   286
            fprintf(stderr,
slouken@1985
   287
                    "Usage: %s %s [--blend none|mask|blend|add|mod] [--scale none|fast|slow|best] [--cyclecolor] [--cyclealpha]\n",
slouken@1916
   288
                    argv[0], CommonUsage(state));
slouken@1895
   289
            quit(1);
slouken@1895
   290
        }
slouken@1914
   291
        i += consumed;
slouken@1895
   292
    }
slouken@1914
   293
    if (!CommonInit(state)) {
slouken@1914
   294
        quit(2);
slouken@1895
   295
    }
slouken@1895
   296
slouken@1895
   297
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   298
    sprites =
slouken@1914
   299
        (SDL_TextureID *) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   300
    if (!sprites) {
slouken@1895
   301
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   302
        quit(2);
slouken@1895
   303
    }
slouken@1914
   304
    for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   305
        SDL_SelectRenderer(state->windows[i]);
slouken@2884
   306
        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2884
   307
        SDL_RenderFill(NULL);
slouken@1895
   308
    }
slouken@1895
   309
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   310
        quit(2);
slouken@1895
   311
    }
slouken@1895
   312
slouken@1895
   313
    /* Allocate memory for the sprite info */
slouken@1895
   314
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   315
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   316
    if (!positions || !velocities) {
slouken@1895
   317
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   318
        quit(2);
slouken@1895
   319
    }
slouken@1895
   320
    srand(time(NULL));
slouken@1965
   321
    if (scaleMode != SDL_TEXTURESCALEMODE_NONE) {
slouken@1917
   322
        sprite_w += sprite_w / 2;
slouken@1917
   323
        sprite_h += sprite_h / 2;
slouken@1917
   324
    }
slouken@1895
   325
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   326
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   327
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   328
        positions[i].w = sprite_w;
slouken@1895
   329
        positions[i].h = sprite_h;
slouken@1895
   330
        velocities[i].x = 0;
slouken@1895
   331
        velocities[i].y = 0;
slouken@1895
   332
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   333
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   334
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   335
        }
slouken@1895
   336
    }
slouken@1895
   337
slouken@1915
   338
    /* Main render loop */
slouken@1895
   339
    frames = 0;
slouken@1895
   340
    then = SDL_GetTicks();
slouken@1895
   341
    done = 0;
slouken@1895
   342
    while (!done) {
slouken@1895
   343
        /* Check for events */
slouken@1895
   344
        ++frames;
slouken@1895
   345
        while (SDL_PollEvent(&event)) {
slouken@1914
   346
            CommonEvent(state, &event, &done);
slouken@1895
   347
            switch (event.type) {
slouken@1895
   348
            case SDL_WINDOWEVENT:
slouken@1895
   349
                switch (event.window.event) {
slouken@1895
   350
                case SDL_WINDOWEVENT_EXPOSED:
slouken@1895
   351
                    SDL_SelectRenderer(event.window.windowID);
slouken@2884
   352
                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
slouken@2884
   353
                    SDL_RenderFill(NULL);
slouken@1895
   354
                    break;
slouken@1895
   355
                }
slouken@1895
   356
                break;
slouken@1895
   357
            default:
slouken@1895
   358
                break;
slouken@1895
   359
            }
slouken@1895
   360
        }
slouken@1914
   361
        for (i = 0; i < state->num_windows; ++i) {
slouken@1914
   362
            MoveSprites(state->windows[i], sprites[i]);
slouken@1895
   363
        }
slouken@1895
   364
    }
slouken@1895
   365
slouken@1895
   366
    /* Print out some timing information */
slouken@1895
   367
    now = SDL_GetTicks();
slouken@1895
   368
    if (now > then) {
slouken@2786
   369
        double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   370
        printf("%2.2f frames per second\n", fps);
slouken@1895
   371
    }
slouken@1895
   372
    quit(0);
slouken@1895
   373
}
slouken@1895
   374
slouken@1895
   375
/* vi: set ts=4 sw=4 expandtab: */