test/testsprite.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1516 4d241ea8a1cd
child 1666 6e7ec5cb83c3
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

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