I want to make a countdown timer for my racing game such as "3... 2... 1... GO!" I'm using GLUT, OpenGL, and C++. I was wondering what the most appropriate method to handle this in C++. clock()? glutTimerFunc()? I plan on having 4 textured polygons that shrink and fade one after the other.
1 Answer
You need to accumulate time in your update
loop. Don't think in terms of "what time it is", instead, keep a double
variable, called timeTotal
. Each frame, increment it by (1.0/60.0
) (or whatever your fixed timestep is). When the game starts, you can reset the timer, and your race start introduction would just go something like this:
void update()
{
totalTime += 1.0/60.0 ; //increase time
if( timeTotal > 1 ) // 3.
else if( timeTotal > 2 ) // 2.
else if( timeTotal > 3 ) // 1
else { GO } // race on
}
You can refactor this in any number of ways, but this is the basic idea of how to trip time based events in a game system.
glutGet(GLUT_ELAPSED_TIME);
That way you have real frame time correlation with what the user should see on their screen. Some code here. That's probably easier since you don't have to worry about performing really OS specific time calls. \$\endgroup\$