home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MSMOUSE1.ZIP / EGA.ZIP / MOVE.ZIP / SNOW.C < prev   
Encoding:
C/C++ Source or Header  |  1989-02-10  |  5.1 KB  |  201 lines

  1. /*------------------------------------------------------------------
  2.  *  SNOW.C
  3.  *
  4.  *  Demonstrates mouse functions and EGA animation
  5.  *  Reads and writes palette information using EGA Register Interface
  6.  *
  7.  *  C 5.1 Make File:
  8.  *
  9.  *      snow.exe: snow.c
  10.  *        cl snow.c
  11.  *
  12.  *   QuickC Program List:
  13.  *
  14.  *      SNOW.C
  15.  *
  16.  *--------------------------------------------------------------------*/
  17.  
  18. #include <bios.h>
  19. #include <graph.h>
  20. #include <dos.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23.  
  24. #define MOUSE 0x33
  25. #define VIDEO 0x10
  26.  
  27. void delay( int );
  28. void DrawTree( int, int );
  29.  
  30. #define MAXFLAKES 150
  31. #define MAXTREES 30
  32.  
  33. char far paltable[16];
  34.  
  35. main ()
  36. {
  37.     union REGS inregs, outregs;
  38.     struct SREGS segregs;
  39.     unsigned int i, color, x, y, ystep;
  40.     int bound_rand (int);
  41.  
  42.     _setvideomode( _ERESCOLOR );            /* EGA 640x350 16 color */
  43.  
  44.     inregs.x.ax = 0;                        /* Reset mouse */
  45.     int86 (MOUSE, &inregs, &outregs);
  46.  
  47.     for (i = 0; i < MAXTREES; i++ )         /* Draw the forest */
  48.         {
  49.         x = bound_rand(639);
  50.         y = 299 * i / MAXTREES + 50;
  51.         DrawTree( x, y );
  52.         }
  53.  
  54.     for (i = 0; i < MAXFLAKES; i++)         /* Create snowflake trails */
  55.         {
  56.         x = bound_rand (639);
  57.         y = 0;
  58.         ystep = bound_rand(3) + 1;
  59.         color = bound_rand(10) + 5;
  60.         do
  61.             {
  62.             inregs.x.cx = x;
  63.             inregs.x.dx = y;
  64.             inregs.h.al = color++;
  65.             inregs.h.bh = 0;
  66.             inregs.h.ah = 0x0c;             /* Write graphics pixel */
  67.             int86 (VIDEO, &inregs, &outregs);
  68.  
  69.             color = color & 0x0f;           /* Rotate color 5 to 15 */
  70.             if (!color)
  71.                 color = 5;
  72.             x += bound_rand(6) - 3;
  73.             y += ystep;
  74.             } while ((y < 350) && (x >= 0) && (x < 640));
  75.         }
  76.  
  77.     paltable[0] = 0;                        /* Colors 0 to 4 stay constant */
  78.     paltable[1] = 1;
  79.     paltable[2] = 2;
  80.     paltable[3] = 0x3c;
  81.     paltable[4] = 0x3f;
  82.     for (i = 5; i < 16; i++)                /* Set colors 5 to 15 to 0 */
  83.         paltable[i] = 0;
  84.     color = 5;
  85.     do
  86.         {
  87.         paltable[color] = 0x3f;             /* one color entry to white */
  88.         inregs.h.ah = 0xf3;
  89.         inregs.x.cx = 0x0010;
  90.         inregs.x.dx = 0x18;
  91.         segregs.es = (unsigned int)(((unsigned long)paltable >> 16) & 0xffff);
  92.         inregs.x.bx = (unsigned int)(((unsigned long)paltable) & 0xffff);
  93.  
  94.     /* Make sure not in vertical retrace */
  95.         while (inp(0x3da) & 8)
  96.             ;
  97.  
  98.         /* Wait for beginning of vertical retrace */
  99.         while (!(inp(0x3da) & 8))
  100.             ;
  101.  
  102.         /* Make the palette register changes */
  103.         int86x (VIDEO, &inregs, &outregs, &segregs);
  104.  
  105.         /* Assure EGA's ACR is pointing to AAR */
  106.         inp (0x3da);
  107.  
  108.         /* Activate new palette */
  109.         outp (0x3c0, 0x20);
  110.  
  111.         paltable[color++] = 0;              /* blacken this snowflake */
  112.         color = color & 0x0f;
  113.         if (!color)                         /* loop thru colors 5 to 15 */
  114.             color = 5;
  115.  
  116.         delay( 1 );                         /* Delay for 1/18.2 seconds */
  117.  
  118.         do
  119.             {
  120.             inregs.x.ax = 3;                /* Get mouse status */
  121.             int86 (MOUSE, &inregs, &outregs);
  122.  
  123.             /* Freeze while left button pressed */
  124.             } while ((outregs.x.bx & 1));
  125.  
  126.         inregs.x.ax = 3;                    /* Get mouse status */
  127.         int86 (MOUSE, &inregs, &outregs);
  128.  
  129.         /* loop until left mouse button is pressed */
  130.         } while ((outregs.x.bx & 3) != 2 );
  131.  
  132.     inregs.x.ax = 2;                        /* Hide Mouse cursor */
  133.     int86 (MOUSE, &inregs, &outregs);
  134.  
  135.     _clearscreen( _GCLEARSCREEN );
  136.     _setvideomode( _DEFAULTMODE );
  137. }
  138.  
  139. int bound_rand (bound)
  140. int bound;
  141. {
  142.     int i;
  143.  
  144.     do
  145.         {
  146.         i = (int)((long)rand() * (bound+1) / 32767);
  147.         } while (i == (bound+1));
  148.     return (i);
  149. }
  150.  
  151. #define BOX 16
  152.  
  153. void DrawTree( x, y )
  154. int x, y;
  155. {
  156.     int i;
  157.  
  158.     /* fill in the trunk */
  159.     _setcolor( 1 );
  160.     _rectangle( _GFILLINTERIOR, x, y, x + BOX, y + BOX );
  161.  
  162.     /* outline the trunk */
  163.     _setcolor( 3 );
  164.     _rectangle( _GBORDER, x, y, x + BOX, y + BOX );
  165.  
  166.     /* put green needles on the tree */
  167.     _setcolor( 2 );
  168.     for ( i = x - BOX; i < x + 2 * BOX; i++ )
  169.         {
  170.         _moveto( i, y);
  171.         _lineto( x + BOX / 2, y - 5 * BOX );
  172.         }
  173.  
  174.     /* outline the branches */
  175.     _setcolor( 4 );
  176.     _moveto( x-BOX, y );
  177.     _lineto( x+2*BOX, y );
  178.     _lineto( x+BOX/2, y-5*BOX );
  179.     _lineto( x-BOX, y );
  180. }
  181.  
  182.  
  183. /* Delay for a number of clock ticks */
  184. void delay( ticks )
  185. int ticks;
  186. {
  187.     long end_tick, now_tick;
  188.     unsigned midnight_flag;
  189.  
  190.     _bios_timeofday( _TIME_GETCLOCK, &now_tick );
  191.     end_tick = now_tick + (long) ticks;
  192.  
  193.     do
  194.         {
  195.         midnight_flag = _bios_timeofday( _TIME_GETCLOCK, &now_tick );
  196.         if (midnight_flag)
  197.             end_tick -= 1573040;
  198.         }
  199.     while ( now_tick < end_tick );
  200. }
  201.