home *** CD-ROM | disk | FTP | other *** search
/ MORE WinGames / WINGAMES.iso / chess / board.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  6KB  |  243 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30  Daryl Baker
  5.  
  6.  
  7.   Based on Ideas and code segments of Charles Petzold from artices in
  8.   MicroSoft Systems Journal January 1990 Vol 5 Number1.
  9.  
  10.  
  11.   This file is part of CHESS.
  12.  
  13.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  14.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  15.   the consequences of using it or for whether it serves any particular
  16.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  17.   General Public License for full details.
  18.  
  19.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  20.   only under the conditions described in the CHESS General Public License.
  21.   A copy of this license is supposed to have been given to you along with
  22.   CHESS so you can know your rights and responsibilities.  It should be in a
  23.   file named COPYING.  Among other things, the copyright notice and this
  24.   notice must be preserved on all copies.
  25. */
  26.  
  27. #define NOATOM 
  28. #define NOCLIPBOARD
  29. #define NOCREATESTRUCT
  30. #define NOSOUND
  31. #define NOWH
  32. #define NOWINOFFSETS
  33. #define NOCOMM
  34. #define NOKANJI
  35.  
  36. #include <windows.h>
  37. #include <stdio.h>
  38. #include "defs.h"
  39.  
  40. /* All units defined in pixels */
  41.  
  42. #define BRD_HORZFRONT   48
  43. #define BRD_HORZBACK    32
  44. #define BRD_VERT        32
  45. #define BRD_EDGE        8
  46. #define BRD_HORZMARGIN  32
  47. #define BRD_BACKMARGIN  5
  48. #define BRD_FRONTMARGIN 5
  49.  
  50. static void _near DrawOneSquare ( HDC hDC, short x, short y);
  51. static int HilitSq;
  52.  
  53. void QueryBoardSize ( POINT *pptl )
  54. {
  55.    pptl->x = 2*BRD_HORZMARGIN + 8*BRD_HORZFRONT;
  56.    pptl->y = BRD_BACKMARGIN + 8*BRD_VERT + 2*BRD_FRONTMARGIN + 2*BRD_EDGE;
  57. }
  58.  
  59. void QuerySqSize ( POINT *pptl ) {
  60.    pptl->x = BRD_HORZFRONT;
  61.    pptl->y = BRD_VERT;
  62. }
  63.  
  64. void QuerySqOrigin ( short x, short y, POINT *pptl)
  65. {
  66.    pptl->x = BRD_HORZMARGIN + y * (BRD_HORZFRONT-BRD_HORZBACK)/2 +
  67.              x * (y*BRD_HORZBACK + (8-y)*BRD_HORZFRONT)/8;
  68.    pptl->y = (BRD_BACKMARGIN+8*BRD_VERT+BRD_FRONTMARGIN)  - y*BRD_VERT;
  69. }
  70.  
  71. void QuerySqCoords ( short x, short y, POINT aptl[] )
  72. {
  73.    QuerySqOrigin ( x,  y,  aptl+0);
  74.    QuerySqOrigin ( x+1,y,  aptl+1);
  75.    QuerySqOrigin ( x+1,y+1,aptl+2);
  76.    QuerySqOrigin ( x,  y+1,aptl+3);
  77. }
  78.  
  79. static void _near DrawOneSquare ( HDC hDC, short x, short y)
  80. {
  81.    POINT aptl[4];
  82.  
  83.    QuerySqCoords ( x,y, aptl);
  84.    Polygon( hDC, aptl, 4);
  85. }
  86.  
  87. /*
  88.    Draw the board.  Pass the routine the upper left connor and the
  89.    colors to draw the squares.
  90. */
  91.  
  92. void Draw_Board ( HDC hDC, int reverse,
  93.                   DWORD DarkColor, DWORD LightColor )
  94. {
  95.    int x, y, OldPolyMode;
  96.    HBRUSH hOldBrush, hBrush_lt, hBrush_dk;
  97.    HPEN hOldPen;
  98.    POINT aptl[4];
  99.  
  100.    hBrush_lt = CreateSolidBrush ( LightColor );
  101.    hBrush_dk = CreateSolidBrush ( DarkColor );
  102.  
  103.    hOldBrush = SelectObject ( hDC, hBrush_lt);
  104.    hOldPen   = SelectObject ( hDC, GetStockObject (BLACK_PEN) );
  105.  
  106.  
  107.    OldPolyMode = SetPolyFillMode ( hDC, WINDING );
  108.  
  109.    for (y=0; y<8; y++) {
  110.       for (x=0; x<8; x++) {
  111.          if ( reverse == 0 ) {
  112.             SelectObject ( hDC, ((x+y)&1) ? hBrush_lt : hBrush_dk);
  113.             DrawOneSquare (hDC, x, y);
  114.          } else {
  115.             SelectObject ( hDC, (((7-x)+(7-y))&1) ? hBrush_lt : hBrush_dk);
  116.             DrawOneSquare (hDC, 7-x, 7-y);
  117.          }
  118.       }
  119.    }
  120.  
  121. /* Now draw the bottom edge of the board */
  122.  
  123.    for (x=0; x<8; x++) {
  124.       QuerySqCoords ( x,0, aptl);
  125.  
  126.       aptl[2].x = aptl[1].x;
  127.       aptl[2].y = aptl[1].y + BRD_EDGE;
  128.  
  129.       aptl[3].x = aptl[0].x;
  130.       aptl[3].y = aptl[0].y + BRD_EDGE;
  131.  
  132.       SelectObject ( hDC, (x&1) ? hBrush_lt : hBrush_dk);
  133.       Polygon ( hDC, aptl, 4 );
  134.    }
  135.    SetPolyFillMode (hDC, OldPolyMode);
  136.  
  137.    SelectObject (hDC, hOldPen);
  138.    SelectObject (hDC, hOldBrush);
  139.  
  140.    DeleteObject ( hBrush_lt);
  141.    DeleteObject ( hBrush_dk);
  142. }
  143.  
  144. void DrawCoords ( HDC hDC, int reverse, DWORD clrBackGround, DWORD clrText)
  145. {
  146.    HFONT hFont, hOldFont;
  147.    int i, OldBkMode;
  148.    DWORD OldBkColor, OldTextColor;
  149.    short xchar, ychar;
  150.    POINT pt;
  151.    TEXTMETRIC tm;
  152.  
  153.    hFont = CreateFont ( 13, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
  154.                         ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
  155.                         DEFAULT_QUALITY, FIXED_PITCH | FF_SWISS, "Helv" );
  156.  
  157.    hOldFont = SelectObject ( hDC, hFont);
  158.    OldBkColor = SetBkColor ( hDC, clrBackGround);
  159.    OldBkMode = SetBkMode ( hDC, TRANSPARENT);
  160.    OldTextColor = SetTextColor (hDC, clrText);
  161.  
  162.    GetTextMetrics ( hDC, &tm);
  163.    xchar = tm.tmMaxCharWidth;
  164.    ychar = tm.tmHeight;
  165.  
  166.    for ( i=0; i<8; i++) {
  167.       QuerySqOrigin (0, i, &pt );
  168.       TextOut (hDC, pt.x-xchar, pt.y-BRD_VERT/2-ychar/2,
  169.          (reverse ? "87654321"+i : "12345678"+i) ,1);
  170.       
  171.       QuerySqOrigin (i,0, &pt );
  172.       TextOut (hDC, pt.x+BRD_HORZFRONT/2-xchar/2, pt.y+BRD_EDGE,
  173.          (reverse ? "hgfedcba"+i : "abcdefgh"+i), 1);
  174.  
  175.    }
  176.    
  177.    SelectObject (hDC, hOldFont);
  178.    DeleteObject ( hFont);
  179.  
  180.    SetBkColor ( hDC, OldBkColor);
  181.    SetBkMode ( hDC, OldBkMode);
  182.    SetTextColor (hDC, OldTextColor);
  183. }
  184.  
  185.  
  186. void DrawWindowBackGround ( HDC hDC, HWND hWnd, DWORD bkcolor)
  187. {
  188.    RECT rect;
  189.    HBRUSH hBrush, hOldBrush;
  190.  
  191.    hBrush = CreateSolidBrush ( bkcolor);
  192.    hOldBrush = SelectObject ( hDC, hBrush);
  193.    GetClientRect ( hWnd, &rect);
  194.    FillRect ( hDC, &rect, hBrush);
  195.    SelectObject ( hDC, hOldBrush);
  196.    DeleteObject ( hBrush);
  197. }
  198.  
  199. void HiliteSquare ( HWND hWnd, int Square )
  200. {
  201.    HDC hDC;
  202.    int x,y;
  203.    POINT aptl[4];
  204.    HRGN hRgn;
  205.  
  206.    y = Square / 8;
  207.    x = Square % 8;
  208.  
  209.    QuerySqCoords ( x,y, aptl+0);
  210.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  211.  
  212.    hDC = GetDC ( hWnd);
  213.    InvertRgn ( hDC, hRgn );
  214.    ReleaseDC ( hWnd, hDC );
  215.  
  216.    DeleteObject ( hRgn);
  217.    HilitSq = Square;
  218. }
  219.  
  220. void UnHiliteSquare ( HWND hWnd, int Square )
  221. {
  222.    HDC hDC;
  223.    int x,y;
  224.    POINT aptl[4];
  225.    HRGN hRgn;
  226.  
  227.    if ( HilitSq == -1 ) return;
  228.  
  229.    y = Square / 8;
  230.    x = Square % 8;
  231.  
  232.    QuerySqCoords ( x,y, aptl+0);
  233.    hRgn = CreatePolygonRgn( aptl, 4, WINDING);
  234.  
  235.    hDC = GetDC ( hWnd);
  236.    InvertRgn ( hDC, hRgn );
  237.    ReleaseDC ( hWnd, hDC );
  238.  
  239.    DeleteObject ( hRgn);
  240.  
  241.    HilitSq = -1;
  242. }
  243.