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

Author Topic: Click and change fill color help!  (Read 5069 times)

0 Members and 1 Guest are viewing this topic.

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Click and change fill color help!
« 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
   



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Click and change fill color help!
« Reply #1 on: March 10, 2014, 11:14:16 am »
Not really...

You either use event handling (see the tutorial) or use real time input (see the tutorial), 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). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Click and change fill color help!
« Reply #2 on: March 11, 2014, 07:56:03 am »
Not really...

You either use event handling (see the tutorial) or use real time input (see the tutorial), 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). ;)

Thanks!

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Click and change fill color help!
« Reply #3 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?
« Last Edit: March 11, 2014, 01:51:45 pm by delio »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Click and change fill color help!
« Reply #4 on: March 11, 2014, 01:51:30 pm »
  • Please reread the tutorial about events, handling events must be done in the event loop
  • Lets see, if you create a new rectangle shape each frame how do you think it is going to stay red?
« Last Edit: March 11, 2014, 01:53:52 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Click and change fill color help!
« Reply #5 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
« Last Edit: March 11, 2014, 02:12:58 pm by delio »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Click and change fill color help!
« Reply #6 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 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...
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

delio

  • Newbie
  • *
  • Posts: 49
    • View Profile
    • Email
Re: Click and change fill color help!
« Reply #7 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 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!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Click and change fill color help!
« Reply #8 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Click and change fill color help!
« Reply #9 on: March 11, 2014, 03:10:45 pm »
The site 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.