How to place a chess piece on a bmp square ?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

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 »

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
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
User avatar
ilari
Posts: 750
Joined: Mon Mar 27, 2006 7:45 pm
Location: Finland

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

Post by ilari »

May I ask why you're using a font instead of some more flexible image format? It would probably be more convenient to use piece pixmaps with an alpha layer for transparency (eg. PGN), basic pixmaps with a color mask for transparency, or scalable vector graphics (eg. SVG).

For Cute Chess we chose SVGs because unlike pixmaps, they stay sharp at any board size or resolution, and because it's easy to add new piece graphics or even complete themes. I would avoid the Xboard/Winboard way of solving the pixelation problem; they have separate pixmaps for each board size, which is a ridiculous solution.
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 »

Chess fonts are a good idea in principle but drawing fonts in two colors (white and black) on a colored background is not really supported by the Windows font APIs, as far as I know. This is why people are doing bitmap hacks but that has the effect of destroying any anti-aliasing you would get by directly writing the font. You can probably add back your own anti-aliasing support but then you are talking about a lot of code and it's probably not as good as what the font engine would give you.

Something like SVG will likely give you better results if you are willing to spend the time constructing pieces in that format.

But if you do go the font route, for ensuring fonts are installed you might want to look at a setup tool like InnoSetup. Makes this very easy.

--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 »

ilari wrote:May I ask why you're using a font instead of some more flexible image format? It would probably be more convenient to use piece pixmaps with an alpha layer for transparency (eg. PGN), basic pixmaps with a color mask for transparency, or scalable vector graphics (eg. SVG).

For Cute Chess we chose SVGs because unlike pixmaps, they stay sharp at any board size or resolution, and because it's easy to add new piece graphics or even complete themes. I would avoid the Xboard/Winboard way of solving the pixelation problem; they have separate pixmaps for each board size, which is a ridiculous solution.
Hi Ilari,
thanks for trying to help, but I don't understand anything of what you have said.
I am a mech. engineer and have never heard of pixmaps and svg.
regards,
Matthias.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
User avatar
ilari
Posts: 750
Joined: Mon Mar 27, 2006 7:45 pm
Location: Finland

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

Post by ilari »

jdart wrote:Something like SVG will likely give you better results if you are willing to spend the time constructing pieces in that format.
For Cute Chess we just grabbed the free SVG graphics from Wikipedia: http://en.wikipedia.org/wiki/Template:T ... ess_pieces
For Capablanca chess I had to draw a couple of new pieces with Inkscape, but that only took a few minutes.
User avatar
ilari
Posts: 750
Joined: Mon Mar 27, 2006 7:45 pm
Location: Finland

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

Post by ilari »

Matthias Gemuh wrote:
ilari wrote:May I ask why you're using a font instead of some more flexible image format? It would probably be more convenient to use piece pixmaps with an alpha layer for transparency (eg. PGN), basic pixmaps with a color mask for transparency, or scalable vector graphics (eg. SVG).

For Cute Chess we chose SVGs because unlike pixmaps, they stay sharp at any board size or resolution, and because it's easy to add new piece graphics or even complete themes. I would avoid the Xboard/Winboard way of solving the pixelation problem; they have separate pixmaps for each board size, which is a ridiculous solution.
Hi Ilari,
thanks for trying to help, but I don't understand anything of what you have said.
I am a mech. engineer and have never heard of pixmaps and svg.
regards,
Matthias.
By pixmap I mean any image format which consists of pixels (eg. BMP, JPG, PNG). SVG is an abbreviation of Scalable Vector Graphics: http://en.wikipedia.org/wiki/Svg
SVG is especially great for drawing well-defined shapes like chess pieces.
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 »

ilari wrote:
Matthias Gemuh wrote: Hi Ilari,
thanks for trying to help, but I don't understand anything of what you have said.
I am a mech. engineer and have never heard of pixmaps and svg.
regards,
Matthias.
By pixmap I mean any image format which consists of pixels (eg. BMP, JPG, PNG). SVG is an abbreviation of Scalable Vector Graphics: http://en.wikipedia.org/wiki/Svg
SVG is especially great for drawing well-defined shapes like chess pieces.

Thanks, I shall look at it.

Matthias.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
CThinker
Posts: 388
Joined: Wed Mar 08, 2006 10:08 pm

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

Post by CThinker »

jdart wrote:Chess fonts are a good idea in principle but drawing fonts in two colors (white and black) on a colored background is not really supported by the Windows font APIs, as far as I know. This is why people are doing bitmap hacks but that has the effect of destroying any anti-aliasing you would get by directly writing the font. You can probably add back your own anti-aliasing support but then you are talking about a lot of code and it's probably not as good as what the font engine would give you.

Something like SVG will likely give you better results if you are willing to spend the time constructing pieces in that format.

But if you do go the font route, for ensuring fonts are installed you might want to look at a setup tool like InnoSetup. Makes this very easy.

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

Checkout ThinkerBoard. The background is some user-defined bitmap (it is essentially a "skin" of any shape and layout). You can see that all sorts of text is drawn over the skin.

Still, in ThinkerBoard, I chose to use bitmap for pieces (again, defined by users). This is because it is easier for users to create more intricate images with bitmaps than with fonts. Bitmap pieces can also have any number of color combination and varying color shades.

In older versions of the Pocket PC ThinkerBoard, I also used fonts for pieces. But drawing fonts is slower than bit block transfer. With the slower Pocket PC, this is noticeable. So, I also eventually went with bitmap pieces for that.
jarkkop
Posts: 198
Joined: Thu Mar 09, 2006 2:44 am
Location: Helsinki, Finland

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

Post by jarkkop »

Can't you use pieces that scale like TrueType chess fonts

Look the link http://www.chessvariants.com/d.font/
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 »

jarkkop wrote:Can't you use pieces that scale like TrueType chess fonts

Look the link http://www.chessvariants.com/d.font/

Those are the fonts this thread has been talking about all along.

Matthias.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de