draw line sfml

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Line");

    sf::Vertex line[] =
    {
        sf::Vertex(sf::Vector2f(100.f, 100.f)),
        sf::Vertex(sf::Vector2f(400.f, 300.f))
    };

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

        window.clear();
        window.draw(line, 2, sf::Lines);
        window.display();
    }

    return 0;
}

Explanation: 1. #include <SFML/Graphics.hpp>: Includes the necessary SFML graphics library.

  1. sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Line");: Creates a window of size 800x600 pixels with the title "SFML Line".

  2. sf::Vertex line[] = ...: Defines an array of sf::Vertex objects called line. Two vertices are created - one at position (100, 100) and another at (400, 300), creating a line between these points.

  3. while (window.isOpen()) { ... }: Enters the main loop that runs as long as the window is open.

  4. Nested while (window.pollEvent(event)) { ... }: Checks and processes events such as window closure.

  5. window.clear();: Clears the window for the next frame.

  6. window.draw(line, 2, sf::Lines);: Draws the line defined by the sf::Vertex array using sf::Lines primitive type.

  7. window.display();: Displays the updated window content.

  8. return 0;: Indicates successful completion of the program.