Waiting for user input inside a game loop

Started by
3 comments, last by Mullanimal 14 years, 8 months ago
I am making a simple poker game using Dev C++ and the SDL library. Following a suggestion from a tutorial i have separated the logic of a game into a game loop. Here is some rough code from the game loop. Basically this code increments player turn and runs AI so what i am unsure about is when it is a human players turn, how do i stop the loop from continuing until i have received a user action given this is a GUI application. while(!gameNotOver){ playerTurn++; if( playerTurn.type == AI ) { runAI(); } else if ( playerTurn.type == HUMAN ) { // wait/get user input} }
Advertisement
You don't. You should access a message quewe from wich you'll retrieve input events from each frame.
[size="2"]I like the Walrus best.
You can put your "playerTurn++" inside the brackets.
if( playerTurn.type == AI ) { runAI(); playerTurn++;}else if ( playerTurn.type == HUMAN ) {   bool userHasInput = GetUserInput(); // check keyboard, mouse, etc.   // return false is player is not done yet   if(userHasInput) playerTurn++;   else { // sleep, let other applications have the CPU for a while }}

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Mandatory reading material for users of Dev-C++.
Thanks buckeye!

This topic is closed to new replies.

Advertisement