Loop untill user hits a button

closed account (10oTURfi)
When programs execution reaches while(true) loop program goes non-responsive.
Any ideas how to fix that?

Program was supposed to loop untill user hits escape or 'R' button.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "Game.h"

int main()
{
    bool PlayAgain = true;

    //TODO: Main Menu

    while(PlayAgain)
    {
        Game *myGame = new Game;
        myGame->Start();
        delete myGame;

        sf::RenderWindow mywindow(sf::VideoMode(300,300,32), "Do you want to play again?");
        sf::Image imagefile;
        imagefile.LoadFromFile("PlayAgain.png");
        sf::Sprite mysprite(imagefile);
        mysprite.Resize(300, 300);
        mywindow.Draw(mysprite);
        sf::Event currentEvent;

        while(true)
        {
            mywindow.Display();

            if((currentEvent.Type == sf::Event::KeyPressed) && (currentEvent.Key.Code == sf::Key::Escape))
            {
                PlayAgain = false;
                break;
            }
            else if((currentEvent.Type == sf::Event::KeyPressed) && (currentEvent.Key.Code == sf::Key::R))
            {
                PlayAgain = true;
                break;
            }
        }
    }

	return 0;
}
while what is true? PlayAgain? Try while ( PlayAgain == true )
Last edited on
closed account (10oTURfi)
while(true) is a valid piece of code. Basically, its a forever-loop. It loops untill break is encountered :)
while(PlayAgain == true) would in many ways be invalid in my code.

You would be suprised when you figure that if(true) is valid as well.
@Krofna

Thanks for pointing that out to me. And yes, I am surprised. I learn a lot from all the programmers on this site. Hopefully, I never get to old, to learn something new.
The program does not exit while(true) loop even when Esc is pressed? Do you even pass the mywindow.Display(); statement? Maybe you never reach beyond this point. I don't know it's just a thought.
closed account (10oTURfi)
mywindow.Display(); is executed. After that, if i hit a button (any button) program goes non responsive. Still got the problem.
if I understand my sfml programming:
1
2
while(true)
{


should be:
1
2
while ( mywindow.pollEvent(&CurrentEvent) )
{


It should be something like that. I would have to look it up to make a little clearer. I haven't practiced with smfl to remember all its nuances.
closed account (10oTURfi)
Screw good practise!
I added few gotos and program works like charm.

**Solved**
I know this is solved, but if you want to use good practice instead,
you need to do what Azagaros suggested. :D
Your code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 while(true)
        {
            mywindow.PollEvent(currentEvent); //Just add this line.
            mywindow.Display();

            if((currentEvent.Type == sf::Event::KeyPressed) && (currentEvent.Key.Code == sf::Key::Escape))
            {
                PlayAgain = false;
                break;
            }
            else if((currentEvent.Type == sf::Event::KeyPressed) && (currentEvent.Key.Code == sf::Key::R))
            {
                PlayAgain = true;
                break;
            }
        }
Topic archived. No new replies allowed.