home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Boxes / Window.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-21  |  3.2 KB  |  166 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Window.c - "Boxes" application
  3.  */
  4.  
  5. # include    "TransSkel.h"
  6.  
  7. # include    "Boxes.h"
  8.  
  9.  
  10.  
  11. static WindowPtr    wind = (WindowPtr) nil;
  12.  
  13.  
  14. void
  15. ForceWindowUpdate (void)
  16. {
  17.     InvalRect (&wind->portRect);
  18. }
  19.  
  20.  
  21. /*
  22.  * Got an activate or deactivate.
  23.  */
  24.  
  25. static pascal void
  26. Activate (Boolean active)
  27. {
  28. }
  29.  
  30.  
  31. /*
  32.  * Current grafPort will be set to wind by TransSkel, no GetPort() necessary.
  33.  */
  34.  
  35. static pascal void
  36. Update (Boolean resized)
  37. {
  38.     EraseRect (&wind->portRect);
  39.     DrawBoard ();
  40.     DrawScoreBoard ();
  41. }
  42.  
  43.  
  44. static pascal void
  45. Mouse (Point pt, long t, short mods)
  46. {
  47. short    h, v;
  48. short    squares = 0;
  49. Boolean    gotLinkClick = false;
  50. short    player = GetPlayer ();    /* save before it changes */
  51.  
  52.     if (SidesLeft () == 0)
  53.         return;
  54.  
  55.     if (VLinkHitTest (pt, &h, &v))
  56.     {
  57.         gotLinkClick = true;
  58.         squares = SetVSide (h, v, player);
  59.     }
  60.     else if (HLinkHitTest (pt, &h, &v))
  61.     {
  62.         gotLinkClick = true;
  63.         squares = SetHSide (h, v, player);
  64.     }
  65.  
  66.     if (!gotLinkClick)
  67.         return;
  68.  
  69.     /*
  70.      * A link was completed.  If a square was not completed, go to next player.
  71.      * Otherwise the same player can keep playing - unless the game is now done.
  72.      * In all cases optimize for 1-player to avoid extra redrawing.
  73.      */
  74.     if (squares == 0 && GetPlayerCount () > 1)    /* no squares completed, next player */
  75.         NextPlayer ();
  76.     else                            /* squares completed, stay with same player... */
  77.     {
  78.         IncrementScore (player, squares);
  79.         if (SidesLeft () > 0)
  80.             DrawScore (player);    /* draw player's new score */
  81.         else
  82.             DrawScoreBoard ();        /* ...unless done with game (draw winners) */
  83.     }
  84. }
  85.  
  86.  
  87. static pascal void
  88. Clobber (void)
  89. {
  90.     HideWindow (wind);
  91.     DisposeWindow (wind);
  92. }
  93.  
  94.  
  95.  
  96. void
  97. NewGame (void)
  98. {
  99. Rect    r, r2;
  100. short    sbWidth;
  101.  
  102.     /*
  103.         Figure out new window size
  104.     */
  105.  
  106.     CalcBoardSize (&r);
  107.     CalcScoreBoardSize (&r2);
  108.     sbWidth = r2.right;
  109.     PlaceScoreBoard (hPad + r.right + hPad, vPad);
  110.     if (r.bottom < r2.bottom)            /* window needs to be at least */
  111.         r.bottom = r2.bottom;            /* as deep as the scoreboard */
  112.     r.bottom += 2 * vPad;
  113.     r.right += r2.right + 3 * hPad;
  114.     SizeWindow (wind, r.right, r.bottom, true);
  115.     InvalRect (&wind->portRect);
  116.  
  117.     InitializeBoard ();
  118.     InitializeScoreBoard ();
  119.     SetOptionsMenuPlayerCount ();
  120. }
  121.  
  122.  
  123. /*
  124.  * Initialize window.  Size of window rect and location of button are
  125.  * irrelevant at this point.  The window will be sized and the button
  126.  * placed in NewGame ().
  127.  */
  128.  
  129. void
  130. WindInit (void)
  131. {
  132. Rect    bounds;
  133.  
  134.     /* create window and install handler */
  135.  
  136.     SetRect (&bounds, 0, 0, 0, 0);
  137.  
  138.     if (SkelQuery (skelQHasColorQD))
  139.         wind = NewCWindow (nil, &bounds, "\pBoxes", false, noGrowDocProc, nil, false, 0L);
  140.     else
  141.         wind = NewWindow (nil, &bounds, "\pBoxes", false, noGrowDocProc, nil, false, 0L);
  142.  
  143.     SkelWindow (wind,    /* the window */
  144.                 Mouse,        /* mouse click handler */
  145.                 nil,        /* key click handler */
  146.                 Update,        /* window updating procedure */
  147.                 Activate,    /* window activate/deactivate procedure */
  148.                 nil,        /* window close procedure */
  149.                 Clobber,    /* window disposal procedure */
  150.                 nil,        /* idle proc */
  151.                 true);        /* idle only when frontmost */
  152.  
  153.     TextFont (0);
  154.     TextSize (0);
  155.     GetFontInfo (&fontInfo);
  156.  
  157.     SetBoardSize (5, 5);
  158.     SetPlayerCount (2);
  159.     NewGame ();
  160.  
  161.     SkelPositionWindow (wind, skelPositionOnMainDevice,
  162.                                 FixRatio (1, 2), FixRatio (1, 5));
  163.     SelectWindow (wind);
  164.     ShowWindow (wind);
  165. }
  166.