我正在编写一个跳棋游戏。我有一个CBoard类,它具有指向CGamePieces
和CTiles
的两个指针向量。第一次绘制木板时,它可以完美绘制。如果我尝试对矢量进行任何修改并再次绘制,则会重新绘制原始板,而不会反映出更改。
这是我向这些向量添加元素的方式:
void CBoard::getPieces()
{
while(mGamePieces.size() < 12)
{
shared_ptr<CWhitePiece> white(new CWhitePiece());
white->setBoard(this);
white->setTile(mTiles[tileIndex]);
mGamePieces.push_back(white);
}
}
要绘制电路板,我要遍历所有看起来像这样的矢量,从而在所有CTiles
和CGamePieces
上调用Draw函数:
void CGamePiece::Draw(Gdiplus::Graphics * graphics)
{
double wid = 100;
double hit = 100;
graphics->DrawImage(mImage.get(),
float(mTile->getColumn() / 2) + 100 * mTile->getColumn(), float(mTile->getRow() / 2) + 100 * mTile->getRow(),
100.0, 100.0);
}
使用调试器,我看到正在迭代的向量具有反映的更改-如果删除某项,则少一个项目,如果添加某项,则该项目存在于向量中。重画电路板之前将调用此代码:
void CBoard::move()
{
shared_ptr<CBrownPiece> brown(new CBrownPiece());
brown->setBoard(this);
brown->setTile(mTiles[30]);
mGamePieces.push_back(brown);
}
由于调试时似乎对矢量进行了更改,因此我不确定为什么不会在屏幕上绘制此附加片段,尤其是当我看到在其上调用Draw函数时。如果通过在这些矢量的每一项上调用draw来绘制面板,那么是否应该从屏幕上绘制/删除添加到矢量中的项目?
点击一块时我会重画屏幕
/**
* When the mouse is pressed down, we will run the HitTest function.
* If HitTest does not return a nullpointer, we will run the
* move function.
*/
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
// Check if a piece has been clicked
mClickedPiece = mBoard.HitTest(point.x, point.y);
if (mClickedPiece != nullptr && !mClickedPiece->getClicked())
{
mClickedPiece->setClicked(true); // This will allow us to find the piece to move in the vector of CGamePieces
mBoard.move();
OnPaint();
//mClickedPiece->setClicked(false);
}
}
/** This function is called to draw in the window.
*
* This function is called in response to a drawing message
* whenever we need to redraw the window on the screen.
* It is responsible for painting the window.
*/
void CChildView::OnPaint()
{
CPaintDC paintDC(this); // device context for painting
CDoubleBufferDC dc(&paintDC); // device context for painting
Graphics graphics(dc.m_hDC);
mBoard.OnDraw(&graphics);
}