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