SFML community forums

Help => System => Topic started by: Chiggy_Playz on April 16, 2019, 01:05:33 pm

Title: How to detect if mouse clicked inside a certain box or rectangle
Post by: Chiggy_Playz on April 16, 2019, 01:05:33 pm
By box or rectangle I do not refer to the rect drawn by me. I want to know if a user clicked in a place. I am making a tic tac toe program and I want to register the click. i have been using this


                Vector2i winpos = Mouse::getPosition(window);
                int loc_x = winpos.x;
                int loc_y = winpos.y;

                INPUT:
                if (Mouse::isButtonPressed(Mouse::Left)) {
                       

                        if (72 < loc_x < 250 && 600 < loc_y < 500) {

                                cout << "works" << endl;
                               
                        }

                }
               
 
also i have placed this code inside the while loop
it should cout "works" in the cmd when i click in the first block but gives me "works" everytime i click inside
the window. it should not do that. it should only gimme works if mouse clicks in 1st cell/block.
look picture for clarification.
Title: Re: How to detect if mouse clicked inside a certain box or rectangle
Post by: G. on April 16, 2019, 01:15:47 pm
Is this C++? Because that's not how you check if a number is between 2 others in C++.  ???
random google result (https://stackoverflow.com/questions/3830644/comparing-a-variable-to-a-range-of-values)

SFML rectangles have a contains function (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Rect.php#a910b998c92756157e1407e1363f93212) to check if a point (the mouse position) is inside a rectangle.
Title: Re: How to detect if mouse clicked inside a certain box or rectangle
Post by: Chiggy_Playz on April 16, 2019, 01:20:17 pm
Is this C++? Because that's not how you check if a number is between 2 others in C++.  ???
random google result (https://stackoverflow.com/questions/3830644/comparing-a-variable-to-a-range-of-values)

SFML rectangles have a contains function (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Rect.php#a910b998c92756157e1407e1363f93212) to check if a point (the mouse position) is inside a rectangle.

yeah that can really help me out but another problem i will face is that the 9 boxes you see arent 9 rectangles. they are 2 big rectangles. one placed horizontally and one placed vertically in the respective places and an additional on in the Centre coz one rectangle overlapped the other. get it? so what function can i use?
also i used the link you provided and tried that just now. doesnt cout "work" even once.