home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d126 / dance.lha / Dance / dance2.c < prev    next >
C/C++ Source or Header  |  1988-01-02  |  7KB  |  278 lines

  1. /****************************************************************
  2.  * Polygon drawing demo by John M. Olsen
  3.  * This demo uses the AreaMove, AreaDraw and AreaEnd Functions.
  4.  * It uses a hold and modify (HAM) screen, and the polygons can be
  5.  * any of the possible 4096 colors.  This is done by using a patterned
  6.  * area fill, using separate patterns to modify the red, green, and blue
  7.  * to get to the correct color as fast as possible.
  8.  *
  9.  * My appologies for the sparceness of comments.  It is hopefully clear
  10.  * what I was doing for the most part.
  11.  *
  12.  * This is meant to be compiled with the Manx compiler, and has not been
  13.  * tried under Lettuce.
  14.  *
  15.  * This software is in the public domain, and you can do whatever you want
  16.  * to or with it.
  17.  *
  18.  * John M. Olsen
  19.  * 1547 Jamestown Drive
  20.  * Salt Lake City, UT  84121
  21.  ****************************************************************/
  22.  
  23. #include <exec/types.h>
  24. #include <graphics/gfxbase.h>
  25. #include <graphics/gfxmacros.h>
  26. #include <graphics/rastport.h>
  27. #include <intuition/intuition.h>
  28. #include <stdio.h>
  29.  
  30. void *OpenLibrary();
  31. struct Window *OpenWindow(), *w, *w2;
  32. struct Screen *OpenScreen(), *s;
  33. struct IntuiMessage *GetMsg(), *msg;
  34.  
  35. struct IntuitionBase *IntuitionBase;
  36. struct GfxBase *GfxBase;
  37.  
  38. BYTE *AllocRaster();
  39. WORD areabuffer[250];
  40. struct TmpRas tmpras;
  41. struct AreaInfo myAreaInfo;
  42.  
  43. USHORT a[] = { 0x9429, 0x4294 };
  44. USHORT b[] = { 0x4294, 0x2942 };
  45.  
  46. /*****************************************************************
  47. definition of the 320 X 200 HAM screen.
  48. *****************************************************************/
  49. struct NewScreen ss =
  50. {                /*****************/
  51.     0,            /* LeftEdge      */
  52.     0,            /* TopEdge       */
  53.     320,            /* Width         */
  54.     200,            /* Height        */
  55.     6,            /* Depth         */
  56.     0,            /* DetailPen     */
  57.     1,            /* BlockPen      */
  58.     HAM,            /* ViewModes     */
  59.     CUSTOMSCREEN,        /* Type          */
  60.     NULL,            /* *Font         */
  61.     (UBYTE *) "A Dancing Polygon",    /* *DefaultTitle */
  62.     NULL,            /* *Gadgets      */
  63.     NULL            /* *CustomBitMap */
  64. };                /*****************/
  65.  
  66. struct NewWindow ww =
  67. {                /****************/
  68.     0,            /* LeftEdge    */
  69.     10,            /* TopEdge    */
  70.     320,            /* Width    */
  71.     190,            /* Height    */
  72.     -1,            /* DetailPen    */
  73.     -1,            /* BlockPen    */
  74.     CLOSEWINDOW,        /* IDCMP    */
  75.     WINDOWCLOSE | ACTIVATE | NOCAREREFRESH | WINDOWDRAG,
  76.     NULL,            /* *FirstGadget    */
  77.     NULL,            /* *CheckMark    */
  78.     (UBYTE *)"By John M. Olsen",    /* *Title    */
  79.     NULL,            /* Screen    */
  80.     NULL,            /* *Bitmap    */
  81.     0,0,0,0,        /* Min/Max w,h    */
  82.     CUSTOMSCREEN        /* Type        */
  83. };
  84. struct NewWindow ww2 =
  85. {                /****************/
  86.     0,            /* LeftEdge    */
  87.     0,            /* TopEdge    */
  88.     320,            /* Width    */
  89.     190,            /* Height    */
  90.     -1,            /* DetailPen    */
  91.     -1,            /* BlockPen    */
  92.     NULL,            /* IDCMP    */
  93.     BACKDROP | NOCAREREFRESH,
  94.     NULL,            /* *FirstGadget    */
  95.     NULL,            /* *CheckMark    */
  96.     (UBYTE *)NULL,        /* *Title    */
  97.     NULL,            /* Screen    */
  98.     NULL,            /* *Bitmap    */
  99.     0,0,0,0,        /* Min/Max w,h    */
  100.     CUSTOMSCREEN        /* Type        */
  101. };
  102.  
  103. main(argc,argv)
  104. int argc;
  105. char *argv[];
  106. {
  107.     setup();
  108.     drawstuff();
  109.     die(0);
  110. }
  111.  
  112. setup()
  113. {
  114.     if(!(GfxBase = OpenLibrary("graphics.library",0l)))
  115.         die(1);
  116.     if(!(IntuitionBase = OpenLibrary("intuition.library", 0l)))
  117.         die(2);
  118.     if(!(s = OpenScreen(&ss)))
  119.         die(4);
  120.     ww.Screen = s;
  121.     ww2.Screen = s;
  122.     if(!(w = OpenWindow(&ww2)))
  123.         die(4);
  124.     if(!(w2 = OpenWindow(&ww)))
  125.         die(4);
  126.  
  127.     /* Set up a buffer for the AreaMove, AreaDraw, and AreaEnd commands */
  128.  
  129.     InitArea(&myAreaInfo, areabuffer, 100l);
  130.     w->RPort->AreaInfo = &myAreaInfo;
  131.     tmpras.RasPtr = (BYTE *) AllocRaster(320l, 200l);
  132.     tmpras.Size = (long) RASSIZE(320l, 200l);
  133.     w->RPort->TmpRas = &tmpras;
  134. }
  135.  
  136. drawstuff()
  137. {
  138.     struct RastPort *r;
  139.     register long loop;
  140.     long red, green, blue, x[10], y[10], dx[10], dy[10];
  141.  
  142.  
  143.     for(loop = 0l; loop < 5l; loop++)
  144.     { /* init the position */
  145.         x[loop] = (long)random(w->Width - w->BorderLeft
  146.             - w->BorderRight) + w->BorderLeft;
  147.         y[loop] = (long)random(w->Height - w->BorderTop
  148.             - w->BorderBottom) + w->BorderTop;
  149.         dx[loop] = dy[loop] = 0L;
  150.     }
  151.     r = w->RPort;
  152.     msg = GetMsg(w2->UserPort);
  153.     while(msg->Class != CLOSEWINDOW)
  154.     {
  155.         if(msg)
  156.             ReplyMsg(msg);
  157.         msg = GetMsg(w2->UserPort);
  158.  
  159.         /* draw with 2 colors this time, and just one the next to */
  160.         /* get all 3 colors modified. */
  161.  
  162.         SetDrMd(r, JAM2);
  163.  
  164.         red += random(3) - 1;
  165.         green += random(3) - 1;
  166.         blue += random(3) - 1;
  167.  
  168.         if(red < 32L) red = 32L;
  169.         if(red > 47L) red = 47L;
  170.         if(green < 48L) green = 48L;
  171.         if(green > 63L) green = 63L;
  172.         if(blue < 16L) blue = 16L;
  173.         if(blue > 31L) blue = 31L;
  174.  
  175.         SetAPen(r, (long)red);
  176.         SetBPen(r, (long)green);
  177.  
  178.         /* use 1L as the last parameter if you want to use a 2 word */
  179.         /* pattern, which will give sevier jaggies. */
  180.  
  181.         SetAfPt(r, &a, 0L);
  182.  
  183.         /* Do a 5 point polygon */
  184.         for(loop = 0l; loop < 5l; loop++)
  185.         {
  186.             dx[loop] += (long)random(3) - 1L;
  187.             dy[loop] += (long)random(3) - 1L;
  188.  
  189.             if(dy[loop] > 5L) dy[loop] = 5L;
  190.             if(dx[loop] > 5L) dx[loop] = 5L;
  191.             if(dy[loop] < -5L) dy[loop] = -5L;
  192.             if(dx[loop] < -5L) dx[loop] = -5L;
  193.  
  194.             if(x[loop] + dx[loop] >  w->Width - w->BorderRight
  195.             || x[loop] + dx[loop] <  w->BorderLeft)
  196.                 dx[loop] *= -1;
  197.  
  198.             if(y[loop] + dy[loop] >  w->Height - w->BorderBottom
  199.             || y[loop] + dy[loop]<  w->BorderTop)
  200.                 dy[loop] *= -1;
  201.  
  202.             x[loop] += dx[loop];
  203.             y[loop] += dy[loop];
  204.  
  205.             if(x[loop] > w->Width - w->BorderRight)
  206.                 x[loop] = w->Width - w->BorderRight;
  207.             if(x[loop] < w->BorderLeft)
  208.                 x[loop] = w->BorderLeft;
  209.             if(y[loop] > w->Height - w->BorderBottom)
  210.                 y[loop] = w->Height - w->BorderBottom;
  211.             if(y[loop] < w->BorderTop)
  212.                 y[loop] = w->BorderTop;
  213.  
  214.             if(loop == 0l)
  215.                 AreaMove(r, x[loop], y[loop]);
  216.             else
  217.                 AreaDraw(r, x[loop], y[loop]);
  218.         }
  219.         AreaEnd(r);
  220.  
  221.         SetDrMd(r, JAM1);
  222.         SetAPen(r, (long)blue);
  223.         SetAfPt(r, &b, 0L);
  224.         for(loop = 0l; loop < 5l; loop++)
  225.         {
  226.             if(loop == 0l)
  227.                 AreaMove(r, x[loop], y[loop]);
  228.             else
  229.                 AreaDraw(r, x[loop], y[loop]);
  230.         }
  231.         AreaEnd(r);
  232. WaitTOF();
  233.         ClipBlit(r, 0L, 10L, w2->RPort, 0L, 10L,
  234.             (long)w->Width, (long)w->Height-10L, 0xc0L);
  235.     }
  236.     if(msg)
  237.         ReplyMsg(msg);
  238. }
  239.  
  240. /****************************************************************
  241.  * I don't think this random number will set any speed records, but
  242.  * it is guaranteed to be as random as you can get.
  243.  ****************************************************************/
  244. random(max)
  245. int max;
  246. {
  247.     static ULONG num;
  248.     ULONG sec, mic;
  249.  
  250.     CurrentTime(&sec, &mic);
  251.     num *= sec;
  252.     num += mic;
  253.     while(num > 32000)
  254.         num = num >> 1;
  255.     return((int)(num % (ULONG)max));
  256. }
  257.  
  258. die(kind)
  259. int kind;
  260. {
  261.     static char *msgs[] = {    "",
  262.         /* err 1 */    "Unable to open graphics library.\n",
  263.         /* err 2 */    "Unable to open intuition library.\n",
  264.         /* err 4 */    "Unable to open a window.\n",
  265.                 "\n"
  266.                 };
  267.  
  268.     if(kind)        puts(msgs[kind]);
  269.     if(tmpras.RasPtr)    FreeRaster(tmpras.RasPtr,320l,200l);
  270.     if(w)            CloseWindow(w);
  271.     if(w2)            CloseWindow(w2);
  272.     if(s)            CloseScreen(s);
  273.     if(GfxBase)        CloseLibrary(GfxBase);
  274.     if(IntuitionBase)    CloseLibrary(IntuitionBase);
  275.     exit(kind);
  276. }
  277.  
  278.