home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 049.lha / blobs.c < prev    next >
C/C++ Source or Header  |  1986-11-20  |  8KB  |  288 lines

  1. /************************************************************************
  2.  * Blobs.c
  3.  *
  4.  *            A simple graphics program on the Amiga by
  5.  *            M. Peter Engelbrite
  6.  *            Modified from "Sparks by Scott Ballantyne"
  7.  ************************************************************************/
  8.  
  9. /* Because of a wierd bug in the current Manx C compiler I am using */
  10. /* (versions 1.99G), the comments below have been moved to their current */
  11. /* location, from the end of the include file lines.  Fred Fish, 3/8/86 *
  12.  
  13. /* Not all of these are used -- I include them */
  14. /* 'cause I'm sick of compiler warnings.       */
  15. #include <exec/types.h>
  16. #include <exec/nodes.h>
  17. #include <exec/lists.h>
  18. #include <exec/exec.h>
  19. #include <exec/execbase.h>
  20. #include <exec/ports.h>
  21. #include <exec/devices.h>
  22. #include <exec/memory.h>
  23. #include <hardware/blit.h>
  24. #include <graphics/copper.h>
  25. #include <graphics/regions.h>
  26. #include <graphics/rastport.h>
  27. #include <graphics/gfxbase.h>
  28. #include <graphics/gfxmacros.h>
  29. #include <graphics/gels.h>
  30. #include <intuition/intuition.h>
  31.  
  32. struct IntuitionBase *IntuitionBase = NULL;
  33. struct GfxBase *GfxBase = NULL;
  34.  
  35.  
  36. #define MAXX   640
  37. #define MAXY   200
  38.  
  39. struct NewScreen MyScreen =
  40. { 0,0,MAXX,MAXY,4,0,1,HIRES, CUSTOMSCREEN, NULL, "Blobs", 0,0,};
  41.  
  42.    struct NewWindow DrawWindow = {
  43.       0,0,MAXX,MAXY,
  44.       0,1,
  45.       MENUPICK,
  46.       BORDERLESS | BACKDROP | ACTIVATE,
  47.       NULL,
  48.       NULL,
  49.       NULL,
  50.       NULL,
  51.       NULL,
  52.       0,0,0,0,
  53.       CUSTOMSCREEN,
  54.    };
  55.  
  56. struct Screen *Screen = NULL;
  57. struct Window *Backdrop = NULL;
  58. struct RastPort *DrawRP;
  59. struct ViewPort *DrawVP;
  60. struct IntuiMessage  *message;
  61.  
  62. struct MenuItem OnlyMenuItems[5];
  63. struct IntuiText OnlyMenuText[5];
  64. struct Menu OnlyMenu[1];
  65. static int sparkleflag;
  66.  
  67. #define MAXDOTS  200
  68. #define COLRS     7
  69. #define ERASE     0
  70. static int xp[COLRS][MAXDOTS];
  71. static UBYTE yp[COLRS][MAXDOTS];
  72.  
  73. static int curx[COLRS];
  74. static UBYTE cury[COLRS];
  75.  
  76. main()
  77. {
  78.  
  79.    register int  ndx;
  80.    register int color;
  81.    register int xx, yy;
  82.    register int chase;
  83.    ULONG class;
  84.    USHORT code, ItemNum;
  85.  
  86.    if (!(IntuitionBase =
  87.          (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  88.          exit(1);
  89.  
  90.    if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0))) {
  91.       cleanitup();
  92.       exit(2);
  93.    }
  94.  
  95.    if(!(Screen = (struct Screen *)OpenScreen(&MyScreen))) {
  96.       cleanitup();
  97.       exit(3);
  98.    }
  99.  
  100.    DrawWindow.Screen = Screen;
  101.  
  102.    if(!(Backdrop = (struct Window *)OpenWindow(&DrawWindow))) {
  103.       cleanitup();
  104.       exit(4);
  105.    }
  106.  
  107.  
  108.    DrawRP = Backdrop->RPort;     /* Draw into backdrop window */
  109.    DrawVP = &Screen->ViewPort;   /* Set colors in Screens VP  */
  110.    setcolors();
  111.    sparkleflag = 0;
  112.    chase = 0;
  113.  
  114.    initmenuitems();
  115.    initmenu();
  116.    SetMenuStrip(Backdrop, &OnlyMenu[0]);
  117.  
  118.    for(color = 0; color < COLRS; color++)
  119.      {
  120.        curx[color] = 160;
  121.        cury[color] = 100;
  122.        for(ndx = 0; ndx < MAXDOTS; ndx++)
  123.          {
  124.            xp[color][ndx] = 1;
  125.            yp[color][ndx] = 1;
  126.          }
  127.      }
  128.  
  129.    ndx = 0;
  130.  
  131.    FOREVER {
  132.       while(message = (struct IntuiMessage *)GetMsg(Backdrop->UserPort)) {
  133.          class = message->Class;
  134.          code = message->Code;
  135.          ReplyMsg(message);
  136.  
  137.          if (class == MENUPICK && code != MENUNULL) {
  138.             ItemNum = ITEMNUM( code );
  139.             switch (ItemNum) {
  140.                case 0:
  141.                   ShowTitle(Screen, FALSE);
  142.                   break;
  143.                case 1:
  144.                   ShowTitle(Screen, TRUE);
  145.                   break;
  146.                case 2:
  147.                   sparkleflag = !sparkleflag;
  148.                   break;
  149.                case 3:
  150.                   chase = !chase;
  151.                   break;
  152.                case 4:
  153.                   ClearMenuStrip(Backdrop);
  154.                   cleanitup();
  155.                   exit(0);
  156.             }
  157.          }
  158.       }
  159.  
  160.       for(color = 0; color < COLRS; color++)
  161.         {
  162.           SetAPen(DrawRP, 0);
  163.           WritePixel(DrawRP,xp[color][ndx],yp[color][ndx]);
  164.  
  165.           xx = curx[color] + RangeRand(3) - 1;
  166.           yy = cury[color] + RangeRand(3) - 1;
  167.           if(chase)
  168.             {
  169.               if(RangeRand(color + 2) == 0)
  170.                 {
  171.                   xx = curx[color];
  172.                   yy = cury[color];
  173.                   if(Backdrop->MouseX > xx) xx++; else xx--;
  174.                   if(Backdrop->MouseY > yy) yy++; else yy--;
  175.                 }
  176.             }
  177.  
  178.           if(xx >= MAXX) xx = MAXX - 1;
  179.           if(xx < 1) xx = 1;
  180.           if(yy >= MAXY) yy = MAXY - 1;
  181.           if(yy < 1) yy = 1;
  182.  
  183.           xp[color][ndx] = curx[color] = xx;
  184.           yp[color][ndx] = cury[color] = yy;
  185.           SetAPen(DrawRP, color + 3);
  186.           WritePixel(DrawRP,xx,yy);
  187.  
  188.         }
  189.       if (++ndx >= MAXDOTS)
  190.         ndx = 0;
  191.       if(sparkleflag) newcolors();
  192.    }
  193. }
  194.  
  195. cleanitup()    /* release allocated resources */
  196. {
  197.    if (Backdrop)
  198.       CloseWindow(Backdrop);
  199.    if (Screen)
  200.       CloseScreen(Screen);
  201.    if (GfxBase)
  202.       CloseLibrary(GfxBase);
  203.    if (IntuitionBase)
  204.       CloseLibrary(IntuitionBase);
  205. }
  206.  
  207. newcolors()
  208. {
  209.  
  210.   SetRGB4(DrawVP, RangeRand(12) + 3,
  211.           RangeRand(16), RangeRand(16), RangeRand(16)
  212.          );
  213. }
  214.  
  215. setcolors()
  216. {
  217.    SetRGB4(DrawVP, 0, 0, 0, 0);
  218.    SetRGB4(DrawVP, 1, 3, 5, 10);
  219.    SetRGB4(DrawVP, 2, 15, 15, 15);
  220.    SetRGB4(DrawVP, 3, 15, 6, 0);
  221.    SetRGB4(DrawVP, 4, 14, 3, 0);
  222.    SetRGB4(DrawVP, 5, 15, 11, 0);
  223.    SetRGB4(DrawVP, 6, 15, 15, 2);
  224.    SetRGB4(DrawVP, 7, 11, 15, 0);
  225.    SetRGB4(DrawVP, 8, 5, 13, 0);
  226.    SetRGB4(DrawVP, 9, 0, 0, 15);
  227.    SetRGB4(DrawVP, 10, 3, 6, 15);
  228.    SetRGB4(DrawVP, 11, 7, 7, 15);
  229.    SetRGB4(DrawVP, 12, 12, 0, 14);
  230.    SetRGB4(DrawVP, 13, 15, 2, 14);
  231.    SetRGB4(DrawVP, 15, 13, 11, 8);
  232. }
  233.  
  234. initmenuitems()
  235. {
  236.    short n;
  237.  
  238.    for(n = 0; n < 5; n++) {  /* One struct for each item */
  239.       OnlyMenuItems[n].LeftEdge = 0;
  240.       OnlyMenuItems[n].TopEdge = 10 * n;
  241.       OnlyMenuItems[n].Width = 112;
  242.       OnlyMenuItems[n].Height = 10;
  243.       OnlyMenuItems[n].Flags = ITEMTEXT | ITEMENABLED | HIGHCOMP;
  244.       OnlyMenuItems[n].MutualExclude = 0;
  245.       OnlyMenuItems[n].SelectFill = NULL;
  246.       OnlyMenuItems[n].Command = 0;
  247.       OnlyMenuItems[n].SubItem = NULL;
  248.       OnlyMenuItems[n].NextSelect = 0;
  249.  
  250.       OnlyMenuText[n].FrontPen = 0;
  251.       OnlyMenuText[n].BackPen = 1;
  252.       OnlyMenuText[n].DrawMode = JAM2;
  253.       OnlyMenuText[n].LeftEdge = 0;
  254.       OnlyMenuText[n].TopEdge = 1;
  255.       OnlyMenuText[n].ITextFont = NULL;
  256.       OnlyMenuText[n].NextText = NULL;
  257.    }
  258.    OnlyMenuItems[0].NextItem = &OnlyMenuItems[1]; /* next item */
  259.    OnlyMenuItems[1].NextItem = &OnlyMenuItems[2]; /* next item */
  260.    OnlyMenuItems[2].NextItem = &OnlyMenuItems[3]; /* next item */
  261.    OnlyMenuItems[3].NextItem = &OnlyMenuItems[4]; /* next item */
  262.    OnlyMenuItems[4].NextItem = NULL; /* Last item */
  263.  
  264.    OnlyMenuItems[0].ItemFill = (APTR)&OnlyMenuText[0];
  265.    OnlyMenuItems[1].ItemFill = (APTR)&OnlyMenuText[1];
  266.    OnlyMenuItems[2].ItemFill = (APTR)&OnlyMenuText[2];
  267.    OnlyMenuItems[3].ItemFill = (APTR)&OnlyMenuText[3];
  268.    OnlyMenuItems[4].ItemFill = (APTR)&OnlyMenuText[4];
  269.  
  270.    OnlyMenuText[0].IText = (UBYTE *)"Hide Title Bar";
  271.    OnlyMenuText[1].IText = (UBYTE *)"Show Title Bar";
  272.    OnlyMenuText[2].IText = (UBYTE *)"Flash";
  273.    OnlyMenuText[3].IText = (UBYTE *)"Chase";
  274.    OnlyMenuText[4].IText = (UBYTE *)"QUIT!";
  275. }
  276.  
  277. initmenu()
  278. {
  279.    OnlyMenu[0].NextMenu = NULL;                 /* No more menus */
  280.    OnlyMenu[0].LeftEdge = 0;
  281.    OnlyMenu[0].TopEdge = 0;
  282.    OnlyMenu[0].Width = 85;
  283.    OnlyMenu[0].Height = 10;
  284.    OnlyMenu[0].Flags = MENUENABLED;             /* All items selectable */
  285.    OnlyMenu[0].MenuName = "Actions";
  286.    OnlyMenu[0].FirstItem = OnlyMenuItems;   /* Pointer to first item */
  287. }
  288.