SFML community forums

Help => Graphics => Topic started by: mpaw on June 02, 2020, 10:36:33 pm

Title: App freezes after few seconds
Post by: mpaw on June 02, 2020, 10:36:33 pm
Hi.

After running a program, I get freeze thing (Aplication stops to response) and I have to terminate it by system manager. In CodeBlocks it says
Process returned -805306369 (0xCFFFFFFF)

case Game::Playing:
{
    while(gameState!=ShowingMenu)
    {
        mainWindow.clear(sf::Color(80,70,200));
        player1.Draw(mainWindow);
        mainWindow.display();

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
        {
            ShowMenu();
            gameState = ShowingMenu;
            cout << 1;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            player1.mirror(false);
            mainWindow.clear(sf::Color(80,70,200));
            player1.moving(-2,0);
            player1.Draw(mainWindow);
            mainWindow.display();
            cout << 2;
        }
        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            player1.mirror(true);
            mainWindow.clear(sf::Color(80,70,200));
            player1.moving(2,0);
            player1.Draw(mainWindow);
            mainWindow.display();
            cout << 3;
        }
    }

    if(currentEvent.type == sf::Event::Closed)
        gameState=Game::Exiting;
}
 

it works fine in about 5-10 seconds, and after it hangs (freezes). What can couse such behaviour?

Thanks Mike.
Title: Re: App freezes after few seconds
Post by: Laurent on June 03, 2020, 07:50:49 am
There's no event loop in your code.
Title: Re: App freezes after few seconds
Post by: mpaw on June 03, 2020, 11:09:43 am
void Game::GameLoop()
{
    sf::Event currentEvent;
    while(mainWindow.pollEvent(currentEvent))
    {
        if (currentEvent.type == sf::Event::Closed)
            mainWindow.close();

        switch(gameState)
        {
            case Game::ShowingMenu:
            {
                ShowMenu();
                break;
            }
            case Game::ShowingSplash:
            {
                ShowSplashScreen();
                break;
            }
            case Game::Playing:
            {
                while(gameState!=ShowingMenu)
                {
                    mainWindow.clear(sf::Color(80,70,200));
                    player1.Draw(mainWindow);
                    mainWindow.display();

                    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                    {
                        ShowMenu();
                        gameState = ShowingMenu;
                        cout << 1;
                    }
                    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                    {
                        player1.mirror(false);
                        mainWindow.clear(sf::Color(80,70,200));
                        player1.moving(-2,0);
                        player1.Draw(mainWindow);
                        mainWindow.display();
                        cout << 2;
                    }
                    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                    {
                        player1.mirror(true);
                        mainWindow.clear(sf::Color(80,70,200));
                        player1.moving(2,0);
                        player1.Draw(mainWindow);
                        mainWindow.display();
                        cout << 3;
                    }
                }

                if(currentEvent.type == sf::Event::Closed)
                    gameState=Game::Exiting;
            }
        }
    }
}
Title: Re: App freezes after few seconds
Post by: Laurent on June 03, 2020, 03:40:57 pm
Your code structure is a mess. You have an event loop which contains a game loop, with drawing and real-time event checking... To anwser your question: the inner loop "while(gameState!=ShowingMenu)" contains no event loop and until it ends your window will be unresponsive.

But a better answer is: read carefully the provided examples, tutorials, other codes here on the forum, wiki, etc. and rewrite your code without all these unrelated things nested in each other ;D