home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / lineclip.lha / CLIPTEST.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-29  |  4.3 KB  |  166 lines

  1. /* Cliptest.c, by John Schultz, a modification of: */
  2.  
  3. /* This is a line drawing demo for the Commodore/Amiga  */
  4. /* Written by    John Riley, Lattice, Inc.        */
  5. /*                             */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/nodes.h>
  9. #include <exec/lists.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/text.h>
  12. #include <proto/exec.h>
  13. #include <proto/graphics.h>
  14. #include <proto/intuition.h>
  15. #include <hardware/custom.h>
  16.  
  17. /***************** This is all you need for the FC clipper **************/
  18.  
  19. typedef struct {short px,py,qx,qy;} line;
  20.  
  21. extern short __asm clipline(register __a0 line * l);
  22.  
  23. extern unsigned short far minX,far minY,far maxX,far maxY;
  24.  
  25. /************************************************************************/
  26.  
  27. line l;
  28.  
  29. extern struct Custom far custom;
  30.                                             
  31. USHORT wakeup;    /* Wake me up for event */
  32. USHORT class;    /* Intu event class */
  33. USHORT code;    /* Intu event code */
  34.  
  35. struct Window *w;
  36. struct RastPort *rp,*cdrp;
  37. struct ViewPort *vp;
  38. struct IntuiMessage *message;
  39. int event(void);
  40. long rand(void);
  41. void srand(int);
  42.  
  43. /************************ Window Defines ********************************/
  44.  
  45. struct NewWindow nw = {
  46.         0,0,            /* Starting corner */
  47.         80,40,            /* Width, height */
  48.         2,1,            /* detail, block pens */
  49.     CLOSEWINDOW | NEWSIZE,        /* IDCMP flags */
  50.     WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO | WINDOWSIZING,
  51.                     /* Window flags */
  52.         NULL,            /* Pointer to first gadget */
  53.         NULL,            /* Pointer to checkmark */
  54.         "FC Clipper Test",    /* title */
  55.         NULL,            /* screen pointer */
  56.         NULL,            /* bitmap pointer */
  57.         0,0,640,400,        /* window not sized */
  58.         WBENCHSCREEN        /* type of screen */
  59.         };
  60.  
  61. int co,xlim,ylim;
  62.  
  63. short centerx,centery;
  64.  
  65. main(int argc,char * argv[])
  66. {
  67.   unsigned short linesize,halflinesize;
  68.  
  69.   if (argc > 1) {
  70.     linesize = atoi(argv[1]);
  71.     if (linesize > 32767) {
  72.       printf("Maximum line size exceeded, using 32767 (max).\n");
  73.       linesize = 32767;
  74.     }
  75.   } else {
  76.     printf("USAGE: cliptest <linesize>\n");
  77.     printf("  using default linesize of 500.\n");
  78.     linesize = 500;
  79.   } 
  80.   halflinesize = linesize >> 1;
  81.  
  82.  
  83. /************************ Set-Up routines **********************************/
  84.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  85.     if(GfxBase == NULL) return;
  86.     IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0);
  87.     if(IntuitionBase == NULL) 
  88.         {
  89.         CloseLibrary((struct Library *)GfxBase);
  90.         return;
  91.         }
  92.     w = OpenWindow(&nw);
  93.     rp = w->RPort;            /* Get the raster port pointer */
  94.     vp = &w->WScreen->ViewPort;    /* Get the view port pointer */
  95.     SetAPen(rp,3);            /* Set foreground pen to white */
  96.     SetDrMd(rp,JAM1);        /* Draw with foreground pen */
  97.  
  98.     minX = 0;
  99.     minY = 0;
  100.     maxX = w->Width-1;
  101.     maxY = w->Height-1;
  102.  
  103.     centerx = w->Width >> 1;
  104.     centery = w->Height >> 1;
  105.  
  106.     co = 1;
  107.     do {
  108.  
  109.  /**************************  FC clipper test code *************************/
  110.  
  111.                 srand(custom.vhposr); /* video beam position */
  112.         SetAPen(rp,co);
  113.                 co = (co+1) & 3;
  114.  
  115.                 l.px = (rand() & linesize) - halflinesize + centerx;
  116.                 l.py = (rand() & linesize) - halflinesize + centery;
  117.                 l.qx = (rand() & linesize) - halflinesize + centerx;
  118.                 l.qy = (rand() & linesize) - halflinesize + centery;
  119.  
  120.                 if (clipline(&l)) {
  121.                   if ((l.px < minX) || (l.px > maxX)
  122.                    || (l.py < minY) || (l.py > maxY)  
  123.                    || (l.qx < minX) || (l.qx > maxX)
  124.                    || (l.qy < minY) || (l.qy > maxY)) {
  125.                     printf("FC Clip Error.\n");
  126.                   } else {  
  127.                     Move(rp,l.px,l.py);
  128.             Draw(rp,l.qx,l.qy);
  129.                   }
  130.                 }
  131.  
  132.  /************************** End FC clipper test code ***********************/
  133.  
  134.         if(w->UserPort->mp_SigBit)
  135.         {
  136.             message = (struct IntuiMessage *)GetMsg(w->UserPort);
  137.             if(message != NULL)
  138.             {
  139.                 class = message->Class;
  140.                 code = message->Code;
  141.                 ReplyMsg((struct Message *)message);
  142.             }
  143.         }
  144.     } while(event());
  145.     CloseWindow(w);
  146.     CloseLibrary((struct Library *)GfxBase);
  147.     CloseLibrary((struct Library *)IntuitionBase);
  148. }
  149.  
  150. int event()
  151. {
  152.     switch(class)
  153.     {
  154.         case CLOSEWINDOW:
  155.             return(0);
  156.         case NEWSIZE:
  157.                 maxX = w->Width-1;
  158.             maxY = w->Height-1;
  159.              centerx = w->Width >> 1;
  160.             centery = w->Height >> 1;
  161.             return(1);
  162.  
  163.     }
  164.     return(1);
  165. }
  166.