1)
Using the vector method now means that it stores multiple vertex arrays (one for each point).
A vertex array can store multiple points and draw them all at once.
Modifying ramaskrik's code, you could do this:
#include <SFML/Graphics.hpp>
int main()
{
sf::Texture image;
image.loadFromFile("tex.jpg");
sf::Sprite mySprite(image);
// You can create the window directly in its constructor
sf::RenderWindow myWindow(sf::VideoMode(image.getSize().x,image.getSize().y),"screen");
sf::VertexArray points(sf::Points); // starts with no points
sf::Event event;
while (true)
{
while (myWindow.pollEvent(event))
{
if (event.type == sf::Event::MouseButtonPressed)
{
points.append(sf::Vertex(sf::Vector2f(event.mouseButton.x + 0.5f, event.mouseButton.y + 0.5f))); // without these halves, you will probably notice errors at some point. long story...
}
}
myWindow.draw(mySprite);
myWindow.draw(points);
myWindow.display();
}
return EXIT_SUCCESS;
}
2)
However, it may be that you're trying to manipulate the image rather than just draw dots on top of it.
You can change the pixel of an image using
setPixel(). The sprite, though, is linked to a texture so you'll need to constantly transfer between them.
Modifying code from 1):
#include <SFML/Graphics.hpp>
int main()
{
sf::Texture image;
image.loadFromFile("tex.jpg");
sf::Texture texture;
texture.loadFromImage(image);
sf::Sprite mySprite(texture);
// You can create the window directly in its constructor
sf::RenderWindow myWindow(sf::VideoMode(image.getSize().x,image.getSize().y),"screen");
sf::Event event;
while (true)
{
while (myWindow.pollEvent(event))
{
if (event.type == sf::Event::MouseButtonPressed)
{
image.setPixel(event.mouseButton.x, event.mouseButton.y, sf::Color::Black);
texture.update(image);
}
}
myWindow.draw(mySprite);
myWindow.display();
}
return EXIT_SUCCESS;
}
Note that copying an image to a texture can be slow, especially if trying to do a lot of times quickly.