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

Author Topic: How to make a button in SFML?  (Read 51976 times)

0 Members and 1 Guest are viewing this topic.

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
How to make a button in SFML?
« on: November 18, 2013, 09:33:03 pm »
Ok guys so I'm trying to make a main menu in SFML, and I wrote the code that I think should work but it doesn't
So if anyone could tell me what I'm doing wrong and how to correct it, it would be greatly appreciated.Thanks!
Here's my code:
#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) //This line Celtic Minstrel
                                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;
}
 
It tells me Error:operand types are incompatible("bool"(*)(Obj obj, int mx, int my)" and "bool")
P.S. English is not my native language so if I made any mistakes I apologize.
« Last Edit: November 20, 2013, 10:39:22 pm by Nedim1 »

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: How to make a button in SFML?
« Reply #1 on: November 18, 2013, 10:32:07 pm »
Well, for starters, event.MouseButtonReleased is not a thing, though technically it does exist. MouseButtonReleased is in fact a static constant in sf::Event, much like sf::Event::Closed, which tells you that this event was a mouseup event. So you should be checking it event.type == sf::Event::MouseButtonReleased and then check if event.mouseButton.button == sf::Mouse::Right.

As for the error you listed at the bottom, I can't help you with it because I can't figure out where in your code it's being caused.

By the way, instead of BOX you can use sf::IntRect.

Austin J

  • Newbie
  • *
  • Posts: 29
    • View Profile
    • Email
Re: How to make a button in SFML?
« Reply #2 on: November 18, 2013, 11:31:51 pm »
You kind of have the right idea. I actually have a Button class in my current project that so far works pretty well. I'll share a bit of the idea behind it.

First off, make a button class. There isn't any reason for this box within obj deal you have going on. Your button needs to know its width, its height, and its position. I also give the Button class a boolean "mouseIsHovering". This makes things more organizational. As far as checking if the mouse is hovering over the button goes, you're on the right track. (Assuming you don't want to use
bool sf::FloatRect::contains(<mouse coords here>)
)

I find it better for the button to constantly know 4 values. Xmin, Xmax, Ymin, and Ymax. As you already
figured out, the min is the position and the max is the min + the width or height.

sf::Vector2f mp;
mp.x = sf::Mouse::getPosition(<window here>).x;
mp.y = sf::Mouse::getPosition(<window here>).y;
if(mp.x > button.Xmin && mp.x < button.Xmax && mp.y > button.YMin && mp.y < button.Ymax)
{
     button.setHoveringTrue();
}
 

Then basically at some point inside sf::Event::mouseButtonReleased
if(button.isHovering() == true)
{
     button.perform(); // or whatever function you use on click
}
 




G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to make a button in SFML?
« Reply #3 on: November 19, 2013, 09:56:23 am »
Don't use active like a variable whereas it's a function.

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: How to make a button in SFML?
« Reply #4 on: November 19, 2013, 12:10:06 pm »
As I'm also programming a GUI atm and much more than a button at that, I collected a few inspirations I can share.
An interesting talk about GUI programming and even a whole forum to read:
- https://mollyrocket.com/forums/viewtopic.php?t=134
- http://silverspaceship.com/inner/imgui/

A nice tutorial:
- http://sol.gfxile.net/imgui/

Some additional links I collected:
- http://www.fysx.org/2010/09/13/quest-for-a-gui/
- http://www.johno.se/book/imgui.html

Btw., you dont need to replicate info for hotness(hovering), activation, mouse pointer position and so on for all buttons, once for the whole GUI is enough.
( I would write more but no time now.)

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #5 on: November 20, 2013, 10:43:21 pm »
Celtic Minstrel I have commented the line where the error happens.
Austin J I'm very interested in your way could you please write the whole button class here, it would be greatly appreciated.
G how should i write that piece of code?
wintertime thanks for the links they are really interesting.
Thanks!

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to make a button in SFML?
« Reply #6 on: November 20, 2013, 11:15:28 pm »
G how should i write that piece of code?
With parenthesis.
//Your code, where you use "active" like a boolean variable
if(active == true && event.MouseButtonReleased == sf::Mouse::Right)
//Correct code, where the result of the function "active" is compared to true
if(active() == true && event.MouseButtonReleased == sf::Mouse::Right)

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #7 on: November 21, 2013, 10:17:09 pm »
OK G. I've written it like you said but, now it tells me Error:too few arguments in a function call, so what now?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How to make a button in SFML?
« Reply #8 on: November 21, 2013, 10:24:39 pm »
You wrote the function, so I guess you should know how to use it better than anyone. ;)
Your "active" function has 3 parameters: 1 Obj and 2 int. You don't have any Obj declared in your program so I don't know what I could do.
If you don't know what variables and functions are, and how to use them you're in DEEP shit.
if(active(someObj, someInt, someInt) == true && event.MouseButtonReleased == sf::Mouse::Right)
//Replace someObj, someInt and someInt by the real variables you want to use

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #9 on: November 21, 2013, 11:43:43 pm »
Wow I'm really stuck can someone please write the whole button class code and post it here PLEASE!!!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to make a button in SFML?
« Reply #10 on: November 21, 2013, 11:56:28 pm »
Wow I'm really stuck can someone please write the whole button class code and post it here PLEASE!!!
No, that's your task.

But you can search the forum (buttons have been discussed numerous times) or look at a GUI library such as SFGUI or TGUI.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
_
« Reply #11 on: November 22, 2013, 12:28:23 am »
You've been on this for a month now... http://www.cplusplus.com/forum/windows/114185/
Maybe it's time to learn programming and C++ or use WYSIWYG tools...

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #12 on: November 22, 2013, 10:07:43 am »
Well I knew it was only a matter of time before someone would find the cplusplus forum post.Good work G.

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #13 on: November 22, 2013, 09:27:52 pm »
K so I was thinking to make a tiny sprite that locks to the mouse coordinates and then to check if it is over the other rectangle, is that a god way?Also should I include Rect.hpp?Any advice is welcome.Thanks!

MrMuffins

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: How to make a button in SFML?
« Reply #14 on: November 23, 2013, 12:29:55 am »
Why use another sprite for collision checking when you already have the mouse coordinates?

Make a simple function for checking if your mouse is over the button (or any sprite for that matter).
Or use the sprites FloatRect contains function for checking.

if (button.getGlobalBounds().contains(mouse.X, mouse.Y)) {
    std::cout << "Button Hover" << std::endl;
}
 

or a more less static way

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

 

anything