我正在尝试使用SFML编写Hex Game,为此,我现在想绘制像网格一样对齐的六边形。为此,我尝试使用无效的凸形和相同的顶点阵列。
这是我的渲染功能:
void Game::render(std::vector<int>& Vector)
{
mWindow.clear(sf::Color(100, 100, 100));
drawCells(Vector);
mWindow.display();
}
这是drawCells函数:
void Game::drawCells(std::vector<int>& Vector)
{
for (unsigned int x = 0; x < mColCount; ++x)
{
for (unsigned int y = 0; y < mRowCount; ++y)
{
sf::VertexArray hexagone(sf::Triangles, 6);
sf::Color color;
if (Vector[getIndex(x, y)] == WHITE)
{
color = sf::Color(255, 255, 255);
}
else if (Vector[getIndex(x, y)] == BLUE)
{
color = sf::Color(200, 50, 50);
}
else // Red hexagon
{
color = sf::Color(50, 50, 200);
}
hexagone[0] = sf::Vertex(sf::Vector2f((x + 1.0 / 2.0) * mCellHorSize + mHorOffset, y * mCellVerSize + mVerOffset), color);
hexagone[1] = sf::Vertex(sf::Vector2f((x + 1.0) * mCellHorSize + mHorOffset, (y + 1.0 / 3.0) * mCellVerSize + mVerOffset), color);
hexagone[2] = sf::Vertex(sf::Vector2f((x + 1.0) * mCellHorSize + mHorOffset, (y + 2.0 / 3.0) * mCellVerSize + mVerOffset), color);
hexagone[3] = sf::Vertex(sf::Vector2f((x + 1.0 / 2.0) * mCellHorSize + mHorOffset, (y + 1) * mCellVerSize + mVerOffset), color);
hexagone[4] = sf::Vertex(sf::Vector2f(x*mCellHorSize + mHorOffset, (y + 2.0 / 3.0) * mCellVerSize + mVerOffset), color);
hexagone[5] = sf::Vertex(sf::Vector2f(x*mCellHorSize + mHorOffset, (y + 1.0 / 3.0) * mCellVerSize + mVerOffset), color);
/*sf::VertexArray hexagone(6);
hexagone.setOutlineColor(sf::Color::Black);
hexagone.setOutlineThickness(4);
hexagone.setPoint(0, sf::Vector2f((x + 1.0 / 2.0)*mCellHorSize + mHorOffset, y*mCellVerSize + mVerOffset));
hexagone.setPoint(1, sf::Vector2f((x + 1.0)*mCellHorSize + mHorOffset, (y + 1.0 / 3.0)*mCellVerSize + mVerOffset));
hexagone.setPoint(2, sf::Vector2f((x + 1.0)*mCellHorSize + mHorOffset, (y + 2.0 / 3.0)*mCellVerSize + mVerOffset));
hexagone.setPoint(3, sf::Vector2f((x + 1.0 / 2.0)*mCellHorSize + mHorOffset, (y+1)*mCellVerSize + mVerOffset));
hexagone.setPoint(4, sf::Vector2f(x*mCellHorSize + mHorOffset, (y + 2.0 / 3.0)*mCellVerSize + mVerOffset));
hexagone.setPoint(5, sf::Vector2f(x*mCellHorSize + mHorOffset, (y + 1.0 / 3.0)*mCellVerSize + mVerOffset));
if (Vector[getIndex(x, y)] == WHITE)
{
hexagone.setFillColor(sf::Color(255, 255, 255));
}
else if (Vector[getIndex(x, y)] == BLUE)
{
hexagone.setFillColor(sf::Color(200, 50, 50));
}
else // Red hexagon
{
hexagone.setFillColor(sf::Color(50, 50, 200));
}*/
mWindow.draw(hexagone);
}
}
}
我已经在此上花费了很多时间,所以我想继续下一步,所以我问大家是否知道为什么六边形没有渲染。我忘了提一下,但是window.clear可以完美工作,因此它必须是drawCells函数。
PS:如果您发现英语错误,请改正它们,因为我想提高我的英语水平。谢谢。