home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 220.lha / lineart.c < prev    next >
C/C++ Source or Header  |  1996-02-15  |  10KB  |  364 lines

  1. /*
  2.     This program is called Line Art. It draws pretty lines on the 
  3.     screen like the Early suns used to do.
  4.  
  5.     This is not my idea, but the implementation is.
  6.  
  7.     lineart -t      - This leaves a trail (one out of every 'l' lines) remains
  8.                       on the screen.
  9.             -n      - This is the 'nice' option, this makes LineArt much more
  10.                       friendly in a multitasking environment (It takes less
  11.                       CPU time.)
  12.             -cXXXX  - This switch allows you to specify the initial color that
  13.                       the program will cycle from. Suggested color values start
  14.                       from 1500 (decimal) and up.
  15.             -lXXXX  - This switch allows you to specify the number of lines to
  16.                       be drawn before the last one is erased.
  17.             -b      - draw boxes (4 connected lines) instead of lines.
  18.             nnn     - This is an integer between 1 and 9 that will change the 
  19.                       minimum distance between sucessive endpoints for each
  20.                       line drawn. A high number will give the illusion of
  21.                       unbelievable speed!
  22.  
  23. The defaults (if you just type 'lineart') are as if you entered:
  24.  
  25.     LineArt 3 -t20 -c1280 -l10
  26.  
  27.     This program is PD. Use it or abuse it, only you will know.
  28.  
  29.     Steve -Raz- Berry
  30.     A-7 Sinai Circle
  31.     Chelmsford, Ma. 01824
  32.  
  33.     This program is a product of The Checkered Ball 1989.
  34.  
  35.     2/19/89
  36.  
  37. */
  38.  
  39. #define NORMALFLAGS BORDERLESS|SMART_REFRESH|BACKDROP
  40.  
  41. struct NewScreen scr = {
  42.     0,0,                /* x,y start */
  43.     640,400,            /* width, height */
  44.     1,                  /* depth */
  45.     2,1,                /* detail, block pens */
  46.     HIRES|LACE,         /* view mode */
  47.     CUSTOMSCREEN,       /* screen type */
  48.     NULL,               /* font */
  49.     NULL,               /* title */
  50.     NULL,               /* gadgets */
  51.     NULL                /* bitmap pntr */
  52.     };
  53.  
  54. struct NewWindow mywin = {
  55.     0,0,                        /* x,y start */
  56.     640,400,                    /* width, height */
  57.     2,1,                        /* detail, block pens */
  58.     VANILLAKEY | REFRESHWINDOW | GADGETDOWN | GADGETUP | 
  59.                                 /* IDCMP flags */
  60.     MOUSEBUTTONS | REQCLEAR | SELECTDOWN | SELECTUP,
  61.     NORMALFLAGS,                /* window flags */
  62.     NULL, NULL,                 /* pntr gadget, checkmark */
  63.     NULL,                   /* title */
  64.     NULL,                   /* screen pointer filled in later (run time) */
  65.     NULL,                   /* bitmap pntr */
  66.     0, 0, 8000, 8000,       /* default x,y, &size */
  67.     CUSTOMSCREEN
  68.     };
  69.  
  70. UWORD coltbl[2] = {
  71.     0x000,              /* background color */
  72.     0x500
  73. };
  74.  
  75. LONG GfxBase, IntuitionBase;
  76.  
  77. struct Window *win;
  78. struct RastPort *rp;
  79. struct ViewPort *vp;
  80. struct Screen *scrptr;
  81.  
  82. long Enable_Abort = 0;
  83. long _stack = 10000, _priority = -11, _BackGroundIO = 1;
  84. char *_procname = "Line-Art";
  85.  
  86. int scale1, scale2;
  87. long input, maxlines, ctemp;
  88.  
  89. #define MAXLINES 1000
  90.  
  91. long point1[MAXLINES+1][2], point2[MAXLINES+1][2], direction[2][2];
  92.  
  93. main(argc,argv)
  94. int argc;
  95. char *argv[];
  96. {
  97.     int i, count, trails, nice, boxes;
  98.     register int offset, newoffset, tempx, tempy;
  99.     struct IntuiMsg *msg;
  100.  
  101.     input = 3;          /* Some defaults for switches */
  102.     trails = 1;
  103.     maxlines = 10;
  104.     nice = FALSE;
  105.     boxes = FALSE;
  106.  
  107.     while(--argc >= 1) {
  108.         switch (argv[argc][0]) {
  109.             case '-':
  110.                 switch (argv[argc][1]) {
  111.                     case 't':
  112.                         trails = 0;
  113.                         break;
  114.                     case 'n':
  115.                         nice = TRUE;
  116.                         break;
  117.                     case 'l':
  118.                         sscanf(&argv[argc][2],"%d",&maxlines);
  119.                         maxlines = (maxlines > MAXLINES) ? MAXLINES : (maxlines < 1) ? 1 : maxlines;
  120.                         break;
  121.                     case 'c':
  122.                         sscanf(&argv[argc][2],"%d",&ctemp);
  123.                         coltbl[1] = (ctemp > 4096) ? 4096 : (ctemp < 1) ? 1 : ctemp;
  124.                         break;
  125.                     case 'b':
  126.                         boxes = TRUE;
  127.                         break;
  128.                     default:
  129.                         exit(20);
  130.                 }
  131.                 break;
  132.             default:
  133.                 sscanf(argv[argc],"%d",&input);
  134.                 break;
  135.         }
  136.     }
  137.  
  138.     input = (input > 9) ? 9 : (input < 0) ? 1 : input;
  139.  
  140.     openstuff();
  141.  
  142.     Enable_Abort = 1;
  143.     for(i=0;i<VBeamPos();i++)
  144.         get_rand_point();
  145.  
  146.     point1[0][0] = get_rand_point();    /* Initial start x,y (one endpoint) */
  147.     point1[0][1] = get_rand_point();
  148.  
  149.     point2[0][0] = get_rand_point();
  150.     point2[0][1] = get_rand_point();
  151.  
  152.     for(i=0;i<2;i++){
  153.         direction[i][0] = get_rand_dir();
  154.         direction[i][1] = get_rand_dir();
  155.     }
  156.  
  157.     offset = 0;
  158.     count = 0;
  159.     scale1 = 2;
  160.     scale2 = 2;
  161.  
  162.     rp = win->RPort;
  163.     SetAPen(rp, 1L);
  164.  
  165.     while(1){
  166.  
  167.         Chk_Abort();
  168.  
  169.         if (nice)
  170.             WaitTOF();
  171.  
  172.         msg = GetMsg(win->UserPort);
  173.         if (msg == NULL) {
  174.  
  175.             Move(rp, point1[offset][0], point1[offset][1]);
  176.             Draw(rp, point2[offset][0], point2[offset][1]);
  177.  
  178.             if (boxes) {
  179.                 dobox(offset);
  180.             }
  181.  
  182.             if (checkbounce1(offset)) {
  183.  
  184.                 tempx = direction[0][0];
  185.                 tempy = direction[0][1];
  186.  
  187.                 direction[0][0] = (tempx == -1 & point1[offset][0] < 10) ? 1 : 
  188.                 (tempx == 1 & point1[offset][0] > 630) ? -1 : get_rand_dir();
  189.  
  190.                 direction[0][1] = (tempy == -1 & point1[offset][1] < 10) ? 1 : 
  191.                 (tempy == 1 & point1[offset][1] > 390) ? -1 : get_rand_dir();
  192.  
  193.                 scale1 = get_rand_scale();
  194.             }
  195.  
  196.             if (checkbounce2(offset)) {
  197.  
  198.                 tempx = direction[1][0];
  199.                 tempy = direction[1][1];
  200.  
  201.                 direction[1][0] = (tempx == -1 & point2[offset][0] < 10) ? 1 : 
  202.                 (tempx == 1 & point2[offset][0] > 630) ? -1 : get_rand_dir();
  203.  
  204.                 direction[1][1] = (tempy == -1 & point2[offset][1] < 10) ? 1 : 
  205.                 (tempy == 1 & point2[offset][1] > 390) ? -1 : get_rand_dir();
  206.  
  207.                 scale2 = get_rand_scale();
  208.             }
  209.  
  210.             if (++count > 60000) {
  211.                 count = maxlines;
  212.                 if (trails == 0) {
  213.                     Move(rp, 0, 0);
  214.                     ClearScreen(rp);
  215.                 }
  216.             }
  217.  
  218.             if (++offset > maxlines){
  219.                 LoadRGB4(vp,coltbl,2L);
  220.                 if (coltbl[1] == 0xfff)
  221.                     coltbl[1] = ctemp;
  222.                 else
  223.                     *coltbl[1]++;
  224.                 offset = 0;
  225.             }
  226.  
  227.             /* Erase the oldest line... */
  228.  
  229.             if (count > maxlines-1) {
  230.  
  231.                 newoffset = (offset == maxlines) ? 0 : offset + trails;
  232.                 SetAPen(rp, 0L);
  233.                 Move(rp, point1[newoffset][0], point1[newoffset][1]);
  234.                 Draw(rp, point2[newoffset][0], point2[newoffset][1]);
  235.  
  236.                 if (boxes) {
  237.                     dobox(newoffset);
  238.                 }
  239.  
  240.                 SetAPen(rp, 1L);
  241.             }
  242.  
  243.             /* Calculate the next point */
  244.  
  245.             newoffset = (offset > 0) ? offset-1 : maxlines;
  246.             point1[offset][0] = scale1 * direction[0][0] + point1[newoffset][0];
  247.             point1[offset][1] = scale1 * direction[0][1] + point1[newoffset][1];
  248.  
  249.             point2[offset][0] = scale2 * direction[1][0] + point2[newoffset][0];
  250.             point2[offset][1] = scale2 * direction[1][1] + point2[newoffset][1];
  251.  
  252.         }
  253.         else {
  254.             ReplyMsg(msg);
  255.             msg = GetMsg(win->UserPort);            
  256.             while (msg != NULL) {
  257.                 ReplyMsg(msg);
  258.                 msg = GetMsg(win->UserPort);            
  259.             }
  260.             break;
  261.         }
  262.     }
  263. _abort:
  264.     CloseWindow(win);
  265.     CloseScreen(scrptr);
  266.     CloseLibrary(GfxBase);
  267.     CloseLibrary(IntuitionBase);
  268. }
  269.  
  270. openstuff()
  271. {
  272.     GfxBase = OpenLibrary("graphics.library",0L);
  273.     if (GfxBase == NULL) {
  274.         exit(100);
  275.     }
  276.  
  277.     IntuitionBase = OpenLibrary("intuition.library",0L);
  278.     if (IntuitionBase == NULL) {
  279.         exit(50);
  280.     }
  281.  
  282.     scrptr = OpenScreen(&scr);
  283.     if (scrptr == NULL) {
  284.         exit(30L);
  285.     }
  286.  
  287.     mywin.Screen = scrptr;
  288.     win = OpenWindow(&mywin);
  289.     if (win == NULL) {
  290.         CloseScreen(scrptr);
  291.         exit(20);
  292.     }
  293.     
  294.     ShowTitle(scrptr, FALSE);
  295.     vp = &scrptr->ViewPort;
  296.  
  297.     LoadRGB4(vp,coltbl,2L);
  298.  
  299. }
  300.  
  301. dobox(offset)
  302. register int offset;
  303. {
  304.     register int tempx, tempy;
  305.  
  306.     tempx = 640 - point1[offset][0];
  307.     tempy = 400 - point1[offset][1];
  308.     Draw(rp, tempx, tempy);
  309.  
  310.     tempx = 640 - point2[offset][0];
  311.     tempy = 400 - point2[offset][1];
  312.     Draw(rp, tempx, tempy);
  313.  
  314.     Draw(rp, point1[offset][0], point1[offset][1]);
  315. }
  316.  
  317. long checkbounce1(index)
  318. register int index;
  319. {
  320.  
  321.     return (point1[index][0] > 630) | (point1[index][1] > 390) | 
  322.             (point1[index][0] < 10) | (point1[index][1] < 10);
  323. }
  324.  
  325. long checkbounce2(index)
  326. register int index;
  327. {
  328.     return (point2[index][0] > 630) | (point2[index][1] > 390) | 
  329.             (point2[index][0] < 10) | (point2[index][1] < 10);
  330. }
  331.  
  332. int get_rand_point()
  333. {
  334.     register short temp;
  335.  
  336.     temp = ran();
  337.     if (temp < 0)
  338.         temp = temp * -1;
  339.     temp = temp/319+20;
  340.  
  341.     return temp;
  342. }
  343.  
  344. int get_rand_dir()
  345. {
  346.     register short num;
  347.  
  348.     num = ran((short)3);
  349.  
  350.     return (num < -5000) ? -1 : (num > 5000) ? 1 : 0;
  351. }
  352.  
  353. int get_rand_scale()
  354. {
  355.     register short temp;
  356.  
  357.     temp = ran();
  358.     if (temp < 0)
  359.         temp = temp * -1;
  360.     temp = temp/6560 + (short)input;
  361.  
  362.     return temp;
  363. }
  364.