test/testsprite2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5184 d976b67150c5
child 5520 09b500e0656d
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

Fixed API for updating window rects for consistency with other APIs - the order is pointer to array followed by count in array.
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@5147
     9
#define NUM_SPRITES    100
slouken@5147
    10
#define MAX_SPEED     1
slouken@1895
    11
slouken@1914
    12
static CommonState *state;
slouken@1895
    13
static int num_sprites;
slouken@3685
    14
static SDL_Texture **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@5140
    23
static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
slouken@1895
    24
slouken@1895
    25
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    26
static void
slouken@1895
    27
quit(int rc)
slouken@1895
    28
{
slouken@1895
    29
    if (sprites) {
slouken@1895
    30
        SDL_free(sprites);
slouken@1895
    31
    }
slouken@1895
    32
    if (positions) {
slouken@1895
    33
        SDL_free(positions);
slouken@1895
    34
    }
slouken@1895
    35
    if (velocities) {
slouken@1895
    36
        SDL_free(velocities);
slouken@1895
    37
    }
slouken@1914
    38
    CommonQuit(state);
slouken@1895
    39
    exit(rc);
slouken@1895
    40
}
slouken@1895
    41
slouken@1895
    42
int
slouken@1895
    43
LoadSprite(char *file)
slouken@1895
    44
{
slouken@1895
    45
    int i;
slouken@1895
    46
    SDL_Surface *temp;
slouken@1895
    47
slouken@1895
    48
    /* Load the sprite image */
slouken@1895
    49
    temp = SDL_LoadBMP(file);
slouken@1895
    50
    if (temp == NULL) {
slouken@1895
    51
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    52
        return (-1);
slouken@1895
    53
    }
slouken@1895
    54
    sprite_w = temp->w;
slouken@1895
    55
    sprite_h = temp->h;
slouken@1895
    56
slouken@1895
    57
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    58
    if (temp->format->palette) {
slouken@3560
    59
        SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
lestat@3183
    60
    } else {
lestat@3183
    61
        switch (temp->format->BitsPerPixel) {
lestat@3183
    62
        case 15:
slouken@3560
    63
            SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
lestat@3183
    64
            break;
lestat@3183
    65
        case 16:
slouken@3560
    66
            SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
lestat@3183
    67
            break;
lestat@3183
    68
        case 24:
slouken@3560
    69
            SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
lestat@3183
    70
            break;
lestat@3183
    71
        case 32:
slouken@3560
    72
            SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
lestat@3183
    73
            break;
lestat@3183
    74
        }
slouken@1895
    75
    }
slouken@1895
    76
slouken@1895
    77
    /* Create textures from the image */
slouken@1914
    78
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
    79
        SDL_Renderer *renderer = state->renderers[i];
slouken@5158
    80
        sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
slouken@3285
    81
        if (!sprites[i]) {
slouken@1895
    82
            fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@1895
    83
            SDL_FreeSurface(temp);
slouken@1895
    84
            return (-1);
slouken@1895
    85
        }
slouken@1985
    86
        SDL_SetTextureBlendMode(sprites[i], blendMode);
slouken@1895
    87
    }
slouken@1895
    88
    SDL_FreeSurface(temp);
slouken@1895
    89
slouken@1895
    90
    /* We're ready to roll. :) */
slouken@1895
    91
    return (0);
slouken@1895
    92
}
slouken@1895
    93
slouken@1895
    94
void
slouken@5297
    95
MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
slouken@1895
    96
{
slouken@1895
    97
    int i, n;
slouken@5297
    98
    SDL_Rect viewport, temp;
slouken@2783
    99
    SDL_Rect *position, *velocity;
slouken@1895
   100
slouken@1895
   101
    /* Query the sizes */
slouken@5297
   102
    SDL_RenderGetViewport(renderer, &viewport);
slouken@1895
   103
slouken@1985
   104
    /* Cycle the color and alpha, if desired */
slouken@1985
   105
    if (cycle_color) {
slouken@1985
   106
        current_color += cycle_direction;
slouken@1985
   107
        if (current_color < 0) {
slouken@1985
   108
            current_color = 0;
slouken@1985
   109
            cycle_direction = -cycle_direction;
slouken@1985
   110
        }
slouken@1985
   111
        if (current_color > 255) {
slouken@1985
   112
            current_color = 255;
slouken@1985
   113
            cycle_direction = -cycle_direction;
slouken@1985
   114
        }
slouken@1985
   115
        SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
slouken@1985
   116
                               (Uint8) current_color);
slouken@1985
   117
    }
slouken@1985
   118
    if (cycle_alpha) {
slouken@1985
   119
        current_alpha += cycle_direction;
slouken@1985
   120
        if (current_alpha < 0) {
slouken@1985
   121
            current_alpha = 0;
slouken@1985
   122
            cycle_direction = -cycle_direction;
slouken@1985
   123
        }
slouken@1985
   124
        if (current_alpha > 255) {
slouken@1985
   125
            current_alpha = 255;
slouken@1985
   126
            cycle_direction = -cycle_direction;
slouken@1985
   127
        }
slouken@1985
   128
        SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
slouken@1985
   129
    }
slouken@1985
   130
slouken@3264
   131
    /* Draw a gray background */
slouken@5147
   132
    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   133
    SDL_RenderClear(renderer);
slouken@3264
   134
slouken@3265
   135
    /* Test points */
slouken@5147
   136
    SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
slouken@5147
   137
    SDL_RenderDrawPoint(renderer, 0, 0);
slouken@5297
   138
    SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
slouken@5297
   139
    SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
slouken@5297
   140
    SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
slouken@3264
   141
slouken@3265
   142
    /* Test horizontal and vertical lines */
slouken@5147
   143
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5297
   144
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
slouken@5297
   145
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
slouken@5297
   146
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
slouken@5297
   147
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
slouken@3265
   148
slouken@3266
   149
    /* Test fill and copy */
slouken@5147
   150
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
slouken@3266
   151
    temp.x = 1;
slouken@3266
   152
    temp.y = 1;
slouken@3266
   153
    temp.w = sprite_w;
slouken@3266
   154
    temp.h = sprite_h;
slouken@5147
   155
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   156
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   157
    temp.x = viewport.w-sprite_w-1;
slouken@3265
   158
    temp.y = 1;
slouken@3265
   159
    temp.w = sprite_w;
slouken@3265
   160
    temp.h = sprite_h;
slouken@5147
   161
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   162
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   163
    temp.x = 1;
slouken@5297
   164
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   165
    temp.w = sprite_w;
slouken@3265
   166
    temp.h = sprite_h;
slouken@5147
   167
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   168
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@5297
   169
    temp.x = viewport.w-sprite_w-1;
slouken@5297
   170
    temp.y = viewport.h-sprite_h-1;
slouken@3265
   171
    temp.w = sprite_w;
slouken@3265
   172
    temp.h = sprite_h;
slouken@5147
   173
    SDL_RenderFillRect(renderer, &temp);
slouken@5147
   174
    SDL_RenderCopy(renderer, sprite, NULL, &temp);
slouken@3265
   175
slouken@3265
   176
    /* Test diagonal lines */
slouken@5147
   177
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@5147
   178
    SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
slouken@5297
   179
                       viewport.w-sprite_w-2, viewport.h-sprite_h-2);
slouken@5297
   180
    SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
slouken@5297
   181
                       sprite_w, viewport.h-sprite_h-2);
slouken@3265
   182
slouken@1895
   183
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
   184
    n = 0;
slouken@1895
   185
    for (i = 0; i < num_sprites; ++i) {
slouken@1895
   186
        position = &positions[i];
slouken@1895
   187
        velocity = &velocities[i];
slouken@1895
   188
        position->x += velocity->x;
slouken@5297
   189
        if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
slouken@1895
   190
            velocity->x = -velocity->x;
slouken@1895
   191
            position->x += velocity->x;
slouken@1895
   192
        }
slouken@1895
   193
        position->y += velocity->y;
slouken@5297
   194
        if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
slouken@1895
   195
            velocity->y = -velocity->y;
slouken@1895
   196
            position->y += velocity->y;
slouken@1895
   197
        }
slouken@1895
   198
slouken@1895
   199
        /* Blit the sprite onto the screen */
slouken@5147
   200
        SDL_RenderCopy(renderer, sprite, NULL, position);
slouken@1895
   201
    }
slouken@1895
   202
slouken@1895
   203
    /* Update the screen! */
slouken@5147
   204
    SDL_RenderPresent(renderer);
slouken@1895
   205
}
slouken@1895
   206
slouken@1895
   207
int
slouken@1895
   208
main(int argc, char *argv[])
slouken@1895
   209
{
slouken@1895
   210
    int i, done;
slouken@1895
   211
    SDL_Event event;
slouken@1895
   212
    Uint32 then, now, frames;
slouken@1895
   213
slouken@1914
   214
    /* Initialize parameters */
slouken@1914
   215
    num_sprites = NUM_SPRITES;
slouken@1914
   216
slouken@1914
   217
    /* Initialize test framework */
slouken@1914
   218
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1914
   219
    if (!state) {
slouken@1914
   220
        return 1;
slouken@1895
   221
    }
slouken@1914
   222
    for (i = 1; i < argc;) {
slouken@1914
   223
        int consumed;
slouken@1895
   224
slouken@1914
   225
        consumed = CommonArg(state, i);
slouken@1915
   226
        if (consumed == 0) {
slouken@1916
   227
            consumed = -1;
slouken@1916
   228
            if (SDL_strcasecmp(argv[i], "--blend") == 0) {
slouken@1916
   229
                if (argv[i + 1]) {
slouken@1916
   230
                    if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
slouken@2884
   231
                        blendMode = SDL_BLENDMODE_NONE;
slouken@1916
   232
                        consumed = 2;
slouken@1916
   233
                    } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
slouken@2884
   234
                        blendMode = SDL_BLENDMODE_BLEND;
slouken@1916
   235
                        consumed = 2;
slouken@1916
   236
                    } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
slouken@2884
   237
                        blendMode = SDL_BLENDMODE_ADD;
slouken@1916
   238
                        consumed = 2;
slouken@5184
   239
                    } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
slouken@5184
   240
                        blendMode = SDL_BLENDMODE_MOD;
slouken@5184
   241
                        consumed = 2;
slouken@1916
   242
                    }
slouken@1916
   243
                }
slouken@1985
   244
            } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
slouken@1985
   245
                cycle_color = SDL_TRUE;
slouken@1985
   246
                consumed = 1;
slouken@1985
   247
            } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
slouken@1985
   248
                cycle_alpha = SDL_TRUE;
slouken@1985
   249
                consumed = 1;
slouken@1916
   250
            } else if (SDL_isdigit(*argv[i])) {
slouken@1916
   251
                num_sprites = SDL_atoi(argv[i]);
slouken@1916
   252
                consumed = 1;
slouken@1916
   253
            }
slouken@1915
   254
        }
slouken@1914
   255
        if (consumed < 0) {
slouken@1917
   256
            fprintf(stderr,
slouken@5184
   257
                    "Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
slouken@1916
   258
                    argv[0], CommonUsage(state));
slouken@1895
   259
            quit(1);
slouken@1895
   260
        }
slouken@1914
   261
        i += consumed;
slouken@1895
   262
    }
slouken@1914
   263
    if (!CommonInit(state)) {
slouken@1914
   264
        quit(2);
slouken@1895
   265
    }
slouken@1895
   266
slouken@1895
   267
    /* Create the windows, initialize the renderers, and load the textures */
slouken@1914
   268
    sprites =
slouken@3685
   269
        (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
slouken@1914
   270
    if (!sprites) {
slouken@1895
   271
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   272
        quit(2);
slouken@1895
   273
    }
slouken@1914
   274
    for (i = 0; i < state->num_windows; ++i) {
slouken@5147
   275
        SDL_Renderer *renderer = state->renderers[i];
slouken@5147
   276
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@5147
   277
        SDL_RenderClear(renderer);
slouken@1895
   278
    }
slouken@1895
   279
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   280
        quit(2);
slouken@1895
   281
    }
slouken@1895
   282
slouken@1895
   283
    /* Allocate memory for the sprite info */
slouken@1895
   284
    positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   285
    velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
slouken@1895
   286
    if (!positions || !velocities) {
slouken@1895
   287
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   288
        quit(2);
slouken@1895
   289
    }
aschiffler@4884
   290
    srand((unsigned int)time(NULL));
slouken@1895
   291
    for (i = 0; i < num_sprites; ++i) {
slouken@1914
   292
        positions[i].x = rand() % (state->window_w - sprite_w);
slouken@1914
   293
        positions[i].y = rand() % (state->window_h - sprite_h);
slouken@1895
   294
        positions[i].w = sprite_w;
slouken@1895
   295
        positions[i].h = sprite_h;
slouken@1895
   296
        velocities[i].x = 0;
slouken@1895
   297
        velocities[i].y = 0;
slouken@1895
   298
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   299
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   300
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   301
        }
slouken@1895
   302
    }
slouken@1895
   303
slouken@1915
   304
    /* Main render loop */
slouken@1895
   305
    frames = 0;
slouken@1895
   306
    then = SDL_GetTicks();
slouken@1895
   307
    done = 0;
slouken@1895
   308
    while (!done) {
slouken@1895
   309
        /* Check for events */
slouken@1895
   310
        ++frames;
slouken@1895
   311
        while (SDL_PollEvent(&event)) {
slouken@1914
   312
            CommonEvent(state, &event, &done);
slouken@1895
   313
        }
slouken@1914
   314
        for (i = 0; i < state->num_windows; ++i) {
slouken@5297
   315
            MoveSprites(state->renderers[i], sprites[i]);
slouken@1895
   316
        }
slouken@1895
   317
    }
slouken@1895
   318
slouken@1895
   319
    /* Print out some timing information */
slouken@1895
   320
    now = SDL_GetTicks();
slouken@1895
   321
    if (now > then) {
slouken@2786
   322
        double fps = ((double) frames * 1000) / (now - then);
slouken@2783
   323
        printf("%2.2f frames per second\n", fps);
slouken@1895
   324
    }
slouken@1895
   325
    quit(0);
slouken@5147
   326
    return 0;
slouken@1895
   327
}
slouken@1895
   328
slouken@1895
   329
/* vi: set ts=4 sw=4 expandtab: */