test/testwin.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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