home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0106.zip / Timur / GAME.C < prev    next >
Text File  |  1992-08-05  |  7KB  |  198 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 "game.h"
  25. #include "hexes.h"
  26. #include "target.h"
  27. #include "dialog.h"
  28. #include "files.h"
  29.  
  30. MRESULT EXPENTRY AboutDlgProc(HWND, ULONG, MPARAM, MPARAM);
  31.  
  32. // The width and the heigh of the client window, in pixels
  33. #define WINDOW_WIDTH (1+HEX_DIAM+(NUM_COLUMNS-1)*(HEX_DIAM+HEX_SIDE)/2)
  34. #define WINDOW_HEIGHT (1+HEX_HEIGHT*(NUM_ROWS+1)/2)
  35.  
  36. // Standard variables for PM programs
  37. char *szClassName="TIMUR";                     // What?  You don't like it?
  38. char *szWinTitle="The Ultimate OS/2 Game";
  39. HAB hab;
  40. HMQ hmq;
  41. HPS hps=0;                          // For all drawing functions
  42. QMSG qmsg;
  43. HWND hwndFrame,hwndClient;
  44. ULONG flStyle = (ULONG) (FCF_TITLEBAR|FCF_SYSMENU|FCF_TASKLIST|FCF_MINBUTTON|FCF_MENU|FCF_ICON);
  45.  
  46. // For determining the title and menu bar heights
  47. HWND hwndTitleBar,hwndMenu;
  48. RECTL rclTitleBar,rclMenu;
  49.  
  50. // What the user is currently doing
  51. enum {MODE_TARGET, MODE_EDIT} eMode = MODE_TARGET;
  52.  
  53. // The Menu ID of the currently selected terrain type
  54. USHORT usCheckedTerrain=IDM_TER_CLEAR_GROUND;
  55.  
  56. MRESULT EXPENTRY WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  57.   RECTL rcl;
  58.   POINTL ptl={0,0};
  59.   HEXINDEX hi;
  60.   static HDC hdc;             // We don't need this on the stack
  61.  
  62.   switch (msg) {
  63.     case WM_CREATE:
  64.       hdc=WinOpenWindowDC(hwnd);
  65.       DevQueryCaps(hdc,CAPS_COLORS,1L,&lNumColors);
  66.       TgtInitialize(hwnd);
  67.       hpsHex=WinGetPS(hwnd);
  68.       HexInitMap();
  69.       return 0;
  70.     case WM_PAINT:
  71.       target.fActive=FALSE;                            // Cancel any targetting
  72.       hps=WinBeginPaint(hwnd,0UL,NULL);
  73.       WinQueryWindowRect(hwnd,&rcl);
  74.       GpiSetBackMix(hps,BM_OVERPAINT);
  75.       GpiSetPattern(hps,HexTerrainPattern(mapDefault.bTerrain));
  76.       GpiSetColor(hps,HexTerrainColor(mapDefault.bTerrain));
  77.       GpiMove(hps,&ptl);
  78.       ptl.x=rcl.xRight;
  79.       ptl.y=rcl.yTop;
  80.       GpiBox(hps,DRO_OUTLINEFILL,&ptl,0,0);            // WinFillRect() doesn't
  81.       WinEndPaint(hps);                                //  support patterns
  82.  
  83.       for (hi.c=0;hi.c<NUM_COLUMNS;hi.c++)
  84.         for (hi.r=hi.c & 1;hi.r<NUM_ROWS-(hi.c & 1);hi.r+=2)
  85.           if (amap[hi.c][hi.r].bTerrain==mapDefault.bTerrain) 
  86.             HexDraw(hpsHex,hi);
  87.           else
  88.             HexFillDraw(hi);
  89.  
  90.       return 0L;
  91.     case WM_BUTTON1DOWN:
  92.       if (target.fActive) break;
  93.       ptl.x=SHORT1FROMMP(mp1);
  94.       ptl.y=SHORT2FROMMP(mp1);
  95.       if (!HexLocate(ptl,&hi)) break;                  // hi = origin hex
  96.  
  97.       switch (eMode) {
  98.         case MODE_TARGET:
  99.           TgtStart(hi);
  100.           break;
  101.         case MODE_EDIT:
  102.           amap[hi.c][hi.r].bTerrain=(BYTE) usCheckedTerrain;
  103.           HexFillDraw(hi);
  104.           break;
  105.        }
  106.        break;
  107.     case WM_BUTTON1UP:
  108.       if (target.fActive) TgtEnd();
  109.       break;
  110.     case WM_MOUSEMOVE:
  111.       if (!target.fActive) break;
  112.       ptl.x=SHORT1FROMMP(mp1);                 // get the X,Y coordinates
  113.       ptl.y=SHORT2FROMMP(mp1);
  114.       if (!HexLocate(ptl,&hi)) break;          // Find out which hex it is
  115.       if HI_EQUAL(hi,target.hiEnd) break;      // Test for redundancy
  116.  
  117.       if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Erase the existing line if it exists
  118.         GpiMove(target.hpsLine,&target.ptlStart);
  119.         GpiLine(target.hpsLine,&target.ptlEnd);
  120.       }
  121.       target.ptlEnd=HexMidpoint(target.hiEnd=hi);    // Set the new endpoint and draw the line
  122.       if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Draw the new line if it exists
  123.         GpiMove(target.hpsLine,&target.ptlStart);
  124.         GpiLine(target.hpsLine,&target.ptlEnd);
  125.       }
  126.       break;
  127.     case WM_COMMAND:
  128.       switch (SHORT1FROMMP(mp1)) {
  129.         case IDM_ABOUT:
  130.           WinDlgBox(HWND_DESKTOP,hwnd,AboutDlgProc,NULLHANDLE,IDD_ABOUT,NULL);
  131.           return 0;
  132.         case IDM_MAP_LOAD:
  133.           WinDlgBox(HWND_DESKTOP,hwnd,LoadDlgProc,NULLHANDLE,IDD_LOAD_MAP,NULL);
  134.           return 0;
  135.         case IDM_MAP_SAVE:
  136.           WinDlgBox(HWND_DESKTOP,hwnd,SaveDlgProc,NULLHANDLE,IDD_SAVE_MAP,NULL);
  137.           return 0;
  138.         case IDM_TER_CLEAR_GROUND:
  139.         case IDM_TER_ROUGH_GROUND:
  140.         case IDM_TER_WATER:
  141.         case IDM_TER_LIGHT_WOODS:
  142.         case IDM_TER_HEAVY_WOODS:
  143.         case IDM_TER_PAVEMENT:
  144.         case IDM_TER_BRIDGE:
  145.         case IDM_TER_LIGHT_BLDG:
  146.         case IDM_TER_MEDIUM_BLDG:
  147.         case IDM_TER_HEAVY_BLDG:
  148.         case IDM_TER_HARD_BLDG:
  149.           WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(usCheckedTerrain,TRUE),MPFROM2SHORT(MIA_CHECKED,0));
  150.           usCheckedTerrain=SHORT1FROMMP(mp1);
  151.           WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(usCheckedTerrain,TRUE),MPFROM2SHORT(MIA_CHECKED,MIA_CHECKED));
  152.           return 0L;
  153.         case IDM_MAP_EDIT:
  154.           if (eMode==MODE_EDIT) {
  155.             eMode=MODE_TARGET;
  156.             WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(IDM_MAP_EDIT,TRUE),MPFROM2SHORT(MIA_CHECKED,0));
  157.           } else {
  158.             eMode=MODE_EDIT;
  159.             WinSendMsg(hwndMenu,MM_SETITEMATTR,MPFROM2SHORT(IDM_MAP_EDIT,TRUE),MPFROM2SHORT(MIA_CHECKED,MIA_CHECKED));
  160.           }
  161.           return 0L;
  162.       } // end switch (SHORT1FROMMP(mp1))
  163.  
  164.   } // end switch (msg)
  165.  
  166.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  167. }
  168.  
  169. int main(void) {
  170.   hab=WinInitialize(0);
  171.   hmq=WinCreateMsgQueue(hab,0);
  172.   
  173.   if (!WinRegisterClass(hab,szClassName,WinProc,CS_SIZEREDRAW,0UL)) DosExit(EXIT_PROCESS,0);
  174.   hwndFrame=WinCreateStdWindow(HWND_DESKTOP,0,&flStyle,szClassName,"",CS_SIZEREDRAW,0UL,ID_RESOURCE,&hwndClient);
  175.   if (!hwndFrame) DosExit(EXIT_PROCESS,0);
  176.  
  177.   WinSetWindowText(hwndFrame,szWinTitle);
  178.  
  179. // We must display the window before we can get the height of the title and menu bars
  180.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,SWP_ACTIVATE|SWP_MOVE|SWP_SHOW|SWP_SIZE);
  181.  
  182.   hwndTitleBar=WinWindowFromID(hwndFrame,FID_TITLEBAR);
  183.   if (!WinQueryWindowRect(hwndTitleBar,&rclTitleBar)) DosExit(EXIT_PROCESS,0);
  184.   hwndMenu=WinWindowFromID(hwndFrame,FID_MENU);
  185.   if (!WinQueryWindowRect(hwndMenu,&rclMenu)) DosExit(EXIT_PROCESS,0);
  186.  
  187. // Now that we have the bar heights, re-size the window again.
  188.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT+rclTitleBar.yTop+rclMenu.yTop,SWP_SIZE);
  189.  
  190.   while (WinGetMsg(hab,&qmsg,0,0,0)) WinDispatchMsg(hab,&qmsg);
  191.  
  192.   TgtShutdown();
  193.   WinDestroyWindow(hwndFrame);
  194.   WinDestroyMsgQueue(hmq);
  195.   WinTerminate(hab);
  196.   return 0;
  197. }
  198.