home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap15 / race.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-07  |  4.4 KB  |  161 lines

  1. /* race.c -- race of the patterned circles             */
  2. /*    Illustrates animation with _getimage() and       */
  3. /*    _putimage(), random number use with srand() and  */
  4. /*    rand(), and system clock use with time() and     */
  5. /*    ftime().
  6. /* Program list: race.c (for srand(), rand(), and      */
  7. /*                       ftime())                      */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <graph.h>
  13. #include <time.h>
  14. #include <sys\types.h>
  15. #include <sys\timeb.h>
  16.  
  17. #define END 25
  18. #define FIGNUM 3
  19. typedef char far *PTFRCHAR;
  20. PTFRCHAR Bufs[FIGNUM];
  21. unsigned char Masks[FIGNUM][8] = {
  22.             {0xFF,0x00,0xFF,0x00,0xFF,0x00,0xFF,0x00},
  23.             {0xF0,0xF0,0xF0,0xF0,0x0F,0x0F,0x0F,0x0F},
  24.             {0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA}};
  25. short Xul[FIGNUM], Yul[FIGNUM];  /* figure locations */
  26. short Xsize, Ysize;              /* figure size      */
  27. struct videoconfig Vc;
  28. void Initialize(void);
  29. void Draw_n_store(void);
  30. void Move_figs(void);
  31. void Wait(double);
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     int mode = _MRES4COLOR;
  38.  
  39.     if (argc > 1)
  40.         mode = atoi(argv[1]);
  41.     if (_setvideomode(mode) == 0)
  42.     {
  43.         fprintf(stderr,"mode %d not supported\n",mode);
  44.         exit(1);
  45.     }
  46.     Initialize();
  47.     Draw_n_store();
  48.     _settextcolor(2);
  49.     _settextposition(1, 1);
  50.     _outtext("Place your bets and type a key");
  51.     _settextposition(25, 1);
  52.     _outtext("Type a key again when done");
  53.     getch();
  54.     Move_figs();
  55.     getch();
  56.     _setvideomode(_DEFAULTMODE);
  57. }
  58.  
  59. void Initialize()
  60. {
  61.     int i;
  62.     float ar;  /* aspect ratio */
  63.  
  64.     _getvideoconfig(&Vc);
  65.     ar = (float)(10 * Vc.numypixels) / (6.5 * Vc.numxpixels);
  66.       /* set size, initial positions */
  67.     Xsize = Vc.numxpixels / 30;
  68.     Ysize = ar * Xsize;
  69.     for(i = 0; i < FIGNUM; i++)
  70.     {
  71.         Xul[i] = 0;
  72.         Yul[i] = (i + 1) * Vc.numypixels /
  73.                       (FIGNUM + 1);
  74.     }
  75.     _selectpalette(0);
  76.     _setcolor(1);
  77.         /*  draw finish line */
  78.     _moveto(END * Xsize, 0);
  79.     _lineto(END * Xsize, Vc.numypixels - 1);
  80. }
  81.  
  82. void Draw_n_store() /* draw images, save them */
  83. {
  84.     int i;
  85.  
  86.     for (i = 0; i < FIGNUM; i++)
  87.         {
  88.         _setcolor(i + 1);
  89.         _setfillmask(Masks[i]);
  90.         _ellipse(_GFILLINTERIOR ,Xul[i], Yul[i],
  91.                  Xul[i] + Xsize, Yul[i] + Ysize);
  92.         _ellipse(_GBORDER ,Xul[i], Yul[i],
  93.                  Xul[i] + Xsize, Yul[i] + Ysize);
  94.         Bufs[i] = (PTFRCHAR) malloc((unsigned int)
  95.                    _imagesize(0,Yul[i], Xul[i] +
  96.                    Xsize, Yul[i] + Ysize));
  97.         _getimage(Xul[i],Yul[i], Xul[i] + Xsize, Yul[i] +
  98.                   Ysize, Bufs[i]);
  99.  
  100.         }
  101. }
  102. void Move_figs()
  103. {
  104.     int i, j;
  105.     static int dx[FIGNUM] = {0, 0, 0}; /* displacements */
  106.     time_t tval;
  107.  
  108.     time(&tval);    /*   use the current time value   */
  109.     srand(tval);    /*   to initialize rand()         */
  110.     while (dx[0] < END && dx[1] < END && dx[2] < END)
  111.         {
  112.         for (i = 0; i < FIGNUM; i++)
  113.             {
  114.             /* advance the figure one position if  */
  115.             /* rand() returns an even number       */
  116.             if (rand() % 2 == 0)
  117.                 {
  118.                 /* erase old image */
  119.                 _putimage(dx[i] * Xsize, Yul[i],
  120.                           Bufs[i], _GXOR);
  121.                 /* redraw in new position */
  122.                 _putimage((1 + dx[i]) * Xsize, Yul[i],
  123.                           Bufs[i], _GPSET);
  124.                 dx[i]++;
  125.                 }
  126.             }
  127.         Wait(0.15);
  128.         }
  129.     for (j = 0; j < 5; j++)
  130.         {
  131.         for(i = 0; i < FIGNUM; i++)
  132.             {
  133.             /* flash winning figure */
  134.             if (dx[i] >= END)
  135.                 {
  136.                 Wait(0.2);
  137.                 _putimage(dx[i] * Xsize,Yul[i],
  138.                           Bufs[i], _GPRESET);
  139.                 Wait(0.2);
  140.                 _putimage(dx[i] * Xsize,Yul[i],
  141.                           Bufs[i], _GPSET);
  142.                 }
  143.             }
  144.         }
  145. }
  146.  
  147. void Wait(pause) /* wait for pause seconds */
  148. double pause;
  149. {
  150.     struct timeb start, end;
  151.     long delay;
  152.  
  153.     delay = 1000 * pause;  /* convert to milliseconds */
  154.     ftime(&start);
  155.     ftime(&end);
  156.     while ((1000 * (end.time - start.time) +
  157.             + end.millitm - start.millitm) < delay)
  158.         ftime(&end);
  159. }
  160.  
  161.