test/testsprite.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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