/*-------------------------------------------------------------------
   DROP.C         *** WATER DROP ANIMATION SEQUENCE ***

   Programmed By Amaury M. Valdes    July 30, 1990
   (C) Copyright 1990 Omega Software, Inc.
   All Rights Reserved

   This program will demostrate how to incorporate sprites created with
   "SPRED" and compiled with "DEFINE" both of which are included with
   the package.

   If you have any comments or suggestions please feel free to drop 
   me a line.  GOOD LUCK & ENJOY !!!

   COMPUSERVE: 73447,2273  or   MAIL:             Amaury Valdes
						  11 Myrtle Avenue
						  Passaic, NJ 07055
--------------------------------------------------------------------*/

#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <alloc.h>

#define TRUE 1;
#define FALSE 0;

int DROP1[]={5,6,0,1,9,1,0,1,9,9,1,1,1,9,1,1,1,1,1,1,1,1,1,1,
             1,1,1,0,1,1,1,0};

int DROP2[]={7,6,0,0,1,9,1,0,0,0,1,9,9,1,1,0,1,1,9,1,1,1,1,1,
             1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0};

int DROP3[]={7,4,0,1,9,1,1,1,0,1,9,9,1,1,1,1,1,1,1,1,1,1,1,0,
             0,1,1,1,0,0};

int DROP4[]={12,7,0,0,0,0,0,0,0,0,9,0,0,0,0,1,9,1,0,0,0,1,1,0,
             0,0,0,0,0,1,0,0,0,1,0,0,9,1,1,9,0,0,0,0,0,0,
             0,9,1,1,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,9,
             9,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0};

int DROP5[]={22,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,0,0,0,1,0,
             0,9,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,9,0,0,0,
             0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,0,0,
             0,0,0,9,9,1,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
             0,0,9,1,1,1,1,1,0,0,0,0,0,0,1,9,0,0,0,0,0,9,
             0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,9,0,0,0,0,0,
             0,0,0,0,0,0,0,0,0,9,9,9,0,0,0,0,1,0,0,0,0,0
             };

int DROP6[]={22,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9,1,0,0,0,0,0,
             9,0,1,9,1,0,0,0,9,9,9,0,0,0,0,1,9,1,0,1,9,1
             };

int COPYRGHT[]={8,9,0,0,14,14,14,14,0,0,0,14,0,0,0,0,14,0,14,0,0,14,14,0,
                0,14,14,0,14,0,0,14,0,14,14,0,14,0,0,0,0,14,14,0,14,0,
                0,14,0,14,14,0,0,14,14,0,0,14,0,14,0,0,0,0,14,0,0,0,
                14,14,14,14,0,0};

int FAUCET1[]={23,17,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,7,7,0,
               0,0,0,0,0,0,8,7,7,7,7,8,7,0,0,0,8,7,7,7,7,7,
               7,7,0,0,0,0,0,7,8,0,8,0,8,8,0,0,0,8,7,8,8,7,
               8,8,7,0,0,0,0,0,0,0,0,8,0,7,7,7,7,7,8,8,8,7,
               7,8,8,0,0,0,0,0,0,7,7,7,7,7,8,8,8,8,8,8,8,8,
               8,8,8,0,0,0,8,8,7,8,7,8,8,8,8,8,8,8,7,8,8,8,
               8,8,0,0,0,0,8,7,8,8,8,8,8,8,8,8,8,8,0,7,8,8,
               8,8,8,0,0,0,0,8,7,8,8,8,8,0,0,0,0,0,0,0,7,8,
               7,8,8,8,0,0,0,0,8,7,8,8,8,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

int FAUCET2[]={23,11,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,7,7,0,
               0,0,0,0,0,0,8,7,7,7,7,8,7,0,0,0,8,7,7,7,7,7,
               7,7,0,0,0,0,0,7,8,0,8,0,8,8,0,0,0,8,7,8,8,7,
               8,8,7,0,0,0,0,0,0,0,0,8,0,7,7,7,7,7,8,8,8,7,
               7,8,8,0,0,0,0,0,0,7,7,7,7,7,8,8,8,8,8,8,8,8,
               8,8,8,0,0,0,8,8,7,8,7,8,8,8,8,8,8,8,7,8,8,8,
               8,8,0,0,0,0,8,7,8,8,8,8,8,8,8,8,8,8,0,7,8,8,
               8,8,8,0,0,0,0,8,7,8,8,8,8,0,0,0,0,0,0,0,7,8,
               7,8,8,8,0,0,0,0,8,7,8,8,8,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0};

int FAUCET3[]={23,13,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,7,7,0,
	       0,0,0,0,0,0,8,7,7,7,7,8,7,0,0,0,8,7,7,7,7,7,
	       7,7,0,0,0,0,0,7,8,0,8,0,8,8,0,0,0,8,7,8,8,7,
	       8,8,7,0,0,0,0,0,0,0,0,8,0,7,7,7,7,7,8,8,8,7,
	       7,8,8,0,0,0,0,0,0,7,7,7,7,7,8,8,8,8,8,8,8,8,
	       8,8,8,0,0,0,8,8,7,8,7,8,8,8,8,8,8,8,7,8,8,8,
	       8,8,0,0,0,0,8,7,8,8,8,8,8,8,8,8,8,8,0,7,8,8,
	       8,8,8,0,0,0,0,8,7,8,8,8,8,0,0,0,0,0,0,0,7,8,
	       7,8,8,8,0,0,0,0,8,7,8,8,8,0,0,0,0,0,0,0,0,0,
	       0,0,0,0,0,0,0,0,0,1,9,1,1,0,0,0,0,0,0,0,0,0,
	       0,0,0,0,0,0,0,0,0,0,9,9,1,1,0,0,0,0,0,0,0,0,
	       0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,
	       0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,
	       0,0,0,0,0,0,0,0,0,0,0,0,0};

int FAUCET4[]={23,15,0,0,0,0,0,0,0,7,7,0,0,0,0,0,0,0,0,7,7,7,7,0,
	       0,0,0,0,0,0,8,7,7,7,7,8,7,0,0,0,8,7,7,7,7,7,
               7,7,0,0,0,0,0,7,8,0,8,0,8,8,0,0,0,8,7,8,8,7,
               8,8,7,0,0,0,0,0,0,0,0,8,0,7,7,7,7,7,8,8,8,7,
               7,8,8,0,0,0,0,0,0,7,7,7,7,7,8,8,8,8,8,8,8,8,
               8,8,8,0,0,0,8,8,7,8,7,8,8,8,8,8,8,8,7,8,8,8,
               8,8,0,0,0,0,8,7,8,8,8,8,8,8,8,8,8,8,0,7,8,8,
               8,8,8,0,0,0,0,8,7,8,8,8,8,0,0,0,0,0,0,0,7,8,
               7,8,8,8,0,0,0,0,8,7,8,8,8,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,1,9,1,1,0,0,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,9,9,1,1,1,0,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,1,9,9,1,1,0,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,1,9,9,1,1,0,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
               0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};


void *image;

int x,y,len,width,ch,cc;
long size;

int main(void)
{
  init_graphics_system();
  put(230,30,"WATER DROP ANIMATION",2);
  rectangle(260,50,350,200);
  pos_sprite(COPYRGHT,10,339);
  put(25,340,"OMEGA SOFTWARE, INC.                        Programmed By Amaury M. Valdes",14);
  while (!kbhit())
  {
    pos_sprite(FAUCET1,302,72);delay(100);
    pos_sprite(FAUCET2,302,72);delay(100);
    pos_sprite(FAUCET3,302,72);delay(100);
    pos_sprite(FAUCET4,302,72);delay(100); 
    pos_sprite(FAUCET1,302,72);
    animate_drop();delay(300);
  }
  closegraph();
  _setcursortype(_NORMALCURSOR);
  return 0;
}

int init_graphics_system(void)
{
  int gdriver,gmode,errorcode;

  /*-------------------------------------------------------
	 INITIALIZE SYSTEM FOR EGA GRAPHICS ONLY !!!
  --------------------------------------------------------*/
  gdriver=EGA;
  gmode=EGAHI;
  initgraph(&gdriver,&gmode,"");
  errorcode=graphresult();
  if (errorcode!=0) 
  {
    clrscr();color(4,0);
    printf("\nERROR: PROGRAM WILL ONLY WORK ON COMPUTERS \n");
    printf("       WITH EGA GRAPHICS CAPABILITIES !!! \n");
    color(7,0);
    exit(1);
  }
  return 0;
}

int animate_drop(void)
{
  pos_sprite(DROP1,301,86);           /*   POSITION DROP @ X,Y       	*/
  size=imagesize(300,85,308,92);      /*   STORE IMAGE IN BUFFER 	*/
  image=malloc(size);  		      /*   USE HEAP SPACE        	*/	
  if (size<coreleft()) 		      /*   CHECK IF ENOUGH MEMORY       */
  {
    getimage(300,85,308,92,image);
    for (y=0;y<=85;y++) 
    {
      putimage(300,85+y,image,XOR_PUT);    /*  ERASE OLD IMAGE  */    
      putimage(300,85+y,image,COPY_PUT);   /*  PUT NEW ONE      */
      delay(10);
    }
  }
  putimage(300,170,image,XOR_PUT);         /*  ERASE LAST IMAGE  */ 
  free(image);                             /*  FREE BUFFER SPACE */
  beep(800,10);beep(700,10);  
  pos_sprite(DROP2,300,170);delay(50);     /* PLACE DROP2 ON SCREEN */
  xor_pos_sprite(DROP2,300,170);           /* WAIT, AND ERASE       */
  pos_sprite(DROP3,300,170);delay(50);     /*  "  "   "   "   "	    */
  xor_pos_sprite(DROP3,300,170);           /* THE FOLLOWING SIX     */
  pos_sprite(DROP4,295,165);delay(50);     /* STEPS ARE ALL THE     */
  xor_pos_sprite(DROP4,295,165);           /* USING DIFF SPRITES    */
  pos_sprite(DROP5,295,165);delay(50);     /* FOR THE ANIMATION     */
  xor_pos_sprite(DROP5,295,165);           /* SEQUENCE.		    */	
  pos_sprite(DROP6,295,173);
  return 0;
}

int pos_sprite(int cur_data[],int startx,int starty)
{
  int a,p,xx,yy,x,y;

  p=1;
  len=cur_data[0];
  width=cur_data[1];
  for (y=2;y<width+2;y++)
   for (x=2;x<len+2;x++)
   {
     a=((y-2)*40)+(x-2);
     p++;
     xx=a % 40;
     yy=a / 40;
     putpixel(startx+xx,starty+yy,cur_data[p]);
   } 
  return 0;
}

int enlarge_pos_sprite(int cur_data[],int startx,int starty,int sizex,int sizey)
{
  int a,p,xx,yy,x,y,cx,cy;

  p=1;
  len=cur_data[0];
  width=cur_data[1];
  for (y=2;y<width+2;y++)
   for (x=2;x<len+2;x++)
   {
     a=((y-2)*40)+(x-2);
     p++;
     xx=(a % 40)*sizex;
     yy=(a / 40)*sizey;
     for (cx=0;cx<=sizex;cx++) for (cy=0;cy<=sizey;cy++)
       putpixel(startx+xx+cx,starty+yy+cy,cur_data[p]);
   } 
  return 0;
}

int xor_pos_sprite(int cur_data[],int startx,int starty)
{
  int a,p,xx,yy,x,y;

  p=1;
  len=cur_data[0];
  width=cur_data[1];
  for (y=2;y<width+2;y++)
   for (x=2;x<len+2;x++)
   {
     a=((y-2)*40)+(x-2);
     p++;
     xx=a % 40;
     yy=a / 40;
     if (cur_data[p]!=0) putpixel(startx+xx,starty+yy,0);
   } 
  return 0;
}

int put(int x, int y, char *str1, int col)
{
   setcolor(col);
   outtextxy(x,y,str1);
   return 0;
}

int pausekb(void)
{
  int ch;

   ch=getch();
   if (!ch) ch=getch();
   return 0;
}

int color(int fore,int back)
{
  textcolor(fore);
  textbackground(back);
  return 0;
}

int beep(int freq,int duration)
{
  sound(freq);
  delay(duration);
  nosound();
  return 0;
}