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