Firstly, your code doesn't compile.
The draw method should point to the first element (or the beginning of the actual data):
window.draw(vertices.data(), vertices.size(), sf::LinesStrip);
or
window.draw(&vertices.front(), vertices.size(), sf::LinesStrip);
or
window.draw(&vertices[0], vertices.size(), sf::LinesStrip);
There no need to loop through the vertices either. Drawing the object will draw them all:
if (vertices.size() > 1)
window.draw(vertices.data(), vertices.size(), LinesStrip);
Note that there's no point drawing if there is only one vertex as a line requires at least two.
Your subtractions for the window position to get the current mouse position is highly unreliable. Mainly because the window could be in any position.
To get the current position relative to the mouse, you can pass the window to getPosition, thus:
sf::Mouse::getPosition(window)
Note that the event actually provides the correct pixel position already:
const sf::Vector2i eventPosition(event.mouseButton.x, event.mouseButton.y);
Be aware, though, that this position should be mapped to account for any view:
const sf::Vector2f mousePosition(window.mapPixelToCoords(eventPosition));
Then it is just simply a matter of constructing the vertex from that Vector2f:
vertices.push_back(sf::Vertex(mousePosition));
#include <SFML/Graphics.hpp>
#include <vector>
int main()
{
sf::RenderWindow window(sf::VideoMode(500, 500), "LINES");
std::vector<sf::Vertex> vertices;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::MouseButtonPressed) // this is "else if" because if the event type was Closed, there's no point checking for MouseButtonPressed
{
if (event.mouseButton.button == sf::Mouse::Left)
{
const sf::Vector2i eventPosition(event.mouseButton.x, event.mouseButton.y);
const sf::Vector2f mousePosition(window.mapPixelToCoords(eventPosition));
vertices.emplace_back(mousePosition);
}
}
}
window.clear();
if (vertices.size() > 1)
window.draw(vertices.data(), vertices.size(), sf::LinesStrip);
window.display();
}
return EXIT_SUCCESS;
}