home *** CD-ROM | disk | FTP | other *** search
- /*
- * Window.c - "Boxes" application
- */
-
- # include "TransSkel.h"
-
- # include "Boxes.h"
-
-
-
- static WindowPtr wind = (WindowPtr) nil;
-
-
- void
- ForceWindowUpdate (void)
- {
- InvalRect (&wind->portRect);
- }
-
-
- /*
- * Got an activate or deactivate.
- */
-
- static pascal void
- Activate (Boolean active)
- {
- }
-
-
- /*
- * Current grafPort will be set to wind by TransSkel, no GetPort() necessary.
- */
-
- static pascal void
- Update (Boolean resized)
- {
- EraseRect (&wind->portRect);
- DrawBoard ();
- DrawScoreBoard ();
- }
-
-
- static pascal void
- Mouse (Point pt, long t, short mods)
- {
- short h, v;
- short squares = 0;
- Boolean gotLinkClick = false;
- short player = GetPlayer (); /* save before it changes */
-
- if (SidesLeft () == 0)
- return;
-
- if (VLinkHitTest (pt, &h, &v))
- {
- gotLinkClick = true;
- squares = SetVSide (h, v, player);
- }
- else if (HLinkHitTest (pt, &h, &v))
- {
- gotLinkClick = true;
- squares = SetHSide (h, v, player);
- }
-
- if (!gotLinkClick)
- return;
-
- /*
- * A link was completed. If a square was not completed, go to next player.
- * Otherwise the same player can keep playing - unless the game is now done.
- * In all cases optimize for 1-player to avoid extra redrawing.
- */
- if (squares == 0 && GetPlayerCount () > 1) /* no squares completed, next player */
- NextPlayer ();
- else /* squares completed, stay with same player... */
- {
- IncrementScore (player, squares);
- if (SidesLeft () > 0)
- DrawScore (player); /* draw player's new score */
- else
- DrawScoreBoard (); /* ...unless done with game (draw winners) */
- }
- }
-
-
- static pascal void
- Clobber (void)
- {
- HideWindow (wind);
- DisposeWindow (wind);
- }
-
-
-
- void
- NewGame (void)
- {
- Rect r, r2;
- short sbWidth;
-
- /*
- Figure out new window size
- */
-
- CalcBoardSize (&r);
- CalcScoreBoardSize (&r2);
- sbWidth = r2.right;
- PlaceScoreBoard (hPad + r.right + hPad, vPad);
- if (r.bottom < r2.bottom) /* window needs to be at least */
- r.bottom = r2.bottom; /* as deep as the scoreboard */
- r.bottom += 2 * vPad;
- r.right += r2.right + 3 * hPad;
- SizeWindow (wind, r.right, r.bottom, true);
- InvalRect (&wind->portRect);
-
- InitializeBoard ();
- InitializeScoreBoard ();
- SetOptionsMenuPlayerCount ();
- }
-
-
- /*
- * Initialize window. Size of window rect and location of button are
- * irrelevant at this point. The window will be sized and the button
- * placed in NewGame ().
- */
-
- void
- WindInit (void)
- {
- Rect bounds;
-
- /* create window and install handler */
-
- SetRect (&bounds, 0, 0, 0, 0);
-
- if (SkelQuery (skelQHasColorQD))
- wind = NewCWindow (nil, &bounds, "\pBoxes", false, noGrowDocProc, nil, false, 0L);
- else
- wind = NewWindow (nil, &bounds, "\pBoxes", false, noGrowDocProc, nil, false, 0L);
-
- SkelWindow (wind, /* the window */
- Mouse, /* mouse click handler */
- nil, /* key click handler */
- Update, /* window updating procedure */
- Activate, /* window activate/deactivate procedure */
- nil, /* window close procedure */
- Clobber, /* window disposal procedure */
- nil, /* idle proc */
- true); /* idle only when frontmost */
-
- TextFont (0);
- TextSize (0);
- GetFontInfo (&fontInfo);
-
- SetBoardSize (5, 5);
- SetPlayerCount (2);
- NewGame ();
-
- SkelPositionWindow (wind, skelPositionOnMainDevice,
- FixRatio (1, 2), FixRatio (1, 5));
- SelectWindow (wind);
- ShowWindow (wind);
- }
-