简体   繁体   English

如何在 C++ 中使用 SFML 绘制高级形状?

[英]How do I draw an advanced shape with SFML in C++?

What is the best way to draw advanced custom shapes with a lot of roundings while using the SFML library in C++?使用 C++ 中的SFML库绘制具有大量舍入的高级自定义形状的最佳方法是什么?

I know how to draw shapes like rectangles or circles with the SFML library in C++.我知道如何使用 C++ 中的 SFML 库绘制矩形或圆形等形状 But if you want to make a custom shape you can make use of a vertex array.但是,如果您想制作自定义形状,则可以使用顶点数组。

If I want to draw a shape like this for example:例如,如果我想绘制这样的形状:
高级形状示例
You can start with a point on the top-left to the top-right.您可以从左上角到右上角的一个点开始。 But then you have to deal with a lot of bumps.但是你必须处理很多颠簸。 What is the best way to draw this shape?绘制这种形状的最佳方法是什么?

A function can be created to calculate all the points in the curve, But is it correct to use 1000 points for small shape like this?可以创建一个 function 来计算曲线中的所有点,但是像这样的小形状使用 1000 个点是否正确? (especially if you have a lot of these shapes) (特别是如果你有很多这些形状)

Here's something that I made for you.这是我为你做的东西。 This might sound silly, but what it does is, you can draw your shapes outline, then click the right mouse button to convert the rectangles positions (X,Y) into points which a convex shape will take and create itself into a nice shape.这可能听起来很傻,但它的作用是,您可以绘制形状轮廓,然后单击鼠标右键将矩形位置 (X,Y) 转换为凸形将采用的点并将其自身创建为一个漂亮的形状。

//This is not necessary, but it helps if you going to move the camera.
    Vector2i pixelPos = Mouse::getPosition(*Twin);
    Vector2f worldPos = Twin->mapPixelToCoords(pixelPos);

    // declare the variables
    RectangleShape* brush;
    vector<RectangleShape> brushV;
    ConvexShape yourShape;
    vector<Vector2f> pos;
    brush = new RectangleShape;

    // the brush, which creates rectangles
    if (Mouse::isButtonPressed(Mouse::Left))
    {
        brushV.push_back(*brush);
        brushV[brushV.size() - 1].setSize(Vector2f(10, 10));
        brushV[brushV.size() - 1].setPosition(Vector2f(worldPos));
        pos.push_back(worldPos);
    }

    // creates the whole shape which you drew
    if (Mouse::isButtonPressed(Mouse::Right))
    {
        yourShape.setPointCount(pos.size());
        for (int i = 0; i < pos.size(); i++)
        {
            yourShape.setPoint(i, pos[i]);
        }
        brushV.clear();
    }

    // render
    for (int i = 0; i < brushV.size(); i++)
    {
        window->draw(brushV[i]);
    }
    window->draw(yourShape);

If the edges look rough you can try using antialiased.如果边缘看起来很粗糙,您可以尝试使用抗锯齿。

sf::ContextSettings settings;
settings.antialiasingLevel = 8;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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