Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Button  (Read 2355 times)

0 Members and 1 Guest are viewing this topic.

TheDianamu

  • Newbie
  • *
  • Posts: 20
    • View Profile
SFML Button
« on: November 13, 2013, 07:54:40 am »
Hi, I made a small Button set since SFML doesnt have a button class directly inputted into it, so I tried to make my own, and I got this, you can add/change/modify it to your liking

//Get the position of the Mouse
int MouseX = sf::Mouse::getPosition().x;
int MouseY = sf::Mouse::getPosition().y;

//Create a Shape for the button
sf::RectangleShape button; //Doesnt have to be a rectangle

//Give the Button a color
button.setFillColor(sf::Color::Blue);

//Set size of the button
button.setSize(sf::Vector2f(100,48)); //Make it what ever you like

//Set the position of the button
button.setPosition(200,200);//or where ever you like

//The Fun Stuff!!
if(button.getGlobalBounds().width <=> MouseX
&& button.getGlobalBounds().height <=> MouseY){
//Do What you want to do with the button
button.setFillColor(sf::Color::Green);
}

 

Now this may not be the most efficient way to do this, but it worked for me when I tried it.
« Last Edit: November 13, 2013, 08:02:55 am by TheDianamu »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Major WTF
« Reply #1 on: November 13, 2013, 08:40:55 am »
WTF is this thing? :o
I don't see how it's supposed to work or know what is <=> in C++. You're not even taking in account the position of the button. Moreover if you want to check if a point (or a mouse) is inside an sf::Rect, you can use the contains function.
« Last Edit: November 13, 2013, 08:42:54 am by G. »