How to draw images in SFML and check if the mouse is over them?

Pages: 12
Ok guys here's my question how to draw images in SFML,and check if the mouse is over them.And if you could include an example in code I would really appreciate it.Thanks!
Last edited on
1. Keep track of where you drew the images at
2. Handle mouse-moved events and check if the mouse coords are over the image
Ok I got the checking if the mouse is over the image thing,but how to draw the actual image?And could you give me an example in code please?Thanks!
Last edited on
Oh, I had assumed you already knew how to draw the image. You load it into an sf::Sprite, set the coordinates, and tell it to draw itself.
Technically you tell the sf::RenderWindow instance to draw the sprite, but the point is that it's a simple piece of code. Here is the tutorial: http://sfml-dev.org/tutorials/2.1/graphics-sprite.php
Ok so I did a little research and I think this is the answer.
1
2
3
4
5
6
7
8
9
bool active(Obj obj, int mx, int my)
{
    if(mx > obj.box.x && mx < obj.box.x + obj.box.w && my > obj.box.y && my < obj.box.y + obj.box.h)
    {
        return true;
    }

    return false;
}

But it tells me "Error: identifier "Obj" is undefined.
What am i doing wrong?
Here's my 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
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <dos.h>
#include <TGUI/TGUI.hpp>


int main()
{	
	sf::RenderWindow window(sf::VideoMode(800, 600),"My first Visual Studio window!");
	tgui::Gui gui(window);

	sf::Texture texture;
	if(!texture.loadFromFile("button1.png"))
	{
		return 1;
	}
	sf::Sprite sprite;
	sprite.setTexture(texture);



	while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();			
			bool active(Obj obj, int mx, int my)
{
    if(mx > obj.box.x && mx < obj.box.x + obj.box.w && my > obj.box.y && my < obj.box.y + obj.box.h)
    {
        return true;
    }

    return false;
}
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))
			{
				sf::RenderWindow window2(sf::VideoMode(400, 200),"Another window!");
				tgui::Gui gui(window2);
			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))
			{
				sf::RenderWindow window3(sf::VideoMode(500, 300),"The third window!");
				tgui::Gui gui(window3);	
				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;
}

Thanks!
Last edited on
Why did you define that function inside of the main function? You cannot define functions inside of functions.
So?
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
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <dos.h>
#include <TGUI/TGUI.hpp>

bool active(Obj obj, int mx, int my)
{
    if(mx > obj.box.x && mx < obj.box.x + obj.box.w && my > obj.box.y && my < obj.box.y + obj.box.h)
    {
        return true;
    }

    return false;
}

int main()
{	
	sf::RenderWindow window(sf::VideoMode(800, 600),"My first Visual Studio window!");
	tgui::Gui gui(window);

	sf::Texture texture;
	if(!texture.loadFromFile("button1.png"))
	{
		return 1;
	}
	sf::Sprite sprite;
	sprite.setTexture(texture);



	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))
			{
				sf::RenderWindow window2(sf::VideoMode(400, 200),"Another window!");
				tgui::Gui gui(window2);
			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))
			{
				sf::RenderWindow window3(sf::VideoMode(500, 300),"The third window!");
				tgui::Gui gui(window3);	
				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;
}

But I still keep getting the same error.What am I doing wrong now?
Thanks!
What you'e doing wrong is copying code straight from the internet without understanding how it works and just expecting it to magically work.

The code you copied assumes that you have a class named Obj, but obviously you don't, so you need to change the code.
Please refer to the tutorials on this site. I hate to say RTFM but you seem lost here , you need to have a good grasp on the c++ language to go for graphics.

As to what error you have , you haven't defined any Obj class/struct/union/enum and there's no universal class in c++.
Last edited on
OK guys thanks for the advice,I'll definitely read the manual,but in the meantime could you please write the solution in my code and post it.It would really help me a lot.Thanks!
Last edited on
Guys?
Come on guys I really need help.
Like LB said two weeks ago, you need to define your 'Obj' struct\class. Also, get rid of dos.h, I doubt it's actually hurting anything but there is nothing in that library that you would want if you're already using SFML.
Computergeek01 ok I'll try to figure it out but could you also post the solution here in case I fail?Because I'm really lost here.Thanks!

PS. also thanks for the dos.h thing I really didn't need it.
Last edited on
I really can't, I have no idea what 'Obj' is supposed to be. All I know that it contains at the very least another object called 'box' that has at least four integer variables named 'x', 'y', 'w' and 'h'. So I guess you need:
1
2
3
4
5
6
7
8
9
10
11
class BOX
{
public:
int x, y, w, h;
};

class Obj
{
public:
BOX Box;
};


If I would have done this then 'Obj' would be a pointer, but at that point we're getting into individual styles.
As the code stands for now, just remove completely active function to make it compile, anyway it is not used anywhere.
Ok I think I figured it out

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
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>

class BOX
{
public:
int x, y, w, h;
};

class Obj
{
public:
BOX box;
};

bool active(Obj obj, int mx, int my)
{
    if(mx > obj.box.x && mx < obj.box.x + obj.box.w && my > obj.box.y && my < obj.box.y + obj.box.h)
    {
        return true;
    }

    return false;
}

int main()
{	
	sf::RenderWindow window(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);

	while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();			
		
			if(active == true && event.MouseButtonReleased == sf::Mouse::Right)
				sf::RenderWindow window(sf::VideoMode(400, 200),"The button worked!");
			
			if(sf::Keyboard::isKeyPressed(sf::Keyboard::N))
			{
				sf::RenderWindow window2(sf::VideoMode(400, 200),"Another window!");
				(window2);
			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))
			{
				sf::RenderWindow window3(sf::VideoMode(500, 300),"The third window!");
				(window3);	
				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;
}

But I get an error:operand types are incompatible("bool"(*)(Obj obj, int mx, int my)" and "bool")
What's wrong now?
Thanks!
Last edited on
Come on guys I need help.I'm nearly there.
Last edited on
Come on guys I'm really stuck and I fell like I'm really close to the answer.
I need help!!!
Pages: 12