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

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   Based on Ideas and code segments of Charles Petzold from artices in
  9.   MicroSoft Systems Journal.
  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 NOFONT
  31. #define NOREGION
  32. #define NOSOUND
  33. #define NOWH
  34. #define NOWINOFFSETS
  35. #define NOCOMM
  36. #define NOKANJI
  37.  
  38. #include <windows.h>
  39. #include <stdio.h>
  40.  
  41. #include "chess.h"
  42. #include "defs.h"
  43.  
  44. extern struct PIECEBITMAP pieces[6];
  45.  
  46. #define PIECE_XAXIS 32
  47. #define PIECE_YAXIS 32
  48.  
  49. static void _near QuerySqCenter ( short x, short y, POINT *pptl);
  50. static short _near ConvertCoordToIndex ( short x, short y);
  51. static void _near PieceOriginFromCenter ( POINT *pptl);
  52. static void _near QuerySqPieceOrigin ( short x, short y, POINT *pptl);
  53. static void _near DrawOnePiece ( HDC hDC, short x, short y, struct PIECEBITMAP *piece, DWORD color);
  54. static void _near ShowPiece ( HDC hDC, POINT *pptl, struct PIECEBITMAP *Piece_bitmap,
  55.                   DWORD Color );
  56.  
  57. static void _near QuerySqCenter ( short x, short y, POINT *pptl)
  58. {
  59.    POINT aptl[4];
  60.  
  61.    QuerySqCoords ( x, y, aptl );
  62.  
  63.    pptl->x = (aptl[0].x + aptl[1].x + aptl[2].x + aptl[3].x)/4;
  64.    pptl->y = (aptl[0].y + aptl[2].y)/2;
  65. }
  66.  
  67.  
  68. static void _near PieceOriginFromCenter ( POINT *pptl)
  69. {
  70.    pptl->x -= PIECE_XAXIS / 2;
  71.    pptl->y -= PIECE_YAXIS / 2;
  72. }
  73.  
  74. static void _near QuerySqPieceOrigin ( short x, short y, POINT *pptl)
  75. {
  76.       QuerySqCenter ( x, y, pptl);
  77.       PieceOriginFromCenter (pptl);
  78. }
  79.  
  80.  
  81. /*
  82.    Draw a piece in the specificed point
  83.  
  84.    Piece_bitmap is a structure with the handles for the mask,
  85.    outline and piece.
  86.  
  87. */
  88.  
  89. static void _near ShowPiece ( HDC hDC, POINT *pptl, struct PIECEBITMAP *Piece_bitmap,
  90.                   DWORD Color )
  91. {
  92.    HDC hMemDC;
  93.    HBRUSH hBrush, hOldBrush;
  94.    HPEN hOldPen;
  95.  
  96.  
  97.    hOldBrush = SelectObject ( hDC, GetStockObject (BLACK_BRUSH) );
  98.    hOldPen = SelectObject ( hDC, GetStockObject ( BLACK_PEN) );
  99.    
  100.    hMemDC = CreateCompatibleDC ( hDC);
  101.  
  102.    /* Write the mask to clear the space */
  103.    SelectObject ( hMemDC, Piece_bitmap->mask);
  104.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0,SRCAND);
  105.  
  106.    /* Write out the piece with an OR */
  107.  
  108.    hBrush = CreateSolidBrush ( Color );
  109.    SelectObject ( hDC, hBrush );
  110.  
  111.    SelectObject ( hMemDC, Piece_bitmap->piece);
  112.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0,0xB80746L);
  113.  
  114.    /* The draw the outline */
  115.  
  116.    SelectObject ( hDC, GetStockObject ( BLACK_BRUSH ) );
  117.    SelectObject ( hMemDC, Piece_bitmap->outline);
  118.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0, 0xB80746L);
  119.  
  120.  
  121.    SelectObject ( hDC, hOldBrush );
  122.    SelectObject ( hDC, hOldPen );
  123.    DeleteObject ( hBrush);
  124.  
  125.    if ( DeleteDC(hMemDC)==0) MessageBeep(0);
  126. }
  127.  
  128. static short _near ConvertCoordToIndex ( short x, short y)
  129. {
  130.    return (y*8 + x );
  131. }
  132.  
  133. static void _near DrawOnePiece ( HDC hDC, short x, short y, struct PIECEBITMAP *piece, DWORD color)
  134. {
  135.    POINT origin;
  136.  
  137.    QuerySqPieceOrigin ( x, y, &origin );
  138.    ShowPiece ( hDC, &origin, piece, color);
  139.  
  140. }
  141.  
  142.  
  143.  
  144. void DrawAllPieces ( HDC hDC,  int reverse, short *pbrd, short *color,
  145.                      DWORD clrblack, DWORD clrwhite )
  146. {
  147.    short x,y;
  148.    short i;
  149.  
  150.    for ( y=0; y<8; y++) {
  151.       for (x=0; x<8; x++) {
  152.          i = ConvertCoordToIndex (x, y);
  153.          if ( *(color+i) != NETURAL ) {
  154.             if (reverse == 0)
  155.                DrawOnePiece ( hDC, x, y, pieces+*(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  156.             else
  157.                DrawOnePiece ( hDC, 7-x, 7-y, pieces+*(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  158.          }
  159.       }
  160.    }
  161. }
  162.  
  163.