home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Demo Folder / Fgg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  6.5 KB  |  339 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Blob Manager Demonstration:  Fox, Goose and Grain module
  3.  *
  4.  * 1 July 1986    Paul DuBois
  5.  */
  6.  
  7. # include    "TransSkel.h"
  8.  
  9.  
  10. # include    "BlobMgr.h"
  11. # include    "BlobDemo.h"
  12.  
  13.     
  14. # define    left    0        /* beginning indices of blobs on each river bank */
  15. # define    right    3
  16.  
  17.  
  18. /*
  19.  * positional constants - as yet woefully incomplete
  20.  */
  21.  
  22. # define    xMid        185        /* x-midpoint of screen */
  23. # define    yOffMesg    110        /* y-offset of status message */
  24. # define    yOffBox        160        /* y-offset of text box */
  25.  
  26.  
  27. static WindowPtr        wind;
  28. static BlobSetHandle    donors;
  29. static BlobSetHandle    receptors;
  30. static BlobHandle        fox;
  31. static BlobHandle        goose;
  32. static BlobHandle        grain;
  33. static BlobHandle        whew;
  34. static BlobHandle        rats;
  35. static BlobHandle        farmer;
  36. static short            bank;
  37.  
  38. static ControlHandle    moveCtl;
  39. static ControlHandle    restartCtl;
  40. static Boolean            wait;
  41. static Str255            statusStr = "\p";
  42.     
  43.  
  44. static void
  45. StatusMesg (StringPtr s)
  46. {
  47. Rect    r;
  48.  
  49.     SetRect (&r, xMid - 90, yOffMesg, xMid + 90, yOffMesg + 20);
  50.     TextBox (s+1, (long) s[0], &r, teJustCenter);
  51.     StrCpy (statusStr, s);
  52. }
  53.  
  54.  
  55. static void
  56. GameOver (StringPtr msg, BlobHandle g)
  57. {
  58.     wait = true;
  59.     HideControl (moveCtl);
  60.     ShowControl (restartCtl);
  61.     StatusMesg (msg);
  62.     GlueGlob (g, farmer);
  63. }
  64.  
  65.  
  66. static void
  67. FreezeBank (short bank)
  68. {
  69. short    i;
  70.  
  71.     for (i = bank; i < bank + 3; ++i)
  72.         FreezeBlob (GetBlobHandle (receptors, i));
  73. }
  74.  
  75.  
  76. static void
  77. Restart (void)
  78. {
  79.     StatusMesg ("\p");
  80.     UnglueGlobSet (receptors);
  81.     ThawBlobSet (receptors);
  82.     MoveBlob (farmer, inFullBlob, 100, 63);
  83.     bank = left;
  84.     GlueGlob (fox, GetBlobHandle (receptors, left));
  85.     GlueGlob (goose, GetBlobHandle (receptors, left+1));
  86.     GlueGlob (grain, GetBlobHandle (receptors, left+2));
  87.     FreezeBank (right);
  88.     HideControl (restartCtl);
  89.     SetCTitle (moveCtl, "\pMove Right");
  90.     ShowControl (moveCtl);
  91.     wait = false;
  92.     /*Update (false);*/
  93.     InvalRect (&wind->portRect);
  94. }
  95.  
  96.  
  97. static short
  98. LookFor (short bank, short i)
  99. {
  100. BlobHandle    b;
  101. short j;
  102.  
  103.     for (j = bank; j < bank + 3; j++)
  104.     {
  105.         b = GetBlobHandle (receptors, j);
  106.         if (GetBRefCon (BGlob (b)) == i)
  107.         {
  108.             switch (i)
  109.             {
  110.             case gooseIconRes:
  111.                 GameOver ("\pYour goose is cooked!", rats);
  112.                 return (1);
  113.             case grainIconRes:
  114.                 GameOver ("\pToo bad about that grain!", rats);
  115.                 return (1);
  116.             }
  117.         }
  118.     }
  119.     return (0);
  120. }
  121.  
  122.  
  123. /*
  124.  * Look for pairs that can't be together alone on a bank
  125.  */
  126.  
  127. static void
  128. CheckStatus (short bank)
  129. {
  130. BlobHandle    b;
  131. short i;
  132.  
  133.     for (i = bank; i < bank + 3; i++)
  134.     {
  135.         b = GetBlobHandle (receptors, i);
  136.         switch (GetBRefCon (BGlob (b)))
  137.         {
  138.         case foxIconRes:
  139.             if (LookFor (bank, gooseIconRes))
  140.                 return;
  141.             break;
  142.         case gooseIconRes:
  143.             if (LookFor (bank, grainIconRes))
  144.                 return;
  145.             break;
  146.         }
  147.     }
  148. }
  149.  
  150.  
  151. /*
  152.  * These are created disabled so they aren't drawn.  (Some of them are
  153.  * glued strictly programmatically and are not normally displayed.)
  154.  */
  155.  
  156. static BlobHandle
  157. MakeDonor (short iconNum)
  158. {
  159. BlobHandle    b;
  160. Handle    h;
  161. Rect    r;
  162.  
  163.     b = NewBlob (donors, false, 1, false, (long) iconNum);
  164.     SetRect (&r, 0, 0, 32, 32);
  165.     h = GetIcon (iconNum);
  166.     OpenBlob ();
  167.     PlotIcon (&r, h);
  168.     CloseRectBlob (b, &r, &r);
  169.     return (b);
  170. }
  171.  
  172.  
  173. static void
  174. MakeFarmerBlob (void)
  175. {
  176. Rect    r, r2;
  177. Handle    h;
  178.  
  179.     farmer = NewBlob (receptors, true, 0, false, 0L);
  180.     SetRect (&r, 0, 0, 70, 36);
  181.     SetRect (&r2, 2, 2, 34, 34);
  182.     h = GetIcon (farmerIconRes);
  183.     OpenBlob ();
  184.     FrameRect (&r);
  185.     EraseRect (&r2);
  186.     FrameRect (&r2);
  187.     OffsetRect (&r2, 34, 0);
  188.     PlotIcon (&r2, h);
  189.     OffsetRect (&r2, -34, 0);
  190.     CloseRectBlob (farmer, &r2, &r);    /* will be moved by FggRestart */
  191.     MoveBlob (farmer, inFullBlob, 100, 63);
  192. }
  193.  
  194.  
  195. static void
  196. MakeBankBlobs (short bank, short xPos)
  197. {
  198. BlobHandle    b;
  199. Rect    r;
  200. short    i;
  201.  
  202.     for (i = bank; i < bank + 3; ++i)
  203.     {
  204.         b = NewBlob (receptors, true, 0, false, 0L);
  205.         SetRect (&r, 0, 0, 32, 32);
  206.         OffsetRect (&r, xPos, (short) 30 + (i-bank)*35);
  207.         OpenBlob ();
  208.         EraseRect (&r);
  209.         FrameRect (&r);
  210.         CloseRectBlob (b, &r, &r);
  211.     }
  212. }
  213.  
  214.  
  215. static pascal void
  216. Mouse (Point pt, long t, short mods)
  217. {
  218. BlobHandle        b;
  219. ControlHandle    ctl;
  220. short            i, otherBank;
  221.  
  222.     if (FindControl (pt, wind, &ctl))
  223.     {
  224.         if (TrackControl (ctl, pt, nil))
  225.         {
  226.             if (ctl == restartCtl)
  227.                 Restart ();
  228.             else if (ctl == moveCtl)
  229.             {
  230.                 otherBank = bank;
  231.                 if (bank == left)
  232.                 {
  233.                     bank = right;
  234.                     MoveBlob (farmer, inFullBlob, 125, 63);
  235.                     MoveBlob (farmer, inFullBlob, 150, 63);
  236.                     MoveBlob (farmer, inFullBlob, 175, 63);
  237.                     MoveBlob (farmer, inFullBlob, 200, 63);
  238.                     SetCTitle (moveCtl, "\pMove Left");
  239.                 }
  240.                 else
  241.                 {
  242.                     bank = left;
  243.                     MoveBlob (farmer, inFullBlob, 175, 63);
  244.                     MoveBlob (farmer, inFullBlob, 150, 63);
  245.                     MoveBlob (farmer, inFullBlob, 125, 63);
  246.                     MoveBlob (farmer, inFullBlob, 100, 63);
  247.                     SetCTitle (moveCtl, "\pMove Right");
  248.                 }
  249.                 ThawBlobSet (receptors);
  250.                 FreezeBank (otherBank);
  251.                 ValidRect (&wind->portRect);
  252.                 CheckStatus (otherBank);
  253.             }
  254.         }
  255.     }
  256.     if (wait) return;
  257.     BlobClick (pt, t, donors, receptors);
  258.  
  259.     for (i = right; i < right + 3; ++i)    /* check if all transported */
  260.     {
  261.         if (BGlob (GetBlobHandle (receptors, i)) == nil)
  262.             return;                /* nope */
  263.     }
  264.     GameOver ("\pMission Accomplished.", whew);
  265. }
  266.  
  267.  
  268. static pascal void
  269. Update (Boolean resized)
  270. {
  271. Rect    r;
  272.  
  273.     DrawControls (wind);
  274.     DrawBlobSet (receptors);
  275.     StatusMesg (statusStr);
  276.     MoveTo (90, 10);
  277.     LineTo (90, 140);
  278.     MoveTo (279, 10);
  279.     LineTo (279, 140);
  280.     MoveTo (15, 15);
  281.     DrawString ("\pLeft Bank");
  282.     MoveTo (290, 15);
  283.     DrawString ("\pRight Bank");
  284. }
  285.  
  286.  
  287. static pascal void
  288. Activate (Boolean active)
  289. {
  290.     if (active)
  291.     {
  292.         SetDragRects (wind);
  293.         SetBCPermissions (false, true, false, true, true);
  294.     }
  295. }
  296.  
  297.  
  298. void
  299. FggInit (void)
  300. {
  301. Rect    r;
  302.  
  303.     SkelWindow (wind = GetDemoWind (fggWindRes),
  304.                 Mouse,            /* mouse clicks */
  305.                 nil,            /* key clicks */
  306.                 Update,            /* updates */
  307.                 Activate,        /* activate/deactivate events */
  308.                 nil,            /* close window */
  309.                 DoWClobber,        /* dispose of window */
  310.                 nil,            /* idle proc */
  311.                 false);            /* irrelevant, since no idle proc */
  312.  
  313.     donors = NewBlobSet ();
  314.     receptors = NewBlobSet ();
  315.     
  316.     fox = MakeDonor (foxIconRes);        /* create fox, goose, grain blobs */
  317.     goose = MakeDonor (gooseIconRes);
  318.     grain = MakeDonor (grainIconRes);
  319.     MakeBankBlobs (left, 50);    /* make riverbank blobs - left MUST be 1st */
  320.     MakeBankBlobs (right, 288);
  321.     MakeFarmerBlob ();            /* create Farmer blob - MUST follow banks */
  322.     whew = MakeDonor (whewIconRes);        /* create "Whew!" blob */
  323.     rats = MakeDonor (ratsIconRes);        /* create "Rats!" blob */
  324.  
  325.     /*
  326.      * both controls are at same position - only one is shown at a time.
  327.      */
  328.  
  329.     SetRect (&r, 135, 10, 235, 30);
  330.     moveCtl =
  331.         NewControl (wind, &r, "\pMove Right", false, 0, 0, 0, pushButProc, 0L);
  332.     restartCtl =
  333.         NewControl (wind, &r, "\pRestart", false, 0, 0, 0, pushButProc, 0L);
  334.  
  335.     Restart ();
  336.  
  337.     MakeFrontWind (wind);
  338. }
  339.