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

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

0 Members and 1 Guest are viewing this topic.

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #30 on: November 30, 2013, 11:53:02 am »
K so how to make the sprite change when the mouse is over it?
Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
AW: How to make a button in SFML?
« Reply #31 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".
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nedim1

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

santiaboy

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

Nedim1

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

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #35 on: December 17, 2013, 02:13:06 pm »
Bump.

wintertime

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

G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: How to make a button in SFML?
« Reply #37 on: December 17, 2013, 04:34:17 pm »
But it would require actual thinking...

Kalima

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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to make a button in SFML?
« Reply #39 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #40 on: December 20, 2013, 11:34:23 pm »
Wtf so instead of answers i get hate?Thanks a lot guys!!!

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 #41 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();
            }
        }
« Last Edit: December 21, 2013, 03:59:53 am by zsbzsb »
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 #42 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!

AlexAUT

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

Nedim1

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: How to make a button in SFML?
« Reply #44 on: December 22, 2013, 01:58:39 pm »
AlexAUT can you please add a link, I can't find the example.Thanks!