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

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

0 Members and 1 Guest are viewing this topic.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: How to make a button in SFML?
« Reply #15 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.

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #16 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!

G.

  • Hero Member
  • *****
  • Posts: 1471
    • View Profile
_
« Reply #17 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.
« Last Edit: November 24, 2013, 11:32:16 pm by G. »

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #18 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!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: How to make a button in SFML?
« Reply #19 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  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #20 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?

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #21 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;
}
 

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: How to make a button in SFML?
« Reply #22 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.
I use the latest build of SFML2

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: How to make a button in SFML?
« Reply #23 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.
« Last Edit: November 26, 2013, 12:19:00 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6226
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to make a button in SFML?
« Reply #24 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.
« Last Edit: November 26, 2013, 06:00:21 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #25 on: November 26, 2013, 08:20:56 pm »
Lethn you mean the switch statements right?

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: How to make a button in SFML?
« Reply #26 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.
« Last Edit: November 27, 2013, 07:37:31 am by Lethn »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32464
    • View Profile
    • SFML's website
    • Email
Re: How to make a button in SFML?
« Reply #27 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...
Laurent Gomila - SFML developer

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: How to make a button in SFML?
« Reply #28 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32464
    • View Profile
    • SFML's website
    • Email
Re: How to make a button in SFML?
« Reply #29 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.
Laurent Gomila - SFML developer