How to place a chess piece on a bmp square ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

jdart
Posts: 4367
Joined: Fri Mar 10, 2006 5:23 am
Location: http://www.arasanchess.org

Re: How to place a chess piece on a bmp square ?

Post by jdart »

> In Windows, you actually can draw any font on any background.

Care to give me a hint how you did this?

The common free chess fonts (Chess Berlin etc.) are really designed for printing. So the font background color is not transparent.

Even if you solve this you have the issue of drawing a White piece on a white or light colored background, where just drawing it directly won't give a good effect.

--Jon
User avatar
Matthias Gemuh
Posts: 3245
Joined: Thu Mar 09, 2006 9:10 am

Re: How to place a chess piece on a bmp square ?

Post by Matthias Gemuh »

Matthias Gemuh wrote:
Karlo Bala wrote:
I have somewhere GUI for 4-in-line game. It is not totally finished, but it has graphic. If you want I can mail it to you
I have already written these 2 routines which create a board and pieces on it. Yet you can send me the 4-in-line GUI.
Now I need to find out how to check for (and eventually install) a chess font programmatically.

Code: Select all

void DrawChessPiece(CHESSGUI *ChsGUI, UINT32 Colors[], Graphics::TBitmap *Bitmap1, int X, int Y, int xx, int yy,
        int nPieceNr, int nSqColour, int nPieceColour, TImage *Image1, char *FontName, bool bMainBoard)
{
    DWORD nPieceColor, nSqColor;
    char *ch[] = { "*", "p", "n", "b", "r", "q", "k" };
//

    Bitmap1->Width = 2*xx; Bitmap1->Height = 2*yy;
    Bitmap1->Canvas->Pen->Color = 0x00000000;
    Bitmap1->Canvas->Font->Height = 2*yy;
    Bitmap1->Canvas->Brush->Style = bsSolid;
    if (bMainBoard) { if (nPieceColour) nPieceColor = Colors[2]; else nPieceColor = Colors[3]; }
    if (!bMainBoard) { if (nPieceColour) nPieceColor = Colors[6]; else nPieceColor = Colors[7]; }
    Bitmap1->Canvas->Brush->Color = nPieceColor;
    Bitmap1->Canvas->Font->Name = FontName;
    Bitmap1->Canvas->TextOut(0, 0, ch[nPieceNr]);
    if (bMainBoard) { if (nSqColour) nSqColor = Colors[0]; else nSqColor = Colors[1]; }
    if (!bMainBoard) { if (nSqColour) nSqColor = Colors[4]; else nSqColor = Colors[5]; }
    Bitmap1->Canvas->Brush->Color = nSqColor;
    Bitmap1->Canvas->FloodFill(1, 1, nPieceColor, fsSurface);
    StretchBlt(Image1->Canvas->Handle, X, Y, xx, yy, Bitmap1->Canvas->Handle, 0, 0, 2*xx, 2*yy, SRCCOPY);
}

void CreatePieces(CHESSGUI *ChsGUI, UINT32 Colors[], bool bMainBoard, TImage *Image1)
{
    int x, y, x1, y1, xx, yy, x3, y3, px, py, nCnt;
    int nPieceNr, nSquareColour, nPieceColour;
    struct { int x1, y1, nPieceNr, nSquareColour, nPieceColour; } pp[] = {
        { 0, 0, 0, 1, 1 }, { 2, 0, 0, 1, 1 }, { 4, 0, 0, 1, 1 }, { 6, 0, 0, 1, 1 },
        { 1, 0, 0, 0, 0 }, { 3, 0, 0, 0, 0 }, { 5, 0, 0, 0, 0 }, { 7, 0, 0, 0, 0 },
        { 0, 1, 1, 0, 0 }, { 0, 2, 1, 1, 0 }, { 0, 3, 1, 0, 1 }, { 0, 4, 1, 1, 1 },
        { 1, 1, 2, 1, 0 }, { 1, 2, 2, 0, 0 }, { 1, 3, 2, 1, 1 }, { 1, 4, 2, 0, 1 },
        { 2, 1, 3, 0, 0 }, { 2, 2, 3, 1, 0 }, { 2, 3, 3, 0, 1 }, { 2, 4, 3, 1, 1 },
        { 3, 1, 4, 1, 0 }, { 3, 2, 4, 0, 0 }, { 3, 3, 4, 1, 1 }, { 3, 4, 4, 0, 1 },
        { 4, 1, 5, 0, 0 }, { 4, 2, 5, 1, 0 }, { 4, 3, 5, 0, 1 }, { 4, 4, 5, 1, 1 },
        { 5, 1, 6, 1, 0 }, { 5, 2, 6, 0, 0 }, { 5, 3, 6, 1, 1 }, { 5, 4, 6, 0, 1 },
        { 0, 5, 0, 0, 1 }, { 2, 5, 0, 0, 1 }, { 4, 5, 0, 0, 1 }, { 6, 5, 0, 0, 1 },
        { 1, 5, 0, 1, 0 }, { 3, 5, 0, 1, 0 }, { 5, 5, 0, 1, 0 }, { 7, 5, 0, 1, 0 },
        { 0, 6, 0, 1, 1 }, { 2, 6, 0, 1, 1 }, { 4, 6, 0, 1, 1 }, { 6, 6, 0, 1, 1 },
        { 1, 6, 0, 0, 0 }, { 3, 6, 0, 0, 0 }, { 5, 6, 0, 0, 0 }, { 7, 6, 0, 0, 0 }
    };
    Graphics::TBitmap *Bitmap1;
    char FontName[32];
//

    Bitmap1 = new Graphics::TBitmap();
    xx = yy = Image1->Width/8;

    strcpy(FontName, "Chess Merida");   // strcpy(FontName, "Chess Cases");
    Image1->AutoSize = false; Image1->Stretch = true;
    Image1->Canvas->Brush->Color = clBtnFace;
    Image1->Canvas->FillRect(Image1->ClientRect);

    nCnt = (sizeof(pp)/sizeof(pp[0]));
    for &#40;int i=0; i < nCnt; i++) &#123;
        DrawChessPiece&#40;ChsGUI, Colors, Bitmap1, pp&#91;i&#93;.x1*xx, pp&#91;i&#93;.y1*yy, xx, yy,
                pp&#91;i&#93;.nPieceNr, pp&#91;i&#93;.nSquareColour, pp&#91;i&#93;.nPieceColour, Image1, FontName, bMainBoard&#41;;
    &#125;

//
    Image1->Repaint&#40;); Image1->Update&#40;);
    delete Bitmap1;
&#125;
Matthias.
mgemuh[@]yahoo{.}com

With a slight modification to include more pieces:

http://biglion.110mb.com/chess/Chess10x8_1.bmp

I cut and paste during games.

Matthias.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
User avatar
Matthias Gemuh
Posts: 3245
Joined: Thu Mar 09, 2006 9:10 am

Re: How to place a chess piece on a bmp square ?

Post by Matthias Gemuh »

Matthias Gemuh wrote: With a slight modification to include more pieces:

http://biglion.110mb.com/chess/Chess10x8_1.bmp

I cut and paste during games.

Matthias.
http://w2410tmq9.homepage.t-online.de/c ... 10x8_1.gif
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de