Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: draw rectangles with clicking mouse  (Read 4912 times)

0 Members and 1 Guest are viewing this topic.

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
draw rectangles with clicking mouse
« on: December 19, 2014, 11:53:51 am »
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();
           
        }

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: draw rectangles with clicking mouse
« Reply #1 on: December 19, 2014, 12:15:18 pm »
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.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: draw rectangles with clicking mouse
« Reply #2 on: December 19, 2014, 12:33:52 pm »
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();
    }
}

iTh3End

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: draw rectangles with clicking mouse
« Reply #3 on: December 19, 2014, 12:45:52 pm »
Quote
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.
« Last Edit: December 19, 2014, 01:06:52 pm by iTh3End »

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: draw rectangles with clicking mouse
« Reply #4 on: December 19, 2014, 01:40:03 pm »
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)