home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------
- * SNOW.C
- *
- * Demonstrates mouse functions and EGA animation
- * Reads and writes palette information using EGA Register Interface
- *
- * C 5.1 Make File:
- *
- * snow.exe: snow.c
- * cl snow.c
- *
- * QuickC Program List:
- *
- * SNOW.C
- *
- *--------------------------------------------------------------------*/
-
- #include <bios.h>
- #include <graph.h>
- #include <dos.h>
- #include <stdio.h>
- #include <stdlib.h>
-
- #define MOUSE 0x33
- #define VIDEO 0x10
-
- void delay( int );
- void DrawTree( int, int );
-
- #define MAXFLAKES 150
- #define MAXTREES 30
-
- char far paltable[16];
-
- main ()
- {
- union REGS inregs, outregs;
- struct SREGS segregs;
- unsigned int i, color, x, y, ystep;
- int bound_rand (int);
-
- _setvideomode( _ERESCOLOR ); /* EGA 640x350 16 color */
-
- inregs.x.ax = 0; /* Reset mouse */
- int86 (MOUSE, &inregs, &outregs);
-
- for (i = 0; i < MAXTREES; i++ ) /* Draw the forest */
- {
- x = bound_rand(639);
- y = 299 * i / MAXTREES + 50;
- DrawTree( x, y );
- }
-
- for (i = 0; i < MAXFLAKES; i++) /* Create snowflake trails */
- {
- x = bound_rand (639);
- y = 0;
- ystep = bound_rand(3) + 1;
- color = bound_rand(10) + 5;
- do
- {
- inregs.x.cx = x;
- inregs.x.dx = y;
- inregs.h.al = color++;
- inregs.h.bh = 0;
- inregs.h.ah = 0x0c; /* Write graphics pixel */
- int86 (VIDEO, &inregs, &outregs);
-
- color = color & 0x0f; /* Rotate color 5 to 15 */
- if (!color)
- color = 5;
- x += bound_rand(6) - 3;
- y += ystep;
- } while ((y < 350) && (x >= 0) && (x < 640));
- }
-
- paltable[0] = 0; /* Colors 0 to 4 stay constant */
- paltable[1] = 1;
- paltable[2] = 2;
- paltable[3] = 0x3c;
- paltable[4] = 0x3f;
- for (i = 5; i < 16; i++) /* Set colors 5 to 15 to 0 */
- paltable[i] = 0;
- color = 5;
- do
- {
- paltable[color] = 0x3f; /* one color entry to white */
- inregs.h.ah = 0xf3;
- inregs.x.cx = 0x0010;
- inregs.x.dx = 0x18;
- segregs.es = (unsigned int)(((unsigned long)paltable >> 16) & 0xffff);
- inregs.x.bx = (unsigned int)(((unsigned long)paltable) & 0xffff);
-
- /* Make sure not in vertical retrace */
- while (inp(0x3da) & 8)
- ;
-
- /* Wait for beginning of vertical retrace */
- while (!(inp(0x3da) & 8))
- ;
-
- /* Make the palette register changes */
- int86x (VIDEO, &inregs, &outregs, &segregs);
-
- /* Assure EGA's ACR is pointing to AAR */
- inp (0x3da);
-
- /* Activate new palette */
- outp (0x3c0, 0x20);
-
- paltable[color++] = 0; /* blacken this snowflake */
- color = color & 0x0f;
- if (!color) /* loop thru colors 5 to 15 */
- color = 5;
-
- delay( 1 ); /* Delay for 1/18.2 seconds */
-
- do
- {
- inregs.x.ax = 3; /* Get mouse status */
- int86 (MOUSE, &inregs, &outregs);
-
- /* Freeze while left button pressed */
- } while ((outregs.x.bx & 1));
-
- inregs.x.ax = 3; /* Get mouse status */
- int86 (MOUSE, &inregs, &outregs);
-
- /* loop until left mouse button is pressed */
- } while ((outregs.x.bx & 3) != 2 );
-
- inregs.x.ax = 2; /* Hide Mouse cursor */
- int86 (MOUSE, &inregs, &outregs);
-
- _clearscreen( _GCLEARSCREEN );
- _setvideomode( _DEFAULTMODE );
- }
-
- int bound_rand (bound)
- int bound;
- {
- int i;
-
- do
- {
- i = (int)((long)rand() * (bound+1) / 32767);
- } while (i == (bound+1));
- return (i);
- }
-
- #define BOX 16
-
- void DrawTree( x, y )
- int x, y;
- {
- int i;
-
- /* fill in the trunk */
- _setcolor( 1 );
- _rectangle( _GFILLINTERIOR, x, y, x + BOX, y + BOX );
-
- /* outline the trunk */
- _setcolor( 3 );
- _rectangle( _GBORDER, x, y, x + BOX, y + BOX );
-
- /* put green needles on the tree */
- _setcolor( 2 );
- for ( i = x - BOX; i < x + 2 * BOX; i++ )
- {
- _moveto( i, y);
- _lineto( x + BOX / 2, y - 5 * BOX );
- }
-
- /* outline the branches */
- _setcolor( 4 );
- _moveto( x-BOX, y );
- _lineto( x+2*BOX, y );
- _lineto( x+BOX/2, y-5*BOX );
- _lineto( x-BOX, y );
- }
-
-
- /* Delay for a number of clock ticks */
- void delay( ticks )
- int ticks;
- {
- long end_tick, now_tick;
- unsigned midnight_flag;
-
- _bios_timeofday( _TIME_GETCLOCK, &now_tick );
- end_tick = now_tick + (long) ticks;
-
- do
- {
- midnight_flag = _bios_timeofday( _TIME_GETCLOCK, &now_tick );
- if (midnight_flag)
- end_tick -= 1573040;
- }
- while ( now_tick < end_tick );
- }
-