home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / piece.c < prev    next >
Text File  |  1994-04-20  |  9KB  |  302 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:    1994-4-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. //   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. //  Fuctions defined in this module:
  23. //
  24. //      static void  QuerySqCenter (short x, short y, POINTL *pptl);
  25. //      static short ConvertCoordToIndex ( short x, short y);
  26. //      static void  PieceOriginFromCenter ( POINTL *pptl);
  27. //      static void  QuerySqPieceOrigin ( short x, short y, POINTL *pptl);
  28. //      static void  DrawOnePiece ( HPS hps, HPS hpsPieces, short x, short y, short piece, ULONG color);
  29. //      static void  ShowPiece(HPS hps, HPS hpsPieces, POINTL *pptl, short piece,ULONG Color ); 
  30. //                      
  31. //  License:
  32. //
  33. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  34. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  35. //    the consequences of using it or for whether it serves any particular
  36. //    purpose or works at all, unless he says so in writing.  Refer to the
  37. //    CHESS General Public License for full details.
  38. //
  39. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  40. //    but only under the conditions described in the CHESS General Public
  41. //    License.  A copy of this license is supposed to have been given to you
  42. //    along with CHESS so you can know your rights and responsibilities.  It
  43. //    should be in a file named COPYING.  Among other things, the copyright
  44. //    notice and this notice must be preserved on all copies.
  45. //
  46.  
  47. #define INCL_DOS
  48. #define INCL_PM
  49. #define INCL_WIN
  50. #define INCL_WINERRORS
  51. #include <os2.h>
  52. #include <stdio.h>
  53. #include "PmChess.h"
  54. #include "GnuChess.h"
  55. #include "Defs.h"
  56. #include "Resource.h"
  57.  
  58.  
  59. //
  60. //  Define local variables.
  61. //
  62. static USHORT cxBitmap, cyBitmap;           // Size of the chess piece bitmap.
  63. extern float ScaleFactor;
  64.  
  65. #define PIECE_XAXIS 32
  66. #define PIECE_YAXIS 32
  67.  
  68.  
  69. static void  QuerySqCenter (short x, short y, POINTL *pptl);
  70. static short ConvertCoordToIndex ( short x, short y);
  71. static void  PieceOriginFromCenter ( POINTL *pptl);
  72. static void  QuerySqPieceOrigin ( short x, short y, POINTL *pptl);
  73. static void  DrawOnePiece ( HPS hps, HPS hpsPieces, short x, short y, short piece, ULONG color);
  74. static void  ShowPiece(HPS hps, HPS hpsPieces, POINTL *pptl, short piece, ULONG Color ); 
  75.                  
  76. //***************************************************************************
  77. //
  78. //  Routine: LoadChessPieces(In):Original Code by Kent Cedola
  79. //
  80. //  Remarks: This routine is called during the initialization process to load
  81. //           the bitmaps used to draw the various chess pieces into a memory
  82. //           presentation space.  This permits us to use fast GpiBitBlt's
  83. //           without the overhead of moving each bitmap into a memory ps.
  84. //
  85. //  Returns: Handle of memory presentation space.
  86. //
  87. HPS LoadChessPieces(HAB hab)
  88.   {
  89.   HDC     hdc, hdcTemp;
  90.   HPS     hps, hpsTemp;
  91.   HBITMAP hbm;
  92.  
  93.   BITMAPINFOHEADER2 bmp;
  94.  
  95.   POINTL  aptl[3];
  96.   SIZEL   sizel;
  97.   SHORT   row, col;
  98.  
  99.  
  100.   //
  101.   //  Allocate a tempoary presentation space (just for loading bitmaps).
  102.   //
  103.   hdcTemp  = DevOpenDC(hab, OD_MEMORY, "*", 0L, NULL, (HDC)NULL);
  104.   sizel.cx = 0L;
  105.   sizel.cy = 0L;
  106.   hpsTemp  = GpiCreatePS(hab, hdcTemp, &sizel,
  107.                          PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  108.  
  109.   //
  110.   //  Load and release a bitmap piece to determine the size of it.  Thus we
  111.   //  will know how big to create the monochrome bitmap to hold them all.
  112.   //
  113.   hbm = GpiLoadBitmap(hpsTemp, 0, IDB_PAWNBASE + 1, 0, 0);
  114.   bmp.cbFix = sizeof(bmp);
  115.  
  116.   GpiQueryBitmapInfoHeader(hbm, &bmp);
  117.  
  118.   cxBitmap = bmp.cx;
  119.   cyBitmap = bmp.cy;
  120.  
  121.   GpiDeleteBitmap(hbm);
  122.  
  123.   //
  124.   //  Allocate memory presentation space.
  125.   //
  126.   hdc = DevOpenDC(hab, OD_MEMORY, "*", 0L, NULL, (HDC)NULL);
  127.   sizel.cx = 0L;
  128.   sizel.cy = 0L;
  129.   hps = GpiCreatePS(hab, hdc, &sizel,
  130.                     PU_PELS | GPIF_DEFAULT | GPIT_MICRO | GPIA_ASSOC);
  131.  
  132.   //
  133.   //  Create the master bitmap that will contain all other bitmaps.
  134.   //
  135.   bmp.cbFix     = sizeof(bmp);
  136.   bmp.cx        = cxBitmap * 6;
  137.   bmp.cy        = cyBitmap * 3;
  138.   bmp.cPlanes   = 1;
  139.   bmp.cBitCount = 1;
  140.  
  141.   hbm = GpiCreateBitmap(hps, &bmp, 0, 0, 0);
  142.   GpiSetBitmap(hps, hbm);
  143.  
  144.   //
  145.   //  Load each bitmap and copy over to the pieces memory presentation space.
  146.   //
  147.   aptl[2].x = 0;
  148.   aptl[2].y = 0;
  149.   for (row = 0; row < 3; row++)
  150.     {
  151.     for (col = pawn; col <= king; col++)
  152.       {
  153.       hbm = GpiLoadBitmap(hpsTemp, 0, IDB_PAWNBASE + row * king + col, 0, 0);
  154.  
  155.       GpiSetBitmap(hpsTemp, hbm);
  156.  
  157.       aptl[0].x = cxBitmap * (col - 1);
  158.       aptl[0].y = cyBitmap * row;
  159.       aptl[1].x = aptl[0].x + cxBitmap;
  160.       aptl[1].y = aptl[0].y + cyBitmap;
  161.  
  162.       GpiBitBlt(hps, hpsTemp, 3, aptl, ROP_SRCCOPY, 0);
  163.  
  164.       GpiSetBitmap(hpsTemp, (HBITMAP)NULL);
  165.       GpiDeleteBitmap(hbm);
  166.       }
  167.     }
  168.  
  169.   //
  170.   //  Free the tempoary memory presentation space.
  171.   //
  172.   GpiDestroyPS(hpsTemp);
  173.   DevCloseDC(hdcTemp);
  174.  
  175.   return (hps);
  176.   }
  177.  
  178.  
  179. static void QuerySqCenter ( short x, short y, POINTL *pptl)
  180. {
  181.    POINTL aptl[4];
  182.  
  183.    QuerySqCoords ( x, y, aptl );
  184.  
  185.    pptl->x = (aptl[0].x + aptl[1].x + aptl[2].x + aptl[3].x)/4;
  186.    pptl->y = (aptl[0].y + aptl[2].y)/2;
  187. }
  188.  
  189.  
  190. static void PieceOriginFromCenter ( POINTL *pptl)
  191. {
  192.    pptl->x -= (SHORT)(PIECE_XAXIS*ScaleFactor / 2);
  193.    pptl->y -= (SHORT)(PIECE_YAXIS*ScaleFactor / 2);
  194. }
  195.  
  196. static void QuerySqPieceOrigin ( short x, short y, POINTL *pptl)
  197. {
  198.       QuerySqCenter ( x, y, pptl);
  199.       PieceOriginFromCenter (pptl);
  200. }
  201.  
  202.  
  203. /*
  204.    Draw a piece in the specificed point
  205.  
  206.    Piece_bitmap is a structure with the handles for the mask,
  207.    outline and piece.
  208.  
  209. */
  210.  
  211. static void ShowPiece(HPS hps, HPS hpsPieces, POINTL *pptl, short piece,
  212.                       ULONG Color)
  213.   {
  214.   SIZEL   sizel;
  215.   POINTL aptl[4];
  216.  
  217.   //
  218.   //  Setup the destination to write out the chess piece.
  219.   //
  220.   aptl[0].x = pptl->x;   
  221.   aptl[0].y = pptl->y;  
  222.   aptl[1].x = pptl->x + cxBitmap*ScaleFactor;
  223.   aptl[1].y = pptl->y + cyBitmap*ScaleFactor;
  224.                         
  225.   //
  226.   //  Select the bitmap for the specified chess piece.
  227.   //
  228.   aptl[2].x = (piece - 1) * cxBitmap;
  229.   aptl[2].y = 0;
  230.   aptl[3].x = (piece - 1) * cxBitmap + 32;
  231.   aptl[3].y = 32;
  232.  
  233.  //   Mask out the space so we can store a multi-color chess piece.
  234.   
  235.   GpiSetColor(hps, CLR_WHITE);
  236.   GpiSetBackColor(hps, CLR_BLACK);
  237.   GpiBitBlt(hps, hpsPieces, 4, aptl, ROP_SRCAND, BBO_OR);
  238.   //
  239.   //  Fill in the piece's color.
  240.   //
  241.  
  242.   aptl[2].x = (piece - 1) * cxBitmap;
  243.   aptl[2].y = 32;
  244.   aptl[3].x = (piece - 1) * cxBitmap + 32;
  245.   aptl[3].y = 64;
  246.  
  247.   GpiSetColor(hps, CLR_BLACK);
  248.   GpiSetBackColor(hps, Color);
  249.   GpiBitBlt(hps, hpsPieces, 4, aptl, ROP_SRCPAINT, BBO_OR);
  250.  
  251.   //
  252.   //  Draw the piece's outline.
  253.   //
  254.   aptl[2].x = (piece - 1) * cxBitmap;
  255.   aptl[2].y = 64;
  256.   aptl[3].x = (piece - 1) * cxBitmap+32;
  257.   aptl[3].y = 96;
  258.   GpiSetColor(hps, CLR_WHITE);
  259.   GpiSetBackColor(hps, CLR_BLACK);
  260.   GpiBitBlt(hps, hpsPieces, 4, aptl, ROP_SRCAND, BBO_OR);
  261.   }
  262.  
  263.  
  264. static short ConvertCoordToIndex ( short x, short y)
  265. {
  266.    return (y*8 + x );
  267. }
  268.  
  269. static void DrawOnePiece(HPS hps, HPS hpsPieces, short x, short y, short piece, ULONG color)
  270.   {
  271.   POINTL origin;
  272.  
  273.   QuerySqPieceOrigin(x, y, &origin);
  274.   ShowPiece (hps, hpsPieces, &origin, piece, color);
  275.   }
  276.  
  277.  
  278.  
  279. void DrawAllPieces(HPS hps, HPS hpsPieces, short reverse, short *pbrd, short *color,
  280.                      ULONG clrblack, ULONG clrwhite )
  281.   {
  282.   short x,y;
  283.   short i;
  284.  
  285.  
  286.   for ( y=0; y<8; y++)
  287.     {
  288.     for (x=0; x<8; x++)
  289.       {
  290.       i = ConvertCoordToIndex(x, y);
  291.  
  292.       if ( *(color+i) != NETURAL)
  293.         {
  294.         if (reverse == 0)
  295.           DrawOnePiece(hps, hpsPieces, x, y, *(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  296.         else
  297.           DrawOnePiece(hps, hpsPieces, 7-x, 7-y, *(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  298.         }
  299.       }
  300.     }
  301.   }
  302.