簡體   English   中英

VertexArray的SFML繪制圖元

[英]SFML drawing primitives from VertexArray

如何從構造的VertexArray中繪制我選擇的圖元? 在下面的示例中,我向“ vertices”數組中添加了兩個頂點,並嘗試使用“ window.draw(vertices,2,sf :: Lines)”繪制它,但這給了我一個錯誤。 我知道我可以使用'sf :: Vertex foo [] = {..}'創建線對象,但是我希望能夠繼續將頂點添加到數組中,而不是一次全部初始化。

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML");
    sf::Clock clock;

    sf::VertexArray vertices;
    sf::Vertex vertex;

    vertex.position = sf::Vector2f(0, 0);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);
    vertex.position = sf::Vector2f(100, 100);
    vertex.color = sf::Color(100, 0, 200);
    vertices.append(vertex);

    // Start the game loop
    bool running = true;
    while (running)
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                running = false;
        }

        window.clear(sf::Color::Black);
        window.draw(vertices,2 ,sf::Lines);

        window.display();
    }

    return 0;
}

您可以調用vertices.setPrimitiveType(sf::Lines); 在聲明sf::VertexArray vertices; ,然后繪制它: window.draw(vertices);

或者,您可以在構造函數中設置其原始類型以及點數,然后可以使用operator[]訪問這些點:

sf::VertexArray line(sf::Lines, 2); //or sf::LineStrip
line[0].position = sf::Vector2f(0, 0);
line[0].color = sf::Color(100, 0, 200);
line[1].position = sf::Vector2f(100, 100);
line[1].color = sf::Color(100, 0, 200);
...
window.draw(line);

參考: 枚舉sf :: PrimitiveTypesf :: VertexArrayVertexArray教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM