home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / board.c < prev    next >
Text File  |  1994-04-20  |  10KB  |  342 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //
  4. //  Project:    OS/2 PM Port of GNU CHESS 4.0 (PmChess)
  5. //
  6. //  Version:    1994-4-17
  7. //
  8. //   Module:    Playing Board Display (Board.c)
  9. //
  10. //   Porter:    Ported to Windows 3.0 by Darly Baker
  11. //
  12. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  13. //
  14. //   Porter:    Revised and ported to OS/2 2.1 by Yibing Fan
  15. //
  16. //   System:    OS2 2.1 using emx0.8g 
  17. //
  18. //  Remarks:    This code is based on Ideas and code segments of Charles
  19. //              Petzold from artices in Micrsoft Systems Journal.  This code
  20. //              is mostly just editing changes to convert to PM.
  21. //
  22. //   This module defines following funtions:
  23. //      static LONG DrawOneSquare ( HPS hps, short x, short y);
  24. //      void QueryBoardSize ( POINTL *pptl)
  25. //      void QuerySqSize ( POINTL *pptl) {
  26. //      void QuerySqOrigin ( short x, short y, POINTL *pptl)
  27. //      void QuerySqCoords ( short x, short y, POINTL aptl[] )
  28. //      void Draw_Board(HPS hps, int reverse, ULONG DarkColor, ULONG LightColor)
  29. //      void DrawCoords(HPS hps, int reverse, ULONG clrBackGround, ULONG clrText,SHORT coords)
  30. //      void DrawWindowBackGround(HPS hps, HWND hWnd, ULONG bkcolor)
  31. //      void HiliteSquare(HWND hWnd, int Square)
  32. //      VOID CkdQueryHitCoords (HPS hps, PPOINTL ptlMouse, SHORT *px, SHORT *py)
  33. //      void UnHiliteSquare(HWND hWnd, int Square)
  34. //
  35. //  License:
  36. //
  37. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  38. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  39. //    the consequences of using it or for whether it serves any particular
  40. //    purpose or works at all, unless he says so in writing.  Refer to the
  41. //    CHESS General Public License for full details.
  42. //
  43. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  44. //    but only under the conditions described in the CHESS General Public
  45. //    License.  A copy of this license is supposed to have been given to you
  46. //    along with CHESS so you can know your rights and responsibilities.  It
  47. //    should be in a file named COPYING.  Among other things, the copyright
  48. //    notice and this notice must be preserved on all copies.
  49. //
  50.  
  51. #define INCL_DOS
  52. #define INCL_PM
  53. #include <os2.h>
  54. #include <stdio.h>
  55. #include "PmChess.h"
  56. #include "Defs.h"
  57.  
  58.  
  59. /* All units defined in pixels */
  60.  
  61. #define BRD_HORZFRONT   48
  62. #define BRD_HORZBACK    32
  63. #define BRD_VERT        32
  64. #define BRD_EDGE        8
  65. #define BRD_HORZMARGIN  32
  66. #define BRD_BACKMARGIN  5
  67. #define BRD_FRONTMARGIN 5
  68.  
  69. extern float ScaleFactor;       /* This varible scales board size */
  70. extern short cyClient;                  /* Height of client area.*/
  71. // extern short coords;                  /* Height of client area.*/
  72.  
  73. INT brd_horzfront  ;
  74. INT brd_horzback   ;
  75. INT brd_vert       ;
  76. INT brd_edge       ;
  77. INT brd_horzmargin ;
  78. INT brd_backmargin ;
  79. INT brd_frontmargin;
  80.                                     
  81. static LONG DrawOneSquare ( HPS hps, short x, short y);
  82. static int HilitSq;
  83.  
  84. void QueryBoardSize ( POINTL *pptl)
  85. {
  86.  brd_horzfront   = BRD_HORZFRONT    * ScaleFactor;
  87.  brd_horzback    = BRD_HORZBACK     * ScaleFactor;
  88.  brd_vert        = BRD_VERT         * ScaleFactor;
  89.  brd_edge        = BRD_EDGE         * ScaleFactor;
  90.  brd_horzmargin  = BRD_HORZMARGIN   * ScaleFactor;
  91.  brd_backmargin  = BRD_BACKMARGIN   * ScaleFactor;
  92.  brd_frontmargin = BRD_FRONTMARGIN  * ScaleFactor;
  93.                                     
  94.    pptl->x = 2*brd_horzmargin + 8*brd_horzfront;
  95.    pptl->y = brd_backmargin + 8*brd_vert + 2*brd_frontmargin + 2*brd_edge;
  96. }
  97.  
  98. void QuerySqSize ( POINTL *pptl) {
  99.  brd_horzfront   = BRD_HORZFRONT    * ScaleFactor;
  100.  brd_vert        = BRD_VERT         * ScaleFactor;
  101.                                     
  102.    pptl->x = brd_horzfront; 
  103.    pptl->y = brd_vert;      
  104. }
  105.  
  106. void QuerySqOrigin ( short x, short y, POINTL *pptl)
  107. {
  108.  brd_horzfront   = BRD_HORZFRONT    * ScaleFactor;
  109.  brd_horzback    = BRD_HORZBACK     * ScaleFactor;
  110.  brd_vert        = BRD_VERT         * ScaleFactor;
  111.  brd_edge        = BRD_EDGE         * ScaleFactor;
  112.  brd_horzmargin  = BRD_HORZMARGIN   * ScaleFactor;
  113.  brd_backmargin  = BRD_BACKMARGIN   * ScaleFactor;
  114.  brd_frontmargin = BRD_FRONTMARGIN  * ScaleFactor;
  115.                                     
  116.    pptl->x = brd_horzmargin + y * (brd_horzfront-brd_horzback)/2 +     
  117.              x * (y*brd_horzback + (8-y)*brd_horzfront)/8;             
  118.    pptl->y = (brd_backmargin+8*brd_vert+brd_frontmargin)  - y*brd_vert;
  119.  
  120.    pptl->y = cyClient - pptl->y;
  121. }
  122.  
  123. void QuerySqCoords ( short x, short y, POINTL aptl[] )
  124. {
  125.    QuerySqOrigin ( x,  y,  aptl+0 );
  126.    QuerySqOrigin ( x+1,y,  aptl+1 );
  127.    QuerySqOrigin ( x+1,y+1,aptl+2 );
  128.    QuerySqOrigin ( x,  y+1,aptl+3 );
  129. }
  130.  
  131. static LONG DrawOneSquare(HPS hps, short x, short y )
  132.   {
  133.   POINTL     aptl[4] ;
  134.   GpiBeginArea (hps, BA_ALTERNATE | BA_BOUNDARY) ;
  135.  
  136.   QuerySqCoords (x, y, aptl) ;
  137.  
  138.   GpiMove (hps, aptl + 3) ;
  139.   GpiPolyLine (hps, 4L, aptl) ;
  140.   return (GpiEndArea (hps));
  141.   }
  142.  
  143.  
  144. /*
  145.    Draw the board.  Pass the routine the upper left connor and the
  146.    colors to draw the squares.
  147. */
  148.  
  149. void Draw_Board(HPS hps, int reverse, ULONG DarkColor, ULONG LightColor)
  150.   {
  151.   AREABUNDLE abnd ;
  152.   LINEBUNDLE lbnd ;
  153.   POINTL     aptl[32];
  154.   int x, y;
  155.  
  156.  brd_horzfront   = BRD_HORZFRONT    * ScaleFactor;
  157.  brd_horzback    = BRD_HORZBACK     * ScaleFactor;
  158.  brd_vert        = BRD_VERT         * ScaleFactor;
  159.  brd_edge        = BRD_EDGE         * ScaleFactor;
  160.  brd_horzmargin  = BRD_HORZMARGIN   * ScaleFactor;
  161.  brd_backmargin  = BRD_BACKMARGIN   * ScaleFactor;
  162.  brd_frontmargin = BRD_FRONTMARGIN  * ScaleFactor;
  163.                                     
  164.   GpiSavePS(hps) ;
  165.  
  166.   lbnd.lColor = CLR_BLACK ;
  167.   GpiSetAttrs(hps, PRIM_LINE, LBB_COLOR, 0L, &lbnd);
  168.  
  169.   for (y=0; y<8; y++) {
  170.     for (x=0; x<8; x++) {
  171.       if ( reverse == 0 ) {
  172.         abnd.lColor = (x + y) & 1 ? LightColor : DarkColor;
  173.         GpiSetAttrs (hps, PRIM_AREA, LBB_COLOR, 0L, &abnd) ;
  174.         DrawOneSquare (hps, x, y);
  175.       } else {
  176.         abnd.lColor = ((7-x) + (7-y)) & 1 ? LightColor : DarkColor;
  177.         GpiSetAttrs (hps, PRIM_AREA, LBB_COLOR, 0L, &abnd) ;
  178.         DrawOneSquare(hps, 7-x, 7-y);
  179.       }
  180.     }
  181.  
  182.   GpiRestorePS(hps, 1);
  183.   }
  184.  
  185. /* Now draw the bottom edge of the board */
  186.  
  187.    for (x=0; x<8; x++) {
  188.       QuerySqCoords ( x,0, aptl);
  189.  
  190.       aptl[2].x = aptl[1].x;
  191.       aptl[2].y = aptl[1].y - brd_edge;
  192.  
  193.       aptl[3].x = aptl[0].x;
  194.       aptl[3].y = aptl[0].y - brd_edge;
  195.  
  196.       abnd.lColor = (x & 1) ? LightColor : DarkColor;
  197.       GpiSetAttrs (hps, PRIM_AREA, LBB_COLOR, 0L, &abnd) ;
  198.  
  199.       GpiBeginArea (hps, BA_ALTERNATE | BA_BOUNDARY) ;
  200.  
  201.       GpiMove (hps, aptl + 3) ;
  202.       GpiPolyLine(hps, 4L, aptl) ;
  203.       GpiEndArea(hps) ;
  204.    }
  205.  
  206.   GpiRestorePS (hps, -1L) ;
  207.   }
  208.  
  209.  
  210. void DrawCoords(HPS hps, int reverse, ULONG clrBackGround, ULONG clrText,SHORT coords)
  211.   {
  212.   short  xchar, ychar;
  213.   FONTMETRICS fm;
  214.   POINTL ptl;
  215.   short  i;
  216.  
  217.  brd_horzfront   = BRD_HORZFRONT    * ScaleFactor;
  218.  brd_horzback    = BRD_HORZBACK     * ScaleFactor;
  219.  brd_vert        = BRD_VERT         * ScaleFactor;
  220.  brd_edge        = BRD_EDGE         * ScaleFactor;
  221.  brd_horzmargin  = BRD_HORZMARGIN   * ScaleFactor;
  222.  brd_backmargin  = BRD_BACKMARGIN   * ScaleFactor;
  223.  brd_frontmargin = BRD_FRONTMARGIN  * ScaleFactor;
  224.                                     
  225.  
  226.   //
  227.   //  Load the diamensions of the default system font.
  228.   //
  229.   GpiQueryFontMetrics(hps, sizeof(fm), &fm);
  230.   xchar = (SHORT)fm.lEmInc;
  231.   ychar = (SHORT)fm.lMaxBaselineExt+(SHORT)fm.lExternalLeading;
  232.   if (coords) {
  233.         GpiSetColor(hps, clrText);
  234.   } else {
  235.         GpiSetColor(hps, clrBackGround);
  236.   } /* endif */
  237.   GpiSetBackColor(hps, clrBackGround);
  238.  
  239.    for ( i=0; i<8; i++) {
  240.       QuerySqOrigin (0, i, &ptl);
  241.       ptl.x -= xchar;
  242.       ptl.y -= brd_vert/2 - ychar * 2; // -ychar/2;
  243.       GpiCharStringAt(hps, &ptl, 1,
  244.          (PCHAR)(reverse ? "87654321"+i : "12345678"+i));
  245.       
  246.       QuerySqOrigin (i,0, &ptl);
  247.       ptl.x += brd_horzfront/2-xchar/2;
  248.       ptl.y -= brd_edge + ychar;
  249.       GpiCharStringAt(hps, &ptl, 1,
  250.          (PCHAR)(reverse ? "hgfedcba"+i : "abcdefgh"+i));
  251.    }
  252.   }
  253.  
  254.  
  255. void DrawWindowBackGround(HPS hps, HWND hWnd, ULONG bkcolor)
  256.   {
  257.   RECTL rcl ;
  258.  
  259.   WinQueryWindowRect(hWnd, &rcl) ;
  260.   WinFillRect(hps, &rcl, bkcolor);
  261.   }
  262.  
  263.  
  264. void HiliteSquare(HWND hWnd, int Square)
  265.   {
  266.   HPS hps;
  267.   short x, y;
  268.  
  269.  
  270.   hps = WinGetPS(hWnd);
  271.  
  272.   y = Square / 8;
  273.   x = Square % 8;
  274.  
  275.   GpiSetMix(hps, FM_INVERT);
  276.  
  277.   DrawOneSquare(hps, x, y);
  278.  
  279.   WinReleasePS(hps);
  280.  
  281.   HilitSq = Square;
  282.   }
  283.  
  284. void UnHiliteSquare(HWND hWnd, int Square)
  285.   {
  286.   HPS hps;
  287.   short x, y;
  288.  
  289.  
  290.   if (HilitSq == -1)
  291.     return;
  292.  
  293.   hps = WinGetPS(hWnd);
  294.  
  295.   y = Square / 8;
  296.   x = Square % 8;
  297.  
  298.   GpiSetMix(hps, FM_INVERT);
  299.  
  300.   DrawOneSquare(hps, x, y);
  301.  
  302.   WinReleasePS(hps);
  303.  
  304.   HilitSq = -1;
  305.   }
  306.  
  307.  
  308. /**********************************************************************
  309. *       CkdQueryHitCoords: Obtains coords from mouse pointer position
  310. \**********************************************************************/
  311.  
  312. VOID CkdQueryHitCoords (HPS hps, PPOINTL ptlMouse, SHORT *px, SHORT *py)
  313.      {
  314.      SIZEL sizlAperture;
  315.  
  316.  
  317.      sizlAperture.cx = 1 ;
  318.      sizlAperture.cy = 1 ;
  319.      GpiSetPickApertureSize (hps, PICKAP_REC, &sizlAperture) ;
  320.  
  321.      GpiSetPickAperturePosition (hps, ptlMouse) ;
  322.  
  323.      GpiSetDrawControl (hps, DCTL_DISPLAY,   DCTL_OFF) ;
  324.      GpiSetDrawControl (hps, DCTL_CORRELATE, DCTL_ON) ;
  325.  
  326.      for (*py = 0; *py < 8; (*py)++)
  327.         {
  328.         for (*px = 0; *px < 8; (*px)++)
  329.           {
  330.           if (DrawOneSquare(hps, *px, *py) == GPI_HITS)
  331.             goto Done;
  332.           }
  333.         }
  334.  
  335.      *px = -1;
  336.      *py = -1;
  337.  
  338. Done:
  339.      GpiSetDrawControl (hps, DCTL_DISPLAY,   DCTL_ON) ;
  340.      GpiSetDrawControl (hps, DCTL_CORRELATE, DCTL_OFF) ;
  341.      }
  342.