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