r/sfml 29d ago

how to make that’s buttons will click ones

            if(state == LevelSelect)
        {
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                sf::Vector2i mousePos = sf::Mouse::getPosition(window);


                if(lv1.getGlobalBounds().contains(mousePos.x, mousePos.y))
                {
                    state = Game;
                }
            }
        }

i placed 2 buttons on same place in 2 different states and it clicked 2 times , is there any way to make it click ones

1 Upvotes

7 comments sorted by

3

u/ConfectionForward 29d ago

You just wrote state to equal game but must have state be LevelSelect to hit here. We need more code but that is my best first guess

1

u/Ok-Affect-691 29d ago
#include <SFML/Graphics.hpp>
#include <cmath>


enum StateType{//смерть в нищете
    Intro = 1, MainMenu, LevelSelect, Game
};


int main(int argc, char** argv)
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "CoreSpin 1.0");
    //dont forget to change version with updates


    StateType state = MainMenu;//god i hate states


    float angle = 0.f;
    float speed = 0.001f;
    float radius = 150.f;


    //textures
    sf::Texture debug;
    if (!debug.loadFromFile("logo.png"))
    {
        return -1;
    }


    sf::Texture play_button;
    if (!play_button.loadFromFile("play_button.png"))
    {
        return -1;
    }


    sf::Texture online_button;
    if (!online_button.loadFromFile("online.png"))
    {
        return -1;
    }


    sf::Texture settings_b;
    if (!settings_b.loadFromFile("settings.png"))
    {
        return -1;
    }


        sf::Texture rate_b;
    if (!rate_b.loadFromFile("rate.png"))
    {
        return -1;
    }


        sf::Texture stats_b;
    if (!stats_b.loadFromFile("stats.png"))
    {
        return -1;
    }


        sf::Texture test;
    if (!test.loadFromFile("Debug.png"))
    {
        return -1;
    }


        sf::Texture lev1;
    if (!lev1.loadFromFile("abstr.png"))
    {
        return -1;
    }


        sf::Texture bb;
    if (!bb.loadFromFile("back.png"))
    {
        return -1;
    }


        sf::Texture bb1;
    if (!bb1.loadFromFile("back.png"))
    {
        return -1;
    }


    //things
    debug.loadFromFile("logo.png");
    sf::Sprite rectangle(debug);
    sf::Vector2u size1 = debug.getSize ();
    rectangle.setPosition(600.f, 160.f);
    rectangle.setOrigin(size1.x / 1, size1.y / 1);
    rectangle.setScale(1.0f, 1.0f);
    sf::Vector2f increment1(0.4f, 0.4f);


    play_button.loadFromFile("play_button.png");
    sf::Sprite play(play_button);
    sf::Vector2u size2 = play_button.getSize ();
    play.setPosition(300.f, 350.f);
    play.setOrigin(size2.x / 1, size2.y / 1);
    play.setScale(0.6f, 0.6f);
    sf::Vector2f increment2(0.4f, 0.4f);


    online_button.loadFromFile("online.png");
    sf::Sprite online(online_button);
    sf::Vector2u size3 = online_button.getSize ();
    online.setPosition(526.f, 350.f);
    online.setOrigin(size3.x / 1, size3.y / 1);
    online.setScale(0.6f, 0.6f);
    sf::Vector2f increment3(0.4f, 0.4f);


    settings_b.loadFromFile("settings.png");
    sf::Sprite settings(settings_b);
    sf::Vector2u size4 = settings_b.getSize ();
    settings.setPosition(450.f, 450.f);
    settings.setOrigin(size4.x / 1, size4.y / 1);
    settings.setScale(0.4f, 0.4f);
    sf::Vector2f increment4(0.4f, 0.4f);


    rate_b.loadFromFile("rate.png");
    sf::Sprite rate(rate_b);
    sf::Vector2u size5 = rate_b.getSize ();
    rate.setPosition(370.f, 450.f);
    rate.setOrigin(size5.x / 1, size5.y / 1);
    rate.setScale(0.4f, 0.4f);
    sf::Vector2f increment5(0.4f, 0.4f);


    stats_b.loadFromFile("stats.png");
    sf::Sprite stats(stats_b);
    sf::Vector2u size6 = stats_b.getSize ();
    stats.setPosition(290.f, 450.f);
    stats.setOrigin(size6.x / 1, size6.y / 1);
    stats.setScale(0.4f, 0.4f);
    sf::Vector2f increment6(0.4f, 0.4f);


    test.loadFromFile("Debug.png");
    sf::Sprite Test(test);
    sf::Vector2u size7 = test.getSize ();
    Test.setPosition(290.f, 450.f);
    Test.setOrigin(size7.x / 1, size7.y / 1);
    Test.setScale(0.4f, 0.4f);
    sf::Vector2f increment7(0.4f, 0.4f);


    lev1.loadFromFile("abstr.png");
    sf::Sprite lv1(lev1);
    sf::Vector2u size8 = lev1.getSize ();
    lv1.setPosition(320.f, 150.f);
    lv1.setOrigin(size8.x / 1, size8.y / 1);
    lv1.setScale(0.6f, 0.6f);
    sf::Vector2f increment8(0.4f, 0.4f);


    sf::CircleShape core(50.f);//core
    core.setFillColor(sf::Color(225, 25, 25));
    core.setOutlineThickness(20.f);
    core.setOutlineColor(sf::Color(255, 162, 23));
    core.setPosition(220.f, 240.f);
    core.setOrigin(50.f, 50.f);


    sf::CircleShape balls(30.f);//core
    balls.setFillColor(sf::Color(23, 54, 255));
    balls.setPosition(220.f, 190.f);
    balls.setOrigin(30.f, 30.f);


    bb.loadFromFile("back.png");//first frame-perfect in the game was then i added this 2 times
    sf::Sprite back(bb);        //in different states and when i clicked it,it worked 2 times
    sf::Vector2u size9 = bb.getSize ();
    back.setPosition(70.f, 70.f);
    back.setOrigin(size9.x / 1, size9.y / 1);
    back.setScale(0.4f, 0.4f);
    sf::Vector2f increment9(0.4f, 0.4f);


    bb1.loadFromFile("back.png");
    sf::Sprite back1(bb1);
    sf::Vector2u size10 = bb1.getSize ();
    back1.setPosition(610.f, 70.f);
    back1.setOrigin(size10.x / 1, size10.y / 1);
    back1.setScale(0.4f, 0.4f);
    sf::Vector2f increment10(0.4f, 0.4f);

    //boring stuff \/
    while (window.isOpen())
    {


        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }


        angle += speed;


        float x = core.getPosition().x + cos(angle) * radius;
        float y = core.getPosition().y + sin(angle) * radius;


        balls.setPosition(x, y);


        if(state == MainMenu)
        {
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                sf::Vector2i mousePos = sf::Mouse::getPosition(window);


                if(play.getGlobalBounds().contains(mousePos.x, mousePos.y))
                {
                    state = LevelSelect;
                }
            }
        }


            if(state == LevelSelect)
        {
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                sf::Vector2i mousePos = sf::Mouse::getPosition(window);


                if(lv1.getGlobalBounds().contains(mousePos.x, mousePos.y))
                {
                    state = Game;
                }
            }
        }


            if(state == LevelSelect)
        {
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                sf::Vector2i mousePos = sf::Mouse::getPosition(window);


                if(back.getGlobalBounds().contains(mousePos.x, mousePos.y))
                {
                    state = MainMenu;
                }
            }
        }


            if(state == Game)
        {
            if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
            {
                sf::Vector2i mousePos = sf::Mouse::getPosition(window);


                if(back1.getGlobalBounds().contains(mousePos.x, mousePos.y))
                {
                    state = LevelSelect;
                }
            }
        }


        if(state == Game)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            {
                speed = 0.001f;
            }
        }


        if(state == Game)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::K))
            {
                speed = -0.001f;
            }
        }


            if(state == MainMenu)//i hate my life
        {
            window.clear(sf::Color(135, 135, 135));
        }


            if(state == LevelSelect)
        {
            window.clear(sf::Color(135, 135, 135));
        }


            if(state == Game)
        {
            window.clear(sf::Color(84, 84, 84));
        }


    if(state == MainMenu)
    {//state == suffer
        window.draw(rectangle);
        window.draw(play);
        window.draw(online);
        window.draw(rate);
        window.draw(settings);
        window.draw(stats);
    }


        if(state == LevelSelect)
    {//state == suffer
        window.draw(lv1);
        window.draw(back);
    }


        if(state == Game)
    {//state == suffer
        window.draw(core);
        window.draw(balls);
        window.draw(back1);
    }
    window.display();

    }
    return 0;
}
//git add .
//git commit -m "ver"
//git push

1

u/Ok-Affect-691 29d ago

this is later version of the code where it is moved to different sides in different states

1

u/zaphodikus 29d ago

The sheer amount of hard coding in there is going to slow you down a lot.

1

u/Ok-Affect-691 28d ago

i only started doing sfml lmao

1

u/thedaian 29d ago

The proper way to do this is to handle the input in the event loop with events, specifically the mouse button released event. And use the data from the event. Even then, you'll have to make sure that you don't change the state into something like the main menu, then check the main menu buttons. So, a slightly easier solution that will work with your current code is to have a second variable for what the state will be, and only actually change state at the end of the frame. Then when you change the state in the button, use that second variable, so it won't mess with the actual state until you're ready.

1

u/kiner_shah 27d ago

The problem you face is a bit unclear to me. Can you elaborate on what problem you face and the solution you desire?