test/testwin.c
author Mike Gorchak <lestat@i.com.ua>
Sat, 21 Nov 2009 08:42:42 +0000
changeset 3478 f7541260e89c
parent 2821 c4eccbd21d16
child 5535 96594ac5fd1a
permissions -rw-r--r--
Added support for QNX default font. Backspace and Return keys now handled.
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
    /* Display the picture */
slouken@1895
    79
    if (speedy) {
slouken@1895
    80
        SDL_Surface *displayfmt;
slouken@1895
    81
slouken@1895
    82
        fprintf(stderr, "Converting picture\n");
slouken@1895
    83
        displayfmt = SDL_DisplayFormat(picture);
slouken@1895
    84
        if (displayfmt == NULL) {
slouken@1895
    85
            fprintf(stderr, "Couldn't convert image: %s\n", SDL_GetError());
slouken@1895
    86
            goto done;
slouken@1895
    87
        }
slouken@1895
    88
        SDL_FreeSurface(picture);
slouken@1895
    89
        picture = displayfmt;
slouken@1895
    90
    }
slouken@1895
    91
    printf("(image surface located in %s memory)\n",
slouken@1895
    92
           (picture->flags & SDL_HWSURFACE) ? "video" : "system");
slouken@1895
    93
    centered = (screen->w - picture->w) / 2;
slouken@1895
    94
    if (centered < 0) {
slouken@1895
    95
        centered = 0;
slouken@1895
    96
    }
slouken@1895
    97
    dest.y = (screen->h - picture->h) / 2;
slouken@1895
    98
    dest.w = picture->w;
slouken@1895
    99
    dest.h = picture->h;
slouken@1895
   100
    NOTICE("testwin: moving image\n");
slouken@1895
   101
    for (i = 0; i <= centered; ++i) {
slouken@1895
   102
        dest.x = i;
slouken@1895
   103
        update = dest;
slouken@1895
   104
        if (SDL_BlitSurface(picture, NULL, screen, &update) < 0) {
slouken@1895
   105
            fprintf(stderr, "Blit failed: %s\n", SDL_GetError());
slouken@1895
   106
            break;
slouken@1895
   107
        }
slouken@1895
   108
        if (flip) {
slouken@1895
   109
            SDL_Flip(screen);
slouken@1895
   110
        } else {
slouken@1895
   111
            SDL_UpdateRects(screen, 1, &update);
slouken@1895
   112
        }
slouken@1895
   113
    }
slouken@0
   114
slouken@0
   115
#ifdef SCREENSHOT
slouken@1895
   116
    if (SDL_SaveBMP(screen, "screen.bmp") < 0)
slouken@1895
   117
        printf("Couldn't save screen: %s\n", SDL_GetError());
slouken@0
   118
#endif
slouken@0
   119
slouken@0
   120
#ifndef BENCHMARK_SDL
slouken@1895
   121
    /* Let it sit there for a while */
slouken@1895
   122
    SDL_Delay(5 * 1000);
slouken@0
   123
#endif
slouken@1895
   124
    /* Fade the colormap */
slouken@1895
   125
    if (!nofade) {
slouken@1895
   126
        int maxstep;
slouken@1895
   127
        SDL_Color final;
slouken@1895
   128
        SDL_Color palcolors[256];
slouken@1895
   129
        struct
slouken@1895
   130
        {
slouken@1895
   131
            Sint16 r, g, b;
slouken@1895
   132
        } cdist[256];
slouken@0
   133
slouken@1895
   134
        NOTICE("testwin: fading out...\n");
slouken@1895
   135
        memcpy(cmap, colors, ncolors * sizeof(SDL_Color));
slouken@1895
   136
        maxstep = 32 - 1;
slouken@1895
   137
        final.r = 0xFF;
slouken@1895
   138
        final.g = 0x00;
slouken@1895
   139
        final.b = 0x00;
slouken@1895
   140
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1895
   141
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   142
            cdist[i].r = final.r - palcolors[i].r;
slouken@1895
   143
            cdist[i].g = final.g - palcolors[i].g;
slouken@1895
   144
            cdist[i].b = final.b - palcolors[i].b;
slouken@1895
   145
        }
slouken@1895
   146
        for (i = 0; i <= maxstep / 2; ++i) {    /* halfway fade */
slouken@1895
   147
            int c;
slouken@1895
   148
            for (c = 0; c < ncolors; ++c) {
slouken@1895
   149
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1895
   150
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1895
   151
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1895
   152
            }
slouken@1895
   153
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1895
   154
            SDL_Delay(1);
slouken@1895
   155
        }
slouken@1895
   156
        final.r = 0x00;
slouken@1895
   157
        final.g = 0x00;
slouken@1895
   158
        final.b = 0x00;
slouken@1895
   159
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1895
   160
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   161
            cdist[i].r = final.r - palcolors[i].r;
slouken@1895
   162
            cdist[i].g = final.g - palcolors[i].g;
slouken@1895
   163
            cdist[i].b = final.b - palcolors[i].b;
slouken@1895
   164
        }
slouken@1895
   165
        maxstep /= 2;
slouken@1895
   166
        for (i = 0; i <= maxstep; ++i) {        /* finish fade out */
slouken@1895
   167
            int c;
slouken@1895
   168
            for (c = 0; c < ncolors; ++c) {
slouken@1895
   169
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1895
   170
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1895
   171
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1895
   172
            }
slouken@1895
   173
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1895
   174
            SDL_Delay(1);
slouken@1895
   175
        }
slouken@1895
   176
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   177
            colors[i].r = final.r;
slouken@1895
   178
            colors[i].g = final.g;
slouken@1895
   179
            colors[i].b = final.b;
slouken@1895
   180
        }
slouken@1895
   181
        SDL_SetColors(screen, colors, 0, ncolors);
slouken@1895
   182
        NOTICE("testwin: fading in...\n");
slouken@1895
   183
        memcpy(palcolors, colors, ncolors * sizeof(SDL_Color));
slouken@1895
   184
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   185
            cdist[i].r = cmap[i].r - palcolors[i].r;
slouken@1895
   186
            cdist[i].g = cmap[i].g - palcolors[i].g;
slouken@1895
   187
            cdist[i].b = cmap[i].b - palcolors[i].b;
slouken@1895
   188
        }
slouken@1895
   189
        for (i = 0; i <= maxstep; ++i) {        /* 32 step fade in */
slouken@1895
   190
            int c;
slouken@1895
   191
            for (c = 0; c < ncolors; ++c) {
slouken@1895
   192
                colors[c].r = palcolors[c].r + ((cdist[c].r * i)) / maxstep;
slouken@1895
   193
                colors[c].g = palcolors[c].g + ((cdist[c].g * i)) / maxstep;
slouken@1895
   194
                colors[c].b = palcolors[c].b + ((cdist[c].b * i)) / maxstep;
slouken@1895
   195
            }
slouken@1895
   196
            SDL_SetColors(screen, colors, 0, ncolors);
slouken@1895
   197
            SDL_Delay(1);
slouken@1895
   198
        }
slouken@1895
   199
        NOTICE("testwin: fading over\n");
slouken@1895
   200
    }
slouken@1895
   201
slouken@1895
   202
  done:
slouken@1895
   203
    /* Free the picture and return */
slouken@1895
   204
    SDL_FreeSurface(picture);
slouken@1895
   205
    free(colors);
slouken@1895
   206
    free(cmap);
slouken@1895
   207
    return;
slouken@0
   208
}
slouken@0
   209
slouken@1895
   210
int
slouken@1895
   211
main(int argc, char *argv[])
slouken@0
   212
{
slouken@1895
   213
    SDL_Surface *screen;
slouken@1895
   214
    /* Options */
slouken@1895
   215
    int speedy, flip, nofade;
slouken@1895
   216
    int delay;
slouken@1895
   217
    int w, h;
slouken@1895
   218
    int desired_bpp;
slouken@1895
   219
    Uint32 video_flags;
slouken@0
   220
#ifdef BENCHMARK_SDL
slouken@1895
   221
    Uint32 then, now;
slouken@0
   222
#endif
slouken@1895
   223
    /* Set default options and check command-line */
slouken@1895
   224
    speedy = 0;
slouken@1895
   225
    flip = 0;
slouken@1895
   226
    nofade = 0;
slouken@1895
   227
    delay = 1;
icculus@1152
   228
icculus@1152
   229
#ifdef _WIN32_WCE
slouken@1895
   230
    w = 240;
slouken@1895
   231
    h = 320;
slouken@1895
   232
    desired_bpp = 8;
slouken@1895
   233
    video_flags = SDL_FULLSCREEN;
icculus@1152
   234
#else
slouken@1895
   235
    w = 640;
slouken@1895
   236
    h = 480;
slouken@1895
   237
    desired_bpp = 0;
slouken@1895
   238
    video_flags = 0;
icculus@1152
   239
#endif
slouken@1895
   240
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   241
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   242
        return (1);
slouken@1895
   243
    }
icculus@1151
   244
slouken@1895
   245
    while (argc > 1) {
slouken@1895
   246
        if (strcmp(argv[1], "-speedy") == 0) {
slouken@1895
   247
            speedy = 1;
slouken@1895
   248
            argv += 1;
slouken@1895
   249
            argc -= 1;
slouken@1895
   250
        } else if (strcmp(argv[1], "-nofade") == 0) {
slouken@1895
   251
            nofade = 1;
slouken@1895
   252
            argv += 1;
slouken@1895
   253
            argc -= 1;
slouken@1895
   254
        } else if (strcmp(argv[1], "-delay") == 0) {
slouken@1895
   255
            if (argv[2]) {
slouken@1895
   256
                delay = atoi(argv[2]);
slouken@1895
   257
                argv += 2;
slouken@1895
   258
                argc -= 2;
slouken@1895
   259
            } else {
slouken@1895
   260
                fprintf(stderr, "The -delay option requires an argument\n");
slouken@1895
   261
                quit(1);
slouken@1895
   262
            }
slouken@1895
   263
        } else if (strcmp(argv[1], "-width") == 0) {
slouken@1895
   264
            if (argv[2] && ((w = atoi(argv[2])) > 0)) {
slouken@1895
   265
                argv += 2;
slouken@1895
   266
                argc -= 2;
slouken@1895
   267
            } else {
slouken@1895
   268
                fprintf(stderr, "The -width option requires an argument\n");
slouken@1895
   269
                quit(1);
slouken@1895
   270
            }
slouken@1895
   271
        } else if (strcmp(argv[1], "-height") == 0) {
slouken@1895
   272
            if (argv[2] && ((h = atoi(argv[2])) > 0)) {
slouken@1895
   273
                argv += 2;
slouken@1895
   274
                argc -= 2;
slouken@1895
   275
            } else {
slouken@1895
   276
                fprintf(stderr, "The -height option requires an argument\n");
slouken@1895
   277
                quit(1);
slouken@1895
   278
            }
slouken@1895
   279
        } else if (strcmp(argv[1], "-bpp") == 0) {
slouken@1895
   280
            if (argv[2]) {
slouken@1895
   281
                desired_bpp = atoi(argv[2]);
slouken@1895
   282
                argv += 2;
slouken@1895
   283
                argc -= 2;
slouken@1895
   284
            } else {
slouken@1895
   285
                fprintf(stderr, "The -bpp option requires an argument\n");
slouken@1895
   286
                quit(1);
slouken@1895
   287
            }
slouken@1895
   288
        } else if (strcmp(argv[1], "-warp") == 0) {
slouken@1895
   289
            video_flags |= SDL_HWPALETTE;
slouken@1895
   290
            argv += 1;
slouken@1895
   291
            argc -= 1;
slouken@1895
   292
        } else if (strcmp(argv[1], "-hw") == 0) {
slouken@1895
   293
            video_flags |= SDL_HWSURFACE;
slouken@1895
   294
            argv += 1;
slouken@1895
   295
            argc -= 1;
slouken@1895
   296
        } else if (strcmp(argv[1], "-flip") == 0) {
slouken@1895
   297
            video_flags |= SDL_DOUBLEBUF;
slouken@1895
   298
            argv += 1;
slouken@1895
   299
            argc -= 1;
slouken@1895
   300
        } else if (strcmp(argv[1], "-fullscreen") == 0) {
slouken@1895
   301
            video_flags |= SDL_FULLSCREEN;
slouken@1895
   302
            argv += 1;
slouken@1895
   303
            argc -= 1;
slouken@1895
   304
        } else
slouken@1895
   305
            break;
slouken@1895
   306
    }
slouken@0
   307
slouken@1895
   308
    /* Initialize the display */
slouken@1895
   309
    screen = SDL_SetVideoMode(w, h, desired_bpp, video_flags);
slouken@1895
   310
    if (screen == NULL) {
slouken@1895
   311
        fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
slouken@1895
   312
                w, h, desired_bpp, SDL_GetError());
slouken@1895
   313
        quit(1);
slouken@1895
   314
    }
slouken@1895
   315
    printf("Set%s %dx%dx%d mode\n",
slouken@1895
   316
           screen->flags & SDL_FULLSCREEN ? " fullscreen" : "",
slouken@1895
   317
           screen->w, screen->h, screen->format->BitsPerPixel);
slouken@1895
   318
    printf("(video surface located in %s memory)\n",
slouken@1895
   319
           (screen->flags & SDL_HWSURFACE) ? "video" : "system");
slouken@1895
   320
    if (screen->flags & SDL_DOUBLEBUF) {
slouken@1895
   321
        printf("Double-buffering enabled\n");
slouken@1895
   322
        flip = 1;
slouken@1895
   323
    }
slouken@0
   324
slouken@1895
   325
    /* Set the window manager title bar */
slouken@1895
   326
    SDL_WM_SetCaption("SDL test window", "testwin");
slouken@0
   327
slouken@1895
   328
    /* Do all the drawing work */
slouken@0
   329
#ifdef BENCHMARK_SDL
slouken@1895
   330
    then = SDL_GetTicks();
slouken@1895
   331
    DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@1895
   332
    now = SDL_GetTicks();
slouken@1895
   333
    printf("Time: %d milliseconds\n", now - then);
slouken@0
   334
#else
slouken@1895
   335
    DrawPict(screen, argv[1], speedy, flip, nofade);
slouken@0
   336
#endif
slouken@1895
   337
    SDL_Delay(delay * 1000);
slouken@1895
   338
    SDL_Quit();
slouken@1895
   339
    return (0);
slouken@0
   340
}