home *** CD-ROM | disk | FTP | other *** search
- #define ABALONE_C
- #include "Abalone.h"
- #undef ABALONE_C
-
-
- #pragma segment Main
-
- RgnHandle gCursorRgn;
-
- #if (defined (powerc) || defined (__SC__)) && ! defined(__MWERKS__)
- QDGlobals qd;
- #endif
-
-
- //*************************** segment Initialize ******************************
-
-
- #pragma segment Initialize
-
- #ifndef THINK_C
- extern
- #ifdef __cplusplus
- "C"
- #endif
- void _DataInit();
- // So we can unload its segment, %A5Init.
- #endif
-
-
- void
- Initialize (void)
- {
- Handle menuBar;
- MenuHandle aboutMenu;
- long total, contig;
-
- InitGraf ((Ptr) &qd.thePort);
- InitCursor();
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs (0L);
-
- // Make sure System 7 and enough memory are available. If not, exit.
- Assert (System7Available(), NEEDS_SYSTEM_7);
-
- // PPC Toolbox needs System 7
- InitPPC();
-
- Assert ((long) GetApplLimit() - (long) ApplicZone() >= kMinHeap, INSUFFICIENT_MEMORY);
- PurgeSpace (&total, &contig);
- Assert (total >= kMinSpace, INSUFFICIENT_MEMORY);
-
- Assert ((menuBar = GetNewMBar (MENU_BAR)) != 0, RESOURCE_MISSING);
- SetMenuBar (menuBar);
- DisposHandle (menuBar);
- AddResMenu (GetMHandle (APPLE_MENU), 'DRVR'); // add DA names to Apple menu
-
- // Add my hierarchical about menus.
-
- {short i, m;
- Str31 itemText;
-
- for (m = 0; m < 2; m++)
- {
- aboutMenu = NewMenu (m == 0 ? ABOUT_ABALONE_MENU : ABOUT_PROGRAM_MENU, "\pAbout"); // add hierarchical about menu
- for (i = 1; ; i++)
- {
- GetIndString (itemText, m == 0 ? ABOUT_ABALONE_MENU : ABOUT_PROGRAM_MENU, i);
- if (itemText[0] == '\0')
- break;
-
- AppendMenu (aboutMenu, itemText);
- }
- InsertMenu (aboutMenu, hierMenu);
- }}
-
- DrawMenuBar();
-
- InitAppleEvents();
-
- qd.randSeed = TickCount();
- gProgStartTicks = TickCount();
-
- GetSettings();
-
- // Get & Draw the application window.
-
- gAbaloneWindow = GetNewDialog (rWindow, nil, (WindowPtr) -1);
-
- gBlackAndWhite = ! ColorQDAvailable() || (*(((CWindowPtr) gAbaloneWindow)->portPixMap))->pixelSize == 1;
-
- SizeWindow (gAbaloneWindow, kHorTotal, kVerTotal, true);
-
- InstallUserItems (gAbaloneWindow);
- if (ColorQDAvailable()) Init3D();
- RestoreColors();
- }
-
-
-
- void
- InstallUserItems (WindowPtr window)
- {
- short type;
- Rect box;
- Handle hndl;
-
- GetDItem (window, 1, & type, & hndl, & box);
- #ifdef THINK_C
- SetDItem (window, 1, type, (Handle) (DrawBlackPoints), & box);
-
- GetDItem (window, 2, & type, & hndl, & box);
- SetDItem (window, 2, type, (Handle) (DrawWhitePoints), & box);
-
- GetDItem (window, 3, & type, & hndl, & box);
- SetDItem (window, 3, type, (Handle) (DrawGreenPoints), & box);
-
- GetDItem (window, 4, & type, & hndl, & box);
- SetDItem (window, 4, type, (Handle) (DrawNotationItemProc), & box);
- #else
- SetDItem (window, 1, type, (Handle) (NewUserItemProc (DrawBlackPoints)), & box);
-
- GetDItem (window, 2, & type, & hndl, & box);
- SetDItem (window, 2, type, (Handle) (NewUserItemProc (DrawWhitePoints)), & box);
-
- GetDItem (window, 3, & type, & hndl, & box);
- SetDItem (window, 3, type, (Handle) (NewUserItemProc (DrawGreenPoints)), & box);
-
- GetDItem (window, 4, & type, & hndl, & box);
- SetDItem (window, 4, type, (Handle) (NewUserItemProc (DrawNotationItemProc)), & box);
- #endif
- SizeItems (window);
- }
-
-
-
- //*************************** segment Main ******************************
-
-
- #pragma segment Main
-
-
- RgnHandle gCursorRgn;
-
- void
- main (void)
- {
- #if ! defined(THINK_C) && ! defined(__MWERKS__)
- UnloadSeg ((Ptr) _DataInit); // How to do this for Think C?
- #endif
-
- // Try to fairly divide available memory between stack and heap. */
-
- while (StackSpace() > kPrefStack || FreeMem() < kMinHeap)
- {
- SetApplLimit ((void *) ((char *) GetApplLimit() + 1024));
- MaxApplZone(); // expand the heap so code segments load at the top
- }
-
- Initialize();
- #if ! defined(THINK_C)
- UnloadSeg ((Ptr) Initialize); // How to do this for Think C?
- #endif
-
- gCursorRgn = NewRgn(); // maintained by AdjustCursor, but must be created here
-
- AdjustMenus(); // before eventloop to make sure disabled menus are dimmed immediately
-
- (void) UserTime();
-
- EventLoop();
- }
-
-
- // Get events 'forever' by calling UserTime,
- // alternating with AutoPlay to give time to possible computer players.
- // Both UserTime and AutoPlay test if an End Of Game 'event' occurred,
- // and if so, return the id of the winner.
-
- void
- EventLoop()
- {
- short winner = empty;
-
- while (! gQuitFlag)
- {
- if (! gQuitFlag && (winner = UserTime()) != empty)
- DoEndOfGame (winner);
-
- if (! gQuitFlag && (winner = AutoPlay()) != empty)
- DoEndOfGame (winner);
-
- gInterrupted = false;
- }
- }
-
-
-
- short
- UserTime (void)
- {
- EventRecord event;
- Boolean somethingHappened = false;
-
- while (WaitNextEvent (everyEvent, &event, 1L, gCursorRgn))
- {
- if (event.what != kHighLevelEvent)
- AdjustCursor (& event.where);
-
- DoEvent (&event);
- somethingHappened = true;
- }
- AdjustCursor (& event.where);
-
- // Test for end of game, but only do so if something happened (for efficiency).
-
- return somethingHappened ? EndOfGame() : empty;
- }
-
-
- // This is made to be called regularly when computing a move.
-
- void
- SystemTime (void)
- {
- EventRecord event;
-
- while (WaitNextEvent (everyEvent, &event, 0L, gCursorRgn))
- {
- DoEvent (& event);
- AdjustCursor (& event.where);
- }
- }
-
-
- // This is made to be called while waiting for a Apple Event reply.
-
- pascal Boolean
- IdleTime (EventRecord *event, long sleepTime, RgnHandle mouseRgn)
- {
- static Boolean firstTime = true;
-
- if (firstTime)
- {
- firstTime = false;
- sleepTime = 3L;
- mouseRgn = gCursorRgn;
- }
- if (event->what != nullEvent)
- {
- DoEvent (event);
- AdjustCursor (& event->where);
- }
- return false;
- }
-
-
-
- void
- AdjustCursor (Point *mouse)
- {
- WindowPtr window = FrontWindow(); // We only adjust the cursor when we are in front.
-
- if ((! gInBackground) && (! IsDAWindow (window)) && (IsAppWindow (window)))
- {
- Field f;
- Point loc = *mouse;
- Rect globalFieldRect;
- RgnHandle fieldRgn = NewRgn();
-
- SetPort (window);
- GlobalToLocal (& loc);
- f = LocToField (loc.h, loc.v);
- if (f)
- {
- // Make a region for the current field, so we get mouseMoved events
- // when the cursor is moved out of this field
-
- globalFieldRect = FieldToRect (f);
- LocalToGlobal (& TopLeft (globalFieldRect));
- LocalToGlobal (& BotRight (globalFieldRect));
- RectRgn (fieldRgn, & globalFieldRect);
-
- CopyRgn (fieldRgn, gCursorRgn);
- }
-
- if (! PtInRect (loc, & window->portRect))
- CurrentCursor (arrowCursor);
- else if (gSet.PlayerKind[gTheGame.CurrentPlayer] != humanPlayer)
- CurrentCursor (watchCursor);
- else if (EmptyRgn (gCursorRgn))
- CurrentCursor (arrowCursor);
- else if (FieldHasCurrentPlayersBall (f))
- CurrentCursor (blackBallMover + gTheGame.CurrentPlayer - blak);
- else
- CurrentCursor (blackBallSolid + gTheGame.CurrentPlayer - blak);
-
- DisposeRgn (fieldRgn);
- }
- }
-
-
-
- void
- AdjustMenus (void)
- {
- // Enable and disable menus based on the current state.
-
- WindowPtr window = FrontWindow();
- MenuHandle menu = GetMHandle (EDIT_MENU);
- short i;
-
- for (i = EDIT_CUT; i <= EDIT_PASTE; i++)
- EnDisableItem (menu, i, IsDAWindow (window));
-
- EnDisableItem (menu, EDIT_UNDO, BoardsOnStack() > 0);
-
- menu = GetMHandle (SETTINGS_MENU);
-
- EnDisableItem (menu, SETTINGS_GREEN, gTheGame.Players > 2);
-
- for (i = blak; i <= grin; i++)
- SetItemIcon (menu, SETTINGS_BLACK + i - blak, i + (gSet.PlayerKind[i] == macPlayer ? 20 : gSet.PlayerKind[i] != networkPlayer ? 10 : 30));
-
- CheckItem (menu, SETTINGS_2_PLAYERS, gTheGame.Players == 2);
- CheckItem (menu, SETTINGS_3_PLAYERS, gTheGame.Players == 3);
-
- menu = GetMHandle (OPTION_MENU);
-
- EnDisableItem (menu, OPTION_3D_BALLS, ColorQDAvailable());
- CheckItem (menu, OPTION_SOUND, gSet.SoundOn);
- CheckItem (menu, OPTION_3D_BALLS, gSet.Balls3D);
- CheckItem (menu, OPTION_NOTATION, gSet.ShowNotation);
-
- menu = GetMHandle (COLOR_MENU);
-
- EnDisableItem (menu, 0, ! gBlackAndWhite || gSet.Balls3D);
- EnDisableItem (menu, COLOR_GREEN, gTheGame.Players > 2);
-
- menu = GetMHandle (BACKGROUND_MENU);
-
- EnDisableItem (menu, 0, gSet.Balls3D);
-
- DrawMenuBar();
- }
-
-
-
- Boolean
- IsAppWindow (WindowPtr window)
- {
- return (window == nil)
- ? false
- : (((WindowPeek) window)->windowKind >= userKind)
- || (((WindowPeek) window)->windowKind == dialogKind);
- }
-
-
-
- Boolean
- IsDAWindow (WindowPtr window)
- {
- return (window != nil) && (((WindowPeek) window)->windowKind < 0);
- }
-
-
-
- void
- EnDisableItem (MenuHandle m, short item, Boolean onoff)
- {
- if (onoff)
- EnableItem (m, item);
- else
- DisableItem (m, item);
- }
-
-
- // To avoid unnecessary overhead with cursor changes,
- // the current cursor is remembered and AdjustCursor only really changes
- // the cursor if there actually is something to change.
-
- void
- CurrentCursor (short id)
- {
- static short current = -1;
-
- if (id == current)
- return;
-
- current = id;
- SetCursor (id == arrowCursor || ! GetCursor (id) ? &qd.arrow : *GetCursor (id));
- }
-