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

Author Topic: Pause  (Read 5349 times)

0 Members and 1 Guest are viewing this topic.

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Pause
« on: December 27, 2017, 01:00:46 am »
Hello, I'm not really good at coding, so please explain more understandable.
I wanted to create something like a pause for a game and came up with this idea:
int main()
{
//RenderWindow
bool pause = false;
game:
while ((window.isOpen()) && (!pause))
        {
                float time = time = clock.getElapsedTime().asMicroseconds();
                clock.restart();
                time = time / gameSpeed;
                sf::Event event;
                while ((window.pollEvent(event)) && (!pause))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                                return 1;
                        }

                       if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Esc))
                        {
                                pause = true;
                        }
                 }...
                //game body//
        }
while ((window.isOpen()) && (pause))
        {
                sf::Event event;
                while ((window.pollEvent(event)) && (pause))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                                return 1;
                        }

                        if ((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Esc))
                        {
                                pause = false;
                                goto game;
                        }
                }

                window.display();
        }
}
 

The problem is that it doesn't work very well. The game becomes a bit lagging, I mean the animations start to flicker and finally, when I go back to game:, the player falls through the map, nevertheless every loop is in the first while.
What should I do?

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Pause
« Reply #1 on: December 27, 2017, 01:20:08 pm »
to make a pause for your game you need to skip the update section in your game loop

here a pseudo code:

while (mWindow.isOpen())
{
        processEvents();
        if (!pause) {
                update();
        }
        render();
}

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Pause
« Reply #2 on: December 27, 2017, 06:21:51 pm »
I tried to do like this, but I end up with the same problem. The player falls through the map, it dies and when everything is paused, the game starts to glitch. I checked, everything which is in thr "loop" sections is under pause conditions and when pause is on, it doesn't work.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Pause
« Reply #3 on: December 27, 2017, 09:05:35 pm »
Don't make 2 separate game loops.

spoty76

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Pause
« Reply #4 on: December 27, 2017, 10:30:51 pm »
Oh yeah, it worked, the problem was that when I was pausing time = clock... it became undefined and when game was unpaused it affected the states.