SFML community forums

Help => Graphics => Topic started by: delio on March 10, 2014, 10:10:56 am

Title: Click and change fill color help!
Post by: delio on March 10, 2014, 10:10:56 am
int x;
int y;

int main()
{
        while
                {
                        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                                {
                                        x = sf::Event::MouseButtonEvent::x;
                                        y = sf::Event::MouseButtonEvent::y;
                                }
                         //check that What rectangle which (x,y) is inside

                        (Rectangle that I clicked name).setFillColor(sf::Color::Green);
                }

}

This is my approximately code.
Is that Correct?
and Please tell me How can I check that What rectangle which (x,y) is inside

Thanks
Sorry for bad English
   


Title: Re: Click and change fill color help!
Post by: eXpl0it3r on March 10, 2014, 11:14:16 am
Not really...

You either use event handling (see the tutorial (http://www.sfml-dev.org/tutorials/2.1/window-events.php#the-mousebuttonpressed-and-mousebuttonreleased-events)) or use real time input (see the tutorial (http://www.sfml-dev.org/tutorials/2.1/window-inputs.php#mouse)), you should not mix both.

To check whether a point is in a rect, you can simply use the SFML rect.contains(point) function (see documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#aa8a5364c84de6dd5299f833b54e31ef1)). ;)
Title: Re: Click and change fill color help!
Post by: delio on March 11, 2014, 07:56:03 am
Not really...

You either use event handling (see the tutorial (http://www.sfml-dev.org/tutorials/2.1/window-events.php#the-mousebuttonpressed-and-mousebuttonreleased-events)) or use real time input (see the tutorial (http://www.sfml-dev.org/tutorials/2.1/window-inputs.php#mouse)), you should not mix both.

To check whether a point is in a rect, you can simply use the SFML rect.contains(point) function (see documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php#aa8a5364c84de6dd5299f833b54e31ef1)). ;)

Thanks!
Title: Re: Click and change fill color help!
Post by: delio on March 11, 2014, 01:46:29 pm
      int main()
{

       
        cout << "enter width: ";
        cin >> x;
        cout << endl;
        cout << "enter height: ";
        cin >> y;

        sf::RenderWindow window(sf::VideoMode(x * 4, y * 4), "Window");


        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();

                sf::IntRect r1(100, 100, 100, 100);

                sf::RectangleShape rectangle;
                rectangle.setSize(sf::Vector2f(100, 100));
                rectangle.setOutlineColor(sf::Color::Red);
                rectangle.setFillColor(sf::Color::Blue);
                rectangle.setOutlineThickness(1);
                rectangle.setPosition(100, 100);
                window.draw(rectangle);

       
                if (event.type == sf::Event::MouseButtonPressed)
                {
                        if (event.mouseButton.button == sf::Mouse::Left)
                        {
                                mx = event.mouseButton.x;
                                my = event.mouseButton.y;
                                bool b1 = r1.contains(mx, my);
                                if (b1 == true)
                                {


                                        rectangle.setFillColor(sf::Color::Red);
                                        window.draw(rectangle);
                                        cout << mx << my << endl;



                                }
                        }

                }
               
                window.display();

               
               
        }

        system("pause");
}
 

I've try to write my code
Result is my rectangle change to red only I pressed my mouse
How an I fix this?
Title: Re: Click and change fill color help!
Post by: zsbzsb on March 11, 2014, 01:51:30 pm
Title: Re: Click and change fill color help!
Post by: delio on March 11, 2014, 01:58:42 pm
  • Please reread the tutorial about events, handling events must be done in the event loop

while (window.pollEvent(event))
{
    // check the type of the event...
    switch (event.type)
    {
        // window closed
        case sf::Event::Closed:
            window.close();
            break;

        // key pressed
        case sf::Event::KeyPressed:
            ...
            break;

        // we don't process other types of events
        default:
            break;
    }
}
I don't see different between if-loop and while-loop
Please tell me How different?

  • Lets see, if you create a new rectangle shape each frame how do you think it is going to stay red?

How can I create rectangle every frame? I have no idea
I've put this event between window.clear(); and  window.display();
but nothing change

Sorry for ask stupid question...

Thanks
Title: Re: Click and change fill color help!
Post by: zsbzsb on March 11, 2014, 02:22:48 pm
I don't see different between if-loop and while-loop
Please tell me How different?

http://lmgtfy.com/?q=if+vs+while (http://lmgtfy.com/?q=if+vs+while) Really not that hard....

Quote
How can I create rectangle every frame? I have no idea

You don't want to create a rectangle shape every frame...
Title: Re: Click and change fill color help!
Post by: delio on March 11, 2014, 02:32:38 pm
I don't see different between if-loop and while-loop
Please tell me How different?

http://lmgtfy.com/?q=if+vs+while (http://lmgtfy.com/?q=if+vs+while) Really not that hard....

Quote
How can I create rectangle every frame? I have no idea

You don't want to create a rectangle shape every frame...

Oh, My bad
How stupid am I :'(
Thanks!
Title: Re: Click and change fill color help!
Post by: zsbzsb on March 11, 2014, 02:52:37 pm
Oh, My bad
How stupid am I :'(
Thanks!

Well everyone needs to start somewhere, but as it seems you are new to coding I highly suggest you take some time to just learn C++ from a good book. This means take a few weeks away from SFML and focus just on the language and the standard library. There is a list of good books here (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).
Title: Re: Click and change fill color help!
Post by: didii on March 11, 2014, 03:10:45 pm
The site cplusplus.com (http://www.cplusplus.com/) also has some nice tutorials. However, I do not recommend them as a starting point. They are very good if you need to recheck something since they are short and clear, but they do not have enough information to get you started.