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