Search code examples
c++sfml

The application crashes and runs at the same time


I draw the application menu, having previously written the menu class and the function of this class.

When I launch the application, the menu works for 5-7 seconds, but in Windows the cursor are spinning in the form of a blue circle, which does not bode well. If these 5-7 seconds are idle, the whole screen turns white, and Windows says that the application is not responding. In this case, if you press where the buttons were broken, these buttons are triggered, the menu closes and the game starts. The problem lies in the description of the menu class, I think.

Code of Menu class:

class Menu
{
public:
    Image menuImage1, menuImage2, menuImage3;
    Texture menuTexture1, menuTexture2, menuTexture3;

    int menuNum = 0;
    bool isMenu;

    list <Asteroid*> asteroidsMass;
    list <Asteroid*> ::iterator it;

    Sprite menu1, menu2, menu3;

    Menu()
    {
        menuImage1.createMaskFromColor(Color::Black);
        menuImage2.createMaskFromColor(Color::Black);
        menuImage3.createMaskFromColor(Color::Black);

        menuImage1.loadFromFile("title.jpg");
        menuImage2.loadFromFile("start.jpg");
        menuImage3.loadFromFile("exit.jpg");

        menuTexture1.loadFromImage(menuImage1);
        menuTexture2.loadFromImage(menuImage2);
        menuTexture3.loadFromImage(menuImage3);

        menu1.setTexture(menuTexture1);
        menu2.setTexture(menuTexture2);
        menu3.setTexture(menuTexture3);

        menu1.setPosition(scrX / 2, 200);
        menu2.setPosition((scrX / 2) - 172.5, 300);
        menu3.setPosition((scrX / 2) - 140, 450);

        menu1.setOrigin(300, 75);

        for (int i = 0; i < 15; i++)
        {
            Asteroid* aster = new Asteroid(rand() % 1200, rand() % 800, 5, 25, 0);
            asteroidsMass.push_back(aster);
        }
        isMenu = true;
    }

    void update(RenderWindow& app)
    {
        while (isMenu)
        {
            menu2.setColor(Color::White);
            menu3.setColor(Color::White);

            app.clear(Color::Black);

            menuNum = 0;

            if (IntRect((scrX / 2) - 172, 300, 360, 100).contains(Mouse::getPosition(app))) { menu2.setColor(Color::Green); menuNum = 2; }
            else if (IntRect((scrX / 2) - 172, 450, 360, 100).contains(Mouse::getPosition(app))) { menu3.setColor(Color::Green);menuNum = 3; }

            if (Mouse::isButtonPressed(Mouse::Left))
            {

                if (menuNum == 2) 
                {
                    for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
                    {
                        Asteroid* asteroid = *it;
                        it = asteroidsMass.erase(it);
                        delete asteroid;
                    }
                    isMenu = false;
                
                }
                if (menuNum == 3) 
                { 
                    for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
                    {
                        Asteroid* asteroid = *it;
                        it = asteroidsMass.erase(it);
                        delete asteroid;
                    }
                    
                    isMenu = false; 
                    app.close(); 
                    
                }

            }

            for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
            {
                app.draw((*it)->EntityShape);
            }

            for (it = asteroidsMass.begin(); it != asteroidsMass.end(); it++)
            {
                (*it)->update();
            }

            app.draw(menu1);
            app.draw(menu2);
            app.draw(menu3);

            app.display();
        }
    }
};

All code: https://pastebin.pl/view/0c1d6c9e


Solution

  • I SOLVED MY PROBLEM! If you have same problem, just write in "while" loop checking of closing window:

    while(isMenu)
    {
    Event event;
        while (app.pollEvent(event))
            {
                if (event.type == Event::Closed)
                    app.close();
            }
    //some code
    }