home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / SASC6571.LZX / examples / lines.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-24  |  3.7 KB  |  173 lines

  1. /* Copyright (c) 1989-1993 SAS Institute, Inc, Cary, NC, USA */
  2. /* All Rights Reserved */
  3.  
  4. /*   This is a line drawing demo for the Amiga.    
  5.  
  6.      This program is used as an example program for the Sample CodeProbe
  7.      Session chapter in the version 6.50 debugger documentation.  When
  8.      compiled for that purpose, use the following sc command:
  9.      
  10.          sc debug=sf link def NOERASE lines.c
  11.          
  12.      In any case, compile with def NOERASE.  This will leave the trail of 
  13.      lines visible.  If the option is not used, then old lines will be    
  14.      erased as the lines are drawn across the screen.
  15. */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/nodes.h>
  19. #include <exec/lists.h>
  20. #include <intuition/intuition.h>
  21. #include <graphics/text.h>
  22. #include <proto/exec.h>
  23. #include <proto/graphics.h>
  24. #include <proto/intuition.h>
  25.  
  26. USHORT wakeup;    /* Wake me up for event */
  27. USHORT class;    /* Intu event class */
  28. USHORT code;    /* Intu event code */
  29.  
  30. struct Window *w;
  31. struct RastPort *rp,*cdrp;
  32. struct ViewPort *vp;
  33. struct IntuiMessage *message;
  34. int event(void);
  35. long rand(void);
  36.  
  37. /************************ Window Defines ***********************************/
  38.  
  39. struct NewWindow nw = {
  40.         100,100,            /* Starting corner */
  41.         300,100,        /* Width, height */
  42.         2,1,            /* detail, block pens */
  43.     CLOSEWINDOW | NEWSIZE,        /* IDCMP flags */
  44.     WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE | GIMMEZEROZERO | WINDOWSIZING,
  45.                     /* Window flags */
  46.         NULL,            /* Pointer to first gadget */
  47.         NULL,            /* Pointer to checkmark */
  48.         "Nervous Lines",    /* title */
  49.         NULL,            /* screen pointer */
  50.         NULL,            /* bitmap pointer */
  51.         50,50,640,400,        /* window not sized */
  52.         WBENCHSCREEN        /* type of screen */
  53.         };
  54.  
  55. int x[2],y[2],xd[2],yd[2],co,ox[2][16],oy[2][16],xx[128],xlim,ylim;
  56.  
  57.  
  58. /**
  59. *
  60. * main program
  61. *
  62. **/
  63. void main(void)
  64. {
  65.     register short i;
  66.     register int k,co;
  67.     register long j;
  68.  
  69. /************************ Set-Up routines **********************************/
  70. /*      Let the auto init code open our libraries for us.                  */
  71.  
  72.     w = OpenWindow(&nw);
  73.     rp = w->RPort;            /* Get the raster port pointer */
  74.     vp = &w->WScreen->ViewPort;    /* Get the view port pointer */
  75.     SetAPen(rp,3);            /* Set foreground pen to white */
  76.     SetDrMd(rp,JAM1);        /* Draw with foreground pen */
  77.  
  78.     xlim = w->Width;
  79.     ylim = w->Height;
  80.     for(i=0;i<2;i++)
  81.     {
  82.         x[i] = rand() % xlim + 1;
  83.         y[i] = rand() % ylim + 1;
  84.         xd[i] = rand() % 10 + 1;
  85.         yd[i] = rand() % 10 + 1;
  86.     }
  87.     co = 1;
  88.     j = 0;
  89.     do {
  90. #ifndef NOERASE
  91.         SetAPen(rp,0);
  92.         Move(rp,ox[0][j & 15],oy[0][j & 15]);
  93.         Draw(rp,ox[1][j & 15 ],oy[1][j & 15]);
  94. #endif
  95.         SetAPen(rp,co);
  96.         Move(rp,x[0],y[0]);
  97.         Draw(rp,x[1],y[1]);
  98.         if((rand() & 127) < 2)
  99.         {
  100.             ++co;
  101.             if(co > 3)    co = 1;
  102.             SetAPen(rp,co);
  103.         }
  104.         for(i=0;i<2;i++)
  105.         {
  106.             ox[i][(j+10) & 15] = x[i];
  107.             oy[i][(j+10) & 15] = y[i];
  108.             x[i] += xd[i];
  109.             y[i] += yd[i];
  110.             if(x[i] < 2)
  111.             {
  112.                 x[i] = 2;
  113.                 xd[i] = -xd[i];
  114.             }
  115.             else if(x[i] > xlim)
  116.             {
  117.                 x[i] = xlim;
  118.                 xd[i] = -xd[i];
  119.             }
  120.             if(y[i] < 2)
  121.             {
  122.                 y[i] = 2;
  123.                 yd[i] = -yd[i];
  124.             }
  125.             else if(y[i] > ylim)
  126.             {
  127.                 y[i] = ylim;
  128.                 yd[i] = -yd[i];
  129.             }
  130.             if(((rand() >> 5) & 127) < 2)
  131.             {
  132.                 if(xd[i] < 1)     k = 1;
  133.                 xd[i] = (rand() >> 5) & 7;
  134.                 if(k == 1)    xd[i] = -xd[i];
  135.                 k = 0;
  136.             } 
  137.             if(((rand() >> 5) & 255) < 50)
  138.             {
  139.                 if(yd[i] < 1)    k = 1;
  140.                 yd[i] = (rand() >> 5) & 7;
  141.                 if(k == 1)    yd[i] = -yd[i];
  142.                 k = 0;
  143.             }
  144.         }
  145.         ++j;
  146.         if(w->UserPort->mp_SigBit)
  147.         {
  148.             message = (struct IntuiMessage *)GetMsg(w->UserPort);
  149.             if(message != NULL)
  150.             {
  151.                 class = message->Class;
  152.                 code = message->Code;
  153.                 ReplyMsg((struct Message *)message);
  154.             }
  155.         }
  156.     } while(event());
  157.     CloseWindow(w);
  158. }
  159.  
  160. int event()
  161. {
  162.     switch(class)
  163.     {
  164.         case CLOSEWINDOW:
  165.             return(0);
  166.         case NEWSIZE:
  167.             xlim = w->Width;
  168.             ylim = w->Height;
  169.             return(1);
  170.     }
  171.     return(1);
  172. }
  173.