SFML community forums

Help => General => Topic started by: Nedim1 on November 18, 2013, 09:33:03 pm

Title: How to make a button in SFML?
Post by: Nedim1 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.
Title: Re: How to make a button in SFML?
Post by: Celtic Minstrel 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.
Title: Re: How to make a button in SFML?
Post by: Austin J 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
}
 



Title: Re: How to make a button in SFML?
Post by: G. on November 19, 2013, 09:56:23 am
Don't use active like a variable whereas it's a function.
Title: Re: How to make a button in SFML?
Post by: wintertime 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.)
Title: Re: How to make a button in SFML?
Post by: Nedim1 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!
Title: Re: How to make a button in SFML?
Post by: G. 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)
Title: Re: How to make a button in SFML?
Post by: Nedim1 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?
Title: Re: How to make a button in SFML?
Post by: G. 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
Title: Re: How to make a button in SFML?
Post by: Nedim1 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!!!
Title: Re: How to make a button in SFML?
Post by: Nexus 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.
Title: _
Post by: G. 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...
Title: Re: How to make a button in SFML?
Post by: Nedim1 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.
Title: Re: How to make a button in SFML?
Post by: Nedim1 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!
Title: Re: How to make a button in SFML?
Post by: MrMuffins 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;
}
 
Title: Re: How to make a button in SFML?
Post by: Joshua Flynn on November 23, 2013, 02:32:55 pm
Error checking is significantly easier if you encapsulate your code (preferably avoiding Russian doll ideograms).

Two ways to approach the problem, which depends on your preference: C programming style (stand alone, independent functions) or C++ (class based functions).

Something like:

Code: [Select]
class Button
{
    public:
    sf::IntRect Box; //This could easily be protected or private
   
    const bool IsClicked(const sf::Mouse Mouse, const float X, const float Y)
    {
        //We check if it's clicked first because a direct value comparison is less resource intensive than an area check
        if(!Mouse.isButtonPressed(sf::Mouse::Left)){return false;}
        //It's pressed! Let's check it's actually in the box:
        return Box.contains(X,Y);
    }
   
};

That's just an idea. You could alternately make it so the function takes both the window and mouse as an argument and does all the leg work of figuring out if the mouse has clicked on it, itself (although it'd be highly inefficient for, say, 80+ buttons to ALL check the button has been clicked, and be easier to just use rect's 'contains' in a for() loop function inside a clicked button if statement).

Don't worry about efficiency at this stage, just get the idea working then prune and improve it afterwards.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 23, 2013, 11:12:30 pm
So this is my remake:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


        bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp)
        {
                if (sprite.contains(mp)){
                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);

        sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;



        while(window.isOpen())
        {
                sf::Event event;
               
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();                
               
                        if(isSpriteHover) //this should check if the bool is true right?
        {
                if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Right)
                {
                        sf::Window window;
                        window.create(sf::VideoMode(400, 200),"The button worked!");
                }
       
        }

                        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;
}
 
It sort of works now but the problem is no matter where i right click it makes the window I think it has to do with the bool, I think it doesn't check if its true or not, please write how should i check if the bool is true and if I am doing it right why doesn't it work properly?
Thanks!
Title: _
Post by: G. on November 24, 2013, 11:30:17 pm
If you don't know what variables and functions are, and how to use them you're in DEEP shit.
Maybe it's time to learn programming and C++ or use WYSIWYG tools...
Lulz.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 24, 2013, 11:55:01 pm
Actually earlier today I made a button if you wanna know the code its:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


        bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp)
        {
                if (sprite.contains(mp)){
                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);

        sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;



        while(window.isOpen())
        {
                sf::Event event;
               
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();                
               
                        if(isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)) == true)
         {
                if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Left)
                {
                        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();

                        }
              }
                }
         }
       
             }
                window.clear(sf::Color::Black);

                sprite.setPosition(sf::Vector2f(50, 300));
               
                window.draw(sprite);
               
                window.display();

}
       
return 0;
}
 
Now I wanna know if there's a better way to do it.
Thanks!
Title: Re: How to make a button in SFML?
Post by: zsbzsb on November 25, 2013, 02:10:27 am
Now I wanna know if there's a better way to do it.
Thanks!

For starters read the official tutorial about event handling. Reading information from the event union before checking the type is a big nono.

Second your isSpriteHover is entirely redundant, as in not even required.

And third, creating another window inside another window's event loop isn't a good idea  ;)
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 25, 2013, 09:19:07 am
Ok I'll work on the first two but what about the third one where should I create it?
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 25, 2013, 09:28:52 am
How about this(I'm not actually creating another window, I'm just recreating the first one).
Here's the code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


        bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp)
        {
                if (sprite.contains(mp)){
                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);

        sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;



        while(window.isOpen())
        {
                sf::Event event;
               
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();                
               
                        if(isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)) == true)
         {
                if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Left)
                {
                        window.create(sf::VideoMode(400, 200),"The button worked!");
                }
         }
       
             }
                window.clear(sf::Color::Black);

                sprite.setPosition(sf::Vector2f(50, 300));
               
                window.draw(sprite);
               
                window.display();

}
       
return 0;
}
 
Title: Re: How to make a button in SFML?
Post by: OniLinkPlus on November 25, 2013, 09:55:21 am
You don't have to recreate the window to change its properties. You can change the title and the size quite easily using sf::Window's member functions.
Title: Re: How to make a button in SFML?
Post by: Lethn on November 26, 2013, 12:17:07 pm
If I can speak out, I think you're also making the exact same mistakes I've been making with buttons, your events code should look like the tutorial here: http://sfml-dev.org/tutorials/2.1/window-events.php be sure to fix this up as well otherwise you'll have all sorts of problems loading image files etc. for your buttons.

I told you guys the documentation was confusing for noobs, it's like the whole SJLJ and DW2 scenario I went through but I could understand if it's meant to be like that on purpose to test your knowledge.
Title: Re: How to make a button in SFML?
Post by: Nexus on November 26, 2013, 05:51:08 pm
I told you guys the documentation was confusing for noobs
Where did you tell that? Did you also have concrete advice what could be done better?

The much bigger problem than the quality of the documentation is the fact that a lot of people don't read it at all, or they don't read it carefully and try to understand it. Furthermore, this thread (and yours of the past, by the way) is a good example of how people constantly ignore advice given by other SFML users and are not willing to accept that they're lacking C++ knowledge and have to fill the gaps before proceeding with SFML. I don't want to offend anyone, but blaming the SFML documentation when the problem lies totally elsewhere is a bit too easy.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 26, 2013, 08:20:56 pm
Lethn you mean the switch statements right?
Title: Re: How to make a button in SFML?
Post by: Lethn on November 27, 2013, 07:35:33 am
Yeah, I noticed you've only done text so far, but once you try doing images it will become difficult but if you know about switch statements already you should be fine so you can brush off my statements if you like :D I just noticed it in your code.

I mentioned it in my programming questions thread, I've also mentioned the SJLJ and DW2 on a seperate thread which helped out a lot of people who were struggling to even get SFML working on Codeblocks because of the conflicts with the package that was provided. There's not having the C++ knowledge which I freely admit to and I've been having a thorough look at switch statements ( I understand it better now yey! :) ) but equally you guys have simply made the mistake of not matching up the documentation with your tutorials.

In the documentation there is no usage of switch statements and break statements so how can someone new to SFML know that they're needed without just randomly putting them in? It's like putting up a tutorial and then leaving out certain bits then blaming the user when they can't understand why they need to do something that you haven't even mentioned but only you know about.
Title: Re: How to make a button in SFML?
Post by: Laurent on November 27, 2013, 08:41:02 am
Please don't start again, there are already 30 pages of useless posts in your own thread.

Switch statements are not stricly needed. You could do the same job with "if", or even "goto" if you like them. But since you have absolutely no idea what you're writing because you don't want to learn C++ properly before programming games, you think they are an important thing. A slight difference between the doc and tutorials, and you're already lost. Don't you think it's because of a lack of basic knowledge on your side?

The documentation is not confusing for noobs (there are lots of noobs out there that succeed to write simple things). It's confusing for you.

It's like you fail to eat your pasta because you don't know how to use a fork. And then you blame the pasta brand for not explaining how to use a fork on the pasta box.

So please now stop blaming the documentation and try to learn something ;)

PS: sorry for being a little rude, of course; I'm so tired of him...
Title: Re: How to make a button in SFML?
Post by: Lethn on November 27, 2013, 09:08:18 am
Don't start again? I'm not starting anything, that's entirely up to you, I was just making a comment, if you want to take everything personally and get people involved in a flamewar that's entirely your fault.
Title: Re: How to make a button in SFML?
Post by: Laurent on November 27, 2013, 09:17:20 am
Ok, so let's come back to the original problem and avoid referring to your personal issues with the documentation.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on November 30, 2013, 11:53:02 am
K so how to make the sprite change when the mouse is over it?
Thanks!
Title: AW: How to make a button in SFML?
Post by: eXpl0it3r on November 30, 2013, 11:56:11 am
Change the texture rect when the mouse is over the sprite. This assumes your texture holds both "states".
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 09, 2013, 04:00:12 pm
K guys so I have another question I'm sure the solution is simple, but now that I have the recreated window how do I clear it here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>


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


int main()
{      

        sf::RenderWindow window;
                window.create(sf::VideoMode(800, 600),"My first Visual Studio window!");
       
        sf::Texture texture;
        if(!texture.loadFromFile("button1.png"))
        {
                return 1;
        }
       
        sf::Texture backgroundtexture;
        if(!backgroundtexture.loadFromFile("background.png"))
        {
                return 1;
        }
       
        sf::Sprite background;
        background.setTexture(backgroundtexture);
       
        sf::Sprite sprite;
        sprite.setTexture(texture);

        sf::Vector2f mp;
    mp.x = sf::Mouse::getPosition(window).x;
    mp.y = sf::Mouse::getPosition(window).y;

        while(window.isOpen())
        {
                sf::Event event;
               
                while(window.pollEvent(event))
                {
                        if(event.type == sf::Event::Closed)
                                window.close();                
               
                        if(isSpriteHover(sprite.getGlobalBounds(), sf::Vector2f(event.mouseButton.x, event.mouseButton.y)) == true)
                        {  
                if(event.type == sf::Event::MouseButtonReleased &&  event.mouseButton.button == sf::Mouse::Left)
                {
                        window.create(sf::VideoMode(400, 200),"The button worked!");
                        window.clear(sf::Color::White);
                        window.display();
                }
                }
       
            }
                window.clear(sf::Color::Black);

                background.setPosition(sf::Vector2f(0, 0));

                window.draw(background);

                sprite.setPosition(sf::Vector2f(50, 300));

                window.draw(sprite);
               
                window.display();

}
       
return 0;
}
 
Thanks!
Title: Re: How to make a button in SFML?
Post by: santiaboy on December 09, 2013, 07:31:39 pm
K guys so I have another question I'm sure the solution is simple, but now that I have the recreated window how do I clear it here's my code:
...
 
Thanks!

Clear it? Like what you did at the bottom? "window.clear(sf::Color::Black);"

Also, check this

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

This is the same as
    bool isSpriteHover(sf::FloatRect sprite, sf::Vector2f mp) { return sprite.contains(mp) }
 
which is much better. Do you understand why that works? Thing is, sprite.contains() returns a boolean. So instead of doing if(true) {return true} else{return false} just, return the boolean that you receive. Now, there's a whole lot of things why this is better. The two most important ones: It's clearer and you don't have an "if". Getting rid of ifs is something that is great, and there's theory behind it. Basically, if you don't have an if, the computer knows what are the next instructions. If you have an 'if' statement, then you will have 2 paths, and the computer doesn't know which one to execute (at first), so it can't "see" what will come.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 09, 2013, 10:52:42 pm
santiaboy thanks for the reply I've written "window.clear(sf::Color::White)" in the code I posted but it doesn't clear it why not?
Also thanks for the tip on the bool thing, this way is faster right?
Thanks!
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 17, 2013, 02:13:06 pm
Bump.
Title: Re: How to make a button in SFML?
Post by: wintertime on December 17, 2013, 04:17:10 pm
You spend too much time on this, trying to get a few pieces of code from people all over the internet over months. Your persistence would be much better spent by attending a course held by a professional teacher, who would help you get the fundamental knowledge you are missing.
Title: Re: How to make a button in SFML?
Post by: G. on December 17, 2013, 04:34:17 pm
But it would require actual thinking...
Title: Re: How to make a button in SFML?
Post by: Kalima on December 18, 2013, 05:38:01 am
What function does it have to make a button in SFML?I am a new,and interested in it.
Title: Re: How to make a button in SFML?
Post by: Nexus on December 18, 2013, 09:38:17 am
What function does it have to make a button in SFML?
None. You have to handcraft your button using shapes or sprites and check the mouse events to react to clicks.
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 20, 2013, 11:34:23 pm
Wtf so instead of answers i get hate?Thanks a lot guys!!!
Title: Re: How to make a button in SFML?
Post by: zsbzsb on December 21, 2013, 02:40:46 am
Wtf so instead of answers i get hate?Thanks a lot guys!!!

You didn't get hate, but this is really something you should be able to do yourself. If you can't figure out how to make a simple button do you really think you are ready for making games?

Anyways, here is a simple working button example in C#, but I am sure you should be able to convert it to C++ (focus on the logic and not the language).

        static void Main(string[] args)
        {
            SFML.Graphics.RenderWindow window = new SFML.Graphics.RenderWindow(new SFML.Window.VideoMode(500, 500), "Button");
            window.SetVerticalSyncEnabled(true);
            SFML.Graphics.RectangleShape buttonshape = new SFML.Graphics.RectangleShape(new SFML.Window.Vector2f(200, 200)) { Position = new SFML.Window.Vector2f(150, 150), FillColor = SFML.Graphics.Color.Red };
            window.Closed += (sender, e) => { window.Close(); };
            window.MouseButtonPressed += (sender, e) =>
                {
                    SFML.Window.Vector2f mappedpos = window.MapPixelToCoords(new SFML.Window.Vector2i(e.X, e.Y));
                    if (buttonshape.GetGlobalBounds().Contains(mappedpos.X, mappedpos.Y))
                    {
                        // BUTTON CLICKED
                        buttonshape.FillColor = SFML.Graphics.Color.Green;
                    }
                };
            window.MouseButtonReleased += (sender, e) =>
                {
                    // MOUSE RELEASED
                    buttonshape.FillColor = SFML.Graphics.Color.Red;
                };
            while (window.IsOpen())
            {
                window.DispatchEvents();
                window.Clear();
                window.Draw(buttonshape);
                window.Display();
            }
        }
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 21, 2013, 10:29:48 pm
Guys I already made a button, I want to know how to clear the recreated window in the code i posted on this page.That's all.Thanks!
Title: Re: How to make a button in SFML?
Post by: AlexAUT on December 21, 2013, 10:41:40 pm
I want to know how to clear the recreated window in the code

window.clear();

 This will clear all the content from the current framebuffer.

I guess you mean how to to show different screens, like menu/game/scoreboard etc. If so, a "state machine" is the easiest way to achieve it. There is an example of a simple finite state machine in the SFML wiki.


AlexAUT
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 22, 2013, 01:58:39 pm
AlexAUT can you please add a link, I can't find the example.Thanks!
Title: Re: How to make a button in SFML?
Post by: AlexAUT on December 22, 2013, 04:13:18 pm
Here is the link. https://github.com/SFML/SFML/wiki/Tutorial%3A-Manage-different-Screens

Keep in mind that there are different types of state machines. This one can only have one active screen, but in most cases this is enough.


AlexAUT
Title: Re: How to make a button in SFML?
Post by: Nedim1 on December 25, 2013, 08:12:42 pm
AlexAUT thank you, and this is in SFML version 1,6 right?
Title: Re: How to make a button in SFML?
Post by: AlexAUT on December 25, 2013, 08:52:23 pm
AlexAUT thank you, and this is in SFML version 1,6 right?

Yes it's for SFML 1.6 but it shouldn't be a problem to change it to the current API.

Edit: I updated the code in the wiki, now it uses SFML2+

AlexAUT