home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Programming / SDL-Amiga / demo / testwin.c < prev   
C/C++ Source or Header  |  2000-08-22  |  9KB  |  363 lines

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