-
hey,
i want use muose click and get some positions in window, and set positions for draw some rectangles, but i have two problem,
1- i dont know where set number of point should press by user;(for example just 10 positions)
2- my rectangle not saved on screen, when user click on new position The previous rectangle Removed،
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed: ;
cout << "Mouse button has been pressed " << endl;
cout << event.mouseButton.x << " " << event.mouseButton.y << endl;
posx=event.mouseButton.x;
posy=event.mouseButton.y;
break;
}
sf::RectangleShape Rectangle;
Rectangle.setSize(sf::Vector2f(3, 3));
Rectangle.setOutlineColor(sf::Color::Green);
Rectangle.setFillColor(sf::Color::Green);
Rectangle.setPosition(posx,posy);
window.clear();
window.draw( Rectangle);
window.display();
}
-
1) I can't understand what you're trying to say. Sorry.
2) You only have one rectangle object. If you want multiple rectangles, you should make multiple rectangles. It's really that simple.
And on the off chance you're about to ask how to manage a variable number of objects: learn the STL containers. That's need-to-know stuff for any C++ code.
-
Hi there,
is this what you want?
#include <vector>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(640, 480), "SFML works!");
std::vector<sf::RectangleShape> rects;
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 &&
event.mouseButton.button == sf::Mouse::Left) {
sf::RectangleShape rect { { 3, 3 } };
rect.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
rect.setFillColor(sf::Color::Green);
rects.push_back(rect);
}
}
window.clear();
for (const auto& r : rects)
window.draw(r);
window.display();
}
}
-
Hi there,
is this what you want?
yes of course, thanks a lot,,,
and one little question, how can i limit number of click on screen, for example: just 10 time click
In other words i want just 10 rectangle,
thanks again.
-
Well, here is one possible way to do it:
else if (event.type == sf::Event::MouseButtonPressed &&
event.mouseButton.button == sf::Mouse::Left &&
rects.size() < 10)