test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1516 4d241ea8a1cd
child 1907 06c27a737b7a
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@0
     1
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@0
     2
slouken@0
     3
#include <stdlib.h>
slouken@0
     4
#include <time.h>
slouken@0
     5
slouken@0
     6
#include "SDL.h"
slouken@0
     7
slouken@0
     8
#define NUM_SPRITES	100
slouken@0
     9
#define MAX_SPEED 	1
slouken@0
    10
slouken@0
    11
SDL_Surface *sprite;
slouken@0
    12
int numsprites;
slouken@0
    13
SDL_Rect *sprite_rects;
slouken@0
    14
SDL_Rect *positions;
slouken@0
    15
SDL_Rect *velocities;
slouken@0
    16
int sprites_visible;
icculus@1214
    17
int debug_flip;
slouken@288
    18
Uint16 sprite_w, sprite_h;
slouken@0
    19
icculus@1151
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    21
static void
slouken@1895
    22
quit(int rc)
icculus@1151
    23
{
slouken@1895
    24
    SDL_Quit();
slouken@1895
    25
    exit(rc);
icculus@1151
    26
}
icculus@1151
    27
slouken@1895
    28
int
slouken@1895
    29
LoadSprite(char *file)
slouken@0
    30
{
slouken@1895
    31
    SDL_Surface *temp;
slouken@0
    32
slouken@1895
    33
    /* Load the sprite image */
slouken@1895
    34
    sprite = SDL_LoadBMP(file);
slouken@1895
    35
    if (sprite == NULL) {
slouken@1895
    36
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
    37
        return (-1);
slouken@1895
    38
    }
slouken@0
    39
slouken@1895
    40
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
    41
    if (sprite->format->palette) {
slouken@1895
    42
        SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
slouken@1895
    43
                        *(Uint8 *) sprite->pixels);
slouken@1895
    44
    }
slouken@0
    45
slouken@1895
    46
    /* Convert sprite to video format */
slouken@1895
    47
    temp = SDL_DisplayFormat(sprite);
slouken@1895
    48
    SDL_FreeSurface(sprite);
slouken@1895
    49
    if (temp == NULL) {
slouken@1895
    50
        fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
slouken@1895
    51
        return (-1);
slouken@1895
    52
    }
slouken@1895
    53
    sprite = temp;
slouken@0
    54
slouken@1895
    55
    /* We're ready to roll. :) */
slouken@1895
    56
    return (0);
slouken@0
    57
}
slouken@0
    58
slouken@1895
    59
void
slouken@1895
    60
MoveSprites(SDL_Surface * screen, Uint32 background)
slouken@0
    61
{
slouken@1895
    62
    int i, nupdates;
slouken@1895
    63
    SDL_Rect area, *position, *velocity;
slouken@0
    64
slouken@1895
    65
    nupdates = 0;
slouken@1895
    66
    /* Erase all the sprites if necessary */
slouken@1895
    67
    if (sprites_visible) {
slouken@1895
    68
        SDL_FillRect(screen, NULL, background);
slouken@1895
    69
    }
slouken@0
    70
slouken@1895
    71
    /* Move the sprite, bounce at the wall, and draw */
slouken@1895
    72
    for (i = 0; i < numsprites; ++i) {
slouken@1895
    73
        position = &positions[i];
slouken@1895
    74
        velocity = &velocities[i];
slouken@1895
    75
        position->x += velocity->x;
slouken@1895
    76
        if ((position->x < 0) || (position->x >= (screen->w - sprite_w))) {
slouken@1895
    77
            velocity->x = -velocity->x;
slouken@1895
    78
            position->x += velocity->x;
slouken@1895
    79
        }
slouken@1895
    80
        position->y += velocity->y;
slouken@1895
    81
        if ((position->y < 0) || (position->y >= (screen->h - sprite_w))) {
slouken@1895
    82
            velocity->y = -velocity->y;
slouken@1895
    83
            position->y += velocity->y;
slouken@1895
    84
        }
slouken@0
    85
slouken@1895
    86
        /* Blit the sprite onto the screen */
slouken@1895
    87
        area = *position;
slouken@1895
    88
        SDL_BlitSurface(sprite, NULL, screen, &area);
slouken@1895
    89
        sprite_rects[nupdates++] = area;
slouken@1895
    90
    }
slouken@0
    91
slouken@1895
    92
    if (debug_flip) {
slouken@1895
    93
        if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
slouken@1895
    94
            static int t = 0;
slouken@663
    95
slouken@1895
    96
            Uint32 color = SDL_MapRGB(screen->format, 255, 0, 0);
slouken@1895
    97
            SDL_Rect r;
slouken@1895
    98
            r.x =
slouken@1895
    99
                (sin((float) t * 2 * 3.1459) + 1.0) / 2.0 * (screen->w - 20);
slouken@1895
   100
            r.y = 0;
slouken@1895
   101
            r.w = 20;
slouken@1895
   102
            r.h = screen->h;
icculus@1214
   103
slouken@1895
   104
            SDL_FillRect(screen, &r, color);
slouken@1895
   105
            t += 2;
slouken@1895
   106
        }
slouken@1895
   107
    }
icculus@1214
   108
slouken@1895
   109
    /* Update the screen! */
slouken@1895
   110
    if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
slouken@1895
   111
        SDL_Flip(screen);
slouken@1895
   112
    } else {
slouken@1895
   113
        SDL_UpdateRects(screen, nupdates, sprite_rects);
slouken@1895
   114
    }
slouken@1895
   115
    sprites_visible = 1;
slouken@0
   116
}
slouken@0
   117
slouken@0
   118
/* This is a way of telling whether or not to use hardware surfaces */
slouken@1895
   119
Uint32
slouken@1895
   120
FastestFlags(Uint32 flags, int width, int height, int bpp)
slouken@0
   121
{
slouken@1895
   122
    const SDL_VideoInfo *info;
slouken@0
   123
slouken@1895
   124
    /* Hardware acceleration is only used in fullscreen mode */
slouken@1895
   125
    flags |= SDL_FULLSCREEN;
slouken@0
   126
slouken@1895
   127
    /* Check for various video capabilities */
slouken@1895
   128
    info = SDL_GetVideoInfo();
slouken@1895
   129
    if (info->blit_hw_CC && info->blit_fill) {
slouken@1895
   130
        /* We use accelerated colorkeying and color filling */
slouken@1895
   131
        flags |= SDL_HWSURFACE;
slouken@1895
   132
    }
slouken@1895
   133
    /* If we have enough video memory, and will use accelerated
slouken@1895
   134
       blits directly to it, then use page flipping.
slouken@1895
   135
     */
slouken@1895
   136
    if ((flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1895
   137
        /* Direct hardware blitting without double-buffering
slouken@1895
   138
           causes really bad flickering.
slouken@1895
   139
         */
slouken@1895
   140
        if (info->video_mem * 1024 > (height * width * bpp / 8)) {
slouken@1895
   141
            flags |= SDL_DOUBLEBUF;
slouken@1895
   142
        } else {
slouken@1895
   143
            flags &= ~SDL_HWSURFACE;
slouken@1895
   144
        }
slouken@1895
   145
    }
slouken@0
   146
slouken@1895
   147
    /* Return the flags */
slouken@1895
   148
    return (flags);
slouken@0
   149
}
slouken@0
   150
slouken@1895
   151
int
slouken@1895
   152
main(int argc, char *argv[])
slouken@0
   153
{
slouken@1895
   154
    SDL_Surface *screen;
slouken@1895
   155
    Uint8 *mem;
slouken@1895
   156
    int width, height;
slouken@1895
   157
    Uint8 video_bpp;
slouken@1895
   158
    Uint32 videoflags;
slouken@1895
   159
    Uint32 background;
slouken@1895
   160
    int i, done;
slouken@1895
   161
    SDL_Event event;
slouken@1895
   162
    Uint32 then, now, frames;
slouken@0
   163
slouken@1895
   164
    /* Initialize SDL */
slouken@1895
   165
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   166
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   167
        return (1);
slouken@1895
   168
    }
slouken@0
   169
slouken@1895
   170
    numsprites = NUM_SPRITES;
slouken@1895
   171
    videoflags = SDL_SWSURFACE | SDL_ANYFORMAT;
slouken@1895
   172
    width = 640;
slouken@1895
   173
    height = 480;
slouken@1895
   174
    video_bpp = 8;
slouken@1895
   175
    debug_flip = 0;
slouken@1895
   176
    while (argc > 1) {
slouken@1895
   177
        --argc;
slouken@1895
   178
        if (strcmp(argv[argc - 1], "-width") == 0) {
slouken@1895
   179
            width = atoi(argv[argc]);
slouken@1895
   180
            --argc;
slouken@1895
   181
        } else if (strcmp(argv[argc - 1], "-height") == 0) {
slouken@1895
   182
            height = atoi(argv[argc]);
slouken@1895
   183
            --argc;
slouken@1895
   184
        } else if (strcmp(argv[argc - 1], "-bpp") == 0) {
slouken@1895
   185
            video_bpp = atoi(argv[argc]);
slouken@1895
   186
            videoflags &= ~SDL_ANYFORMAT;
slouken@1895
   187
            --argc;
slouken@1895
   188
        } else if (strcmp(argv[argc], "-fast") == 0) {
slouken@1895
   189
            videoflags = FastestFlags(videoflags, width, height, video_bpp);
slouken@1895
   190
        } else if (strcmp(argv[argc], "-hw") == 0) {
slouken@1895
   191
            videoflags ^= SDL_HWSURFACE;
slouken@1895
   192
        } else if (strcmp(argv[argc], "-flip") == 0) {
slouken@1895
   193
            videoflags ^= SDL_DOUBLEBUF;
slouken@1895
   194
        } else if (strcmp(argv[argc], "-debugflip") == 0) {
slouken@1895
   195
            debug_flip ^= 1;
slouken@1895
   196
        } else if (strcmp(argv[argc], "-fullscreen") == 0) {
slouken@1895
   197
            videoflags ^= SDL_FULLSCREEN;
slouken@1895
   198
        } else if (isdigit(argv[argc][0])) {
slouken@1895
   199
            numsprites = atoi(argv[argc]);
slouken@1895
   200
        } else {
slouken@1895
   201
            fprintf(stderr,
slouken@1895
   202
                    "Usage: %s [-bpp N] [-hw] [-flip] [-fast] [-fullscreen] [numsprites]\n",
slouken@1895
   203
                    argv[0]);
slouken@1895
   204
            quit(1);
slouken@1895
   205
        }
slouken@1895
   206
    }
slouken@0
   207
slouken@1895
   208
    /* Set video mode */
slouken@1895
   209
    screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
slouken@1895
   210
    if (!screen) {
slouken@1895
   211
        fprintf(stderr, "Couldn't set %dx%d video mode: %s\n",
slouken@1895
   212
                width, height, SDL_GetError());
slouken@1895
   213
        quit(2);
slouken@1895
   214
    }
slouken@0
   215
slouken@1895
   216
    /* Load the sprite */
slouken@1895
   217
    if (LoadSprite("icon.bmp") < 0) {
slouken@1895
   218
        quit(1);
slouken@1895
   219
    }
slouken@0
   220
slouken@1895
   221
    /* Allocate memory for the sprite info */
slouken@1895
   222
    mem = (Uint8 *) malloc(4 * sizeof(SDL_Rect) * numsprites);
slouken@1895
   223
    if (mem == NULL) {
slouken@1895
   224
        SDL_FreeSurface(sprite);
slouken@1895
   225
        fprintf(stderr, "Out of memory!\n");
slouken@1895
   226
        quit(2);
slouken@1895
   227
    }
slouken@1895
   228
    sprite_rects = (SDL_Rect *) mem;
slouken@1895
   229
    positions = sprite_rects;
slouken@1895
   230
    sprite_rects += numsprites;
slouken@1895
   231
    velocities = sprite_rects;
slouken@1895
   232
    sprite_rects += numsprites;
slouken@1895
   233
    sprite_w = sprite->w;
slouken@1895
   234
    sprite_h = sprite->h;
slouken@1895
   235
    srand(time(NULL));
slouken@1895
   236
    for (i = 0; i < numsprites; ++i) {
slouken@1895
   237
        positions[i].x = rand() % (screen->w - sprite_w);
slouken@1895
   238
        positions[i].y = rand() % (screen->h - sprite_h);
slouken@1895
   239
        positions[i].w = sprite->w;
slouken@1895
   240
        positions[i].h = sprite->h;
slouken@1895
   241
        velocities[i].x = 0;
slouken@1895
   242
        velocities[i].y = 0;
slouken@1895
   243
        while (!velocities[i].x && !velocities[i].y) {
slouken@1895
   244
            velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   245
            velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
slouken@1895
   246
        }
slouken@1895
   247
    }
slouken@1895
   248
    background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
slouken@0
   249
slouken@1895
   250
    /* Print out information about our surfaces */
slouken@1895
   251
    printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
slouken@1895
   252
    if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1895
   253
        printf("Screen is in video memory\n");
slouken@1895
   254
    } else {
slouken@1895
   255
        printf("Screen is in system memory\n");
slouken@1895
   256
    }
slouken@1895
   257
    if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
slouken@1895
   258
        printf("Screen has double-buffering enabled\n");
slouken@1895
   259
    }
slouken@1895
   260
    if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1895
   261
        printf("Sprite is in video memory\n");
slouken@1895
   262
    } else {
slouken@1895
   263
        printf("Sprite is in system memory\n");
slouken@1895
   264
    }
slouken@1895
   265
    /* Run a sample blit to trigger blit acceleration */
slouken@1895
   266
    {
slouken@1895
   267
        SDL_Rect dst;
slouken@1895
   268
        dst.x = 0;
slouken@1895
   269
        dst.y = 0;
slouken@1895
   270
        dst.w = sprite->w;
slouken@1895
   271
        dst.h = sprite->h;
slouken@1895
   272
        SDL_BlitSurface(sprite, NULL, screen, &dst);
slouken@1895
   273
        SDL_FillRect(screen, &dst, background);
slouken@1895
   274
    }
slouken@1895
   275
    if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
slouken@1895
   276
        printf("Sprite blit uses hardware acceleration\n");
slouken@1895
   277
    }
slouken@1895
   278
    if ((sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
slouken@1895
   279
        printf("Sprite blit uses RLE acceleration\n");
slouken@1895
   280
    }
slouken@0
   281
slouken@1895
   282
    /* Loop, blitting sprites and waiting for a keystroke */
slouken@1895
   283
    frames = 0;
slouken@1895
   284
    then = SDL_GetTicks();
slouken@1895
   285
    done = 0;
slouken@1895
   286
    sprites_visible = 0;
slouken@1895
   287
    while (!done) {
slouken@1895
   288
        /* Check for events */
slouken@1895
   289
        ++frames;
slouken@1895
   290
        while (SDL_PollEvent(&event)) {
slouken@1895
   291
            switch (event.type) {
slouken@1895
   292
            case SDL_MOUSEBUTTONDOWN:
slouken@1895
   293
                SDL_WarpMouse(screen->w / 2, screen->h / 2);
slouken@1895
   294
                break;
slouken@1895
   295
            case SDL_KEYDOWN:
slouken@1895
   296
                /* Any keypress quits the app... */
slouken@1895
   297
            case SDL_QUIT:
slouken@1895
   298
                done = 1;
slouken@1895
   299
                break;
slouken@1895
   300
            default:
slouken@1895
   301
                break;
slouken@1895
   302
            }
slouken@1895
   303
        }
slouken@1895
   304
        MoveSprites(screen, background);
slouken@1895
   305
    }
slouken@1895
   306
    SDL_FreeSurface(sprite);
slouken@1895
   307
    free(mem);
slouken@0
   308
slouken@1895
   309
    /* Print out some timing information */
slouken@1895
   310
    now = SDL_GetTicks();
slouken@1895
   311
    if (now > then) {
slouken@1895
   312
        printf("%2.2f frames per second\n",
slouken@1895
   313
               ((double) frames * 1000) / (now - then));
slouken@1895
   314
    }
slouken@1895
   315
    SDL_Quit();
slouken@1895
   316
    return (0);
slouken@0
   317
}