Waiting for input in SFML

Hello, I'm currently writing a 2D mini-game using visual studio 2012 and SFML 2.0 in which I required a section for the beginning of the level to prompt the user to press the space button and show that the user is ready . For this, I wrote a while loop and the prompt works for the first 6 or so seconds allowing me to press space and continue but then the whole program stops responding. Any help is much appreciated.

Here's a bit of my code with the loop:
1
2
3
4
5
6
7
8
9
bIfpressed = false;
while(bIfpressed == false)
{
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
	{
		bIfpressed = true;
		nReadyPrompt = 3;
	}
}
You aren't polling events. You aren't sleeping. You're just running the cpu in a crazy circle waiting for a key to be pressed. It's not surprising the O/S considers your program to be unresponsive - it is unresponsive.

You might consider using a strategy like the following simple program:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <ctime>

enum State { Closed, Paused, Normal } ;

State consumeEvents_paused(sf::Window & w) ;
State consumeEvents_normal(sf::Window & w) ;

void update_normal(sf::RenderWindow & w ) ;
void update_paused(sf::RenderWindow & w ) ;

const std::string fontLocation = "C:/Windows/Fonts/Arial.ttf" ;
sf::Font font ;

struct handleState
{
    State (*consumeEvents)(sf::Window&) ;
    void  (*update)(sf::RenderWindow&) ;
};

handleState pausedStateHandler = { consumeEvents_paused, update_paused } ;
handleState normalStateHandler = { consumeEvents_normal, update_normal } ;

handleState* currentStateHandler = &normalStateHandler ;

int main()
{
    std::srand(std::time(0)) ;
    sf::RenderWindow window( sf::VideoMode(800,600), "Pause me!") ;

    if (!font.loadFromFile(fontLocation))
        return 0 ;

    while ( window.isOpen() )
    {
        State state = currentStateHandler->consumeEvents(window) ;

        if ( state == State::Closed )
        {
            window.close() ;
            continue ;
        }

        window.clear() ;
        currentStateHandler->update(window) ;

        window.display() ;
        sf::sleep( sf::milliseconds(30) ) ;
    }
}

State consumeEvents_normal(sf::Window & w )
{
    State returnState = State::Normal ;

    sf::Event event ;
    while ( w.pollEvent(event) )
    {
        switch(event.type)
        {
        case sf::Event::Closed:
            returnState = State::Closed ;
            break ;

        case sf::Event::KeyReleased:
            if ( event.key.code == sf::Keyboard::P )
            {
                currentStateHandler = &pausedStateHandler ;
                returnState = State::Paused ;
            }
            break ;

        default:
            break ;
        }
    }

    return returnState ;
}

State consumeEvents_paused(sf::Window & w)
{
    State returnState = State::Paused ;

    sf::Event event ;
    while ( w.pollEvent(event) )
    {
        switch( event.type )
        {
        case sf::Event::Closed:
            returnState = State::Closed ;
            break ;

        case sf::Event::KeyReleased:
            currentStateHandler = &normalStateHandler ;
            returnState = State::Normal ;

        default:
            break ;
        }
    }
    return returnState ;
}

void update_normal(sf::RenderWindow & w )
{
    static sf::Text text("Press P to pause!", font) ;
    text.setColor( sf::Color(63, 127, 127) ) ;
    text.setPosition( 200 + std::rand() % 50, 275 + std::rand() %50 ) ;
    w.draw(text) ;
}

void update_paused(sf::RenderWindow & w )
{
    static sf::Text text( "Paused!", font) ;

    const sf::FloatRect bounds= text.getLocalBounds() ;

    float xPos = 400 - bounds.width / 2.0f ;
    float yPos = 300 - bounds.height / 2.0f ;

    text.setPosition(xPos, yPos) ;
 
    sf::RectangleShape rect(sf::Vector2f(bounds.width, bounds.height));
    rect.setPosition(xPos + bounds.left, yPos + bounds.top) ;
    rect.setFillColor( sf::Color(0, 0, 127) ) ;

    w.draw(rect) ;
    w.draw(text) ;
}

Topic archived. No new replies allowed.