In motion, you need to regulate the frame-time. But what should be done when the program is waiting for 'input'?
For example (reason for the question); I'm making a BlackJack-game. At the beginning of each round, the player has to set his bet. I have three buttons, "add 10", "set to 0" and "play round". Further, the current bet and the current money is displayed.
Before drawing the screen, the program needs fill the screen with the backgroundcolor, to generate surfaces with the current bet and money, and blit those surfaces and the buttons.
How should this be done? I can think of three solutions, what is the best one?
//keep on looping:
while (!playRound)
{
... //check for input (are buttons hit?)
... //generate screen
SDL_Flip (screen);
}
//---------------------------------------
//regulate frame time using a timer:
while (!playRound)
{
timer.start();
...//check for input (are buttons hit?)
...//generate screen
SDL_Flip (screen);
while (timer.getTime()<1000/FPS){};
}
//---------------------------------------
//check or something has chanced:
while (!playRound)
{
...//check for input, if a button is hit, set flag
if (ButtonHit)
{
...//generate screen
SDL_Flip(screen);
}
}
For a fullscreen app, method three would be best, you can just let the program run as fast as possible. For windowed apps, probably a card game should support a window option, you should think about using callbacks to minimize CPU usage. Limit to 60fps for example and always check if a redraw is neccessary before redrawing.
I do have a littly experience with windows-programming, but not enof to mix it up with SDL yet. So I'm looking for a solution in just SDL. I'll go whit he third solution, check for input. That will probably do for now. But thanks to your answers I now understand how it is done in normal games, and when I got experienced enough in SDL, I'll learn about windows-programming and use a callback-system.