home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mnth0104.zip / Timur / game.c next >
C/C++ Source or Header  |  1993-02-22  |  5KB  |  149 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_DOS
  22. #define INCL_GPIPRIMITIVES
  23. #define INCL_DEV
  24. #include <os2.h>
  25. #include "hexes.h"
  26.  
  27. // The width and the heigh of the client window, in pixels
  28. #define WINDOW_WIDTH (1+HEX_DIAM+(NUM_COLUMNS-1)*(HEX_DIAM+HEX_SIDE)/2)
  29. #define WINDOW_HEIGHT (1+HEX_HEIGHT*(NUM_ROWS+1)/2)
  30.  
  31. HAB hab;
  32. HMQ hmq;
  33. QMSG qmsg;
  34. HWND hwndFrame,hwndClient,hwndTitleBar;
  35. ULONG flStyle = (ULONG) (FCF_TITLEBAR|FCF_SYSMENU|FCF_TASKLIST|FCF_MINBUTTON|FCF_ICON);
  36.  
  37. extern TARGET target;
  38.  
  39. RECTL rclTitleBar;                       // For determining the title bar height
  40. LONG lNumColors;
  41. char *szClassName="TIMUR";               // What?  You don't like it?
  42. char *szWinTitle="The Ultimate OS/2 Game";
  43.  
  44. static MRESULT EXPENTRY WinProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2 ) {
  45.   HPS hps;
  46.   RECTL rcl;
  47.   POINTL ptl;
  48.   HEXINDEX hi;
  49.   static HDC hdc;   // We don't need this on the stack
  50.  
  51.   switch (msg) {
  52.     case WM_CREATE:
  53.       hdc=WinOpenWindowDC(hwnd);
  54.       DevQueryCaps(hdc,CAPS_COLORS,1L,&lNumColors);
  55.       return 0;
  56.     case WM_PAINT:
  57.       hps=WinBeginPaint(hwnd,0UL,NULL);
  58.       WinQueryWindowRect(hwnd,&rcl);
  59.       WinFillRect(hps,&rcl,CLR_BLACK);
  60.       GpiSetColor(hps,HEX_COLOR);
  61.  
  62. // Draw the map
  63.       for (hi.c=1;hi.c<=NUM_COLUMNS;hi.c++)                   
  64.         for (hi.r=2-(hi.c%2); hi.r<NUM_ROWS+(hi.c%2); hi.r+=2)
  65.           HexDraw(hps,hi);
  66.  
  67.       if (target.fActive) {                       // Cancel any targetting
  68.         target.fActive=FALSE;
  69.         WinReleasePS(target.hpsLine);
  70.       }
  71.       WinEndPaint(hps);
  72.       return 0L;
  73.    case WM_BUTTON1DOWN:
  74.       if (target.fActive) break;
  75.       ptl.x=SHORT1FROMMP(mp1);
  76.       ptl.y=SHORT2FROMMP(mp1);
  77.       if (!HexLocate(ptl,&hi)) break;                  // hi = origin hex
  78.  
  79.       target.fActive=TRUE;                             // Initalize 'target'
  80.       target.hiStart=hi;
  81.       target.hiEnd=hi;
  82.       target.ptlStart=HexMidpoint(hi);
  83.       target.ptlEnd=target.ptlStart;
  84.       target.hpsLine=WinGetPS(hwnd);
  85.       GpiSetColor(target.hpsLine,CLR_BLUE);
  86.       GpiMove(target.hpsLine,&target.ptlStart);
  87.       GpiSetMix(target.hpsLine,FM_XOR);
  88.  
  89. // Initialize paramters for HexHighlight and start the thread
  90.       target.hpsHighlight=WinGetPS(hwnd);
  91.       DosCreateThread(&target.tid,HexHighlight,0UL,0UL,4096UL);
  92.  
  93.       break;
  94.   case WM_BUTTON1UP:
  95.     if (target.fActive) {
  96.        target.fActive=FALSE;                 // Automatically terminates HexHighlight
  97.        if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Erase the line if it exists
  98.          GpiMove(target.hpsLine,&target.ptlStart);
  99.          GpiLine(target.hpsLine,&target.ptlEnd);
  100.        }
  101.        WinReleasePS(target.hpsLine);
  102.     }
  103.     break;
  104.   case WM_MOUSEMOVE:
  105.     if (!target.fActive) break;
  106.     ptl.x=SHORT1FROMMP(mp1);                 // get the X,Y coordinates
  107.     ptl.y=SHORT2FROMMP(mp1);
  108.     if (!HexLocate(ptl,&hi)) break;          // Find out which hex it is
  109.     if HI_EQUAL(hi,target.hiEnd) break;      // Test for redundancy
  110.  
  111.  
  112.     if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Erase the existing line if it exists
  113.       GpiMove(target.hpsLine,&target.ptlStart);
  114.       GpiLine(target.hpsLine,&target.ptlEnd);
  115.     }
  116.     target.ptlEnd=HexMidpoint(target.hiEnd=hi);    // Set the new endpoint and draw the line
  117.     if (!HI_EQUAL(target.hiStart,target.hiEnd)) {  // Draw the new line if it exists
  118.       GpiMove(target.hpsLine,&target.ptlStart);
  119.       GpiLine(target.hpsLine,&target.ptlEnd);
  120.     }
  121.     break;
  122.   }
  123.   return WinDefWindowProc(hwnd,msg,mp1,mp2);
  124. }
  125.  
  126. int main(void) {
  127.   hab=WinInitialize(0);
  128.   hmq=WinCreateMsgQueue(hab,0);
  129.   
  130.   if (!WinRegisterClass(hab,szClassName,WinProc,CS_SIZEREDRAW,0UL)) DosExit(EXIT_PROCESS,0);
  131.   hwndFrame=WinCreateStdWindow(HWND_DESKTOP,0,&flStyle,szClassName,"",CS_SIZEREDRAW,0UL,1,&hwndClient);
  132.   if (!hwndFrame) DosExit(EXIT_PROCESS,0);
  133.  
  134.   WinSetWindowText(hwndFrame,szWinTitle);
  135.   hwndTitleBar=WinWindowFromID(hwndFrame,FID_TITLEBAR);
  136. // We must display the window before we can get the height of the title bar
  137.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT,SWP_ACTIVATE|SWP_MOVE|SWP_SHOW|SWP_SIZE);
  138.   if (!WinQueryWindowRect(hwndTitleBar,&rclTitleBar)) DosExit(EXIT_PROCESS,0);
  139. // Now that we have the title bar height, re-size the window again.
  140.   WinSetWindowPos(hwndFrame,0,0,0,WINDOW_WIDTH,WINDOW_HEIGHT+rclTitleBar.yTop,SWP_SIZE);
  141.  
  142.   while (WinGetMsg(hab,&qmsg,0,0,0)) WinDispatchMsg(hab,&qmsg);
  143.  
  144.   WinDestroyWindow(hwndFrame);
  145.   WinDestroyMsgQueue(hmq);
  146.   WinTerminate(hab);
  147.   return 0;
  148. }
  149.