Oct 20, 2013 at 6:15pm Oct 20, 2013 at 6:15pm UTC
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 Nov 14, 2013 at 10:48am Nov 14, 2013 at 10:48am UTC
Oct 20, 2013 at 7:18pm Oct 20, 2013 at 7:18pm UTC
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
Oct 20, 2013 at 8:04pm Oct 20, 2013 at 8:04pm UTC
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 Oct 20, 2013 at 8:04pm Oct 20, 2013 at 8:04pm UTC
Oct 20, 2013 at 9:37pm Oct 20, 2013 at 9:37pm UTC
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.
Oct 23, 2013 at 8:43pm Oct 23, 2013 at 8:43pm UTC
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 Oct 23, 2013 at 8:44pm Oct 23, 2013 at 8:44pm UTC
Oct 23, 2013 at 9:04pm Oct 23, 2013 at 9:04pm UTC
Why did you define that function inside of the main function? You cannot define functions inside of functions.
Oct 24, 2013 at 12:36am Oct 24, 2013 at 12:36am UTC
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.
Oct 24, 2013 at 12:43am Oct 24, 2013 at 12:43am UTC
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 Oct 24, 2013 at 12:47am Oct 24, 2013 at 12:47am UTC
Oct 24, 2013 at 3:39pm Oct 24, 2013 at 3:39pm UTC
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 Nov 3, 2013 at 3:25pm Nov 3, 2013 at 3:25pm UTC
Nov 3, 2013 at 3:26pm Nov 3, 2013 at 3:26pm UTC
Come on guys I really need help.
Nov 3, 2013 at 3:54pm Nov 3, 2013 at 3:54pm UTC
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.
Nov 3, 2013 at 5:44pm Nov 3, 2013 at 5:44pm UTC
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 Nov 3, 2013 at 5:52pm Nov 3, 2013 at 5:52pm UTC
Nov 3, 2013 at 7:33pm Nov 3, 2013 at 7:33pm UTC
As the code stands for now, just remove completely active
function to make it compile, anyway it is not used anywhere.
Nov 4, 2013 at 9:46pm Nov 4, 2013 at 9:46pm UTC
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 Nov 8, 2013 at 7:00am Nov 8, 2013 at 7:00am UTC
Nov 8, 2013 at 9:22am Nov 8, 2013 at 9:22am UTC
Come on guys I need help.I'm nearly there.
Last edited on Nov 8, 2013 at 6:23pm Nov 8, 2013 at 6:23pm UTC
Nov 13, 2013 at 9:37am Nov 13, 2013 at 9:37am UTC
Come on guys I'm really stuck and I fell like I'm really close to the answer.
I need help!!!