Logging Keystrokes efficiently

I'm making a game with the SFML libraries and i'm using it's RenderWindow class to capture the player's keystrokes. However, I believe it is what is eating up CPU while my program is running.

For example, the following code, when in the while loop, uses my entire core.


1
2
3
4
5
6
7
8
9
				App->Display();
				while (1)
				{
					if (App->GetEvent(Event))
					{
						if(Event.Key.Code == sf::Key::Escape)
						{break;}
					}
				}


I was considering putting a pause in between capturing keystrokes, but i'm afraid that i may miss input that way. Does anyone know a way where i can accurately record keystrokes while still remaining conservative in my CPU usage?

Humbly yours,
Dissimulation
Something like:

1
2
3
4
5
6
7
8
9
10
11
12
    bool waitingForEscape = true ;
    while ( waitingForEscape )
    {
        while ( App->GetEvent(Event) )
        {
             if ( Event.KeyCode == sf::Key::Escape )
                 waitingForEscape = false ;
        }

        if ( waitingForEscape )
            sf::Sleep(/*some small value*/) ;
    }


?
closed account (DSLq5Di1)
I have zero experience with SFML, but judging from the documentation and examples I've had a look at, your game loop is a little screwy.

RenderWindow::Display() renders the window to screen, this should be the last instruction in your main loop!

RenderWindow::GetEvent(sf::Event) called in a loop to process all available events, as cire has above.

RenderWindow::SetFramerateLimit(int) limiting the framerate will reduce cpu usage.

Throwing it all together, you should have main loop that looks similar to this:-
http://redkiing.wordpress.com/2009/08/20/your-first-sfml-game-part-ii-the-board/
Disregard the App->display(); above the loop, I accidently copied it.

this isn't my main game loop this is just a loop at the end of a certain sequence in which i wait for the player to press ESC.

My frameratelimit is 60 atm but i will lower it, I will also add the sf::sleep at the end of any of these other loops in which i wait for input.

thank you
You might consider moving on to SFML 2.0 as well. I don't believe 1.6 will be supported once 2.0 is "officially" released and it has quite a few design issues that are dealt with better in 2.0
Topic archived. No new replies allowed.