home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 179.lha / ClipLines / lbnc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-28  |  10.7 KB  |  526 lines

  1.  
  2. /******************************************************************************
  3. *
  4. *  lbnc.c - Copyright 1988 Commodore-Amiga, Inc.
  5. *
  6. *    Source Control
  7. *    --------------
  8. *    $Header: lbnc.c,v 36.0 88/05/18 20:18:42 bart Exp $
  9. *
  10. *    $Locker: bart $
  11. *
  12. *    $Log:    lbnc.c,v $
  13. *   Revision 36.0  88/05/18  20:18:42  bart
  14. *   added to rcs for updating
  15. *   
  16. *
  17. ******************************************************************************/
  18.  
  19.  
  20. #include <exec/types.h>
  21. #include <exec/nodes.h>
  22. #include <exec/lists.h>
  23. #include <exec/ports.h>
  24. #include <graphics/gfx.h>
  25. #include <graphics/clip.h>
  26. #include <graphics/view.h>
  27. #include <graphics/rastport.h>
  28. #include <graphics/layers.h>
  29. #include <intuition/intuition.h>
  30.  
  31. #define COPY    NANBC|NABC|ABNC|ABC
  32.  
  33. long seed[] = {0xB807C324, 0x9E8732B5, 0x4E50957BC};
  34.  
  35.  
  36. /* random number generator */
  37.  
  38. rand(i)
  39. register short i;
  40. {
  41.     return(seed[i] = seed[i] < 0 ? seed[i] << 1 ^ 0x1D872B41 : seed[i] << 1);
  42. }
  43.  
  44. #define RWidth    Get_RWidth(w)
  45. #define RHeight    Get_RHeight(w)
  46. #define MAX_CLOSEWINDOW 1
  47.  
  48. /* #define DEBUG */
  49. /* #define KPRINTF */
  50.  
  51. #ifdef KPRINTF
  52. #define printf kprintf
  53. #endif
  54.  
  55. #ifndef DEBUG
  56. #define printf ;
  57. #endif
  58.  
  59. int close_count;
  60.  
  61. extern struct Gadget *vgadget_ptr;
  62. extern struct Gadget *hgadget_ptr;
  63.  
  64. struct BitMap *savesuperbitmap;
  65.  
  66. #define CLIPPING
  67.  
  68. clip_for_me(window)
  69. struct Window *window;
  70. {
  71. #ifdef CLIPPING
  72.         struct Rectangle Bounds;
  73.         struct Region *clipregion = NewRegion();
  74.  
  75.         if(clipregion)
  76.         {
  77.  
  78.             Bounds.MinX = 0;
  79.             Bounds.MinY = 0;
  80.             Bounds.MaxX = window->Width - window->BorderRight;
  81.             Bounds.MaxY = window->Height - window->BorderBottom;
  82.  
  83.             /* include only "positive" regions */
  84.  
  85.             if((Bounds.MinX < Bounds.MaxX)&&(Bounds.MinY < Bounds.MaxY))
  86.             {
  87.                 printf("orrectregion:\n");
  88.                         printf("Bounds.MinX = %ld\n",Bounds.MinX);
  89.                         printf("Bounds.MinY = %ld\n",Bounds.MinY);
  90.                         printf("Bounds.MaxX = %ld\n",Bounds.MaxX);
  91.                         printf("Bounds.MaxY = %ld\n",Bounds.MaxY);
  92.                 OrRectRegion(clipregion,&Bounds);
  93.             }
  94.  
  95.             /* calculate the rectangle that holds that little piece */
  96.             /* above the size gadget */
  97.  
  98.             if(TRUE)
  99.             {
  100.                 USHORT exclude = SYSGADGET;
  101.                 struct Gadget *g;
  102.                 for (g = window->FirstGadget; g ; g = g->NextGadget)
  103.                 {
  104.                     if (!(g->GadgetType & exclude))
  105.                     {
  106.                         Bounds.MinX = g->LeftEdge-window->BorderLeft;
  107.                         Bounds.MinY = g->TopEdge-window->BorderTop;
  108.                         Bounds.MaxX = g->LeftEdge+g->Width-1;
  109.                         Bounds.MaxY = g->TopEdge+g->Height-1;
  110.  
  111.                         printf("!(g->GadgetType & exclude):\n");
  112.                         
  113.                         printf("g->Flags== %04lx:\n",g->Flags);
  114.                         if(g->Flags & GRELWIDTH)
  115.                         {
  116.                             printf("(g->Flags & GRELWIDTH):\n");
  117.                             Bounds.MaxX = g->Width+window->Width;
  118.                         }
  119.                         else
  120.                         {
  121.                             printf("!(g->Flags & GRELWIDTH):\n");
  122.                             Bounds.MaxX = Bounds.MinX+g->Width-1;
  123.                         }
  124.                         if(g->Flags & GRELHEIGHT)
  125.                         {
  126.                             printf("(g->Flags & GRELHEIGHT):\n");
  127.                             Bounds.MaxY = g->Height+window->Height;
  128.                         }
  129.                         else
  130.                         {
  131.                             printf("!(g->Flags & GRELHEIGHT):\n");
  132.                             Bounds.MaxY = Bounds.MinY+g->Height-1;
  133.                         }
  134.                         printf("Bounds.MinX = %ld\n",Bounds.MinX);
  135.                         printf("Bounds.MinY = %ld\n",Bounds.MinY);
  136.                         printf("Bounds.MaxX = %ld\n",Bounds.MaxX);
  137.                         printf("Bounds.MaxY = %ld\n",Bounds.MaxY);
  138.                         if(g->Flags & GRELRIGHT)
  139.                         {
  140.                             printf("(g->Flags & GRELRIGHT):\n");
  141.                             Bounds.MinX += window->Width;
  142.                             Bounds.MaxX += window->Width;
  143.                         }
  144.                         if(g->Flags & GRELBOTTOM)
  145.                         {
  146.                             printf("(g->Flags & GRELBOTTOM):\n");
  147.                             Bounds.MinY += window->Height;
  148.                             Bounds.MaxY += window->Height;
  149.                         }
  150.  
  151.                         if (!(g->GadgetType & GZZGADGET))
  152.                         {
  153.                             Bounds.MinX -= window->BorderRight+1;
  154.                             Bounds.MaxX -= window->BorderRight+1;
  155.                             Bounds.MinY -= window->BorderBottom
  156.                                          + window->BorderTop
  157.                                          + 1;
  158.                             Bounds.MaxY -= window->BorderBottom
  159.                                          + window->BorderTop
  160.                                          + 1;
  161.                         }
  162.  
  163.                         printf("test inversion:\n");
  164.                         printf("Bounds.MinX = %ld\n",Bounds.MinX);
  165.                         printf("Bounds.MinY = %ld\n",Bounds.MinY);
  166.                         printf("Bounds.MaxX = %ld\n",Bounds.MaxX);
  167.                         printf("Bounds.MaxY = %ld\n",Bounds.MaxY);
  168.  
  169.                         /* include only "positive" regions */
  170.                         if((Bounds.MinX < Bounds.MaxX)&&
  171.                            (Bounds.MinY < Bounds.MaxY))
  172.                         {
  173.  
  174.                             printf("xorrectregion:\n");
  175.                             printf("Bounds.MinX = %ld\n",Bounds.MinX);
  176.                             printf("Bounds.MinY = %ld\n",Bounds.MinY);
  177.                             printf("Bounds.MaxX = %ld\n",Bounds.MaxX);
  178.                             printf("Bounds.MaxY = %ld\n",Bounds.MaxY);
  179.                                XorRectRegion(clipregion,&Bounds);
  180.                         }
  181.                     }
  182.                 }
  183.             }
  184.         /* swap new one in */
  185.         clipregion = InstallClipRegion(window->WLayer,clipregion);
  186.         if (clipregion)
  187.         {
  188.             DisposeRegion(clipregion);
  189.         }
  190.     }
  191. #endif
  192. }
  193.  
  194. lbnc(w)
  195. struct RastPort *w;
  196. {
  197.     register long i;
  198.     short lx,ly,lvx,lvy;
  199.     short rx,ry,rvx,rvy;
  200.     short clrDepth,linDepth;
  201.     struct Layer *l;
  202.     int counter;
  203.     int lcounter;
  204.     int xoffset,yoffset;
  205.     struct Window *window;
  206.     struct IntuiMessage *message;
  207.  
  208.     close_count = MAX_CLOSEWINDOW;
  209.     counter = 0;
  210.     lcounter = 0;
  211.     l = w->Layer;
  212.     lx = 0;    ly = 0;
  213.     rx = 30;    ry = 10;
  214.     lvx = 2;    lvy = 3;
  215.     rvx = 5;    rvy = 7;
  216.     clrDepth = 320;
  217.     linDepth = 160;
  218.     SetDrMd(w,1);
  219.  
  220.  
  221.     xoffset = Get_xoffset(w);
  222.     yoffset = Get_yoffset(w);
  223.  
  224. #ifdef DEBUG
  225.     printf("xoffset = %ld yoffset = %ld\n",xoffset,yoffset);
  226. #endif
  227.     if (l)    window = l->Window;
  228.  
  229.     clip_for_me(window);
  230.  
  231. restart:
  232.     while ((message = (struct IntuiMessage *)GetMsg(window->UserPort)) == 0)
  233.     {
  234.         if (clrDepth-- == 0)
  235.         {
  236.             clrDepth = 320;
  237.             SetAPen(w,w->FgPen+1);
  238.         }
  239.  
  240.         if (linDepth-- == 0)
  241.         {
  242.             newvel(&lvx);
  243.             newvel(&lvy);
  244.             newvel(&rvx);
  245.             newvel(&rvy);
  246.             linDepth = 320;
  247.         }
  248.  
  249.         Move(w,xoffset+lx,yoffset+ly);
  250.         Draw(w,xoffset+rx,yoffset+ry);
  251.         lx += lvx;    ly += lvy;
  252.         rx += rvx;    ry += rvy;
  253.         if (lx<0)    zbounce(&lx,&lvx);
  254.         if (rx<0)    zbounce(&rx,&rvx);
  255.         if (ly<0)    zbounce(&ly,&lvy);
  256.         if (ry<0)    zbounce(&ry,&rvy);
  257.         if (lx>RWidth-1)    mbounce(&lx,&lvx,RWidth-1);
  258.         if (ly>RHeight-1)     mbounce(&ly,&lvy,RHeight-1);
  259.         if (rx>RWidth-1)    mbounce(&rx,&rvx,RWidth-1);
  260.         if (ry>RHeight-1)    mbounce(&ry,&rvy,RHeight-1);
  261. endit:
  262.     }
  263.  
  264.     switch (message->Class)
  265.     {
  266.  
  267.         case CLOSEWINDOW :
  268.         {
  269.             ReplyMsg(message);
  270.             return;
  271.         }
  272.  
  273.         case GADGETDOWN:
  274.         { 
  275.             struct IntuiMessage copymsg;
  276.  
  277.             /* copy all fields */
  278.             copymsg = *message;
  279.  
  280.             /* reply to original */
  281.             ReplyMsg(message);
  282.  
  283.             /* smooth-scroll until gadget is released */
  284.             {
  285.                 ULONG old_idcmp_flags;
  286.                 ULONG new_idcmp_flags;
  287.  
  288.                 /* save old idcmp_flags */
  289.  
  290.                 old_idcmp_flags = copymsg.IDCMPWindow->IDCMPFlags;
  291.  
  292.                 /* listen for ticks as long as gadget is down */
  293.  
  294.                 new_idcmp_flags = (old_idcmp_flags | INTUITICKS);
  295.  
  296.                 ModifyIDCMP(copymsg.IDCMPWindow,new_idcmp_flags);
  297.  
  298.                 /* loop for intuiticks messages */
  299.  
  300.                 {
  301.                     struct IntuiMessage *loopmsg;
  302.  
  303.                     while((loopmsg =  (struct IntuiMessage *) WaitPort(copymsg.IDCMPWindow->UserPort)) && (loopmsg->Class == INTUITICKS))
  304.                     {
  305.                         if(loopmsg = (struct IntuiMessage *)GetMsg(copymsg.IDCMPWindow->UserPort))
  306.                         {
  307.                             ReplyMsg(loopmsg);
  308.  
  309.                             do_gadget_up(©msg);
  310.  
  311.                             /* draw after smooth scrolling */
  312.                             {
  313.  
  314.                                 if (clrDepth-- == 0)
  315.                                 {
  316.                                     clrDepth = 320;
  317.                                     SetAPen(w,w->FgPen+1);
  318.                                 }
  319.  
  320.                                 if (linDepth-- == 0)
  321.                                 {
  322.                                     newvel(&lvx);
  323.                                     newvel(&lvy); 
  324.                                     newvel(&rvx);
  325.                                     newvel(&rvy);
  326.                                     linDepth = 320;
  327.                                 }
  328.  
  329.                                 Move(w,xoffset+lx,yoffset+ly);
  330.                                 Draw(w,xoffset+rx,yoffset+ry);
  331.                                 lx += lvx;    ly += lvy;
  332.                                 rx += rvx;    ry += rvy;
  333.                                 if (lx<0)    zbounce(&lx,&lvx);
  334.                                 if (rx<0)    zbounce(&rx,&rvx);
  335.                                 if (ly<0)    zbounce(&ly,&lvy);
  336.                                 if (ry<0)    zbounce(&ry,&rvy);
  337.                                 if (lx>RWidth-1)    mbounce(&lx,&lvx,RWidth-1);
  338.                                 if (ly>RHeight-1)     mbounce(&ly,&lvy,RHeight-1);
  339.                                 if (rx>RWidth-1)    mbounce(&rx,&rvx,RWidth-1);
  340.                                 if (ry>RHeight-1)    mbounce(&ry,&rvy,RHeight-1);
  341.  
  342.                             }
  343.                             /* done drawing */
  344.  
  345.                         }
  346.  
  347.                     }
  348.  
  349.                     /* next message was not a tick */
  350.                 }
  351.  
  352.                 /* restore old idcmp_flags */
  353.  
  354.                 ModifyIDCMP(copymsg.IDCMPWindow,old_idcmp_flags);
  355.           }
  356.  
  357.         }
  358.         break;
  359.  
  360.         case GADGETUP: 
  361.         {
  362.             struct IntuiMessage copymsg;
  363.  
  364.             /* copy all fields */
  365.             copymsg = *message;
  366.  
  367.             /* reply to original */
  368.             ReplyMsg(message);
  369.  
  370.             /* process message */
  371.             do_gadget_up(©msg);
  372.         }
  373.         break;
  374.  
  375.         case SIZEVERIFY:
  376.         {
  377.             ReplyMsg(message);
  378.  
  379.             CopySBitMap(window->RPort->Layer);
  380.  
  381.             /* savesuperbitmap = window->RPort->Layer->SuperBitMap; */
  382.             /* window->RPort->Layer->SuperBitMap = NULL; */
  383.  
  384.             /* wait for newsize */
  385.             WaitPort(window->UserPort);
  386.         }
  387.         break;
  388.  
  389.         case NEWSIZE:
  390.         {
  391.             struct IntuiMessage copymsg;
  392.             extern struct Gadget my_gadget;
  393.  
  394.             /* copy all fields */
  395.             copymsg = *message;
  396.  
  397.             /* reply to original */
  398.             ReplyMsg(message);
  399.  
  400.             /* window->RPort->Layer->SuperBitMap = savesuperbitmap; */
  401.             /* savesuperbitmap = NULL; */
  402.             clip_for_me(window);
  403.             RefreshWindowFrame(window);
  404.  
  405.             /* provide vertical gadget address */
  406.             copymsg.IAddress = vgadget_ptr;
  407.  
  408.             /* set vertical scroll */
  409.             do_gadget_up(©msg);
  410.  
  411.             /* bart - 04.23.86 */
  412.             /* re-calculate vertical pot body for new size */
  413.             {
  414.                 struct Window *w; 
  415.                 struct RastPort *rp;
  416.                 struct Layer *l;
  417.  
  418.                 if(w = copymsg.IDCMPWindow)
  419.                 {
  420.                     if(rp = w->RPort)
  421.                     {
  422.                         if(l = rp->Layer)
  423.                         {
  424.                             struct PropInfo *pi;
  425.                             UWORD sb_height;
  426.                             ULONG rel_body;
  427.                  
  428.                             pi = (struct PropInfo *)vgadget_ptr->SpecialInfo;
  429.                                                 
  430.                             sb_height = l->SuperBitMap->Rows;
  431.  
  432.                             rel_body=(MAXPOT*(usable_height(w)))/(sb_height);
  433.  
  434.                             ModifyProp(vgadget_ptr,
  435.                                        window,
  436.                                        NULL,
  437.                                        pi->Flags,
  438.                                        pi->HorizPot,
  439.                                        pi->VertPot,
  440.                                        pi->HorizBody,
  441.                                        rel_body);
  442.                         }
  443.                     }
  444.                 }
  445.             }
  446.  
  447.             /* provide vertical gadget address */
  448.             copymsg.IAddress = hgadget_ptr;
  449.  
  450.             /* set horizontal scroll */
  451.             do_gadget_up(©msg);
  452.  
  453.             /* bart - 04.23.86 */
  454.             /* re-calculate horizontal pot body for new size */
  455.  
  456.             {
  457.                 struct Window *w; 
  458.                 struct RastPort *rp;
  459.                 struct Layer *l;
  460.  
  461.                 if(w = copymsg.IDCMPWindow)
  462.                 {
  463.                     if(rp = w->RPort)
  464.                     {
  465.                         if(l = rp->Layer)
  466.                         {
  467.                             struct PropInfo *pi;
  468.                             UWORD bb_width;
  469.                             ULONG rel_body;
  470.                  
  471.                             pi = (struct PropInfo *)hgadget_ptr->SpecialInfo;
  472.                                                     
  473.                             bb_width = l->SuperBitMap->BytesPerRow << 3;
  474.  
  475.                             rel_body=(MAXPOT*(usable_width(w)))/(bb_width);
  476.  
  477.                             ModifyProp(hgadget_ptr,
  478.                                        window,
  479.                                        NULL,
  480.                                        pi->Flags,
  481.                                        pi->HorizPot,
  482.                                        pi->VertPot,
  483.                                        rel_body,
  484.                                        pi->VertBody);
  485.                         }
  486.                     }
  487.                 }
  488.             }
  489.  
  490.         }
  491.         break;
  492.  
  493.         default:
  494.         {
  495.             ReplyMsg(message);
  496.         }
  497.         break;
  498.  
  499.     }
  500.  
  501.     goto restart;
  502.  
  503. }
  504.  
  505. newvel(v)
  506. short *v;
  507. {
  508.     *v = (15 & rand(0)) - 7;
  509. }
  510.  
  511. zbounce(p,v)
  512. short *p,*v;
  513. {
  514.     *p = -*p;
  515.     *v = -*v;
  516. }
  517.  
  518. mbounce(p,v,c)
  519. short *p,*v;
  520. short c;
  521. {
  522.     *p = 2*c - *p;
  523.     *v = -*v;
  524. }
  525.  
  526.