home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / projmot_453.lzh / ProjMot / src / graph.c < prev    next >
C/C++ Source or Header  |  1991-02-18  |  11KB  |  366 lines

  1. /*   Graph.c (ProjMot)                       *
  2. *    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        *  Libraries Used
  3. *         This Program plots the path        *  ~~~~~~~~~~~~~~~~~
  4. *         of a projectyle fired at           *  req.library
  5. *         origon o, with velocity v          *  intuition.library
  6. *         and angle Ø (theta),               *  graphics.library
  7. *         with constant acceleration         *  mathffp.library
  8. *         g (-9.8 m/s^2).                    *  mathtrans.library
  9. *                                            *
  10. *    © Copyright 1991 Christian E. Hopps
  11. */
  12.  
  13.  
  14. /* Functions                                 * Global Variables
  15.  * ~~~~~~~~~~~~~~~~~~~~~~~~                  * ~~~~~~~~~~~~~~~~~~
  16.  * GLibLoad()                                * IntuitionBase
  17.  * GLibUnload()                              * GfxBase
  18.  * Gcalc()                                   * ReqBase
  19.  * Fire()                                    * MathBase
  20.  * HandleIDCMP()                             * MathTransBase
  21.  * ReClip()                                  * Scr
  22.  * VectorComponents();                       * Win
  23.  * GetTime()                                 * RP
  24.  *                                           *
  25. */
  26. #include <stdio.h>
  27. #include <intuition/intuition.h>
  28. #include <graphics/gfxbase.h>
  29. #include <graphics/gfx.h>
  30. #include <proto/req.h>
  31. #include <libraries/reqbase.h>
  32. #include <exec/memory.h>
  33. #include <libraries/mathffp.h>
  34. #include "window.h"
  35. #include "ggads.h"
  36. #define SIGNALW  (1L << Win->UserPort->mp_SigBit)
  37. #define SIGNALG  (1L << Gwin->UserPort->mp_SigBit)
  38. #define CLIPx1 xy->clip->clipx1
  39. #define CLIPy1 xy->clip->clipy1
  40. #define CLIPx2 xy->clip->clipx2
  41. #define CLIPy2 xy->clip->clipy2
  42.  
  43.     
  44. extern int GLibLoad();
  45. extern void GLibUnload();
  46. extern int HandleIDCMP();
  47. extern void Gcalc();
  48. extern void Fire();
  49. void ReClip();
  50. struct Window *OpenWindow();
  51. float GetTime();
  52. void VectorComponents();
  53.  
  54. struct IntuitionBase     *IntuitionBase;     /* These libraries will be */
  55. struct GfxBase           *GfxBase;           /* initialized by the */
  56. struct ReqLib            *ReqBase;           /* external programg libload.o */
  57. struct MathBase          *MathBase;
  58. struct MathTransBase     *MathTransBase;
  59.  
  60. struct Window            *Win;               /* Window Pointer */
  61. struct Window            *Gwin;              /* Win Pntr to Gads */
  62. struct Screen            *Scr;               /* Screen Pointer */
  63. struct RPort             *RP;                /* RastPort Pointer */
  64.  
  65. struct Clip                                  /* Clipping info */
  66. {
  67.      short clipx1;
  68.      short clipy1;
  69.      short clipx2;
  70.      short clipy2;
  71. };
  72.  
  73. struct XY                                    /* everything else info */
  74. {
  75.     float voy;
  76.     float vox;
  77.     float x;
  78.     float y;
  79.     short xp;
  80.     short yp;
  81.     BOOL clipped;
  82.     int scale;
  83.     struct Clip *clip;
  84. };
  85. char text[] = "\nThe projectile made it %ldm.  \nIt took %ld seconds.\n";
  86.  
  87. main()
  88. {
  89.     struct IntuiMessage *msg;
  90.     struct XY *xy;
  91.     struct Clip *c;
  92.     struct GetLongStruct *GetLongStructVel, *GetLongStructAngle;
  93.     ULONG signal, sec, micro, timeo, acctime;
  94.     USHORT adone = FALSE;
  95.     int success,angle_i,vel_i, class;
  96.     float time;
  97.     BOOL timeisacc;
  98.  
  99.      /* Open Up Everything and Allocate Memory */
  100.  
  101.     if(!(GLibLoad()))
  102.     {
  103.         printf("\nLibraries could not open\n");
  104.         printf("Make sure req.library is in LIBS:\n");    
  105.         GLibUnload();
  106.         exit(20);
  107.     }
  108.     if(!(Scr = (struct Screen *)AllocMem(sizeof(struct Screen),MEMF_PUBLIC | MEMF_CLEAR)))
  109.     {    
  110.         printf("\ncould not allocatemem for screen\n");
  111.         goto exit1;
  112.     }
  113.     if(!(xy = (struct XY *)AllocMem(sizeof(struct XY),MEMF_PUBLIC | MEMF_CLEAR)))
  114.         {    
  115.             printf("\ncould not allocatemem for xy\n");
  116.             goto exit1;
  117.         }
  118.     if(!(c = (struct Clip *)AllocMem(sizeof(struct Clip),MEMF_PUBLIC | MEMF_CLEAR)))
  119.         {    
  120.             printf("\ncould not allocatemem for clip\n");
  121.             goto exit1;
  122.         }
  123.  
  124.     /*     Set up clip areas based on window size at start also modify 
  125.         NewWindowStructure1 to make window as large as screen. */
  126.  
  127.     xy->clip = c;
  128.     GetScreenData( Scr, sizeof(struct Screen), WBENCHSCREEN, NULL );
  129.     CLIPx2 = (NewWindowStructure1.Width = Scr->Width) - 40;
  130.     CLIPy2 = (NewWindowStructure1.Height = Scr->Height) - 20;
  131.     CLIPx1 = 10;
  132.     CLIPy1 = 10;
  133.  
  134.     if(!(Win = OpenWindow(&NewWindowStructure1)))
  135.     {
  136.         printf("\nCould not open window\n");
  137.         GLibUnload();
  138.     }
  139.  
  140.     if(!(Gwin = OpenWindow(&NewWindowStructure2)))
  141.     {
  142.         printf("\nCould not open window\n");
  143.         CloseWindow(Win);
  144.         GLibUnload();
  145.     }
  146.     if((success = (int)AddGList(Gwin, &Gadget1, -1, -1 ,NULL)) == -1)
  147.         goto exit;
  148.     RefreshGList(&Gadget1, Gwin, NULL, -1);
  149.     if(!(GetLongStructVel = (struct GetLongStruct *)AllocMem(sizeof(struct GetLongStruct),MEMF_PUBLIC | MEMF_CLEAR)))
  150.         {
  151.             printf("\ncould not get memory for req.struct\n");
  152.             goto exit1;
  153.         }
  154.     if(!(GetLongStructAngle = (struct GetLongStruct *)AllocMem(sizeof(struct GetLongStruct),MEMF_PUBLIC | MEMF_CLEAR)))
  155.         {
  156.             printf("\ncould not get memory for req.struct\n");
  157.             goto exit1;
  158.         }
  159.  
  160.  
  161.     RP = Win->RPort;                             /* Init some var. */
  162.     vel_i=50; 
  163.     angle_i=45; 
  164.     xy->clipped = 0;
  165.     SetDrMd(RP,JAM1);
  166.     class = 0;
  167.     xy->xp = CLIPx1;
  168.     xy->yp = CLIPy2;
  169.     SetAPen(RP,1);
  170.     acctime = 1;
  171.     timeisacc = FALSE;
  172.     xy->scale = 1;
  173. /* Main Loop */
  174. /* if close gadget is clicked then adone will be set to true */
  175.  
  176.     while(!(adone))
  177.     {
  178.         Move(RP,CLIPx1,CLIPy2);                 /* Move to modified origin */
  179.         if(acctime != 1) timeisacc = TRUE; 
  180.         signal = Wait(SIGNALW | SIGNALG);
  181.  
  182.         if(signal & SIGNALG)                     /* Gadget Window has rec. */
  183.         {                                        /* a message can only be  */
  184.             success = HandleIDCMP((int)1,Gwin); /* a GADGETUP */
  185.             switch(success)
  186.             {
  187.                 case 5:                        /* velocity */
  188.                     GetLongStructVel->titlebar = "Initial Velocity(m/s)";
  189.                     GetLongStructVel->defaultval = vel_i;
  190.                     GetLongStructVel->minlimit = 0;
  191.                     GetLongStructVel->maxlimit = 100000;
  192.                     GetLongStructVel->versionnumber = REQVERSION;
  193.  
  194.                     if(vel_i = GetLong(GetLongStructVel))
  195.                         if(!(vel_i = GetLongStructVel->result))
  196.                             vel_i = 50;
  197.                     break;
  198.  
  199.                 case 1:                        /* angle */
  200.                      GetLongStructAngle->titlebar = "Ø (theta)";
  201.                     GetLongStructAngle->defaultval = angle_i;
  202.                     GetLongStructAngle->minlimit = 0;
  203.                     GetLongStructAngle->maxlimit = 90;
  204.                     GetLongStructAngle->versionnumber = REQVERSION;
  205.                     if(angle_i = GetLong(GetLongStructAngle))
  206.                         if(!(angle_i = GetLongStructAngle->result))
  207.                             angle_i = 45;
  208.                     break;
  209.  
  210.                 case 3:                        /* Fire! */
  211.                     WindowToBack(Gwin);
  212.                     VectorComponents(xy,angle_i,vel_i);
  213.                     CurrentTime(&timeo,µ); 
  214.  
  215.                 /*   Now we go into the secondary loop, draw motion path.*/
  216.                 /*   if left mouse button clicked, close gadget clicked, */
  217.                 /*   size is changed, or function is done, END           */
  218.  
  219.                     while(SPFix(xy->y) >= 0  & 
  220.                         (!(msg = (struct IntuiMessage *)
  221.                                 GetMsg(Win->UserPort))))
  222.                     {
  223.                         time = GetTime(timeo);
  224.                         if(timeisacc) 
  225.                             time = SPMul(time,SPFlt(acctime));
  226.                         Gcalc(time,xy);
  227.                         Fire(xy, RP);
  228.                     }                        /* Done with plot */
  229.  
  230.                     if(msg)                  /* This is to check    */
  231.                     {                        /* if Newsize was rec. */
  232.                         class = msg->Class;    /* if so bypass handle */
  233.                         ReplyMsg(msg);      /* function and do it  */
  234.                         if(class == NEWSIZE)/* right here otherwise*/
  235.                         {                   /* forget message      */
  236.                             ReClip(Win,xy);
  237.                             SetRast(RP,0);
  238.                         }
  239.                     }
  240.                     CurrentTime(&sec,µ);
  241.                     xy->y =SPFlt(1);         /* init y so it can pass */
  242.                     Move(RP,CLIPx1,CLIPy2);  /* while condition next  */
  243.                     xy->clipped = 0;         /* time;  reset origin   */
  244.                     SimpleRequest(&text,(SPFix(xy->x)*xy->scale),(acctime*(sec-timeo)));
  245.                     WindowToFront(Gwin);     /* and send a text req.  */
  246.                     break;
  247.                 case 6:                  /* Clear */
  248.                     SetRast(RP,0);
  249.                     break;
  250.                 case 2:                  /* Scale */
  251.                     GetLongStructVel->titlebar = "Scale (# of m/pixel)";
  252.                     GetLongStructVel->defaultval = xy->scale;
  253.                     GetLongStructVel->minlimit = 1;
  254.                     GetLongStructVel->maxlimit = 10000;
  255.                     GetLongStructVel->versionnumber = REQVERSION;
  256.                     xy->scale = GetLong(GetLongStructVel);
  257.                     xy->scale = GetLongStructVel->result;
  258.                     if(xy->scale == 0) 
  259.                         xy->scale = 1;
  260.                     SetRast(RP,0);
  261.                     break;
  262.                 case 4:                  /* Acc. Time */
  263.                     GetLongStructVel->titlebar = "Time Acceleration";
  264.                     GetLongStructVel->defaultval = acctime;
  265.                     GetLongStructVel->minlimit = 1;
  266.                     GetLongStructVel->maxlimit = 100;
  267.                     GetLongStructVel->versionnumber = REQVERSION;
  268.                     acctime = GetLong(GetLongStructVel);
  269.                     acctime = GetLongStructVel->result;
  270.                     if(acctime == 0) 
  271.                         acctime = 1;
  272.                     timeisacc = TRUE;
  273.                     
  274.                     break;
  275.                 default:
  276.                     break;
  277.             }
  278.         }
  279.         if(signal & SIGNALW)                    /* Handle message from */ 
  280.         {                                       /* the main window     */
  281.             success = HandleIDCMP((int)0,Win);
  282.             switch(success)
  283.             {
  284.                 case 0:
  285.                     adone = TRUE;
  286.                     break;
  287.                 case 5:
  288.                     SetRast(RP,0);
  289.                     ReClip(Win,xy);
  290.                     break;
  291.                 case 2:
  292.                     adone = FALSE;
  293.                     break;
  294.                 defualt:
  295.                     break;
  296.             }
  297.         }
  298.     }
  299.  
  300.  
  301. exit1:
  302.  
  303.  
  304.     if(GetLongStructAngle) 
  305.         FreeMem(GetLongStructAngle, sizeof(struct GetLongStruct));
  306.     if(GetLongStructVel) 
  307.         FreeMem(GetLongStructVel, sizeof(struct GetLongStruct));
  308.     success = RemoveGList(Gwin, &Gadget1,-1);
  309. exit:
  310.     if(Gwin) CloseWindow(Gwin);
  311.     if(Win) CloseWindow(Win);    
  312.     if(Scr) FreeMem(Scr, sizeof(struct Screen));
  313.     if(xy)  FreeMem(xy, sizeof(struct XY)); 
  314.     if(c) FreeMem(c,sizeof(struct Clip));
  315.     GLibUnload();
  316. /* All Done */
  317. }
  318.  
  319.  
  320. void ReClip(Win,xy)                               /* Set up new clip values */
  321. struct XY *xy;
  322. struct Window *Win;
  323. {
  324.  
  325.     CLIPx2 = Win->Width - 40;
  326.     CLIPy2 = Win->Height - 30;
  327. }
  328.  
  329. void VectorComponents(xy,angle_i,vel_i)           /* Break Vector in comp.  */
  330. struct XY *xy;
  331. int angle_i, vel_i;
  332. {
  333.     float vel,cosx,siny,rangle,angle;
  334.     vel = SPDiv(SPFlt(xy->scale),SPFlt(vel_i));
  335.     angle = SPFlt(angle_i); /* change angle to flt */
  336.  
  337.         /* Compute Radian Conversion                        */
  338.         /* NOTE!! there is multiple errors in commodore's      */
  339.         /* Libraries and devices, they incorectly state that*/
  340.         /* the mathtrans functions are like so:             */
  341.         /* f1 = SPDiv(f2,f3) => (f1 = f2 / f3)              */
  342.         /* when in reality the function is equal  to:       */
  343.         /* f1 = f3 / f2; It took a week to find that bug!:-)*/
  344.         /* this is also true for SPSub, and I have an itchy */
  345.         /* feeling for the rest of the functions as well,   */
  346.         /* it doesn't realy matter for the comunitive       */
  347.         /* functions though.                                */
  348.  
  349.     rangle = SPDiv(SPFlt((int)180), SPMul(PI,angle));
  350.     siny = SPSin(rangle);
  351.     cosx = SPCos(rangle);
  352.     xy->vox = SPMul(vel , cosx); /* calc init. x vel.*/
  353.     xy->voy = SPMul(vel , siny); /* calc inti. y vel.*/
  354. }
  355.  
  356. float GetTime(timeo)                           /* Get the time in sec.micro */
  357. long timeo;                                    /* return a float value      */
  358. {
  359.     int sec,micro;
  360.     float m,time;
  361.  
  362.     CurrentTime(&sec,µ);
  363.     m = SPDiv(SPFlt(1000000),SPFlt(micro));
  364.     time = SPAdd(SPFlt(sec - timeo), m);
  365.     return(time);
  366. }