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

Author Topic: App freezes after few seconds  (Read 1373 times)

0 Members and 1 Guest are viewing this topic.

mpaw

  • Newbie
  • *
  • Posts: 5
    • View Profile
App freezes after few seconds
« 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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: App freezes after few seconds
« Reply #1 on: June 03, 2020, 07:50:49 am »
There's no event loop in your code.
Laurent Gomila - SFML developer

mpaw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: App freezes after few seconds
« Reply #2 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;
            }
        }
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: App freezes after few seconds
« Reply #3 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
Laurent Gomila - SFML developer

 

anything