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.
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Line");
: Creates a window of size 800x600 pixels with the title "SFML Line".sf::Vertex line[] = ...
: Defines an array ofsf::Vertex
objects calledline
. Two vertices are created - one at position (100, 100) and another at (400, 300), creating a line between these points.while (window.isOpen()) { ... }
: Enters the main loop that runs as long as the window is open.Nested
while (window.pollEvent(event)) { ... }
: Checks and processes events such as window closure.window.clear();
: Clears the window for the next frame.window.draw(line, 2, sf::Lines);
: Draws the line defined by thesf::Vertex
array usingsf::Lines
primitive type.window.display();
: Displays the updated window content.return 0;
: Indicates successful completion of the program.