home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / pmchessr / board.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-01  |  6.8 KB  |  281 lines

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