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