c++ WxWidgets 的示例代码将图像 blit 到 canvas

Sample code for c++ WxWidgets blit an image to the canvas

我正在尝试使用 wxWidgets 和 wxFormBuilber 制作一个基本的 GUI,以显示国际象棋游戏板和精灵中的棋子 sheet。我真的很感激一些示例代码:

  1. 正在从文件上传图像
  2. 在特定位置 blit 图像

这是我能想到的一个最简单的例子。这使用了这个 set of pieces from wikipedia (licensed under the Creative Commons license)。这个例子只画了黑色的部分,但是你应该可以通过一些简单的修改来弄清楚如何绘制白色的部分。

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWidgets headers)
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif

class MyFrame: public wxFrame
{
    public:
        MyFrame();

    private:
        void OnPaint(wxPaintEvent& event);

        wxPanel* m_board;
        wxBitmap m_blackRook;
        wxBitmap m_blackKnight;
        wxBitmap m_blackBishop;
        wxBitmap m_blackQueen;
        wxBitmap m_blackKing;
        wxBitmap m_blackPawn;
};

MyFrame::MyFrame()
        :wxFrame(NULL, wxID_ANY, "Chess", wxDefaultPosition, wxSize(1000, 1000))
{
    m_board = new wxPanel(this, wxID_ANY);
    m_board->Bind(wxEVT_PAINT, &MyFrame::OnPaint, this);

    // Load the image.
    wxImage im("c:\640px-Chess_Pieces_Sprite.svg.png", wxBITMAP_TYPE_PNG);

    // Extract the images of the pieces from the larger image.
    wxBitmap b(im);
    m_blackKing = b.GetSubBitmap(wxRect(5,113,100,100));
    m_blackQueen = b.GetSubBitmap(wxRect(110,113,100,100));
    m_blackBishop = b.GetSubBitmap(wxRect(215,113,100,100));
    m_blackKnight = b.GetSubBitmap(wxRect(323,113,100,100));
    m_blackRook = b.GetSubBitmap(wxRect(433,113,100,100));
    m_blackPawn = b.GetSubBitmap(wxRect(535,113,100,100));
}

void MyFrame::OnPaint(wxPaintEvent&)
{
    wxPaintDC dc(m_board);
    dc.Clear();

    // Draw thie light squares
    dc.SetPen(wxColour(209,139,71));
    dc.SetBrush(wxColour(209,139,71));
    dc.DrawRectangle(0,0,800,800);

    // Draw thie dark squares
    dc.SetPen(wxColour(255,206,158));
    dc.SetBrush(wxColour(255,206,158));
    for ( int i = 0 ; i< 8 ; ++i )
    {
        for ( int j = i%2 ; j< 8 ; j+=2 )
        {
            dc.DrawRectangle(i*100,j*100,100,100);
        }
    }

    // Draw thie black pieces
    dc.DrawBitmap(m_blackRook, 0, 0, true);
    dc.DrawBitmap(m_blackKnight, 100, 0, true);
    dc.DrawBitmap(m_blackBishop, 200, 0, true);
    dc.DrawBitmap(m_blackQueen, 300, 0, true);
    dc.DrawBitmap(m_blackKing, 400, 0, true);
    dc.DrawBitmap(m_blackBishop, 500, 0, true);
    dc.DrawBitmap(m_blackKnight, 600, 0, true);
    dc.DrawBitmap(m_blackRook, 700, 0, true);

    for ( int i = 0 ; i < 8 ; ++i )
    {
        dc.DrawBitmap(m_blackPawn, 100*i, 100, true);
    }
}

class MyApp : public wxApp
{
    public:
        virtual bool OnInit()
        {
            ::wxInitAllImageHandlers();
            MyFrame* frame = new MyFrame();
            frame->Show();
            return true;
        }
};

wxIMPLEMENT_APP(MyApp);

在 windows 上,应用程序将如下所示:

这里展示的重要项目是

  1. 正在加载图像。
  2. 正在为各个部分提取子位图。
  3. 在板的油漆处理器中绘制零件。

显然,你应该在第 38 行更改 png 文件的位置。如果你想使用 few extra steps,你可以将 png 文件嵌入到你的应用程序中。

我尽量让示例保持简单,因此有很多地方可以改进。例如。我为每种类型的作品使用了单独的位图。如果需要,可以在绘画处理程序中使用 wxImageList instead. In addition it would be better to use an wxAutoBufferedPaintDC