home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / PMCHESSR.ZIP / PIECE.C < prev    next >
C/C++ Source or Header  |  1990-12-01  |  8KB  |  292 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:    Piece Display (Piece.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 "GnuChess.h"
  42. #include "Defs.h"
  43. #include "Resource.h"
  44.  
  45.  
  46. //
  47. //  Define local variables.
  48. //
  49. static USHORT cxBitmap, cyBitmap;           // Size of the chess piece bitmap.
  50.  
  51.  
  52. #define PIECE_XAXIS 32
  53. #define PIECE_YAXIS 32
  54.  
  55.  
  56. static void  QuerySqCenter (short x, short y, POINTL *pptl);
  57. static short ConvertCoordToIndex ( short x, short y);
  58. static void  PieceOriginFromCenter ( POINTL *pptl);
  59. static void  QuerySqPieceOrigin ( short x, short y, POINTL *pptl);
  60. static void  DrawOnePiece ( HPS hps, HPS hpsPieces, short x, short y, short piece, ULONG color);
  61. static void  ShowPiece(HPS hps, HPS hpsPieces, POINTL *pptl, short piece,
  62.                   ULONG Color );
  63.  
  64.  
  65. //***************************************************************************
  66. //
  67. //  Routine: LoadChessPieces(In):Original Code by Kent Cedola
  68. //
  69. //  Remarks: This routine is called during the initialization process to load
  70. //           the bitmaps used to draw the various chess pieces into a memory
  71. //           presentation space.  This permits us to use fast GpiBitBlt's
  72. //           without the overhead of moving each bitmap into a memory ps.
  73. //
  74. //  Returns: Handle of memory presentation space.
  75. //
  76. HPS LoadChessPieces(HAB hab)
  77.   {
  78.   HDC     hdc, hdcTemp;
  79.   HPS     hps, hpsTemp;
  80.   HBITMAP hbm;
  81.  
  82. #ifdef M_I386
  83.   BITMAPINFOHEADER2 bmp;
  84. #else
  85.   BITMAPINFOHEADER bmp;
  86. #endif
  87.  
  88.   POINTL  aptl[3];
  89.   SIZEL   sizel;
  90.   SHORT   row, col;
  91.  
  92.  
  93.   //
  94.   //  Allocate a tempoary presentation space (just for loading bitmaps).
  95.   //
  96.   hdcTemp  = DevOpenDC(hab, OD_MEMORY, "*", 0L, NULL, NULL);
  97.   sizel.cx = 0L;
  98.   sizel.cy = 0L;
  99.   hpsTemp  = GpiCreatePS(hab, hdcTemp, &sizel,
  100.                          PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  101.  
  102.   //
  103.   //  Load and release a bitmap piece to determine the size of it.  Thus we
  104.   //  will know how big to create the monochrome bitmap to hold them all.
  105.   //
  106.   hbm = GpiLoadBitmap(hpsTemp, 0, IDB_PAWNBASE + 1, 0, 0);
  107.   bmp.cbFix = sizeof(bmp);
  108.  
  109. #ifdef M_I386
  110.   GpiQueryBitmapInfoHeader(hbm, &bmp);
  111. #else
  112.   GpiQueryBitmapParameters(hbm, &bmp);
  113. #endif
  114.  
  115.   cxBitmap = bmp.cx;
  116.   cyBitmap = bmp.cy;
  117.  
  118.   GpiDeleteBitmap(hbm);
  119.  
  120.   //
  121.   //  Allocate memory presentation space.
  122.   //
  123.   hdc = DevOpenDC(hab, OD_MEMORY, "*", 0L, NULL, NULL);
  124.   sizel.cx = 0L;
  125.   sizel.cy = 0L;
  126.   hps = GpiCreatePS(hab, hdc, &sizel,
  127.                     PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  128.  
  129.   //
  130.   //  Create the master bitmap that will contain all other bitmaps.
  131.   //
  132.   bmp.cbFix     = sizeof(bmp);
  133.   bmp.cx        = cxBitmap * 6;
  134.   bmp.cy        = cyBitmap * 3;
  135.   bmp.cPlanes   = 1;
  136.   bmp.cBitCount = 1;
  137.  
  138.   hbm = GpiCreateBitmap(hps, &bmp, 0, 0, 0);
  139.   GpiSetBitmap(hps, hbm);
  140.  
  141.   //
  142.   //  Load each bitmap and copy over to the pieces memory presentation space.
  143.   //
  144.   aptl[2].x = 0;
  145.   aptl[2].y = 0;
  146.   for (row = 0; row < 3; row++)
  147.     {
  148.     for (col = pawn; col <= king; col++)
  149.       {
  150.       hbm = GpiLoadBitmap(hpsTemp, 0, IDB_PAWNBASE + row * king + col, 0, 0);
  151.  
  152.       GpiSetBitmap(hpsTemp, hbm);
  153.  
  154.       aptl[0].x = cxBitmap * (col - 1);
  155.       aptl[0].y = cyBitmap * row;
  156.       aptl[1].x = aptl[0].x + cxBitmap;
  157.       aptl[1].y = aptl[0].y + cyBitmap;
  158.  
  159.       GpiBitBlt(hps, hpsTemp, 3, aptl, ROP_SRCCOPY, 0);
  160.  
  161.       GpiSetBitmap(hpsTemp, NULL);
  162.       GpiDeleteBitmap(hbm);
  163.       }
  164.     }
  165.  
  166.   //
  167.   //  Free the tempoary memory presentation space.
  168.   //
  169.   GpiDestroyPS(hpsTemp);
  170.   DevCloseDC(hdcTemp);
  171.  
  172.   return (hps);
  173.   }
  174.  
  175.  
  176. static void QuerySqCenter ( short x, short y, POINTL *pptl)
  177. {
  178.    POINTL aptl[4];
  179.  
  180.    QuerySqCoords ( x, y, aptl );
  181.  
  182.    pptl->x = (aptl[0].x + aptl[1].x + aptl[2].x + aptl[3].x)/4;
  183.    pptl->y = (aptl[0].y + aptl[2].y)/2;
  184. }
  185.  
  186.  
  187. static void PieceOriginFromCenter ( POINTL *pptl)
  188. {
  189.    pptl->x -= PIECE_XAXIS / 2;
  190.    pptl->y -= PIECE_YAXIS / 2;
  191. }
  192.  
  193. static void QuerySqPieceOrigin ( short x, short y, POINTL *pptl)
  194. {
  195.       QuerySqCenter ( x, y, pptl);
  196.       PieceOriginFromCenter (pptl);
  197. }
  198.  
  199.  
  200. /*
  201.    Draw a piece in the specificed point
  202.  
  203.    Piece_bitmap is a structure with the handles for the mask,
  204.    outline and piece.
  205.  
  206. */
  207.  
  208. static void ShowPiece(HPS hps, HPS hpsPieces, POINTL *pptl, short piece,
  209.                       ULONG Color)
  210.   {
  211.   POINTL aptl[3];
  212.  
  213.  
  214.   //
  215.   //  Setup the destination to write out the chess piece.
  216.   //
  217.   aptl[0].x = pptl->x;
  218.   aptl[0].y = pptl->y;
  219.   aptl[1].x = pptl->x + cxBitmap;
  220.   aptl[1].y = pptl->y + cyBitmap;
  221.  
  222.   //
  223.   //  Select the bitmap for the specified chess piece.
  224.   //
  225.   aptl[2].x = (piece - 1) * cxBitmap;
  226.  
  227.   //
  228.   //  Mask out the space so we can store a multi-color chess piece.
  229.   //
  230.   aptl[2].y = 0;
  231.   GpiSetColor(hps, CLR_WHITE);
  232.   GpiSetBackColor(hps, CLR_BLACK);
  233.   GpiBitBlt(hps, hpsPieces, 3, aptl, ROP_SRCAND, 0);
  234.  
  235.   //
  236.   //  Fill in the piece's color.
  237.   //
  238.   aptl[2].y = 32;
  239.   GpiSetColor(hps, CLR_BLACK);
  240.   GpiSetBackColor(hps, Color);
  241.   GpiBitBlt(hps, hpsPieces, 3, aptl, ROP_SRCPAINT, 0);
  242.  
  243.   //
  244.   //  Draw the piece's outline.
  245.   //
  246.   aptl[2].y = 64;
  247.   GpiSetColor(hps, CLR_WHITE);
  248.   GpiSetBackColor(hps, CLR_BLACK);
  249.   GpiBitBlt(hps, hpsPieces, 3, aptl, ROP_SRCAND, 0);
  250.   }
  251.  
  252.  
  253. static short ConvertCoordToIndex ( short x, short y)
  254. {
  255.    return (y*8 + x );
  256. }
  257.  
  258. static void DrawOnePiece(HPS hps, HPS hpsPieces, short x, short y, short piece, ULONG color)
  259.   {
  260.   POINTL origin;
  261.  
  262.  
  263.   QuerySqPieceOrigin(x, y, &origin);
  264.   ShowPiece (hps, hpsPieces, &origin, piece, color);
  265.   }
  266.  
  267.  
  268.  
  269. void DrawAllPieces(HPS hps, HPS hpsPieces, short reverse, short *pbrd, short *color,
  270.                      ULONG clrblack, ULONG clrwhite )
  271.   {
  272.   short x,y;
  273.   short i;
  274.  
  275.  
  276.   for ( y=0; y<8; y++)
  277.     {
  278.     for (x=0; x<8; x++)
  279.       {
  280.       i = ConvertCoordToIndex(x, y);
  281.  
  282.       if ( *(color+i) != NETURAL)
  283.         {
  284.         if (reverse == 0)
  285.           DrawOnePiece(hps, hpsPieces, x, y, *(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  286.         else
  287.           DrawOnePiece(hps, hpsPieces, 7-x, 7-y, *(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  288.         }
  289.       }
  290.     }
  291.   }
  292.