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