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

  1. /*
  2.  * Boxes game
  3.  *
  4.  * 14 Dec 90    Paul DuBois        dubois@primate.wisc.edu
  5.  *
  6.  * 14 Dec 90 V1.00
  7.  * 13 Jun 92
  8.  * - Updated to compile under THINK C 5.02. No functional changes except that
  9.  * calls to alerts and modal dialogs were modified to use SkelDlogFilter().
  10.  * 03 Oct 93
  11.  * - Updated to compile under THINK C 6.0.  Also checks for color depth and
  12.  * disables Colors... menu item if it doesn't apply.
  13.  *
  14.  * 21 Feb 94
  15.  * - Updated for TransSkel 3.11.
  16.  */
  17.  
  18. # include    "TransSkel.h"
  19.  
  20. # include    "Boxes.h"
  21.  
  22.  
  23. # define    bailString    "\pBoxes"
  24.  
  25.  
  26. typedef enum
  27. {
  28.     fileMenuNum = 1000,
  29.     editMenuNum,
  30.     optionsMenuNum
  31. };
  32.  
  33.  
  34. typedef enum        /* File menu item numbers */
  35. {
  36.     info = 1,
  37.     /* --- */
  38.     quit = 3
  39. };
  40.  
  41.  
  42. enum        /* edit menu item numbers */
  43. {
  44.     editUndo = 1,
  45.     /* ---- */
  46.     editCut = 3,
  47.     editCopy = 4,
  48.     editPaste = 5,
  49.     editClear = 6
  50. };
  51.  
  52.  
  53. typedef enum        /* Options menu item numbers */
  54. {
  55.     newGame = 1,
  56.     boardSize,
  57.     colorSelect,
  58.     /* --- */
  59.     onePlayer = 5,
  60.     twoPlayer,
  61.     threePlayer,
  62.     fourPlayer
  63. };
  64.  
  65.  
  66. static void Terminate (void);
  67.  
  68.  
  69. FontInfo    fontInfo;            /* info about system font */
  70.  
  71.  
  72. static WindowPtr    dispWind;
  73. static MenuHandle    fileMenu;
  74. static MenuHandle    editMenu;
  75. static MenuHandle    optionsMenu;
  76.  
  77.  
  78. static void
  79. Bail (StringPtr msg)
  80. {
  81.     Message4 (bailString, "\p: ", msg, "\p  Bailing out.");
  82.     Terminate ();
  83. }
  84.  
  85.  
  86.  
  87. /*
  88.  * Handle selection of About… item from Apple menu
  89.  */
  90.  
  91. static pascal void
  92. DoAppleMenu (short item)
  93. {
  94. short    alrtNum;
  95.  
  96.     alrtNum = ((SkelGetModifiers () & optionKey) == 0 ? aboutAlrt : authorAlrt);
  97.     (void) SkelAlert (alrtNum, SkelDlogFilter (nil, true),
  98.                                     skelPositionOnParentDevice);
  99.     SkelRmveDlogFilter ();
  100. }
  101.  
  102.  
  103. /*
  104.  * File menu handler
  105.  */
  106.  
  107. static pascal void
  108. DoFileMenu (short item)
  109. {
  110.     switch (item)
  111.     {
  112.     case info:
  113.         (void) SkelAlert (infoAlrt, SkelDlogFilter (nil, true),
  114.                                         skelPositionOnParentDevice);
  115.         SkelRmveDlogFilter ();
  116.         break;
  117.     case quit:
  118.         SkelStopEventLoop ();
  119.         break;
  120.     }
  121. }
  122.  
  123.  
  124. /*
  125.  * Edit menu handler.  Since the menu has no meaning for Boxes, all
  126.  * that is done is to route the selection to any desk accessory that
  127.  * might want it.
  128.  */
  129.  
  130. static pascal void
  131. DoEditMenu (short item)
  132. {
  133.     (void) SystemEdit (item - 1);
  134. }
  135.  
  136.  
  137. /*
  138.  * Options menu handler
  139.  */
  140.  
  141. static pascal void
  142. DoOptionsMenu (short item)
  143. {
  144.     switch (item)
  145.     {
  146.     case newGame:
  147.         NewGame ();
  148.         break;
  149.     case boardSize:
  150.         BoardDialog ();
  151.         break;
  152.     case colorSelect:
  153.         ColorDialog ();
  154.         break;
  155.     case onePlayer:
  156.         SetPlayerCount (1);
  157.         NewGame ();
  158.         break;
  159.     case twoPlayer:
  160.         SetPlayerCount (2);
  161.         NewGame ();
  162.         break;
  163.     case threePlayer:
  164.         SetPlayerCount (3);
  165.         NewGame ();
  166.         break;
  167.     case fourPlayer:
  168.         SetPlayerCount (4);
  169.         NewGame ();
  170.         break;
  171.     }
  172. }
  173.  
  174.  
  175. void
  176. SetOptionsMenuPlayerCount (void)
  177. {
  178. short    n = GetPlayerCount ();
  179.  
  180.     SetItemMark (optionsMenu, onePlayer, n == 1 ? checkMark : noMark);
  181.     SetItemMark (optionsMenu, twoPlayer, n == 2 ? checkMark : noMark);
  182.     SetItemMark (optionsMenu, threePlayer, n == 3 ? checkMark : noMark);
  183.     SetItemMark (optionsMenu, fourPlayer, n == 4 ? checkMark : noMark);
  184. }
  185.  
  186.  
  187. static pascal void
  188. AdjustMenus (void)
  189. {
  190. WindowPeek    wPeek;
  191.  
  192.     if ( (wPeek = (WindowPeek) FrontWindow ()) != nil
  193.         && wPeek->windowKind < 0)    /* DA window in front */
  194.     {
  195.         EnableItem (editMenu, editUndo);
  196.         EnableItem (editMenu, editCut);
  197.         EnableItem (editMenu, editCopy);
  198.         EnableItem (editMenu, editPaste);
  199.         EnableItem (editMenu, editClear);
  200.     }
  201.     else                            /* DA not in front */
  202.     {
  203.         DisableItem (editMenu, editUndo);
  204.         DisableItem (editMenu, editCut);
  205.         DisableItem (editMenu, editCopy);
  206.         DisableItem (editMenu, editPaste);
  207.         DisableItem (editMenu, editClear);
  208.     }
  209. }
  210.  
  211.  
  212.  
  213. /*
  214.  * Initialize menus.  \311 is the ellipsis character
  215.  */
  216.  
  217. static void
  218. SetupMenus (void)
  219. {
  220. long        result;
  221. short        enoughColors = 0;
  222. GDHandle    gDevice;
  223.  
  224.     SkelApple ((StringPtr) "\pAbout Boxes\311", DoAppleMenu);
  225.  
  226.     if ((fileMenu = GetMenu (fileMenuNum)) == (MenuHandle) nil
  227.         || !SkelMenu (fileMenu, DoFileMenu, nil, false, false))
  228.         Bail ("\pProblem creating File menu.");
  229.  
  230.     if ((editMenu = GetMenu (editMenuNum)) == (MenuHandle) nil
  231.         || !SkelMenu (editMenu, DoEditMenu, nil, false, false))
  232.         Bail ("\pProblem creating Edit menu.");
  233.  
  234.     if ((optionsMenu = GetMenu (optionsMenuNum)) == (MenuHandle) nil
  235.         || !SkelMenu (optionsMenu, DoOptionsMenu, nil, false, false))
  236.         Bail ("\pProblem creating Options menu.");
  237.  
  238.     DrawMenuBar ();
  239.  
  240.     /* determine whether screen has at least 8 non-BW colors */
  241.  
  242.     if (SkelQuery (skelQHasColorQD))
  243.     {
  244.         if ((gDevice = GetMainDevice ()) != (GDHandle) NULL)
  245.         {
  246.             if ((**(**gDevice).gdPMap).pixelSize >= 4)
  247.                 enoughColors = 1;
  248.         }
  249.     }
  250.     if (!enoughColors)
  251.     {
  252.         DisableItem (optionsMenu, colorSelect);
  253.         /*
  254.         (void) NoteAlert (depthAlrt, SkelDlogFilter (nil, true));
  255.         SkelRmveDlogFilter ();
  256.         */
  257.     }
  258.     SkelSetMenuHook (AdjustMenus);
  259. }
  260.  
  261.  
  262. int
  263. main (void)
  264. {
  265.     /* Initialize TransSkel, create windows */
  266.  
  267.     SkelInit ((SkelInitParamsPtr) nil);
  268.     SetMessageAlertNum (gnrlAlrt);
  269.     SetupMenus ();
  270.  
  271.     WindInit ();
  272.     InitColor ();
  273.  
  274.     /* Process events until user quits, then clean up and exit */
  275.  
  276.     SkelEventLoop ();
  277.  
  278.     Terminate ();
  279. }
  280.  
  281.  
  282. static void
  283. Terminate (void)
  284. {
  285.     SkelCleanup ();
  286.     ExitToShell ();
  287. }