Checking if the bool is true?

So I've been trying to make a button in SFML for a long time now and I think I'm nearly there.I've written the code and no errors, when I right click over the button it works but if I right click anywhere else it also opens the window.I think its down to the checking if the bool is true.Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


	bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp) 
	{
		if (sprite.contains(mp)){
		return true;
		}
		return false;
	}


int main()
{	

	sf::RenderWindow window, window2, window3;
		window.create(sf::VideoMode(800, 600),"My first Visual Studio window!");
	
	sf::Texture texture;
	if(!texture.loadFromFile("button1.png"))
	{
		return 1;
	}
	sf::Sprite sprite;
	sprite.setTexture(texture);

	sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;



	while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();			
		
			if(isSpriteHover) //this should check if the bool is true right?
	{
		if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Right)
		{
			sf::Window window;
			window.create(sf::VideoMode(400, 200),"The button worked!");
		}
	
	}

			if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))
			{
				 window2.create(sf::VideoMode(400, 200),"Another window!");
			while(window2.isOpen())
			{
				sf::Event event;
				while(window2.pollEvent(event))
				{
					if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
						{
							window2.close();
					}
				}
			}
			}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::B))
			{
				window3.create(sf::VideoMode(500, 300),"The third window!");	
				while(window3.isOpen())
					{
						sf::Event event;

						while(window3.pollEvent(event))
							if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
							{
								window3.close();
							}
					}
			 }
		
		}
		
			
		
		
		window.clear(sf::Color::Black);

		sprite.setPosition(sf::Vector2f(50, 300));
		
		window.draw(sprite);
		
		window.display();

	}
	
return 0;
}

So how do I check if the bool is true and if I'm doing this right why isn't the button working how its supposed to?If you could please write the solution in code it would be much appreciated.
Thanks!
Last edited on
if(isSpriteHover) //this should check if the bool is true right?

No, it shouldn't. This checks if the address of the function isSpriteHover is null. Since it's not, the condition always evaluates to true.

If you want to check the return value of a function, you must call the function.

[edit: Simplified code follows]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <deque>


bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp)
{
    return sprite.contains(mp);
}

int main()
{
    sf::RenderWindow window, window2 ;
    window.create(sf::VideoMode(800, 600), "My first Visual Studio window!");

    sf::Texture texture;
    if (!texture.loadFromFile("button1.png"))
    {
        // since I don't have a button1.png, make a quick and dirty drawing
        // of a button.
        sf::RenderTexture buttonTexture;
        buttonTexture.create(100, 100);
        buttonTexture.clear(sf::Color::Blue);
        buttonTexture.display();

        sf::Image buttonImg = buttonTexture.getTexture().copyToImage();
        texture.loadFromImage(buttonImg);
    }

    sf::Sprite sprite;
    sprite.setTexture(texture);
    sprite.setPosition(50, 300);


    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
            case sf::Event::Closed:
                window.close();
                break;

            case sf::Event::MouseButtonReleased:
                if (event.mouseButton.button == sf::Mouse::Right )
                    if (isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)))
                        window2.create(sf::VideoMode(400, 200), "The button worked!");
                break; 

            }
        }

        window.clear(sf::Color::Black);
        window.draw(sprite);
        window.display();
    }
}


This still doesn't behave as one would expect a button to, of course. For that you're going to need keep track of some state.
Last edited on
Wow thank you so much dude I have finally made a button.It all works great, I didn't use your advice all the way(the Simplified code).One thing I don't understand though is this piece of code, so could you explain it to me?
Here's that piece of code:
 
if(isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)) == true)

Also here's my whole code, in case you have some more tips:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


	bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp) 
	{
		if (sprite.contains(mp)){
		return true;
		}
		return false;
	}


int main()
{	

	sf::RenderWindow window, window2, window3;
		window.create(sf::VideoMode(800, 600),"My first Visual Studio window!");
	
	sf::Texture texture;
	if(!texture.loadFromFile("button1.png"))
	{
		return 1;
	}
	sf::Sprite sprite;
	sprite.setTexture(texture);

	sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;



	while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();			
		
			if(isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)) == true)
	{
		if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Left)
		{
			window.create(sf::VideoMode(400, 200),"The button worked!");
			while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();	

			}
					}
		}
		}
	
	}

			if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))
			{
				 window2.create(sf::VideoMode(400, 200),"Another window!");
			while(window2.isOpen())
			{
				sf::Event event;
				while(window2.pollEvent(event))
				{
					if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
						{
							window2.close();
					}
				}
			}
			}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::B))
			{
				window3.create(sf::VideoMode(500, 300),"The third window!");	
				while(window3.isOpen())
					{
						sf::Event event;

						while(window3.pollEvent(event))
							if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
							{
								window3.close();
							}
					}
			 }
		
		
		
			
		
		
		window.clear(sf::Color::Black);

		sprite.setPosition(sf::Vector2f(50, 300));
		
		window.draw(sprite);
		
		window.display();

}
	
return 0;
}

Once again thank you.
Hello?Guys?
@ OP: What's confusing about that function? It's a simplified version of what you had. The sf::Rect class has a member function called "contains" which tells you if the coordinates of the vector you feed it are inside of that instance of the class. It returns a true or a false. cire just skipped the middle man code you had in there.

EDIT: Documentation: http://sfml-dev.org/documentation/2.0/classsf_1_1Rect.php#a24163acdb9b2987c0ea55c201e270d41
Last edited on
Topic archived. No new replies allowed.