简体   繁体   中英

SFML Vertex Array not drawing

I'm trying to code Hex Game using SFML and for that I want to draw hexagones aligned like a grid for now. For that I tried using a convex shape, which didn't work, and a vertex array, which did the same.

Here's my render function:

void Game::render(std::vector<int>& Vector)
{
    mWindow.clear(sf::Color(100, 100, 100));

    drawCells(Vector);

    mWindow.display();
}

And here's the drawCells function:

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);
    }
}

}

I've already spent many hours on this and I'd like to go to the next step so I'm asking you guys if you know why the hexagones aren't rendering. I forgot to mention, but the window.clear works perfectly, so it must be the drawCells function.

PS: If you spot English errors, please correct them as I want to improve my english. Thank you.

While I can't talk about you using sf::ConvexShape , it's certain that the points you're passing won't form a hexagon built from Triangles (which you defined for the sf::VertexArray ). If you pass Triangles , you'll have to define the single triangles for your drawable, not just the outline points.

However, you can skip all that math, simply because you can just draw your hexagons as circles built from 6 points (because that's how you essentially define a hexagon).

Here's a quick example drawing a 10x10 grid:

#include <SFML/Graphics.hpp>
#include <vector>

int main()
{
    sf::RenderWindow window({640, 480}, "Hexagons");

    // We're simply abusing a `CircleShape` here,
    // since a circle defined by 6 points IS a hexagon!
    sf::CircleShape hexagon(25, 6);
    hexagon.setOutlineColor(sf::Color::Black);
    hexagon.setOutlineThickness(5);
    hexagon.setFillColor(sf::Color::White);
    hexagon.setOrigin(25, 25);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear(sf::Color::White);

        for (int y = 0; y < 10; ++y) {
            for (int x = 0; x < 10; ++x) {
                // The horizontal/vertical offsets of 50 and 40
                // might feel off, but that's due to the way SFML
                // calculates the circle outline
                hexagon.setPosition((y % 2 ? 75 : 50) + x * 50.f, 50 + y * 40.f);
                hexagon.setFillColor(sf::Color(x * 25, y * 25, 0));
                window.draw(hexagon);
            }
        }
        window.display();
    }
}

When running this example, you'll end up with a window like this:

例

Also note that it's generally not ideal to (re-)create your drawables over and over again. Reuse them instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM