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