home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Mac Source / Event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-23  |  7.6 KB  |  242 lines  |  [TEXT/CWIE]

  1. /*
  2. ** File:        Event.c
  3. **
  4. ** Written by:    Bill Hayden
  5. **                Nikol Software
  6. **
  7. ** Copyright © 1995 Nikol Software
  8. ** All rights reserved.
  9. */
  10.  
  11.  
  12.  
  13. #include "MacWT.h"
  14. #include "Constants.h"
  15. #include "Menu.h"
  16. #include "Event.h"
  17. #include "input.h"
  18. #include "graphics.h"
  19. #include <math.h>
  20. #include "StringUtils.h"
  21. #include "ShowHideMenubar.h"
  22.  
  23.  
  24.  
  25. static void ProcessGameEvent(void);
  26.  
  27. static void add_special(Intent *intent, int special);
  28.  
  29.  
  30. static Boolean        gInBackground = false;
  31. Intent gIntent;
  32.  
  33.  
  34.  
  35. /*****************************************************************************/
  36.  
  37.  
  38.  
  39.  
  40. void EventLoop(void)
  41. {
  42.     while (!quitting)
  43.         {
  44.         if (gGameOn && !gPaused)
  45.             {
  46.             UpdateGameScreen();
  47.             ProcessGameEvent();
  48.             }
  49.         else
  50.             ProcessEvent();
  51.         }
  52. }
  53.  
  54.  
  55.  
  56.  
  57. /*****************************************************************************/
  58.  
  59.  
  60.  
  61.  
  62. Boolean ProcessEvent(void)
  63. {
  64.     WindowRef        whichWindow;
  65.     long            menuResult;
  66.     short            partCode;
  67.     char            ch;
  68.  
  69.  
  70.     if (!WaitNextEvent(everyEvent, &gTheEvent, gInBackground ? kBackTime : kFrontTime, nil))
  71.         return FALSE;
  72.         
  73.     switch (gTheEvent.what)
  74.         {
  75.         case mouseDown:
  76.  
  77.             switch (partCode = FindWindow(gTheEvent.where, &whichWindow)) {
  78.  
  79.             case inSysWindow:
  80.                 SystemClick(&gTheEvent, (WindowPtr)whichWindow);
  81.                 break;
  82.  
  83.             case inMenuBar:
  84.                 DoAdjustMenus();
  85.                 DoMenuCommand(MenuSelect(gTheEvent.where));
  86.                 break;
  87.  
  88.             case inDrag:
  89.                 SelectWindow(whichWindow);
  90.                 DragWindow(whichWindow, gTheEvent.where, &qd.screenBits.bounds);
  91.                 break;
  92.  
  93.             case inContent:
  94.                 if (whichWindow != FrontWindow())
  95.                     SelectWindow(whichWindow);
  96.                 break;
  97.  
  98.             case inGoAway:
  99.                 if (TrackGoAway(whichWindow, gTheEvent.where))
  100.                     ExitToShell();
  101.                 break;
  102.  
  103.             default:
  104.                 break;
  105.  
  106.             }                                // switch (FindWindow)
  107.             break;
  108.  
  109.  
  110.         case keyDown:
  111.             ObscureCursor();
  112.             // FALL THRU
  113.  
  114.         case autoKey:
  115.             ch = gTheEvent.message;            // automagic "& charCodeMask" :)
  116.             if (gTheEvent.modifiers & cmdKey)
  117.                 {
  118.                 DoAdjustMenus();
  119.                 menuResult = MenuKey(ch);
  120.                 if (menuResult & 0xFFFF0000)
  121.                     {
  122.                     DoMenuCommand(menuResult);
  123.                     break;        // out of this switch if it was a menu command
  124.                     }
  125.                 }
  126.                 
  127.             switch (ch)
  128.                 {
  129.                 case kTab:
  130.                     TogglePause();
  131.                     break;
  132.                     
  133.                 case '8':
  134.                 //case kUpArrow:
  135.                     BeginGame();
  136.                     break;
  137.                 }
  138.             break;
  139.  
  140.  
  141.         case updateEvt:
  142.             {
  143.             GrafPtr    savePort;
  144.             
  145.             whichWindow = (WindowRef)gTheEvent.message;
  146.             
  147.             GetPort(&savePort);
  148.             SetPortWindowPort(whichWindow);
  149.             
  150.             BeginUpdate(whichWindow);
  151.  
  152.             if (whichWindow == (WindowRef)gWindow)
  153.                 {
  154.                 UpdateDialog(gWindow, gWindow->visRgn);
  155.                 DrawGameScreen();
  156.                 if (gPaused)
  157.                     ShowPausedScreen();
  158.                 else if (!gGameOn)
  159.                     MacAttractMode();
  160.                 }
  161.  
  162.             EndUpdate(whichWindow);
  163.             
  164.             SetPort(savePort);
  165.             }
  166.             break;
  167.  
  168.         case diskEvt:
  169.         case activateEvt:
  170.             break;
  171.  
  172.         case osEvt:
  173.             if ((gTheEvent.message << 31) == 0)        // suspend event
  174.                 {
  175.                 if (!gPaused)
  176.                     TogglePause();
  177.                 gInBackground = true;
  178.                 }
  179.             else
  180.                 {
  181.                 gInBackground = false;
  182.                 SetPortWindowPort(GetDialogWindow(gWindow));
  183.                 }
  184.             break;
  185.  
  186.         case kHighLevelEvent:
  187.             AEProcessAppleEvent(&gTheEvent);
  188.             break;
  189.  
  190.         default:
  191.             break;
  192.     }
  193.     
  194.     return TRUE;
  195. }
  196.  
  197.  
  198. /*****************************************************************************/
  199.  
  200.  
  201.  
  202. static void ProcessGameEvent(void)
  203. {
  204.     KeyMap            kmap;
  205.     unsigned char *kmp = (unsigned char *)&kmap;
  206.     Boolean    rotating_cw,
  207.             rotating_ccw,
  208.             moving_forward,
  209.             moving_backward,
  210.             running,
  211.             strafing;
  212.     struct {
  213.         short    rightArrow,
  214.                 leftArrow,
  215.                 upArrow,
  216.                 downArrow,
  217.                 control,
  218.                 escape,
  219.                 shift,
  220.                 space,
  221.                 tab,
  222.                 w,
  223.                 e,
  224.                 r,
  225.                 t,
  226.                 y,
  227.                 plus,
  228.                 minus;
  229.     } keyboard;
  230.  
  231.  
  232.     gIntent.force_x = gIntent.force_y = gIntent.force_z = 0.0;
  233.     gIntent.force_rotate = 0.0;
  234.     gIntent.n_special = 0;
  235.  
  236.     GetKeys(kmap);
  237.  
  238.     keyboard.rightArrow = MacKeyDown(kmp, 0x7C) || MacKeyDown(kmp, 0x58);
  239.     keyboard.leftArrow = MacKeyDown(kmp, 0x7b) || MacKeyDown(kmp, 0x56);
  240.     keyboard.upArrow = MacKeyDown(kmp, 0x7e) || MacKeyDown(kmp, 0x5b);
  241.     keyboard.downArrow = MacKeyDown(kmp, 0x7d) || MacKeyDown(kmp, 0x54);
  242.     keyboard.control = MacKeyDown(kmp, 0x3b) || M