test/testalpha.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Nov 2008 11:24:18 +0000
changeset 2806 938aa47f903a
parent 2799 bbf3aac2672a
child 2822 cf271ff76061
permissions -rw-r--r--
Fixed testalpha screen clear for screen formats with an alpha channel
slouken@0
     1
slouken@0
     2
/* Simple program:  Fill a colormap with gray and stripe it down the screen,
slouken@0
     3
		    Then move an alpha valued sprite around the screen.
slouken@0
     4
 */
slouken@0
     5
slouken@0
     6
#include <stdio.h>
slouken@0
     7
#include <stdlib.h>
slouken@0
     8
#include <string.h>
slouken@0
     9
#include <math.h>
slouken@0
    10
slouken@0
    11
#include "SDL.h"
slouken@0
    12
slouken@1895
    13
#define FRAME_TICKS	(1000/30)       /* 30 frames/second */
slouken@0
    14
icculus@1151
    15
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    16
static void
slouken@1895
    17
quit(int rc)
icculus@1151
    18
{
slouken@1895
    19
    SDL_Quit();
slouken@1895
    20
    exit(rc);
icculus@1151
    21
}
icculus@1151
    22
slouken@1293
    23
/* Fill the screen with a gradient */
slouken@1895
    24
static void
slouken@1895
    25
FillBackground(SDL_Surface * screen)
slouken@1293
    26
{
slouken@1895
    27
    Uint8 *buffer;
slouken@1895
    28
    Uint8 gradient;
slouken@1895
    29
    int i, k;
slouken@1293
    30
slouken@1895
    31
    /* Set the surface pixels and refresh! */
slouken@1895
    32
    if (SDL_LockSurface(screen) < 0) {
slouken@1895
    33
        fprintf(stderr, "Couldn't lock the display surface: %s\n",
slouken@1895
    34
                SDL_GetError());
slouken@1895
    35
        quit(2);
slouken@1895
    36
    }
slouken@1895
    37
    buffer = (Uint8 *) screen->pixels;
slouken@2799
    38
    switch (screen->format->BytesPerPixel) {
slouken@2806
    39
    case 1:
slouken@2806
    40
    case 3:
slouken@2806
    41
        for (i = 0; i < screen->h; ++i) {
slouken@2806
    42
            memset(buffer, (i * 255) / screen->h,
slouken@2806
    43
                   screen->w * screen->format->BytesPerPixel);
slouken@2806
    44
            buffer += screen->pitch;
slouken@2806
    45
        }
slouken@2806
    46
        break;
slouken@2806
    47
    case 2:
slouken@2806
    48
        for (i = 0; i < screen->h; ++i) {
slouken@2806
    49
            Uint16 *buffer16;
slouken@2806
    50
            Uint16 color;
slouken@2806
    51
slouken@2806
    52
            gradient = ((i * 255) / screen->h);
slouken@2806
    53
            color = (Uint16) SDL_MapRGB(screen->format,
slouken@2806
    54
                                        gradient, gradient, gradient);
slouken@2806
    55
            buffer16 = (Uint16 *) buffer;
slouken@2806
    56
            for (k = 0; k < screen->w; k++) {
slouken@2806
    57
                *buffer16++ = color;
slouken@1895
    58
            }
slouken@2806
    59
            buffer += screen->pitch;
slouken@2806
    60
        }
slouken@2806
    61
        break;
slouken@2806
    62
    case 4:
slouken@2806
    63
        for (i = 0; i < screen->h; ++i) {
slouken@2806
    64
            Uint32 *buffer32;
slouken@2806
    65
            Uint32 color;
slouken@2799
    66
slouken@2806
    67
            gradient = ((i * 255) / screen->h);
slouken@2806
    68
            color = SDL_MapRGB(screen->format, gradient, gradient, gradient);
slouken@2806
    69
            buffer32 = (Uint32 *) buffer;
slouken@2806
    70
            for (k = 0; k < screen->w; k++) {
slouken@2806
    71
                *buffer32++ = color;
slouken@2799
    72
            }
slouken@2806
    73
            buffer += screen->pitch;
slouken@2806
    74
        }
slouken@2806
    75
        break;
slouken@1895
    76
    }
slouken@1293
    77
slouken@1895
    78
    SDL_UnlockSurface(screen);
slouken@1895
    79
    SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1293
    80
}
icculus@1151
    81
slouken@0
    82
/* Create a "light" -- a yellowish surface with variable alpha */
slouken@1895
    83
SDL_Surface *
slouken@1895
    84
CreateLight(int radius)
slouken@0
    85
{
slouken@1895
    86
    Uint8 trans, alphamask;
slouken@1895
    87
    int range, addition;
slouken@1895
    88
    int xdist, ydist;
slouken@1895
    89
    Uint16 x, y;
slouken@1895
    90
    Uint16 skip;
slouken@1895
    91
    Uint32 pixel;
slouken@1895
    92
    SDL_Surface *light;
slouken@0
    93
slouken@0
    94
#ifdef LIGHT_16BIT
slouken@1895
    95
    Uint16 *buf;
slouken@0
    96
slouken@1895
    97
    /* Create a 16 (4/4/4/4) bpp square with a full 4-bit alpha channel */
slouken@1895
    98
    /* Note: this isn't any faster than a 32 bit alpha surface */
slouken@1895
    99
    alphamask = 0x0000000F;
slouken@1895
   100
    light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 16,
slouken@1895
   101
                                 0x0000F000, 0x00000F00, 0x000000F0,
slouken@1895
   102
                                 alphamask);
slouken@0
   103
#else
slouken@1895
   104
    Uint32 *buf;
slouken@0
   105
slouken@1895
   106
    /* Create a 32 (8/8/8/8) bpp square with a full 8-bit alpha channel */
slouken@1895
   107
    alphamask = 0x000000FF;
slouken@1895
   108
    light = SDL_CreateRGBSurface(SDL_SWSURFACE, 2 * radius, 2 * radius, 32,
slouken@1895
   109
                                 0xFF000000, 0x00FF0000, 0x0000FF00,
slouken@1895
   110
                                 alphamask);
slouken@1895
   111
    if (light == NULL) {
slouken@1895
   112
        fprintf(stderr, "Couldn't create light: %s\n", SDL_GetError());
slouken@1895
   113
        return (NULL);
slouken@1895
   114
    }
slouken@0
   115
#endif
slouken@0
   116
slouken@1895
   117
    /* Fill with a light yellow-orange color */
slouken@1895
   118
    skip = light->pitch - (light->w * light->format->BytesPerPixel);
slouken@0
   119
#ifdef LIGHT_16BIT
slouken@1895
   120
    buf = (Uint16 *) light->pixels;
slouken@0
   121
#else
slouken@1895
   122
    buf = (Uint32 *) light->pixels;
slouken@0
   123
#endif
slouken@1895
   124
    /* Get a tranparent pixel value - we'll add alpha later */
slouken@1895
   125
    pixel = SDL_MapRGBA(light->format, 0xFF, 0xDD, 0x88, 0);
slouken@1895
   126
    for (y = 0; y < light->h; ++y) {
slouken@1895
   127
        for (x = 0; x < light->w; ++x) {
slouken@1895
   128
            *buf++ = pixel;
slouken@1895
   129
        }
slouken@1895
   130
        buf += skip;            /* Almost always 0, but just in case... */
slouken@1895
   131
    }
slouken@0
   132
slouken@1895
   133
    /* Calculate alpha values for the surface. */
slouken@0
   134
#ifdef LIGHT_16BIT
slouken@1895
   135
    buf = (Uint16 *) light->pixels;
slouken@0
   136
#else
slouken@1895
   137
    buf = (Uint32 *) light->pixels;
slouken@0
   138
#endif
slouken@1895
   139
    for (y = 0; y < light->h; ++y) {
slouken@1895
   140
        for (x = 0; x < light->w; ++x) {
slouken@1895
   141
            /* Slow distance formula (from center of light) */
slouken@1895
   142
            xdist = x - (light->w / 2);
slouken@1895
   143
            ydist = y - (light->h / 2);
slouken@1895
   144
            range = (int) sqrt(xdist * xdist + ydist * ydist);
slouken@0
   145
slouken@1895
   146
            /* Scale distance to range of transparency (0-255) */
slouken@1895
   147
            if (range > radius) {
slouken@1895
   148
                trans = alphamask;
slouken@1895
   149
            } else {
slouken@1895
   150
                /* Increasing transparency with distance */
slouken@1895
   151
                trans = (Uint8) ((range * alphamask) / radius);
slouken@0
   152
slouken@1895
   153
                /* Lights are very transparent */
slouken@1895
   154
                addition = (alphamask + 1) / 8;
slouken@1895
   155
                if ((int) trans + addition > alphamask) {
slouken@1895
   156
                    trans = alphamask;
slouken@1895
   157
                } else {
slouken@1895
   158
                    trans += addition;
slouken@1895
   159
                }
slouken@1895
   160
            }
slouken@1895
   161
            /* We set the alpha component as the right N bits */
slouken@1895
   162
            *buf++ |= (255 - trans);
slouken@1895
   163
        }
slouken@1895
   164
        buf += skip;            /* Almost always 0, but just in case... */
slouken@1895
   165
    }
slouken@1895
   166
    /* Enable RLE acceleration of this alpha surface */
slouken@1895
   167
    SDL_SetAlpha(light, SDL_SRCALPHA | SDL_RLEACCEL, 0);
slouken@0
   168
slouken@1895
   169
    /* We're done! */
slouken@1895
   170
    return (light);
slouken@0
   171
}
slouken@0
   172
slouken@0
   173
static Uint32 flashes = 0;
slouken@0
   174
static Uint32 flashtime = 0;
slouken@0
   175
slouken@1895
   176
void
slouken@1895
   177
FlashLight(SDL_Surface * screen, SDL_Surface * light, int x, int y)
slouken@0
   178
{
slouken@1895
   179
    SDL_Rect position;
slouken@1895
   180
    Uint32 ticks1;
slouken@1895
   181
    Uint32 ticks2;
slouken@0
   182
slouken@1895
   183
    /* Easy, center light */
slouken@1895
   184
    position.x = x - (light->w / 2);
slouken@1895
   185
    position.y = y - (light->h / 2);
slouken@1895
   186
    position.w = light->w;
slouken@1895
   187
    position.h = light->h;
slouken@1895
   188
    ticks1 = SDL_GetTicks();
slouken@1895
   189
    SDL_BlitSurface(light, NULL, screen, &position);
slouken@1895
   190
    ticks2 = SDL_GetTicks();
slouken@1895
   191
    SDL_UpdateRects(screen, 1, &position);
slouken@1895
   192
    ++flashes;
slouken@0
   193
slouken@1895
   194
    /* Update time spend doing alpha blitting */
slouken@1895
   195
    flashtime += (ticks2 - ticks1);
slouken@0
   196
}
slouken@0
   197
slouken@0
   198
static int sprite_visible = 0;
slouken@0
   199
static SDL_Surface *sprite;
slouken@0
   200
static SDL_Surface *backing;
slouken@1895
   201
static SDL_Rect position;
slouken@1895
   202
static int x_vel, y_vel;
slouken@1895
   203
static int alpha_vel;
slouken@0
   204
slouken@1895
   205
int
slouken@1895
   206
LoadSprite(SDL_Surface * screen, char *file)
slouken@0
   207
{
slouken@1895
   208
    SDL_Surface *converted;
slouken@0
   209
slouken@1895
   210
    /* Load the sprite image */
slouken@1895
   211
    sprite = SDL_LoadBMP(file);
slouken@1895
   212
    if (sprite == NULL) {
slouken@1895
   213
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@1895
   214
        return (-1);
slouken@1895
   215
    }
slouken@0
   216
slouken@1895
   217
    /* Set transparent pixel as the pixel at (0,0) */
slouken@1895
   218
    if (sprite->format->palette) {
slouken@1895
   219
        SDL_SetColorKey(sprite, SDL_SRCCOLORKEY, *(Uint8 *) sprite->pixels);
slouken@1895
   220
    }
slouken@0
   221
slouken@1895
   222
    /* Convert sprite to video format */
slouken@1895
   223
    converted = SDL_DisplayFormat(sprite);
slouken@1895
   224
    SDL_FreeSurface(sprite);
slouken@1895
   225
    if (converted == NULL) {
slouken@1895
   226
        fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
slouken@1895
   227
        return (-1);
slouken@1895
   228
    }
slouken@1895
   229
    sprite = converted;
slouken@0
   230
slouken@1895
   231
    /* Create the background */
slouken@1895
   232
    backing = SDL_CreateRGBSurface(SDL_SWSURFACE, sprite->w, sprite->h, 8,
slouken@1895
   233
                                   0, 0, 0, 0);
slouken@1895
   234
    if (backing == NULL) {
slouken@1895
   235
        fprintf(stderr, "Couldn't create background: %s\n", SDL_GetError());
slouken@1895
   236
        SDL_FreeSurface(sprite);
slouken@1895
   237
        return (-1);
slouken@1895
   238
    }
slouken@0
   239
slouken@1895
   240
    /* Convert background to video format */
slouken@1895
   241
    converted = SDL_DisplayFormat(backing);
slouken@1895
   242
    SDL_FreeSurface(backing);
slouken@1895
   243
    if (converted == NULL) {
slouken@1895
   244
        fprintf(stderr, "Couldn't convert background: %s\n", SDL_GetError());
slouken@1895
   245
        SDL_FreeSurface(sprite);
slouken@1895
   246
        return (-1);
slouken@1895
   247
    }
slouken@1895
   248
    backing = converted;
slouken@0
   249
slouken@1895
   250
    /* Set the initial position of the sprite */
slouken@1895
   251
    position.x = (screen->w - sprite->w) / 2;
slouken@1895
   252
    position.y = (screen->h - sprite->h) / 2;
slouken@1895
   253
    position.w = sprite->w;
slouken@1895
   254
    position.h = sprite->h;
slouken@1895
   255
    x_vel = 0;
slouken@1895
   256
    y_vel = 0;
slouken@1895
   257
    alpha_vel = 1;
slouken@0
   258
slouken@1895
   259
    /* We're ready to roll. :) */
slouken@1895
   260
    return (0);
slouken@0
   261
}
slouken@0
   262
slouken@1895
   263
void
slouken@1895
   264
AttractSprite(Uint16 x, Uint16 y)
slouken@0
   265
{
slouken@1895
   266
    x_vel = ((int) x - position.x) / 10;
slouken@1895
   267
    y_vel = ((int) y - position.y) / 10;
slouken@0
   268
}
slouken@0
   269
slouken@1895
   270
void
slouken@1895
   271
MoveSprite(SDL_Surface * screen, SDL_Surface * light)
slouken@0
   272
{
slouken@1895
   273
    SDL_Rect updates[2];
slouken@2267
   274
    Uint8 alpha;
slouken@0
   275
slouken@1895
   276
    /* Erase the sprite if it was visible */
slouken@1895
   277
    if (sprite_visible) {
slouken@1895
   278
        updates[0] = position;
slouken@1895
   279
        SDL_BlitSurface(backing, NULL, screen, &updates[0]);
slouken@1895
   280
    } else {
slouken@1895
   281
        updates[0].x = 0;
slouken@1895
   282
        updates[0].y = 0;
slouken@1895
   283
        updates[0].w = 0;
slouken@1895
   284
        updates[0].h = 0;
slouken@1895
   285
        sprite_visible = 1;
slouken@1895
   286
    }
slouken@0
   287
slouken@1895
   288
    /* Since the sprite is off the screen, we can do other drawing
slouken@1895
   289
       without being overwritten by the saved area behind the sprite.
slouken@1895
   290
     */
slouken@1895
   291
    if (light != NULL) {
slouken@1895
   292
        int x, y;
slouken@0
   293
slouken@2710
   294
        SDL_GetMouseState(0, &x, &y);
slouken@1895
   295
        FlashLight(screen, light, x, y);
slouken@1895
   296
    }
slouken@0
   297
slouken@1895
   298
    /* Move the sprite, bounce at the wall */
slouken@1895
   299
    position.x += x_vel;
slouken@1895
   300
    if ((position.x < 0) || (position.x >= screen->w)) {
slouken@1895
   301
        x_vel = -x_vel;
slouken@1895
   302
        position.x += x_vel;
slouken@1895
   303
    }
slouken@1895
   304
    position.y += y_vel;
slouken@1895
   305
    if ((position.y < 0) || (position.y >= screen->h)) {
slouken@1895
   306
        y_vel = -y_vel;
slouken@1895
   307
        position.y += y_vel;
slouken@1895
   308
    }
slouken@0
   309
slouken@1895
   310
    /* Update transparency (fade in and out) */
slouken@2267
   311
    SDL_GetSurfaceAlphaMod(sprite, &alpha);
slouken@2267
   312
    if (((int) alpha + alpha_vel) < 0) {
slouken@1895
   313
        alpha_vel = -alpha_vel;
slouken@2267
   314
    } else if (((int) alpha + alpha_vel) > 255) {
slouken@1895
   315
        alpha_vel = -alpha_vel;
slouken@1895
   316
    }
slouken@1895
   317
    SDL_SetAlpha(sprite, SDL_SRCALPHA, (Uint8) (alpha + alpha_vel));
slouken@0
   318
slouken@1895
   319
    /* Save the area behind the sprite */
slouken@1895
   320
    updates[1] = position;
slouken@1895
   321
    SDL_BlitSurface(screen, &updates[1], backing, NULL);
slouken@1895
   322
slouken@1895
   323
    /* Blit the sprite onto the screen */
slouken@1895
   324
    updates[1] = position;
slouken@1895
   325
    SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
slouken@1895
   326
slouken@1895
   327
    /* Make it so! */
slouken@1895
   328
    SDL_UpdateRects(screen, 2, updates);
slouken@0
   329
}
slouken@0
   330
slouken@1895
   331
void
slouken@1895
   332
WarpSprite(SDL_Surface * screen, int x, int y)
slouken@0
   333
{
slouken@1895
   334
    SDL_Rect updates[2];
slouken@0
   335
slouken@1895
   336
    /* Erase, move, Draw, update */
slouken@1895
   337
    updates[0] = position;
slouken@1895
   338
    SDL_BlitSurface(backing, NULL, screen, &updates[0]);
slouken@1895
   339
    position.x = x - sprite->w / 2;     /* Center about X */
slouken@1895
   340
    position.y = y - sprite->h / 2;     /* Center about Y */
slouken@1895
   341
    updates[1] = position;
slouken@1895
   342
    SDL_BlitSurface(screen, &updates[1], backing, NULL);
slouken@1895
   343
    updates[1] = position;
slouken@1895
   344
    SDL_BlitSurface(sprite, NULL, screen, &updates[1]);
slouken@1895
   345
    SDL_UpdateRects(screen, 2, updates);
slouken@0
   346
}
slouken@0
   347
slouken@1895
   348
int
slouken@1895
   349
main(int argc, char *argv[])
slouken@0
   350
{
slouken@1895
   351
    const SDL_VideoInfo *info;
slouken@1895
   352
    SDL_Surface *screen;
slouken@1895
   353
    int w, h;
slouken@1895
   354
    Uint8 video_bpp;
slouken@1895
   355
    Uint32 videoflags;
slouken@1895
   356
    int i, done;
slouken@1895
   357
    SDL_Event event;
slouken@1895
   358
    SDL_Surface *light;
slouken@1895
   359
    int mouse_pressed;
slouken@1895
   360
    Uint32 ticks, lastticks;
slouken@691
   361
slouken@0
   362
slouken@1895
   363
    /* Initialize SDL */
slouken@1895
   364
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   365
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   366
        return (1);
slouken@1895
   367
    }
slouken@0
   368
slouken@1895
   369
    /* Alpha blending doesn't work well at 8-bit color */
slouken@1465
   370
#ifdef _WIN32_WCE
slouken@1895
   371
    /* Pocket PC */
slouken@1895
   372
    w = 240;
slouken@1895
   373
    h = 320;
slouken@1465
   374
#else
slouken@1895
   375
    w = 640;
slouken@1895
   376
    h = 480;
slouken@1465
   377
#endif
slouken@1895
   378
    info = SDL_GetVideoInfo();
slouken@1895
   379
    if (info->vfmt->BitsPerPixel > 8) {
slouken@1895
   380
        video_bpp = info->vfmt->BitsPerPixel;
slouken@1895
   381
    } else {
slouken@1895
   382
        video_bpp = 16;
slouken@1895
   383
        fprintf(stderr, "forced 16 bpp mode\n");
slouken@1895
   384
    }
slouken@1895
   385
    videoflags = SDL_SWSURFACE;
slouken@1895
   386
    for (i = 1; argv[i]; ++i) {
slouken@1895
   387
        if (strcmp(argv[i], "-bpp") == 0) {
slouken@1895
   388
            video_bpp = atoi(argv[++i]);
slouken@1895
   389
            if (video_bpp <= 8) {
slouken@1895
   390
                video_bpp = 16;
slouken@691
   391
                fprintf(stderr, "forced 16 bpp mode\n");
slouken@1895
   392
            }
slouken@1895
   393
        } else if (strcmp(argv[i], "-hw") == 0) {
slouken@1895
   394
            videoflags |= SDL_HWSURFACE;
slouken@1895
   395
        } else if (strcmp(argv[i], "-warp") == 0) {
slouken@1895
   396
            videoflags |= SDL_HWPALETTE;
slouken@1895
   397
        } else if (strcmp(argv[i], "-width") == 0 && argv[i + 1]) {
slouken@1895
   398
            w = atoi(argv[++i]);
slouken@1895
   399
        } else if (strcmp(argv[i], "-height") == 0 && argv[i + 1]) {
slouken@1895
   400
            h = atoi(argv[++i]);
slouken@1895
   401
        } else if (strcmp(argv[i], "-resize") == 0) {
slouken@1895
   402
            videoflags |= SDL_RESIZABLE;
slouken@1895
   403
        } else if (strcmp(argv[i], "-noframe") == 0) {
slouken@1895
   404
            videoflags |= SDL_NOFRAME;
slouken@1895
   405
        } else if (strcmp(argv[i], "-fullscreen") == 0) {
slouken@1895
   406
            videoflags |= SDL_FULLSCREEN;
slouken@1895
   407
        } else {
slouken@1895
   408
            fprintf(stderr,
slouken@1895
   409
                    "Usage: %s [-width N] [-height N] [-bpp N] [-warp] [-hw] [-fullscreen]\n",
slouken@1895
   410
                    argv[0]);
slouken@1895
   411
            quit(1);
slouken@1895
   412
        }
slouken@1895
   413
    }
slouken@0
   414
slouken@1895
   415
    /* Set video mode */
slouken@1895
   416
    if ((screen = SDL_SetVideoMode(w, h, video_bpp, videoflags)) == NULL) {
slouken@1895
   417
        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@1895
   418
                w, h, video_bpp, SDL_GetError());
slouken@1895
   419
        quit(2);
slouken@1895
   420
    }
slouken@1895
   421
    FillBackground(screen);
slouken@0
   422
slouken@1895
   423
    /* Create the light */
slouken@1895
   424
    light = CreateLight(82);
slouken@1895
   425
    if (light == NULL) {
slouken@1895
   426
        quit(1);
slouken@1895
   427
    }
slouken@0
   428
slouken@1895
   429
    /* Load the sprite */
slouken@1895
   430
    if (LoadSprite(screen, "icon.bmp") < 0) {
slouken@1895
   431
        SDL_FreeSurface(light);
slouken@1895
   432
        quit(1);
slouken@1895
   433
    }
slouken@0
   434
slouken@1895
   435
    /* Print out information about our surfaces */
slouken@1895
   436
    printf("Screen is at %d bits per pixel\n", screen->format->BitsPerPixel);
slouken@1895
   437
    if ((screen->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1895
   438
        printf("Screen is in video memory\n");
slouken@1895
   439
    } else {
slouken@1895
   440
        printf("Screen is in system memory\n");
slouken@1895
   441
    }
slouken@1895
   442
    if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) {
slouken@1895
   443
        printf("Screen has double-buffering enabled\n");
slouken@1895
   444
    }
slouken@1895
   445
    if ((sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE) {
slouken@1895
   446
        printf("Sprite is in video memory\n");
slouken@1895
   447
    } else {
slouken@1895
   448
        printf("Sprite is in system memory\n");
slouken@1895
   449
    }
slouken@886
   450
slouken@1895
   451
    /* Run a sample blit to trigger blit acceleration */
slouken@1895
   452
    MoveSprite(screen, NULL);
slouken@1895
   453
    if ((sprite->flags & SDL_HWACCEL) == SDL_HWACCEL) {
slouken@1895
   454
        printf("Sprite blit uses hardware alpha acceleration\n");
slouken@1895
   455
    } else {
slouken@1895
   456
        printf("Sprite blit dosn't uses hardware alpha acceleration\n");
slouken@1895
   457
    }
slouken@886
   458
slouken@1895
   459
    /* Set a clipping rectangle to clip the outside edge of the screen */
slouken@1895
   460
    {
slouken@1895
   461
        SDL_Rect clip;
slouken@1895
   462
        clip.x = 32;
slouken@1895
   463
        clip.y = 32;
slouken@1895
   464
        clip.w = screen->w - (2 * 32);
slouken@1895
   465
        clip.h = screen->h - (2 * 32);
slouken@1895
   466
        SDL_SetClipRect(screen, &clip);
slouken@1895
   467
    }
slouken@0
   468
slouken@1895
   469
    /* Wait for a keystroke */
slouken@1895
   470
    lastticks = SDL_GetTicks();
slouken@1895
   471
    done = 0;
slouken@1895
   472
    mouse_pressed = 0;
slouken@1895
   473
    while (!done) {
slouken@1895
   474
        /* Update the frame -- move the sprite */
slouken@1895
   475
        if (mouse_pressed) {
slouken@1895
   476
            MoveSprite(screen, light);
slouken@1895
   477
            mouse_pressed = 0;
slouken@1895
   478
        } else {
slouken@1895
   479
            MoveSprite(screen, NULL);
slouken@1895
   480
        }
slouken@0
   481
slouken@1895
   482
        /* Slow down the loop to 30 frames/second */
slouken@1895
   483
        ticks = SDL_GetTicks();
slouken@1895
   484
        if ((ticks - lastticks) < FRAME_TICKS) {
slouken@0
   485
#ifdef CHECK_SLEEP_GRANULARITY
slouken@1895
   486
            fprintf(stderr, "Sleeping %d ticks\n",
slouken@1895
   487
                    FRAME_TICKS - (ticks - lastticks));
slouken@0
   488
#endif
slouken@1895
   489
            SDL_Delay(FRAME_TICKS - (ticks - lastticks));
slouken@0
   490
#ifdef CHECK_SLEEP_GRANULARITY
slouken@1895
   491
            fprintf(stderr, "Slept %d ticks\n", (SDL_GetTicks() - ticks));
slouken@0
   492
#endif
slouken@1895
   493
        }
slouken@1895
   494
        lastticks = ticks;
slouken@0
   495
slouken@1895
   496
        /* Check for events */
slouken@1895
   497
        while (SDL_PollEvent(&event)) {
slouken@1895
   498
            switch (event.type) {
slouken@1895
   499
            case SDL_VIDEORESIZE:
slouken@1895
   500
                screen =
slouken@1895
   501
                    SDL_SetVideoMode(event.resize.w, event.resize.h,
slouken@1895
   502
                                     video_bpp, videoflags);
slouken@1895
   503
                if (screen) {
slouken@1895
   504
                    FillBackground(screen);
slouken@1895
   505
                }
slouken@1895
   506
                break;
slouken@1895
   507
                /* Attract sprite while mouse is held down */
slouken@1895
   508
            case SDL_MOUSEMOTION:
slouken@1895
   509
                if (event.motion.state != 0) {
slouken@1895
   510
                    AttractSprite(event.motion.x, event.motion.y);
slouken@1895
   511
                    mouse_pressed = 1;
slouken@1895
   512
                }
slouken@1895
   513
                break;
slouken@1895
   514
            case SDL_MOUSEBUTTONDOWN:
slouken@1895
   515
                if (event.button.button == 1) {
slouken@1895
   516
                    AttractSprite(event.button.x, event.button.y);
slouken@1895
   517
                    mouse_pressed = 1;
slouken@1895
   518
                } else {
slouken@1895
   519
                    SDL_Rect area;
slouken@0
   520
slouken@1895
   521
                    area.x = event.button.x - 16;
slouken@1895
   522
                    area.y = event.button.y - 16;
slouken@1895
   523
                    area.w = 32;
slouken@1895
   524
                    area.h = 32;
slouken@2806
   525
                    SDL_FillRect(screen, &area,
slouken@2806
   526
                                 SDL_MapRGB(screen->format, 0, 0, 0));
slouken@1895
   527
                    SDL_UpdateRects(screen, 1, &area);
slouken@1895
   528
                }
slouken@1895
   529
                break;
slouken@1895
   530
            case SDL_KEYDOWN:
slouken@1895
   531
                if (event.key.keysym.sym == SDLK_ESCAPE) {
slouken@1895
   532
                    done = 1;
slouken@1895
   533
                }
slouken@1895
   534
                break;
slouken@1895
   535
            case SDL_QUIT:
slouken@1895
   536
                done = 1;
slouken@1895
   537
                break;
slouken@1895
   538
            default:
slouken@1895
   539
                break;
slouken@1895
   540
            }
slouken@1895
   541
        }
slouken@1895
   542
    }
slouken@1895
   543
    SDL_FreeSurface(light);
slouken@1895
   544
    SDL_FreeSurface(sprite);
slouken@1895
   545
    SDL_FreeSurface(backing);
slouken@0
   546
slouken@1895
   547
    /* Print out some timing information */
slouken@1895
   548
    if (flashes > 0) {
slouken@1895
   549
        printf("%d alpha blits, ~%4.4f ms per blit\n",
slouken@1895
   550
               flashes, (float) flashtime / flashes);
slouken@1895
   551
    }
icculus@1151
   552
slouken@1895
   553
    SDL_Quit();
slouken@1895
   554
    return (0);
slouken@0
   555
}