home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d6xx / d660 / lovemice.lha / LoveMice / LoveMice.c < prev    next >
C/C++ Source or Header  |  1992-05-15  |  6KB  |  237 lines

  1. /* LoveMice (Mice in Love) 
  2.    by Guido Wegener of CLUSTER
  3.    This program is freeware, please read LoveMice.doc for copy-conditions.
  4. */  
  5. #include "math.h"
  6. #include "libraries/dosextens.h"
  7. #include "functions.h"
  8. #include "exec/types.h"
  9. #include "intuition/intuition.h"
  10. #include "graphics/rastport.h"
  11. #include "graphics/gfxbase.h"
  12.  
  13. #define PI 3.14159
  14. #define XADD 100  /* Some constants for transforming */
  15. #define YADD 100  /* float coordinates (-100 -» 100) */
  16. #define XF 3      /* into normal coordinates         */
  17. #define YF 2
  18. #define ANZ 15    /* max number of mice */
  19. float q[ANZ][2],p[ANZ][2]; /* positions of mice (actual and last) */
  20. int n[ANZ];     /* start positions on circle */
  21. int farbs[ANZ]; /* color registers for each mouse */
  22. float teil;     /* part of distance walked in one step */
  23.  
  24. struct IntuitionBase *IntuitionBase;
  25. struct GfxBase *GfxBase; 
  26. struct Screen *MyScreen;
  27. struct DOSBase *DOSBase;
  28.  
  29. struct FileHandle *fh;  
  30.  
  31. struct NewScreen MyNewScreen={0,0,640,500,4,
  32.                               15,14,
  33.                               HIRES|LACE,
  34.                               CUSTOMSCREEN,
  35.                               NULL,
  36.                               (UBYTE *)"Mice in Love by Guido Wegener of CLUSTER",
  37.                               NULL,NULL};
  38. long int col[]={ 0,0,0,     /* color definitions */
  39.              15,15,15,
  40.              15,0,0,
  41.              0,15,0,
  42.              0,0,15,
  43.              15,0,15,
  44.              15,15,0,
  45.              0,15,15,
  46.              
  47.              4,0,15,
  48.              8,4,15,
  49.              12,8,15,
  50.              6,15,15,
  51.              15,6,15,
  52.              10,8,11,
  53.              8,8,12,
  54.              15,15,15
  55.  };
  56. struct RastPort *rp;
  57. int anz; /* real number of mice */
  58. struct Window *MyWindow;
  59. #define FILEN 38 /* display position of filename */
  60. #define FILEL 30 /* length of filename */
  61. struct NewWindow win=
  62.  {
  63.   0,10,640,480,
  64.   15,13,
  65.   CLOSEWINDOW,
  66.   WINDOWCLOSE | SMART_REFRESH | BACKDROP | BORDERLESS | ACTIVATE,
  67.   NULL,
  68.   NULL,
  69.   (UBYTE *)"<=- klick here to exit   PATTERNNAME:                               ",
  70.   NULL,
  71.   NULL,
  72.   50,50,-1,-1,
  73.   CUSTOMSCREEN
  74.  };
  75.  
  76. info() /* is displayed when called without arguments */
  77.  {
  78.   puts("Mice in Love by Guido Wegener of CLUSTER");
  79.   puts("finished on 1.Feb.1990");
  80.   puts("You should have called this programm with an argument !");
  81.   puts("Example : lovemice mice/fighter");
  82.   puts("            (show pattern generated by the file fighter)");
  83.   puts("You really should read LoveMice.Doc !!!");
  84.  }
  85.  
  86. main(argc,argv)
  87. int argc;
  88. char *argv[];
  89.  {
  90.   long int i,t;
  91.   void *mess;   /* message port */
  92.   int closed=0; /* close gadget hit ? */
  93.   
  94.   if(argc!=2) /* arguments */
  95.    {
  96.     info();
  97.     exit(0);
  98.    }
  99.    
  100.   /* open, check and exit (in a stupid way, but first version only 
  101.      opened three things, but as time goes by ... */
  102.   IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",33L);
  103.   if(!IntuitionBase) {puts("Can't open Intuition.library !");exit(20);}
  104.  
  105.   GfxBase=(struct GfxBase *) OpenLibrary("graphics.library",33L);
  106.   if(!IntuitionBase) {puts("Can't open Graphics.library !");CloseLibrary(IntuitionBase);exit(20);}
  107.     
  108.   DOSBase=(struct DOSBase *)OpenLibrary("dos.library",33L);
  109.   if(!DOSBase){puts("Can't open Dos.library !");CloseLibrary(GfxBase);CloseLibrary(IntuitionBase);exit(20);}
  110.  
  111.   MyScreen=OpenScreen(&MyNewScreen);
  112.   if(!MyScreen){puts("Can't open screen !");CloseLibrary(DOSBase);CloseLibrary(GfxBase);CloseLibrary(IntuitionBase);exit(20);}
  113.  
  114.   win.Screen=MyScreen;  /* link to screen */
  115.   for(i=0;i<FILEL;i++)win.Title[i+FILEN]=argv[1][i]; /* copy filename to title */
  116.   MyWindow=(struct Window *)OpenWindow(&win);
  117.   if(!MyWindow){puts("Can't open Window !");CloseScreen(MyScreen);CloseLibrary(DOSBase);CloseLibrary(GfxBase);CloseLibrary(IntuitionBase);exit(20);}
  118.   
  119.   rp=(MyWindow->RPort);
  120.   init(argv[1]);    /* get and set all parameters set in the pattern file */
  121.   for(i=0;i<16;i++) /* use colors */
  122.    {  
  123.     SetRGB4(&(MyScreen->ViewPort),i,col[i*3],col[i*3+1],col[i*3+2]);
  124.    }
  125.  
  126.   starts(); /* compute real start positions and colors */   
  127.   while(!closed)
  128.    {
  129.     mess=GetMsg(MyWindow->UserPort);
  130.     if(mess)
  131.      {
  132.       closed=1;
  133.       ReplyMsg(mess);
  134.      } 
  135.     views();  /* draw lines */
  136.     move();   /* move mice  */
  137.    }
  138.    
  139.   /* close all */
  140.   CloseWindow(MyWindow);
  141.   CloseScreen(MyScreen);      
  142.     
  143.   CloseLibrary(DOSBase);
  144.   CloseLibrary(GfxBase);
  145.     CloseLibrary(IntuitionBase);
  146.  }
  147.  
  148. init(filen)
  149. char *filen;
  150.  {
  151.   int j,i,x,t;
  152.   
  153.   fh=Open(filen,MODE_OLDFILE);
  154.   if(fh==0)  /* trouble ?  let's go ! */
  155.    {
  156.     CloseWindow(MyWindow);
  157.     CloseScreen(MyScreen);
  158.     CloseLibrary(DOSBase);
  159.     CloseLibrary(GfxBase);
  160.     CloseLibrary(IntuitionBase);
  161.     puts("Can't find pattern data-file !");
  162.     exit(20);
  163.    }
  164.   teil=1/(float)readnum(); /* get'n'compute part of distance */
  165.   anz=readnum();  /* nmumber of mice */
  166.   for(i=0;i<anz;i++)       /* get start positions */
  167.     n[i]=readnum();
  168.   for(i=3;i<(anz+1)*3;i++) /* ... and colors */
  169.    {
  170.     col[i]=readnum();  
  171.    }
  172.   Close(fh);
  173.  }
  174.   
  175. readnum()  /* get next number */
  176.  {
  177.   int i=0;
  178.   char buff[80];
  179.   
  180.   do       /* don't use anything but numbers */
  181.    {
  182.     Read(fh,&buff[0],1L);
  183.     i++;
  184.    }
  185.   while((buff[0]<'0' || buff[0]>'9') && i<80);  
  186.   i=1;
  187.   do       /* read numbers */
  188.    {
  189.     Read(fh,&buff[i],1L);
  190.     i++;
  191.    }
  192.   while(i<80 && buff[i-1]>='0' && buff[i-1]<='9');  
  193.   return atoi(buff);
  194.  }
  195.  
  196. starts()
  197.  {
  198.   int i;
  199.   
  200.   for(i=0;i<anz;i++)
  201.    {
  202.     p[i][0]=sin(n[i]*2*PI/anz+PI/anz)*100;  /* <---  that was one          */
  203.     p[i][1]=-cos(n[i]*2*PI/anz+PI/anz)*100; /*   |   of the BIG problems   */
  204.     farbs[i]=n[i]+1;                        /* <-/   (= 1.5 days trying    */
  205.    }
  206.  }
  207.   
  208. views()
  209.  {
  210.   int mouse;
  211.   
  212.   for(mouse=0;mouse<anz;mouse++)
  213.    {                                     /*        |    */
  214.     SetAPen(rp,(long int)farbs[mouse]);  /* this ( V ) is not the best way of doing it, but as time goes by and proggies grow ...*/
  215.     Move(rp,(long int)(10+(p[mouse][0]+XADD)*XF),(long int)(50+(p[mouse][1]+YADD)*YF));
  216.     Draw(rp,(long int)(10+(p[(mouse+1)%anz][0]+XADD)*XF),(long int)(50+(p[(mouse+1)%anz][1]+YADD)*YF));
  217.    }
  218.  }
  219.  
  220. move()
  221.  {
  222.   int m,mg;
  223.   
  224.   for(m=0;m<anz;m++)   /* compute next positions */
  225.    {
  226.     mg=(m+1)%anz;
  227.     q[m][0]=p[m][0]+teil*(p[mg][0]-p[m][0]);
  228.     q[m][1]=p[m][1]+teil*(p[mg][1]-p[m][1]);
  229.    }
  230.   for(m=0;m<anz;m++)   /* use the new positions */
  231.    {
  232.     p[m][0]=q[m][0];
  233.     p[m][1]=q[m][1];
  234.    }
  235.  }
  236.  
  237.