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

Pages: 12
Guys?
closed account (Dy7SLyTq)
you are treating active as a variable instead of a function. where you use active you need to instead write active(the stuff you need to pass to it)
So now it tells me Error type name is not allowed, on the Obj, and an Error expected a ')' on the obj, so what should I do now?

Here is 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#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, 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);

	while(window.isOpen())
	{
		sf::Event event;
		
		while(window.pollEvent(event))
		{
			if(event.type == sf::Event::Closed)
				window.close();			
		
			if(active(Obj obj, int mx, int my) == true && event.MouseButtonReleased == sf::Mouse::Right) // the error is on this line
			{
				sf::Window window;
					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;
}

Thanks!
Last edited on
Guys?
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
I haven't done any SFML, but this is what I do with 2D games.

-Create a tiny sprite which keeps locked to mouse co-ordinates
-Check for collisions of other sprites with that sprite.

Like I said, I have not done SFML, but that info might help you?
Ok but what about the errors?
Error type name is not allowed, on the Obj, and an Error expected a ')' on the obj.I commented the line where the errors occur.Thanks!
Last edited on
On line 49, you are not calling the function properly.
See:
http://www.cplusplus.com/doc/tutorial/functions/
for details on how functions work.
I don't even see him declaring the object anywhere..

Also you should be using a sf::Rect or sf::IntRect for the x , y , w , h.

http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php


Or even use the methods in the sf::Sprite class..

local/global bounds and/or getposition or even get texture rect.
http://www.sfml-dev.org/documentation/2.1/classsf_1_1Sprite.php
Topic archived. No new replies allowed.
Pages: 12