Waiting for user input inside a game loop
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}
}
You don't. You should access a message quewe from wich you'll retrieve input events from each frame.
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 }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement