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