home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0105.zip / Timur / GAME.C next >
Text File  |  1992-06-19  |  7KB  |  185 lines

  1. /* The Ultimate OS/2 Game.
  2. Copyright (c) 1992 Timur Tabi 
  3. Copyright (c) 1992 Fasa Corporation 
  4.  
  5. The following trademarks are the property of Fasa Corporation:
  6. BattleTech, CityTech, AeroTech, MechWarrior, BattleMech, and 'Mech.
  7. The use of these trademarks should not be construed as a challenge to these marks.  
  8.  
  9. The program accompanies Timur Tabi's column "The Ultimate OS/2 Game", which
  10. appears in "OS/2 Monthly".  It is a computer representation of the BattleTech and
  11. Mechwarrior board games, as produced and distributed by the Fasa Coporation.
  12.  
  13. Special thanx go to:
  14.  
  15. Scott Cherkofsky, for great ideas since the very beginning.
  16. Erin Sasaki, for proofreading my articles, even during finals.
  17. Sam Lewis, president of Fasa, for giving me permission.
  18. */
  19.  
  20. #define INCL_WIN
  21. #define INCL_GPIPRIMITIVES
  22. #define INCL_DEV
  23. #include <os2.h>
  24. #include "resource.h"
  25. #include "hexes.h"
  26. #include "target.h"
  27.  
  28. // The width and the heigh of the client window, in pixels
  29. #define WINDOW_WIDTH (1+HEX_DIAM+(NUM_COLUMNS-1)*(HEX_DIAM+HEX_SIDE)/2)
  30. #define WINDOW_HEIGHT (1+HEX_HEIGHT*(NUM_ROWS+1)/2)
  31.  
  32. // Standard variables for PM programs
  33. char *szClassName="TIMUR";                     // What?  You don't like it?
  34. char *szWinTitle="The Ultimate OS/2 Game";
  35. HAB hab;
  36. HMQ hmq;
  37. HPS hps=0;                          // For all drawing functions
  38. QMSG qmsg;
  39. HWND hwndFrame,hwndClient;
  40. ULONG flStyle = (ULONG) (FCF_TITLEBAR|FCF_SYSMENU|FCF_TASKLIST|FCF_MINBUTTON|FCF_MENU|FCF_ICON);
  41.  
  42. // For determining the title and menu bar heights
  43. HWND hwndTitleBar,hwndMenu;
  44. RECTL rclTitleBar,rclMenu;
  45.  
  46. // What the user is currently doing
  47. enum {MODE_TARGET, MODE_EDIT} eMode = MODE_TARGET;
  48.  
  49. // The Menu ID of the currently selected terrain type
  50. USHORT usCheckedTerrain=IDM_TER_CLEAR_GROUND;
  51.  
  52. MRESULT EXPENTRY WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  53.   RECTL rcl;
  54.   POINTL ptl={0,0};
  55.   HEXINDEX hi;
  56.   static HDC hdc;             // We don't need this on the stack
  57.  
  58.   switch (msg) {
  59.     case WM_CREATE:
  60.       hdc=WinOpenWindowDC(hwnd);
  61.       DevQueryCaps(hdc,CAPS_COLORS,1L,&lNumColors);
  62.       TgtInitialize(hwnd);
  63.       hpsHex=WinGetPS(hwnd);
  64.       HexInitMap();
  65.       return 0;
  66.     case WM_PAINT:
  67.       target.fActive=FALSE;                            // Cancel any targetting
  68.       hps=WinBeginPaint(hwnd,0UL,NULL);
  69.       WinQueryWindowRect(hwnd,&rcl);
  70.       GpiSetBackMix(hps,BM_OVERPAINT);
  71.       GpiSetPattern(hps,HexTerrainPattern(mapDefault.bTerrain));
  72.       GpiSetColor(hps,HexTerrainColor(mapDefault.bTerrain));
  73.       GpiMove(hps,&ptl);
  74.       ptl.x=rcl.xRight;
  75.       ptl.y=rcl.yTop;
  76.       GpiBox(hps,DRO_OUTLINEFILL,&ptl,0,0);            // WinFillRect() doesn't
  77.       WinEndPaint(hps);                                //  support patterns
  78.  
  79.       for (hi.c=0;hi.c<NUM_COLUMNS;hi.c++)
  80.         for (hi.r=hi.c & 1;hi.r<NUM_ROWS-(hi.c & 1);hi.r+=2)
  81.           if (amap[hi.c][hi.r].bTerrain==mapDefault.bTerrain) 
  82.             HexDraw(hpsHex,hi);
  83.           else
  84.             HexFillDraw(hi);
  85.  
  86.       return 0L;
  87.     case WM_BUTTON1DOWN:
  88.       if (target.fActive) break;
  89.       ptl.x=SHORT1FROMMP(mp1);
  90.       ptl.y=SHORT2FROMMP(mp1);
  91.       if (!HexLocate(ptl,&hi)) break;                  // hi = origin hex
  92.  
  93.       switch (eMode) {
  94.         case MODE_TARGET:
  95.           TgtStart(hi);
  96.           break;
  97.         case MODE_EDIT:
  98.           amap[hi.c][hi.r].bTerrain=(BYTE) usCheckedTerrain;
  99.           HexFillDraw(hi);
  100.           break;
  101.        }
  102.        break;
  103.     case WM_BUTTON1UP:
  104.       if (target.fActive) TgtEnd();
  105.       break;
  106.     case WM_MOUSEMOVE:
  107.       if (!target.fActive) break;
  108.       ptl.x=SHORT1FROMMP(mp1);                 // get the X,Y coordinates
  109.       ptl.y=SHORT2FROMMP(mp1);
  110.       if (!HexLocate(ptl,&hi)) break;          // Find out which hex it is
  111.       if HI_EQUAL(hi,target.hiEnd) break;      // Test for redundancy
  112.  
  113.       if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Erase the existing line if it exists
  114.         GpiMove(target.hpsLine,&target.ptlStart);
  115.         GpiLine(target.hpsLine,&target.ptlEnd);
  116.       }
  117.       target.ptlEnd=HexMidpoint(target.hiEnd=hi);    // Set the new endpoint and draw the line
  118.       if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Draw the new line if it exists
  119.         GpiMove(target.hpsLine,&target.ptlStart);
  120.         GpiLine(target.hpsLine,&target.ptlEnd);
  121.       }
  122.       break;
  123.     case WM_COMMAND:
  124.       switch (SHORT1FROMMP(mp1)) {
  125.         case IDM_TER_CLEAR_GROUND:
  126.         case IDM_TER_ROUGH_GROUND:
  127.         case IDM_TER_WATER:
  128.         case IDM_TER_LIGHT_WOODS:
  129.         case IDM_TER_HEAVY_WOODS:
  130.         case IDM_TER_PAVEMENT:
  131.         case IDM_TER_BRIDGE:
  132.         case IDM_TER_LIGHT_BLDG:
  133.         case IDM_TER_MEDIUM_BLDG:
  134.         case IDM_TER_HEAVY_BLDG:
  135.         case IDM_TER_HARD_BLDG:
  136.           WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(usCheckedTerrain,TRUE),MPFROM2SHORT(MIA_CHECKED,0));
  137.           usCheckedTerrain=SHORT1FROMMP(mp1);
  138.           WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(usCheckedTerrain,TRUE),MPFROM2SHORT(MIA_CHECKED,MIA_CHECKED));
  139.           return 0L;
  140.         case IDM_MAP_EDIT:
  141.           if (eMode==MODE_EDIT) {
  142.             eMode=MODE_TARGET;
  143.             WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(IDM_MAP_EDIT,TRUE),MPFROM2SHORT(MIA_CHECKED,0));
  144.           } else {
  145.             eMode=MODE_EDIT;
  146.             WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(IDM_MAP_EDIT,TRUE),MPFROM2SHORT(MIA_CHECKED,MIA_CHECKED));
  147.           }
  148.           return 0L;
  149.       } // end switch (SHORT1FROMMP(mp1))
  150.  
  151.   } // end switch (msg)
  152.  
  153.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  154. }
  155.  
  156. int main(void) {
  157.   hab=WinInitialize(0);
  158.   hmq=WinCreateMsgQueue(hab,0);
  159.   
  160.   if (!WinRegisterClass(hab,szClassName,WinProc,CS_SIZEREDRAW,0UL)) DosExit(EXIT_PROCESS,0);
  161.   hwndFrame=WinCreateStdWindow(HWND_DESKTOP,0,&flStyle,szClassName,"",CS_SIZEREDRAW,0UL,ID_RESOURCE,&hwndClient);
  162.   if (!hwndFrame) DosExit(EXIT_PROCESS,0);
  163.  
  164.   WinSetWindowText(hwndFrame,szWinTitle);
  165.  
  166. // We must display the window before we can get the height of the title and menu bars
  167.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,SWP_ACTIVATE|SWP_MOVE|SWP_SHOW|SWP_SIZE);
  168.  
  169.   hwndTitleBar=WinWindowFromID(hwndFrame,FID_TITLEBAR);
  170.   if (!WinQueryWindowRect(hwndTitleBar,&rclTitleBar)) DosExit(EXIT_PROCESS,0);
  171.   hwndMenu=WinWindowFromID(hwndFrame,FID_MENU);
  172.   if (!WinQueryWindowRect(hwndMenu,&rclMenu)) DosExit(EXIT_PROCESS,0);
  173.  
  174. // Now that we have the bar heights, re-size the window again.
  175.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT+rclTitleBar.yTop+rclMenu.yTop,SWP_SIZE);
  176.  
  177.   while (WinGetMsg(hab,&qmsg,0,0,0)) WinDispatchMsg(hab,&qmsg);
  178.  
  179.   TgtShutdown();
  180.   WinDestroyWindow(hwndFrame);
  181.   WinDestroyMsgQueue(hmq);
  182.   WinTerminate(hab);
  183.   return 0;
  184. }
  185.