home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 3.ddi / CHESS.ZIP / DISPLAY.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  15.1 KB  |  620 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <static.h>
  7. #include <filedial.h>
  8. #include <inputdia.h>
  9. #include <bwcc.h>
  10.  
  11. #include "wcdefs.h"
  12. #include "wchess.h"
  13. #include "info.h"
  14. #include "externs.h"
  15.  
  16.  
  17. /*
  18.  *  Global variables
  19.  */
  20.  
  21. BOARDIDTYPE Display[0x78];
  22. char *PieceLetter = " KQRBNP";
  23. char buf[280];   //  general string buffer, used in several modules
  24.  
  25. /*
  26.  *  static global variables
  27.  */
  28.  
  29. static RECT BoardRect;
  30.  
  31.  
  32. /*
  33.  *  get handle to bitmap of current piece
  34.  */
  35.  
  36. HBITMAP GetBitmapHandle(PIECETYPE piece, COLORTYPE pcolor)
  37. {
  38.    if (piece == 0)
  39.       return 0;
  40.    return PieceBmpArray[piece - 1][pcolor];
  41. }
  42.  
  43.  
  44. /*
  45.  *  Clear all information from Info window
  46.  */
  47.  
  48. void ClearInfoWindow()
  49. {
  50.    TInfo->Reset();
  51. }
  52.  
  53. /*
  54.  *  Prints current color to play
  55.  */
  56.  
  57. void ColorToPlay(COLORTYPE color)
  58. {
  59.    if (color == white)
  60.       TInfo->SetTurnText("White");
  61.    else
  62.       TInfo->SetTurnText("Black");
  63. }
  64.  
  65. void Message(char *str)
  66. {
  67.    TInfo->SetMessageText(str);
  68. }
  69.  
  70. void Error(char *str)
  71. {
  72.    if (SoundOn)
  73.       MessageBeep(0);
  74.    strcpy(buf, str);
  75.    SendMessage(hWndMain, WM_COMMAND, EM_ERROR, 0L);
  76. }
  77.  
  78. void Warning(char *str)
  79. {
  80.    if (SoundOn)
  81.       MessageBeep(0);
  82.    Message(str);
  83. }
  84.  
  85.  
  86. /*
  87.  *  convert a move to a string
  88.  */
  89.  
  90. char *MoveStr(MOVETYPE *move)
  91. {
  92.     static char str[7];
  93.  
  94.     strcpy(str, "      ");
  95.     if (move->movpiece != empty)
  96.     {
  97.         if (move->spe && move->movpiece == king)  /*  castling  */
  98.         {
  99.             if (move->new1 > move->old) strcpy(str, "O-O   ");
  100.             else strcpy(str, "O-O-O ");
  101.         }
  102.         else
  103.         {
  104.             str[0] = PieceLetter[move->movpiece];
  105.             str[1] = 'a' + move->old % 16;
  106.             str[2] = '1' + move->old / 16;
  107.             if (move->content == empty)
  108.                 str[3] = '-';
  109.             else
  110.                 str[3] = 'x';
  111.             str[4] = 'a' + move->new1 % 16;
  112.             str[5] = '1' + move->new1 / 16;
  113.         }
  114.     }
  115.     return (str);
  116. }
  117.  
  118.  
  119. void PrintMove(int moveno, COLORTYPE programcolor, MOVETYPE *move, double time)
  120. {
  121.    int minutes;
  122.    minutes = (int)(time / 60.0);
  123.  
  124.    sprintf(buf, "%2.2d:%#04.1f %3.3d. %s", minutes, time - minutes * 60.0, moveno / 2 + 1, MoveStr(move));
  125.    if (programcolor == white)
  126.       TInfo->SetWhiteInfoText(buf);
  127.    else
  128.       TInfo->SetBlackInfoText(buf);
  129. }
  130.  
  131.  
  132. /*
  133.  *  draw 3d type frame
  134.  */
  135.  
  136. void DrawFrame(HDC hDC, RECT& BoardRect, BOOL DrawBackground)
  137. {
  138.     int x1, y1, x2, y2;
  139.     POINT pArray[3];
  140.     HPEN hPen, hOldPen;
  141.     HBRUSH hOldBrush;
  142.      
  143.     x1 = BoardRect.left;
  144.     x2 = BoardRect.right;
  145.     y1 = BoardRect.top;
  146.     y2 = BoardRect.bottom;
  147.     if (DrawBackground == FALSE)
  148.       hOldBrush = (HBRUSH)SelectObject(hDC, GetStockObject(NULL_BRUSH));
  149.     else
  150.       hOldBrush = (HBRUSH)SelectObject(hDC, GetStockObject(LTGRAY_BRUSH));
  151.    
  152.     hOldPen = (HPEN)SelectObject(hDC, GetStockObject(WHITE_PEN));
  153.  
  154.     Rectangle(hDC, x1, y1, x2, y2);
  155.     SelectObject(hDC, hPen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192)));
  156.     Rectangle(hDC, x1+1, y1+1, x2-1, y2-1);
  157.     DeleteObject(SelectObject(hDC, GetStockObject(WHITE_PEN)));
  158.  
  159.     pArray[0].x = x1 + 2;
  160.     pArray[1].y = pArray[0].y = y2 - 3;
  161.     pArray[2].x = pArray[1].x = x2 - 3;
  162.     pArray[2].y = y1 + 2;
  163.  
  164.     Polyline(hDC, pArray, 3);
  165.     hPen = CreatePen(PS_SOLID, 1, RGB(128, 128, 128));
  166.     SelectObject(hDC, hPen);
  167.     pArray[0].x = x1;
  168.     pArray[1].y = pArray[0].y = y2-1;
  169.     pArray[2].x = pArray[1].x = x2-1;
  170.     pArray[2].y = y1;
  171.  
  172.     Polyline(hDC, pArray, 3);
  173.     pArray[1].x = pArray[0].x = x1 + 2;
  174.     pArray[0].y = y2 - 3;
  175.     pArray[2].y = pArray[1].y = y1 + 2;
  176.     pArray[2].x = x2 - 3;
  177.     Polyline(hDC, pArray, 3);
  178.     SelectObject(hDC, hOldBrush);
  179.     DeleteObject(SelectObject(hDC, hOldPen));
  180. }
  181.  
  182.  
  183. /*
  184.  *  Display the current level indicator
  185.  */
  186.  
  187. void PrintCurLevel()
  188. {
  189.    extern BOOL MultiMove;
  190.  
  191.    if (MultiMove)
  192.       strcpy(buf, "Two Player");
  193.    else
  194.       {
  195.       switch (Level)
  196.          {
  197.          case normal:
  198.             sprintf(buf, "%1.0f sec / move", AverageTime);
  199.             break;
  200.          case fullgametime:
  201.             sprintf(buf, "%2.2f min / game", AverageTime);
  202.             break;
  203.          case easygame:
  204.             strcpy(buf, "Easy");
  205.             break;
  206.          case infinite :
  207.             strcpy(buf, "Infinte");
  208.             break;
  209.          case plysearch :
  210.             sprintf(buf, "Ply-Depth = %d", MaxLevel);
  211.             break;
  212.          case matesearch:
  213.             strcpy(buf, "MateSearch");
  214.             break;
  215.          case matching :
  216.             strcpy(buf, "Match users time");
  217.             break;
  218.          }
  219.       }
  220.    TInfo->SetLevelText(buf);
  221. }
  222.  
  223. POINT GetSquareXY(SQUARETYPE square)
  224. {
  225.    POINT p;
  226.  
  227.    if (Turned)
  228.       square ^= 0x77;
  229.    p.x = (square % 8) * SQUARE_SIZE + BORDERSIZE + MYFRAMESIZE;
  230.    p.y = (7 - square / 16) * SQUARE_SIZE + BORDERSIZE + MYFRAMESIZE;
  231.    return p;
  232. }
  233.  
  234. void ClearSquare(SQUARETYPE square)
  235. {
  236.    HANDLE hOldBrush;
  237.    POINT p;
  238.    HDC hDC;
  239.  
  240.    p = GetSquareXY(square);
  241.    
  242.    hDC = GetDC(hWndMain);
  243.    if ((square % 8 + square /16) % 2 == 1)
  244.       hOldBrush = SelectObject(hDC, hWhiteBrush);
  245.    else
  246.       hOldBrush = SelectObject(hDC, hBlackBrush);
  247.    PatBlt(hDC, p.x, p.y, SQUARE_SIZE, SQUARE_SIZE, PATCOPY);
  248.    SelectObject(hDC, hOldBrush);
  249.    ReleaseDC(hWndMain, hDC);
  250. }
  251.  
  252. void ClearDisplay()
  253. {
  254.    SQUARETYPE sq;
  255.    ClearInfoWindow();
  256.    for (sq = 0; sq <= 0x77; sq++)
  257.       Display[sq].piece = empty;
  258. }
  259.  
  260.  
  261. /*
  262.  *  Draw the board on the screen
  263.  */
  264.  
  265. void DrawBoard()
  266. {
  267.    unsigned char no;
  268.    HDC hDC;
  269.    const SQUARETYPE printno[64] = { 0, 0x10, 0x20, 0x30, 0x40, 0x50,
  270.                                         0x60, 0x70, 0x71, 0x72, 0x73,
  271.                                         0x74, 0x75, 0x76, 0x77, 0x67,
  272.                                         0x57, 0x47, 0x37, 0x27, 0x17,
  273.                                            7,    6,    5,    4,    3,
  274.                                            2,    1, 0x11, 0x21, 0x31,
  275.                                          0x41, 0x51, 0x61, 0x62, 0x63,
  276.                                         0x64, 0x65, 0x66, 0x56, 0x46,
  277.                                         0x36, 0x26, 0x16, 0x15, 0x14,
  278.                                         0x13, 0x12, 0x22, 0x32, 0x42,
  279.                                         0x52, 0x53, 0x54, 0x55, 0x45,
  280.                                         0x35, 0x25, 0x24, 0x23, 0x33,
  281.                                         0x43, 0x44, 0x34};
  282.  
  283.    
  284.    BoardRect.left = BoardRect.top = BORDERSIZE;
  285.    BoardRect.right = BoardRect.bottom = BORDERSIZE + (2 * MYFRAMESIZE) +
  286.       (8 * SQUARE_SIZE);
  287.    hDC = GetDC(hWndMain);
  288.    DrawFrame(hDC, BoardRect);
  289.    ReleaseDC(hWndMain, hDC);
  290.    for (no = 0; no < 64; no++)
  291.       ClearSquare(printno[no]);
  292. }
  293.  
  294.                                                                        
  295. void PrintPiece(SQUARETYPE square, PIECETYPE piece, COLORTYPE color, DWORD Rop)
  296. {
  297.    HBITMAP hBitmap, hOldBmp, hMaskBmp;
  298.    BITMAP Bitmap;
  299.    HDC hMemoryDC, hDC;
  300.    POINT p;
  301.  
  302.    if (piece == empty)
  303.       return;
  304.  
  305.    hBitmap = PieceBmpArray[piece-1][color];
  306.    hMaskBmp = MaskArray[piece-1];
  307.  
  308.    hDC = GetDC(hWndMain);
  309.    hMemoryDC = CreateCompatibleDC(hDC);
  310.    GetObject(hBitmap, sizeof(BITMAP), (LPSTR) &Bitmap);
  311.    hOldBmp = (HBITMAP)SelectObject(hMemoryDC, hMaskBmp);
  312.    p = GetSquareXY(square);
  313.  
  314.    BitBlt(hDC, p.x, p.y, Bitmap.bmWidth, Bitmap.bmHeight,
  315.       hMemoryDC, 0, 0, SRCAND);
  316.    SelectObject(hMemoryDC, hBitmap);
  317.    BitBlt(hDC, p.x, p.y, Bitmap.bmWidth, Bitmap.bmHeight,
  318.       hMemoryDC, 0, 0, Rop);
  319.    SelectObject(hMemoryDC, hOldBmp);
  320.    DeleteDC(hMemoryDC);
  321.    ReleaseDC(hWndMain, hDC);
  322. }
  323.  
  324. void InitDisplay()
  325. {
  326.    SQUARETYPE square;
  327.  
  328.    for (square = 0; square <= 0x77; square++)
  329.       if (!(square & 0x88))
  330.          if ((Board[square].piece != Display[square].piece) ||
  331.             (Board[square].piece != empty) && (Board[square].color !=
  332.             Display[square].color))
  333.             {
  334.                Display[square].piece = Board[square].piece;
  335.                Display[square].color = Board[square].color;
  336.             }
  337. }
  338.  
  339.  
  340. /*
  341.  *   Draw a box in the square of an attacked piece.
  342.  *   Square is black if Defended is TRUE, else it is red.
  343.  */
  344.  
  345. static void FrameSquare(SQUARETYPE square, BOOL Defended)
  346. {
  347.    POINT p;
  348.    HPEN   hOldPen;
  349.    HBRUSH hOldBrush;
  350.    HDC hDC;
  351.    hDC = GetDC(hWndMain);
  352.  
  353.    p = GetSquareXY(square);
  354.    hOldBrush = (HBRUSH)SelectObject(hDC, GetStockObject(NULL_BRUSH));
  355.    if (Defended)
  356.       hOldPen = (HPEN)SelectObject(hDC, GetStockObject(BLACK_PEN));
  357.    else
  358.       hOldPen = (HPEN)SelectObject(hDC, CreatePen(PS_SOLID,1,RGB(192, 0, 0)));
  359.  
  360.  
  361.    Rectangle(hDC, p.x+1, p.y+1, p.x+SQUARE_SIZE-1, p.y+SQUARE_SIZE-1);
  362.    SelectObject(hDC, hOldBrush);
  363.    if (Defended)
  364.       SelectObject(hDC, hOldPen);
  365.    else
  366.       DeleteObject(SelectObject(hDC, hOldPen));
  367.    ReleaseDC(hWndMain, hDC);
  368. }
  369.  
  370. void HideAttacks(void)
  371. {
  372.    SQUARETYPE square;
  373.  
  374.    for (square = 0; square <= 0x77; square++)
  375.       if (!(square & 0x88))
  376.          {
  377.             if (Board[square].attacked == TRUE)
  378.             {
  379.             Board[square].attacked = FALSE;
  380.             ClearSquare(square);
  381.             PrintPiece(square, Board[square].piece,
  382.                Board[square].color, SRCINVERT);
  383.             }
  384.          }
  385. }
  386.  
  387. void ShowAttacks()
  388. {
  389.    SQUARETYPE square;
  390.  
  391.    for (square = 0; square <= 0x77; square++)
  392.       if (!(square & 0x88))
  393.          {
  394.          if (Attacks(ComputerColor, square) && Board[square].color != ComputerColor && Board[square].piece != empty)
  395.             {
  396.             Board[square].attacked = TRUE;
  397.             if (Attacks((COLORTYPE)!ComputerColor, square))
  398.                FrameSquare(square, TRUE);
  399.             else
  400.                FrameSquare(square, FALSE);
  401.             }
  402.          else if (Board[square].attacked == TRUE)
  403.             {
  404.             Board[square].attacked = FALSE;
  405.             ClearSquare(square);
  406.             PrintPiece(square, Board[square].piece,
  407.                Board[square].color, SRCINVERT);
  408.             }
  409.          }
  410. }
  411.  
  412. void UpdateBoard()
  413. {
  414.    SQUARETYPE square;
  415.    for (square = 0; square <= 0x77; square++)
  416.       if (!(square & 0x88))
  417.          if ((Board[square].piece != Display[square].piece) ||
  418.             (Board[square].piece != empty) && (Board[square].color !=
  419.             Display[square].color))
  420.             {
  421.                if (Display[square].piece != empty)
  422.                   ClearSquare(square);
  423.                Display[square].piece = Board[square].piece;
  424.                Display[square].color = Board[square].color;
  425.                if (Board[square].piece != empty)
  426.                   PrintPiece(square, Board[square].piece,
  427.                      Board[square].color, SRCINVERT);
  428.             }
  429.    if (Level == easygame && !Editing)
  430.       ShowAttacks();
  431. }
  432.  
  433. static void DrawAlphaNum();
  434.  
  435. void PrintBoard()
  436. {
  437.    SQUARETYPE square;
  438.  
  439.    DrawBoard();
  440.    for (square = 0; square <= 0x77; square++)
  441.       if (!(square & 0x88))
  442.             {
  443.                if (Display[square].piece != empty)
  444.                   PrintPiece(square, Display[square].piece,
  445.                      Display[square].color, SRCINVERT);
  446.             }
  447.    DrawAlphaNum();
  448.    if (Level == easygame && !Editing)
  449.       ShowAttacks();
  450. }
  451.  
  452.  
  453. /*
  454.  *  find a square with a given point and determine whether it
  455.  *  contains a player of the given color
  456.  */
  457.  
  458. SQUARETYPE GetValidSquare(POINT p, COLORTYPE player, BOOL CheckPiece)
  459. {
  460.    POINT point;
  461.    SQUARETYPE square;
  462.    RECT sqrect;
  463.    
  464.    
  465.    for (square = 0; square <= 0x77; square++)
  466.       {
  467.       if (!(square & 0x88))
  468.          {
  469.          point = GetSquareXY(square);
  470.          sqrect.left = point.x;
  471.          sqrect.top = point.y;
  472.          sqrect.right = sqrect.left + SQUARE_SIZE;
  473.          sqrect.bottom = sqrect.top + SQUARE_SIZE;
  474.          if (PtInRect(&sqrect, p))
  475.             {
  476.             if ((Display[square].color == player && Display[square].piece
  477.                != empty) || !CheckPiece)
  478.                return square;
  479.             }
  480.          }
  481.       }
  482.    return -1;      
  483. }
  484.  
  485. void DrawNormalBitmap(SQUARETYPE square)
  486. {
  487.    ClearSquare(square);
  488.    PrintPiece(square, Display[square].piece, Display[square].color, SRCINVERT);
  489. }
  490.  
  491. void DrawInvertedBitmap(SQUARETYPE square)
  492. {
  493.    PrintPiece(square, Display[square].piece, Display[square].color, NOTSRCERASE);
  494. }
  495.  
  496. void OpeningLibMsg()
  497. {
  498.    TInfo->SetMessageText("Using opening library");
  499. }
  500.  
  501. void PrintNodes(NODEVAL *nodes, double time)
  502. {
  503.     double nodereal;
  504.     char buf[80];
  505.  
  506.     nodereal = (nodes->nodebase * MAXINT) + nodes->nodeoffset;
  507.    if (time)
  508.       {
  509.       sprintf(buf, "%7.1f", nodereal/time);
  510.       TInfo->SetSecondsText(buf);
  511.       }
  512.    sprintf(buf, "%7.0f ", nodereal);
  513.    TInfo->SetNodeText(buf);
  514. }
  515.  
  516.  
  517. /*
  518.  *  Print bestline on screen
  519.  */
  520.  
  521. void PrintBestMove(MOVETYPE *mainline, MAXTYPE mainevalu)
  522. {
  523.    DEPTHTYPE dep = 0;
  524.  
  525.    *buf = 0;
  526.  
  527.    if (ShowBestLine == FALSE)
  528.       return;
  529.    while (dep < 7 && (mainline[dep].movpiece != empty))
  530.    {
  531.       strcat(buf, MoveStr(&mainline[dep++]));
  532.       strcat(buf, " ");
  533.    }
  534.    TInfo->SetBestLineText(buf);
  535.    sprintf(buf, "%7.2f", mainevalu /256.0);
  536.    TInfo->SetValueText(buf);
  537. }
  538.  
  539. void ClearBestLine()
  540. {
  541.    TInfo->SetBestLineText("");
  542. }
  543.  
  544. void ClearMessage()
  545. {
  546.    TInfo->SetMessageText("");
  547. }
  548.  
  549. static char * CharArray[] = { "a","b", "c", "d", "e", "f", "g", "h" };
  550. static char * NumArray[] = { "1", "2", "3", "4", "5", "6", "7", "8" };
  551.  
  552. static void DrawBump(HDC hDC, int x, int y)
  553. {
  554.    int x2, y2;
  555.    HPEN hOldPen;
  556.    HBRUSH hOldBrush;
  557.    POINT pArray[3];
  558.  
  559.    x2 = x + CHARSIZE + 2;
  560.    y2 = y-- + LINESIZE + 1;
  561.    x-=2;
  562.    
  563.    hOldBrush = (HBRUSH)SelectObject(hDC, GetStockObject(LTGRAY_BRUSH));
  564.    hOldPen = (HPEN)SelectObject(hDC, GetStockObject(WHITE_PEN));
  565.  
  566.    Rectangle(hDC, x, y, x2, y2);
  567.  
  568.    SelectObject(hDC, CreatePen(PS_SOLID, 1, RGB(128, 128, 128)));
  569.    pArray[0].x = ++x;
  570.    pArray[1].y = pArray[0].y = y2;
  571.    pArray[2].x = pArray[1].x = x2;
  572.    pArray[2].y = ++y;
  573.    Polyline(hDC, pArray, 3);
  574.  
  575.    SelectObject(hDC, hOldBrush);
  576.    DeleteObject(SelectObject(hDC, hOldPen));
  577. }
  578.  
  579. static void DrawAlphaNum()
  580. {
  581.    int i;
  582.    int XPos, YPos;
  583.    HDC hDC;
  584.  
  585.    hDC = GetDC(hWndMain);
  586.  
  587.  
  588.    XPos = (BORDERSIZE + MYFRAMESIZE)/2 - CHARSIZE/2;
  589.    YPos = (BORDERSIZE + SQUARE_SIZE/2) - LINESIZE / 2;
  590.  
  591.    SetBkColor(hDC, RGB(192, 192, 192));
  592.  
  593.    for (i = 7; i >= 0; i--)
  594.       {
  595.       DrawBump(hDC, XPos, YPos);
  596.       if (Turned)
  597.          {
  598.          TextOut(hDC, XPos, YPos, NumArray[7-i], 1);
  599.          }
  600.       else
  601.          TextOut(hDC, XPos, YPos, NumArray[i], 1);
  602.       YPos += SQUARE_SIZE;
  603.       }
  604.  
  605.    XPos = BORDERSIZE + SQUARE_SIZE / 2 - CHARSIZE/2;
  606.    YPos = BORDERSIZE + (8 * SQUARE_SIZE) + (2 * MYFRAMESIZE) + 1;
  607.    
  608.    for (i = 0; i < 8; i++)
  609.       {
  610.       DrawBump(hDC, XPos, YPos);
  611.       if (Turned)
  612.          TextOut(hDC, XPos, YPos, CharArray[7-i], 1);
  613.       else
  614.          TextOut(hDC, XPos, YPos, CharArray[i], 1);
  615.       XPos += SQUARE_SIZE;
  616.       }
  617.    ReleaseDC(hWndMain, hDC);
  618. }
  619.  
  620.