home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d118 / wiredemo.lha / WireDemo / main.c < prev    next >
C/C++ Source or Header  |  1987-12-03  |  8KB  |  353 lines

  1.  
  2. /*
  3.  *  MAIN.C
  4.  *
  5.  *  Bulging Bubbles Demo
  6.  */
  7.  
  8. #include "bubbles.h"
  9.  
  10. TA Ta = { (ubyte *)"topaz.font", 8 };
  11.  
  12. NS Ns = {
  13.     0, 0, 640, 200, 2, 0, 1,
  14.     HIRES, CUSTOMSCREEN|SCREENQUIET, &Ta, (ubyte *)"", NULL, NULL
  15. };
  16.  
  17. NW Nw = {
  18.     0, 2, 64, 32, -1, -1,
  19.     CLOSEWINDOW,
  20.     WINDOWCLOSE|SIMPLE_REFRESH|BORDERLESS|NOCAREREFRESH|RMBTRAP,
  21.     NULL,NULL,NULL,NULL,NULL,16,16,-1,-1,CUSTOMSCREEN
  22. };
  23.  
  24. OBJECT *Grid;
  25. OBJECT *Ball;
  26.  
  27. WIN *Win;
  28. SCR *Scr;
  29. RP  *Rp;
  30.  
  31. long S_width, S_height, S_midwidth, S_dblheight;
  32.  
  33. main(ac,av)
  34. char *av[];
  35. {
  36.     IMESS *imess;
  37.     SCR Screen;
  38.  
  39.     if (ac <= 1) {
  40.     init_grid();
  41.     init_ball();
  42.     } else {
  43.     init_file(av[1]);
  44.     }
  45.     init_objectmodule();
  46.     init_bounce();
  47.     openlibs(INTUITION_LIB|GRAPHICS_LIB);
  48.     GetScreenData(&Screen, sizeof(Screen), WBENCHSCREEN, NULL);
  49.     Ns.Width = Screen.Width;
  50.     Ns.Height= Screen.Height;
  51.     if (Scr = OpenScreen(&Ns)) {
  52.     S_width = Scr->Width;
  53.     S_height= Scr->Height;
  54.     S_midwidth = S_width >> 1;
  55.     S_dblheight= S_height<< 1;
  56.     SetRGB4(&Scr->ViewPort,0, 0, 0, 0);
  57.     SetRGB4(&Scr->ViewPort,3, 5, 5, 15);
  58.     Nw.Screen = Scr;
  59.     if (Win = OpenWindow(&Nw)) {
  60.         Rp = &Scr->RastPort;
  61.         for (;;) {
  62.         bounce_ball();
  63.         toggle_rport();
  64.         displayobj(Rp,Grid);
  65.         displayobj(Rp,Ball);
  66.         if (imess = GetMsg(Win->UserPort))
  67.             break;
  68.         }
  69.         ReplyMsg(imess);
  70.         CloseWindow(Win);
  71.     }
  72.     CloseScreen(Scr);
  73.     }
  74.     closelibs(-1);
  75. }
  76.  
  77. toggle_rport()
  78. {
  79.     if (Rp->Mask & 1) {
  80.     SetRGB4(&Scr->ViewPort,1, 5, 5, 15);
  81.     SetRGB4(&Scr->ViewPort,2, 0, 0, 0);
  82.     Rp->Mask = 0x02;
  83.     SetAPen(Rp,2);
  84.     } else {
  85.     SetRGB4(&Scr->ViewPort,2, 5, 5, 15);
  86.     SetRGB4(&Scr->ViewPort,1, 0, 0, 0);
  87.     Rp->Mask = 0x01;
  88.     SetAPen(Rp,1);
  89.     }
  90.     WaitTOF();
  91.     SetRast(Rp,0);
  92. }
  93.  
  94. /*
  95.  *  Create two objects:
  96.  *    (1) A stationary grid        Z=0, x/y -32768:65536/4
  97.  *    (2) a ball (cube for now)
  98.  */
  99.  
  100. init_grid()
  101. {
  102.     static long points[][3] = {
  103.     -PMAXX,-PMAXY,0,      /* Border   */
  104.      PMAXX,-PMAXY,0,
  105.      PMAXX, PMAXY,0,
  106.     -PMAXX, PMAXY,0,
  107.  
  108.     -0x0400,0x0700,0x0E00,
  109.     -0x0280,0x0700,0x0E00,
  110.     -0x0100,0x0700,0x0E00,
  111.     -0x0D00,-0x0700,0,
  112.     -0x0B80,-0x0700,0,
  113.     -0x0A00,-0x0700,0,
  114.     -0x1000,-0x0300,0x0400,
  115.     -0x0E80,-0x0300,0x0400,
  116.     -0x0D00,-0x0300,0x0400,
  117.     -0x0C80,-0x05E0,0x0110,
  118.     -0x0BC0,-0x0510,0x01F0,  /*  14  */
  119.     -0x0900,-0x0700,0,
  120.     -0x0400,0,0x700,
  121.     -0x0300,-0x0700,0,
  122.     -0x0610,-0x0300,0x0400,
  123.     -0x0390,-0x0300,0x0400,
  124.     -0x0200,-0x0700,0,
  125.     -0x0100,0,0x0700,     /*  21  */
  126.     0x0100,-0x0300,0x0400,
  127.     0x0400,0,0x0700,
  128.     0x0300,-0x0700,0,
  129.     0x0500,-0x0700,0,        /*    25.. I    */
  130.     0x0700,-0x0700,0,
  131.     0x0900,-0x0700,0,
  132.     0x0500,0,0x0700,
  133.     0x0700,0,0x0700,
  134.     0x0900,0,0x0700,
  135.     0x0D00,-0x0100,0x0600,
  136.     0x0C00,0,0x0700,
  137.     0x0A00,0,0x0700,
  138.     0x0980,-0x0400,0x0300,
  139.     0x0A00,-0x0700,0,
  140.     0x0D00,-0x0700,0,
  141.     0x0D00,-0x0500,0x0200,
  142.     0x0C00,-0x0500,0x0200,
  143.     0x0E00,-0x0700,0,
  144.     0x0F80,-0x0300,0x0400,
  145.     0x1100,0,0x0700,
  146.     0x1200,-0x0300,0x0400,
  147.     0x1300,-0x0700,0
  148.     };
  149.     static short connect[][2] = {
  150.     0,1, 1,2, 2,3, 3,0,
  151.     4,5, 5,6, 4,7, 5,8, 6,9, 9,8, 8,7, 7,10, 10,11, 11,13, 13,14, 14,12, 12,11,
  152.     15,18, 18,16, 16,19, 19,17, 18,19,  /* A */
  153.     20,21, 21,22, 22,23, 23,24,        /* M */
  154.     28,29, 29,30, 29,26, 25,26, 26,27,  /* I */
  155.     31,32, 32,33, 33,34, 34,35, 35,36, 36,37, 37,38,    /* G */
  156.     39,40, 40,41, 41,42, 42,43, 40,42
  157.     };
  158.     Grid = makeobject(
  159.     points , sizeof(points)/sizeof(points[0]),
  160.     connect, sizeof(connect)/sizeof(connect[0]),
  161.     0,0,0
  162.     );
  163. }
  164.  
  165. init_ball()
  166. {
  167.     static long points[][3] = {
  168.     -PSKIP/5 ,-PSKIP/5 , 0,
  169.      PSKIP/5 ,-PSKIP/5 , 0,
  170.      PSKIP/5 , PSKIP/5 , 0,
  171.     -PSKIP/5 , PSKIP/5 , 0,
  172.     -PSKIP/5 ,-PSKIP/5 , PSKIP*2/5,
  173.      PSKIP/5 ,-PSKIP/5 , PSKIP*2/5,
  174.      PSKIP/5 , PSKIP/5 , PSKIP*2/5,
  175.     -PSKIP/5 , PSKIP/5 , PSKIP*2/5
  176.     };
  177.     static short connect[][2] = {
  178.     0,1,1,2,2,3,3,0,
  179.     4,5,5,6,6,7,7,4,
  180.     0,4,1,5,2,6,3,7,
  181.     0,6,1,7,2,4,3,5
  182.     };
  183.  
  184.     Ball = makeobject(
  185.     points , sizeof(points)/sizeof(points[0]),
  186.     connect, sizeof(connect)/sizeof(connect[0]),
  187.     0,0,0
  188.     );
  189. }
  190.  
  191. /*
  192.  *  FILE FORMAT:
  193.  *    <blankline>             ignored
  194.  *    # comment            -comment
  195.  *    DB numpoints numconnect     -begin ball definition
  196.  *    DG numpoints numconnect     -begin grid definition
  197.  *    P  X Y Z            -define points (index begins at 0)
  198.  *    C  A B                -define line segments
  199.  *    END                -end of def.
  200.  */
  201.  
  202. init_file(name)
  203. char *name;
  204. {
  205.     FILE *fi = fopen(name,"r");
  206.     char buf[256];
  207.  
  208.     if (fi == NULL) {
  209.     puts("File not found");
  210.     exit(1);
  211.     }
  212.     while (fgets(buf,256,fi)) {
  213.     if (buf[0] == 'D') {
  214.         OBJECT **which;
  215.         long *points;
  216.         uword *connect;
  217.         long npts, ncon, ip, ic, c1, c2;
  218.  
  219.         which = (buf[1] == 'B') ? &Ball : &Grid;
  220.         if (sscanf(buf+2,"%ld %ld", &npts, &ncon) != 2) {
  221.         puts("Illegal format");
  222.         exit(1);
  223.         }
  224.         points  = (long *)malloc(sizeof(long) * 3 * npts);
  225.         connect = (uword *)malloc(sizeof(short) * 2 * ncon);
  226.         ip = ic = 0;
  227.         while (fgets(buf,256,fi)) {
  228.         switch(buf[0]) {
  229.         case 'P':
  230.             if (ip >= npts) {
  231.             puts("Beyond specified parameters");
  232.             exit(1);
  233.             }
  234.             sscanf(buf+1,"%ld %ld %ld", &points[ip*3], &points[ip*3+1], &points[ip*3+2]);
  235.             ++ip;
  236.             break;
  237.         case 'C':
  238.             if (ic >= ncon) {
  239.             puts("Beyond specified parameters");
  240.             exit(1);
  241.             }
  242.             sscanf(buf+1,"%ld %ld", &c1, &c2);
  243.             connect[ic*2] = c1;
  244.             connect[ic*2+1]=c2;
  245.             ++ic;
  246.             break;
  247.         case 'E':
  248.             *which = makeobject(points,ip,connect,ic,0,0,0);
  249.             goto br2;
  250.         }
  251.         }
  252. br2:        ;
  253.     }
  254.     }
  255.     fclose(fi);
  256.     if (!Ball)
  257.     init_ball();
  258.     if (!Grid)
  259.     init_grid();
  260. }
  261.  
  262.  
  263. /*
  264.  *  Bounce the ball within the domain (-PMAX,-PMAX,0) to (PMAX,PMAX,PMAX)
  265.  *    -uniform Z deceleration
  266.  *    -Z < 0 bounce + random acceleration load
  267.  *
  268.  *    -Camera attempts to match ball but is allowed only uniform
  269.  *     acceleration
  270.  *
  271.  *    -My position attempts to match camera but is allow only uniform
  272.  *     acceleration which is less than that for camera
  273.  *
  274.  *  Ball velocity clamped at 100
  275.  */
  276.  
  277. long Bx,By,Bz,BAx,BAy,BAz,BVx,BVy,BVz;    /*  The Ball        */
  278. long Cx,Cy,Cz,CAx,CAy,CAz,CVx,CVy,CVz;    /*  Camera center pt    */
  279. long Mx,My,Mz,MAx,MAy,MAz,MVx,MVy,MVz;    /*  My position     */
  280.  
  281. init_bounce()
  282. {
  283.     Mx = My = Mz = 16384;
  284.     BVx = 123;
  285.     BVy = -56;
  286. }
  287.  
  288. bounce_ball()
  289. {
  290.     BAz = -10;
  291.     BVx += BAx; if (BVx > CLAMP) BVx = CLAMP; if (BVx < -CLAMP) BVx = -CLAMP;
  292.     BVy += BAy; if (BVy > CLAMP) BVy = CLAMP; if (BVy < -CLAMP) BVy = -CLAMP;
  293.     BVz += BAz; if (BVz > CLAMP) BVz = CLAMP; if (BVz < -CLAMP) BVz = -CLAMP;
  294.     Bx += BVx;    if (Bx < -PMAXX|| Bx > PMAXX) { BVx = -BVx; Bx += 2*BVx; }
  295.     By += BVy;    if (By < -PMAXY|| By > PMAXY) { BVy = -BVy; By += 2*BVy; }
  296.     Bz += BVz;    if (Bz >  PMAXZ         ) { BVz = -BVz; Bz += 2*BVz; }
  297.     if (Bz < 0) {
  298.     /*  Elastic bounce plus boost    */
  299.     BVz = -BVz + (ran()&0xFFFF)%200;
  300.     if (BVz > 300)
  301.         BVz = 250;
  302.     }
  303.     Ball->x = Bx;
  304.     Ball->y = By;
  305.     Ball->z = Bz;
  306.  
  307.     {
  308.     uword theta,phi;
  309.     short result;
  310.  
  311.     theta = ibearing(Bx-Cx,By-Cy);
  312.     phi =    ibearing(irange(Bx-Cx,By-Cy),Bz-Cz);
  313.     result = ICOS(phi);
  314.     CAx += (5 * ((ICOS(theta) * result)>>15)) >> 15;
  315.     CAy += (5 * ((ISIN(theta) * result)>>15)) >> 15;
  316.     CAz += (5 * ISIN(phi)) >> 15;
  317.     }
  318.     {
  319.     uword theta,phi;
  320.     short result;
  321.  
  322.     theta = ibearing(Cx-Mx,Cy-My);
  323.     phi =    ibearing(irange(Cx-Mx,Cy-My),Cz-Mz);
  324.     result = ICOS(phi);
  325.     MAx = (10 * ((ICOS(theta) * result)>>15)) >> 15;
  326.     MAy = (10 * ((ISIN(theta) * result)>>15)) >> 15;
  327.     MAz = (10 * ISIN(phi)) >> 15;
  328.     }
  329.     MVx += MAx; Mx += MVx;
  330.     MVy += MAy; My += MVy;
  331.     MVz += MAz; Mz += MVz;
  332.     CVx += CAx; Cx += CVx;
  333.     CVy += CAy; Cy += CVy;
  334.     CVz += CAz; Cz += CVz;
  335.  
  336.     if (CAx > ACLAMP)CAx =ACLAMP; if (CAx < -ACLAMP)CAx = -ACLAMP;
  337.     if (CAy > ACLAMP)CAy =ACLAMP; if (CAy < -ACLAMP)CAy = -ACLAMP;
  338.     if (CAz > ACLAMP)CAz =ACLAMP; if (CAz < -ACLAMP)CAz = -ACLAMP;
  339.     if (CVx > CCLAMP) CVx = CCLAMP; if (CVx < -CCLAMP) CVx = -CCLAMP;
  340.     if (CVy > CCLAMP) CVy = CCLAMP; if (CVy < -CCLAMP) CVy = -CCLAMP;
  341.     if (CVz > CCLAMP) CVz = CCLAMP; if (CVz < -CCLAMP) CVz = -CCLAMP;
  342.  
  343.     if (MAx > ACLAMP)MAx =ACLAMP; if (MAx < -ACLAMP)MAx = -ACLAMP;
  344.     if (MAy > ACLAMP)MAy =ACLAMP; if (MAy < -ACLAMP)MAy = -ACLAMP;
  345.     if (MAz > ACLAMP)MAz =ACLAMP; if (MAz < -ACLAMP)MAz = -ACLAMP;
  346.     if (MVx > CCLAMP) MVx = CCLAMP; if (MVx < -CCLAMP) MVx = -CCLAMP;
  347.     if (MVy > CCLAMP) MVy = CCLAMP; if (MVy < -CCLAMP) MVy = -CCLAMP;
  348.     if (MVz > CCLAMP) MVz = CCLAMP; if (MVz < -CCLAMP) MVz = -CCLAMP;
  349.  
  350.     setcamera(Mx, My, Mz, Cx, Cy, Cz, 0);
  351. }
  352.  
  353.