home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 011.lha / tree.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  7KB  |  334 lines

  1. /************************************************************************
  2. *  Version 1.00       TREE.C - Draw a Recursive Tree         08-May-86  *
  3. *  Commodore Amiga              Only Module                     TREE.C  *
  4. *************************************************************************
  5. *                  Copyright (c) 1986, Robert S. French                 *
  6. * --------------------------------------------------------------------- *
  7. *  This program has been placed in the public domain.  A limited        *
  8. *  license is hereby granted for the unlimited use and distribution of  *
  9. *  this program, provided it is not used for commercial or profit-      *
  10. *  making purposes.  Thank you.                                         *
  11. *************************************************************************
  12. *  Author information:              |           Disclaimer:             *
  13. *                                   |                                   *
  14. *  Name:   Robert S. French         |  The author takes no responsibil- *
  15. *  USnail: 2740 Frankfort Avenue    |  ity for damages incurred during  *
  16. *          Louisville, KY  40206    |  the use of this program.         *
  17. *  Phone:  (502) 897-5096           \-----------------------------------*
  18. *  ARPA:   French#Robert%d@LLL-MFE     UUCP: ihnp4!ptsfa!well!french    *
  19. *************************************************************************
  20. *  Please send any comments, suggestions, or bugs to one of the above   *
  21. *  addresses.                                                           *
  22. *************************************************************************
  23. *                           Acknowledgements                            *
  24. *                           ================                            *
  25. *                                                                       *
  26. *  Original version presented for IBM BASIC by William K. Balthrop in   *
  27. *  Home Computer Magazine, Vol. 5, No. 6.                               *
  28. *                                                                       *
  29. *  Random number generator from the Encyclopedia of Computer Science    *
  30. *  and Engineering, second edition.                                     *
  31. ************************************************************************/
  32.  
  33. /* Necessary includes */
  34.  
  35. #include <exec/types.h>
  36. #include <exec/libraries.h>
  37. #include <devices/keymap.h>
  38. #include <graphics/copper.h>
  39. #include <graphics/display.h>
  40. #include <graphics/gfxbase.h>
  41. #include <graphics/text.h>
  42. #include <graphics/view.h>
  43. #include <graphics/gels.h>
  44. #include <graphics/regions.h>
  45. #include <hardware/blit.h>
  46. #include <intuition/intuition.h>
  47. #include <intuition/intuitionbase.h>
  48. #include <stdio.h>
  49.  
  50. /* Library Pointers */
  51.  
  52. struct IntuitionBase *IntuitionBase = 0;
  53. struct GfxBase *GfxBase = 0;
  54.  
  55. /* Initial Graphics Conditions */
  56.  
  57. struct NewScreen newscr = {
  58.     0,        /* Left */
  59.     0,        /* Top */
  60.     640,    /* Width */
  61.     200,    /* Height */
  62.     4,        /* Depth */
  63.     0,        /* DPen */
  64.     1,        /* BPen */
  65.     HIRES,        /* View Modes */
  66.     CUSTOMSCREEN,    /* Type */
  67.     0,        /* Font */
  68.     "Recursive Tree (1.00) by Robert French - Right mouse button to exit",    /* Title */
  69.     0,        /* Gadgets */
  70.     0        /* Bitmap */
  71. };
  72.  
  73. struct NewWindow newwin = {
  74.     0,        /* Left */
  75.     0,        /* Top */
  76.     640,    /* Width */
  77.     200,    /* Height */
  78.     -1,    /* DPen */
  79.     -1,    /* BPen */
  80.     MOUSEBUTTONS,    /* IDCMP */
  81.     BACKDROP | BORDERLESS | ACTIVATE | RMBTRAP,    /* Flags */
  82.     0,        /* Gadgets */
  83.     0,        /* Check */
  84.     0,        /* Title */
  85.     0,        /* Screen */
  86.     0,        /* Bitmap */
  87.     0,        /* MinWidth */
  88.     0,        /* MinHeight */
  89.     0,        /* MaxWidth */
  90.     0,        /* MaxHeight */
  91.     CUSTOMSCREEN    /* Screen type */
  92. };
  93.  
  94. UWORD colors[16] = {
  95.     0x000, 0xfff, 0xf00, 0xff0, 0x0d0, 0x0b0, 0x090, 0xa84,
  96.     0x973, 0x970, 0x960, 0x860, 0x850, 0x750, 0x740, 0x640
  97. };
  98.  
  99. struct Screen *scr = 0;
  100. struct Window *win = 0;
  101. struct RastPort *rp;
  102. struct ViewPort *vp;
  103.  
  104. /* Random Number Definitions */
  105.  
  106. long seeds[17];
  107. int seedp1,seedp2,seedp3;
  108.  
  109. /* Direction Definitions */
  110.  
  111. struct s_dir {
  112.     int    x;
  113.     int    y;
  114. } offset[8] = {
  115.     {  0, -1 },
  116.     {  2, -1 },
  117.     {  2,  0 },
  118.     {  2,  1 },
  119.     {  0,  1 },
  120.     { -2,  1 },
  121.     { -2,  0 },
  122.     { -2, -1 }
  123. }, boff[4] = {
  124.     { -1,  0 },
  125.     { -1, -1 },
  126.     {  0, -1 },
  127.     {  1, -1 }
  128. };
  129.  
  130. main()
  131. {
  132.     int i;
  133.  
  134.     if ((IntuitionBase = (struct Intuitionbase *)OpenLibrary("intuition.library",0)) == 0)
  135.         abort("Can't open intuition.library");
  136.  
  137.     if ((GfxBase = (struct GfxBase *)OpenLibrary("graphics.library")) == 0)
  138.         abort("Can't open graphics.library");
  139.  
  140.     if ((scr = (struct Screen *)OpenScreen(&newscr)) == 0)
  141.         abort("Can't open screen");
  142.  
  143.     newwin.Screen = scr;
  144.  
  145.     if ((win = (struct Window *)OpenWindow(&newwin)) == 0)
  146.         abort("Can't open window");
  147.  
  148.     rp = win->RPort;
  149.     vp = &scr->ViewPort;
  150.  
  151.     LoadRGB4(vp,colors,16);
  152.     SetDrMd(rp,JAM1);
  153.     init_rand();
  154.  
  155.     for (;;) {
  156.         SetAPen(rp,0);
  157.         RectFill(rp,0,10,639,199);
  158.  
  159.         draw_ground();
  160.  
  161.         branch(320,120,14,0,1);        /* Draw first right branch */
  162.         branch(320,120,14,0,-1);    /* Draw first left branch */
  163.  
  164.         for (i=0;i<10;i++) {
  165.             Delay(60);
  166.             chk_done();
  167.         }
  168.     }
  169.     abort("");
  170. }
  171.  
  172. abort(s)
  173. char *s;
  174. {
  175.     if (win)
  176.         CloseWindow(win);
  177.     if (scr)
  178.         CloseScreen(scr);
  179.     if (IntuitionBase)
  180.         CloseLibrary(IntuitionBase);
  181.     if (GfxBase)
  182.         CloseLibrary(GfxBase);
  183.     printf("%s\n",s);
  184.     exit(0);
  185. }
  186.  
  187. branch(xs,ys,len,dir,rl)
  188. int xs,ys,len,dir;
  189. {
  190.     int r,xe,ye;
  191.  
  192.     chk_done();
  193.  
  194.     if (len > 11)
  195.         len--;
  196.     else {
  197.         r = my_rand();
  198.         switch (r) {
  199.             case 12:
  200.                 len /= 2;
  201.                 break;
  202.             case 1:
  203.                 len *= 7;
  204.                 len /= 10;
  205.                 break;
  206.             case 2:
  207.             case 3:
  208.                 len *= 8;
  209.                 len /= 10;
  210.                 break;
  211.             case 4:
  212.                 len *= 9;
  213.                 len /= 10;
  214.                 break;
  215.             default:
  216.                 len--;
  217.                 break;
  218.         }
  219.     }
  220.  
  221.     dir = (dir+rl) & 7;
  222.     xe = xs+len*offset[dir].x;
  223.     ye = ys+len*offset[dir].y;
  224.     r = my_rand();
  225.     switch (r) {
  226.         case 0:
  227.             xe--;
  228.             break;
  229.         case 1:
  230.             xe++;
  231.             break;
  232.         case 2:
  233.             ye--;
  234.             break;
  235.         case 3:
  236.             ye++;
  237.             break;
  238.     }
  239.     drawbranch(xs,ys,xe,ye,dir,len);
  240.     if (len < 1)
  241.         return;
  242.     branch(xe,ye,len,dir,1);    /* Draw right branch */
  243.     branch(xe,ye,len,dir,-1);    /* Draw left branch */
  244. }
  245.  
  246. drawbranch(x,y,ex,ey,d,l)
  247. int x,y,ex,ey,d,l;
  248. {
  249.     SetAPen(rp,l+2);
  250.  
  251.     if (l > 9) {
  252.         Move(rp,x+boff[d&3].x,y+boff[d&3].y);
  253.         Draw(rp,ex+boff[d&3].x,ey+boff[d&3].y);
  254.     }
  255.     if (l > 6) {
  256.         Move(rp,x-boff[d&3].x,y-boff[d&3].y);
  257.         Draw(rp,ex-boff[d&3].x,ey-boff[d&3].y);
  258.     }
  259.     Move(rp,x,y);
  260.     Draw(rp,ex,ey);
  261. }
  262.  
  263. init_rand()
  264. {
  265.     long sec,mic,x;
  266.     int i;
  267.  
  268.     CurrentTime(&sec,&mic);
  269.  
  270.     x = sec*(mic | 1);
  271.  
  272.     for (i=0;i<17;i++) {
  273.         seeds[i] = x;
  274.         x = x*3+mic;
  275.     }
  276.  
  277.     seedp1 = 4;
  278.     seedp2 = 16;
  279.     seedp3 = 0;
  280. }
  281.  
  282. my_rand()
  283. {
  284.     long result;
  285.  
  286.     result = seeds[seedp1]+seeds[seedp2];
  287.     seeds[seedp3] = result;
  288.     if (++seedp1 > 16)
  289.         seedp1 = 0;
  290.     if (++seedp2 > 16)
  291.         seedp2 = 0;
  292.     if (++seedp3 > 16)
  293.         seedp3 = 0;
  294.  
  295.     return (result & 31);
  296. }
  297.  
  298. draw_ground()
  299. {
  300.     int i,j,x,y,c;
  301.  
  302.     SetAPen(rp,5);
  303.     RectFill(rp,0,194,639,199);
  304.     SetAPen(rp,14);
  305.     x = 28;
  306.     y = 199;
  307.     for (c=0;c<13;c++) {
  308.         for (j=0;j<2;j++) {
  309.             for (i=0;i<c;i++,y--) {
  310.                 if (y < 118)
  311.                     return (0);
  312.                 Move(rp,318-x,y);
  313.                 Draw(rp,322+x,y);
  314.             }
  315.             x--;
  316.         }
  317.     }
  318. }
  319.  
  320. chk_done()
  321. {
  322.     struct IntuiMessage *message;
  323.     ULONG class;
  324.     USHORT code;
  325.  
  326.     while (message = GetMsg(win->UserPort)) {
  327.         class = message->Class;
  328.         code = message->Code;
  329.         ReplyMsg(message);
  330.         if (class == MOUSEBUTTONS && code == MENUDOWN)
  331.             abort("");
  332.     }
  333. }
  334.