test/testwin.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
slouken@0
     2
/* Bring up a window and play with it */
slouken@0
     3
slouken@0
     4
#include <stdlib.h>
slouken@0
     5
#include <stdio.h>
slouken@0
     6
#include <string.h>
slouken@0
     7
slouken@0
     8
#define BENCHMARK_SDL
slouken@0
     9
slouken@0
    10
#define NOTICE(X)	printf("%s", X);
slouken@0
    11
slouken@0
    12
#include "SDL.h"
slouken@0
    13
icculus@1151
    14
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1662
    15
static void
slouken@1668
    16
quit(int rc)
icculus@1151
    17
{
slouken@1668
    18
    SDL_Quit();
slouken@1668
    19
    exit(rc);
icculus@1151
    20
}
icculus@1151
    21
slouken@1662
    22
void
slouken@1668
    23
DrawPict(SDL_Surface * screen, char *bmpfile,
slouken@1668
    24
         int speedy, int flip, int nofade)
slouken@0
    25
{
slouken@1662
    26
    SDL_Surface *picture;
slouken@1662
    27
    SDL_Rect dest, update;
slouken@1662
    28
    int i, centered;
slouken@1662
    29
    int ncolors;
slouken@1662
    30
    SDL_Color *colors, *cmap;
slouken@0
    31
slouken@1662
    32
    /* Load the image into a surface */
slouken@1662
    33
    if (bmpfile == NULL) {
slouken@1662
    34
        bmpfile = "sample.bmp"; /* Sample image */
slouken@1662
    35
    }
slouken@1668
    36
    fprintf(stderr, "Loading picture: %s\n", bmpfile);
slouken@1668
    37
    picture = SDL_LoadBMP(bmpfile);
slouken@1662
    38
    if (picture == NULL) {
slouken@1668
    39
        fprintf(stderr, "Couldn't load %s: %s\n", bmpfile, SDL_GetError());
slouken@1662
    40
        return;
slouken@1662
    41
    }
slouken@0
    42
slouken@1662
    43
    /* Set the display colors -- on a hicolor display this is a no-op */
slouken@1662
    44
    if (picture->format->palette) {
slouken@1662
    45
        ncolors = picture->format->palette->ncolors;
slouken@1668
    46
        colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
slouken@1668
    47
        cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
slouken@1668
    48
        memcpy(colors, picture->format->palette->colors,
slouken@1668
    49
               ncolors * sizeof(SDL_Color));
slouken@1662
    50
    } else {
slouken@1662
    51
        int r, g, b;
slouken@0
    52
slouken@1662
    53
        /* Allocate 256 color palette */
slouken@1662
    54
        ncolors = 256;
slouken@1668
    55
        colors = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
slouken@1668
    56
        cmap = (SDL_Color *) malloc(ncolors * sizeof(SDL_Color));
slouken@0
    57
slouken@1662
    58
        /* Set a 3,3,2 color cube */
slouken@1662
    59
        for (r = 0; r < 8; ++r) {
slouken@1662
    60
            for (g = 0; g < 8; ++g) {
slouken@1662
    61
                for (b = 0; b < 4; ++b) {
slouken@1662
    62
                    i = ((r << 5) | (g << 2) | b);
slouken@1662
    63
                    colors[i].r = r << 5;
slouken@1662
    64
                    colors[i].g = g << 5;
slouken@1662
    65
                    colors[i].b = b << 6;
slouken@1662
    66
                }
slouken@1662
    67
            }
slouken@1662
    68
        }
slouken@1662
    69
    }
slouken@1668
    70
    NOTICE("testwin: setting colors\n");
slouken@1668
    71
    if (!SDL_SetColors(screen, colors, 0, ncolors) &&
slouken@1662
    72
        (screen->format->palette != NULL)) {
slouken@1668
    73
        fprintf(stderr,
slouken@1668
    74
                "Warning: Couldn't set all of the colors, but SDL will map the image\n"
slouken@1668
    75
                "         (colormap fading will suffer - try the -warp option)\n");
slouken@1662
    76
    }
slouken@0
    77
slouken@1662
    78
    /* Set the screen to black (not really necessary) */
slouken@1668
    79
    if (SDL_LockSurface(screen) == 0) {
slouken@1662
    80
        Uint32 black;
slouken@1662
    81
        Uint8 *pixels;
slouken@0
    82
slouken@1668
    83
        black = SDL_MapRGB(screen->format, 0, 0, 0);
slouken@1662
    84
        pixels = (Uint8 *) screen->pixels;
slouken@1662
    85
        for (i = 0; i < screen->h; ++i) {
slouken@1668
    86
            memset(pixels, black, screen->w * screen->format->BytesPerPixel);
slouken@1662
    87
            pixels += screen->pitch;
slouken@1662
    88
        }
slouken@1668
    89
        SDL_UnlockSurface(screen);
slouken@1668
    90
        SDL_UpdateRect(screen, 0, 0, 0, 0);
slouken@1662
    91
    }
slouken@0
    92
slouken@1662
    93
    /* Display the picture */
slouken@1662
    94
    if (speedy) {
slouken@1662
    95
        SDL_Surface *displayfmt;
slouken@1662
    96
slouken@1668
    97
        fprintf(stderr, "Converting picture\n");
slouken@1668
    98
        displayfmt = SDL_DisplayFormat(picture);
slouken@1662
    99
        if (displayfmt == NULL) {
slouken@1668
   100
            fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
slouken@1662
   101
            goto done;
slouken@1662
   102
        }
slouken@1668
   103
        SDL_FreeSurface(picture);
slouken@1662
   104
        picture = displayfmt;
slouken@1662
   105
    }
slouken@1668
   106
    printf("(image surface located in %s memory)\n",
slouken@1668
   107
           (picture->flags & SDL_HWSURFACE) ? "video" : "system");
slouken@1662
   108
    centered = (screen->w - picture->w) / 2;
slouken@1662
   109
    if (centered < 0) {
slouken@1662
   110
        centered = 0;
slouken@1662
   111
    }
slouken@1662
   112
    dest.y = (screen->h - picture->h) / 2;
slouken@1662
   113
    dest.w = picture->w;
slouken@1662
   114
    dest.h = picture->h;
slouken@1668
   115
    NOTICE("testwin: moving image\n");
slouken@1662
   116
    for (i = 0; i <= centered; ++i) {
slouken@1662
   117
        dest.x = i;
slouken@1662
   118
        update = dest;
slouken@1668
   119
        if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
slouken@1668
   120
            fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
slouken@1662
   121
            break;
slouken@1662
   122
        }
slouken@1662
   123
        if (flip) {
slouken@1668
   124
            SDL_Flip(screen);
slouken@1662
   125
        } else {
slouken@1668
   126
            SDL_UpdateRects(screen, 1, &update);
slouken@1662
   127
        }
slouken@1662
   128
    }
slouken@0
   129
slouken@0
   130
#ifdef SCREENSHOT
slouken@1668
   131
    if (SDL_SaveBMP(screen, "screen.bmp") < 0)
slouken@1668
   132
        printf("Couldn't save screen: %s\n", SDL_GetError());
slouken@0
   133
#endif
slouken@0
   134
slouken@0
   135
#ifndef BENCHMARK_SDL
slouken@1662
   136
    /* Let it sit there for a while */
slouken@1668
   137
    SDL_Delay(5 * 1000);
slouken@0
   138
#endif
slouken@1662
   139
    /* Fade the colormap */
slouken@1662
   140
    if (!nofade) {
slouken@1662
   141
        int maxstep;
slouken@1662
   142
        SDL_Color final;
slouken@1662
   143
        SDL_Color palcolors[256];
slouken@1662
   144
        struct
slouken@1662
   145
        {
slouken@1662
   146
            Sint16 r, g, b;
slouken@1662
   147
        } cdist[256];
slouken@0
   148
slouken@1668
   149
        NOTICE("testwin: fading out...\n");
slouken@1668
   150
        memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
slouken@1662
   151
        maxstep = 32 - 1;
slouken@1662
   152
        final.r = 0xFF;
slouken@1662
   153
        final.g = 0x00;
slouken@1662
   154
        final.b = 0x00;
slouken@1668
   155
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1662
   156
        for (i = 0; i < ncolors; ++i) {
slouken@1662
   157
            cdist[i].r = final.r - palcolors[i].r;
slouken@1662
   158
            cdist[i].g = final.g - palcolors[i].g;
slouken@1662
   159
            cdist[i].b = final.b - palcolors[i].b;
slouken@1662
   160
        }
slouken@1662
   161
        for (i = 0; i <= maxstep / 2; ++i) {    /* halfway fade */
slouken@1662
   162
            int c;
slouken@1662
   163
            for (c = 0; c < ncolors; ++c) {
slouken@1662
   164
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1662
   165
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1662
   166
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1662
   167
            }
slouken@1668
   168
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1668
   169
            SDL_Delay(1);
slouken@1662
   170
        }
slouken@1662
   171
        final.r = 0x00;
slouken@1662
   172
        final.g = 0x00;
slouken@1662
   173
        final.b = 0x00;
slouken@1668
   174
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1662
   175
        for (i = 0; i < ncolors; ++i) {
slouken@1662
   176
            cdist[i].r = final.r - palcolors[i].r;
slouken@1662
   177
            cdist[i].g = final.g - palcolors[i].g;
slouken@1662
   178
            cdist[i].b = final.b - palcolors[i].b;
slouken@1662
   179
        }
slouken@1662
   180
        maxstep /= 2;
slouken@1662
   181
        for (i = 0; i <= maxstep; ++i) {        /* finish fade out */
slouken@1662
   182
            int c;
slouken@1662
   183
            for (c = 0; c < ncolors; ++c) {
slouken@1662
   184
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1662
   185
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1662
   186
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1662
   187
            }
slouken@1668
   188
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1668
   189
            SDL_Delay(1);
slouken@1662
   190
        }
slouken@1662
   191
        for (i = 0; i < ncolors; ++i) {
slouken@1662
   192
            colors[i].r = final.r;
slouken@1662
   193
            colors[i].g = final.g;
slouken@1662
   194
            colors[i].b = final.b;
slouken@1662
   195
        }
slouken@1668
   196
        SDL_SetColors(screen, colors, 0, ncolors);
slouken@1668
   197
        NOTICE("testwin: fading in...\n");
slouken@1668
   198
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1662
   199
        for (i = 0; i < ncolors; ++i) {
slouken@1662
   200
            cdist[i].r = cmap[i].r - palcolors[i].r;
slouken@1662
   201
            cdist[i].g = cmap[i].g - palcolors[i].g;
slouken@1662
   202
            cdist[i].b = cmap[i].b - palcolors[i].b;
slouken@1662
   203
        }
slouken@1662
   204
        for (i = 0; i <= maxstep; ++i) {        /* 32 step fade in */
slouken@1662
   205
            int c;
slouken@1662
   206
            for (c = 0; c < ncolors; ++c) {
slouken@1662
   207
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1662
   208
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1662
   209
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1662
   210
            }
slouken@1668
   211
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1668
   212
            SDL_Delay(1);
slouken@1662
   213
        }
slouken@1668
   214
        NOTICE("testwin: fading over\n");
slouken@1662
   215
    }
slouken@1662
   216
slouken@1662
   217
  done:
slouken@1662
   218
    /* Free the picture and return */
slouken@1668
   219
    SDL_FreeSurface(picture);
slouken@1668
   220
    free(colors);
slouken@1668
   221
    free(cmap);
slouken@1662
   222
    return;
slouken@0
   223
}
slouken@0
   224
slouken@1662
   225
int
slouken@1668
   226
main(int argc, char *argv[])
slouken@0
   227
{
slouken@1662
   228
    SDL_Surface *screen;
slouken@1662
   229
    /* Options */
slouken@1662
   230
    int speedy, flip, nofade;
slouken@1662
   231
    int delay;
slouken@1662
   232
    int w, h;
slouken@1662
   233
    int desired_bpp;
slouken@1662
   234
    Uint32 video_flags;
slouken@0
   235
#ifdef BENCHMARK_SDL
slouken@1662
   236
    Uint32 then, now;
slouken@0
   237
#endif
slouken@1662
   238
    /* Set default options and check command-line */
slouken@1662
   239
    speedy = 0;
slouken@1662
   240
    flip = 0;
slouken@1662
   241
    nofade = 0;
slouken@1662
   242
    delay = 1;
icculus@1152
   243
icculus@1152
   244
#ifdef _WIN32_WCE
slouken@1662
   245
    w = 240;
slouken@1662
   246
    h = 320;
slouken@1662
   247
    desired_bpp = 8;
slouken@1662
   248
    video_flags = SDL_FULLSCREEN;
icculus@1152
   249
#else
slouken@1662
   250
    w = 640;
slouken@1662
   251
    h = 480;
slouken@1662
   252
    desired_bpp = 0;
slouken@1662
   253
    video_flags = 0;
icculus@1152
   254
#endif
slouken@1668
   255
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1668
   256
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1662
   257
        return (1);
slouken@1662
   258
    }
icculus@1151
   259
slouken@1662
   260
    while (argc > 1) {
slouken@1668
   261
        if (strcmp(argv[1], "-speedy") == 0) {
slouken@1662
   262
            speedy = 1;
slouken@1662
   263
            argv += 1;
slouken@1662
   264
            argc -= 1;
slouken@1668
   265
        } else if (strcmp(argv[1], "-nofade") == 0) {
slouken@1662
   266
            nofade = 1;
slouken@1662
   267
            argv += 1;
slouken@1662
   268
            argc -= 1;
slouken@1668
   269
        } else if (strcmp(argv[1], "-delay") == 0) {
slouken@1662
   270
            if (argv[2]) {
slouken@1668
   271
                delay = atoi(argv[2]);
slouken@1662
   272
                argv += 2;
slouken@1662
   273
                argc -= 2;
slouken@1662
   274
            } else {
slouken@1668
   275
                fprintf(stderr, "The -delay option requires an argument\n");
slouken@1668
   276
                quit(1);
slouken@1662
   277
            }
slouken@1668
   278
        } else if (strcmp(argv[1], "-width") == 0) {
slouken@1668
   279
            if (argv[2] && ((w = atoi(argv[2])) > 0)) {
slouken@1662
   280
                argv += 2;
slouken@1662
   281
                argc -= 2;
slouken@1662
   282
            } else {
slouken@1668
   283
                fprintf(stderr, "The -width option requires an argument\n");
slouken@1668
   284
                quit(1);
slouken@1662
   285
            }
slouken@1668
   286
        } else if (strcmp(argv[1], "-height") == 0) {
slouken@1668
   287
            if (argv[2] && ((h = atoi(argv[2])) > 0)) {
slouken@1662
   288
                argv += 2;
slouken@1662
   289
                argc -= 2;
slouken@1662
   290
            } else {
slouken@1668
   291
                fprintf(stderr, "The -height option requires an argument\n");
slouken@1668
   292
                quit(1);
slouken@1662
   293
            }
slouken@1668
   294
        } else if (strcmp(argv[1], "-bpp") == 0) {
slouken@1662
   295
            if (argv[2]) {
slouken@1668
   296
                desired_bpp = atoi(argv[2]);
slouken@1662
   297
                argv += 2;
slouken@1662
   298
                argc -= 2;
slouken@1662
   299
            } else {
slouken@1668
   300
                fprintf(stderr, "The -bpp option requires an argument\n");
slouken@1668
   301
                quit(1);
slouken@1662
   302
            }
slouken@1668
   303
        } else if (strcmp(argv[1], "-warp") == 0) {
slouken@1662
   304
            video_flags |= SDL_HWPALETTE;
slouken@1662
   305
            argv += 1;
slouken@1662
   306
            argc -= 1;
slouken@1668
   307
        } else if (strcmp(argv[1], "-hw") == 0) {
slouken@1662
   308
            video_flags |= SDL_HWSURFACE;
slouken@1662
   309
            argv += 1;
slouken@1662
   310
            argc -= 1;
slouken@1668
   311
        } else if (strcmp(argv[1], "-flip") == 0) {
slouken@1662
   312
            video_flags |= SDL_DOUBLEBUF;
slouken@1662
   313
            argv += 1;
slouken@1662
   314
            argc -= 1;
slouken@1668
   315
        } else if (strcmp(argv[1], "-fullscreen") == 0) {
slouken@1662
   316
            video_flags |= SDL_FULLSCREEN;
slouken@1662
   317
            argv += 1;
slouken@1662
   318
            argc -= 1;
slouken@1662
   319
        } else
slouken@1662
   320
            break;
slouken@1662
   321
    }
slouken@0
   322
slouken@1662
   323
    /* Initialize the display */
slouken@1668
   324
    screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
slouken@1662
   325
    if (screen == NULL) {
slouken@1668
   326
        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@1668
   327
                w, h, desired_bpp, SDL_GetError());
slouken@1668
   328
        quit(1);
slouken@1662
   329
    }
slouken@1668
   330
    printf("Set%s %dx%dx%d mode\n",
slouken@1668
   331
           screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
slouken@1668
   332
           screen->w, screen->h, screen->format->BitsPerPixel);
slouken@1668
   333
    printf("(video surface located in %s memory)\n",
slouken@1668
   334
           (screen->flags & SDL_HWSURFACE) ? "video" : "system");
slouken@1662
   335
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1668
   336
        printf("Double-buffering enabled\n");
slouken@1662
   337
        flip = 1;
slouken@1662
   338
    }
slouken@0
   339
slouken@1662
   340
    /* Set the window manager title bar */
slouken@1668
   341
    SDL_WM_SetCaption("SDL test window", "testwin");
slouken@0
   342
slouken@1662
   343
    /* Do all the drawing work */
slouken@0
   344
#ifdef BENCHMARK_SDL
slouken@1668
   345
    then = SDL_GetTicks();
slouken@1668
   346
    DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@1668
   347
    now = SDL_GetTicks();
slouken@1668
   348
    printf("Time: %d milliseconds\n", now - then);
slouken@0
   349
#else
slouken@1668
   350
    DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@0
   351
#endif
slouken@1668
   352
    SDL_Delay(delay * 1000);
slouken@1668
   353
    SDL_Quit();
slouken@1662
   354
    return (0);
slouken@0
   355
}