Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm planning to use the Sleep() function to implement a countdown timer. But this freezes the input of the user at the same time. Thus I wanted to ask if it is possible to have the countdown still running in realtime, whether they're typing or not; or in other words not freeze everything.

Is it possible? If so, how?

Example : Pop Quiz with Timer

share|improve this question

closed as off-topic by Byte56 Jan 23 at 15:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

I don't understand the question. –  Ken Nov 24 '12 at 14:43
For starters, use clock instead and find the time difference between each call. cplusplus.com/reference/ctime/clock –  Jake Nov 24 '12 at 16:48
Please elaborate. Is your problem that the Sleep function halts the program, or that receiving input halts the program? –  ktodisco Nov 25 '12 at 23:10

1 Answer 1

If I understand your question correctly, you should be able to do this by having a few variables:

float countdownDuration = 1.0f;
float cooldown = coutdownDuration;

Each frame, you subtract deltaTime, or the time that has passed since the last frame, from cooldown. When cooldown reaches 0 or below, your countdown timer has 'ticked' down, and you can reset cooldown to countdownDuration minus the value below 0 that cooldown has reached. In this particular case, by the way I initialized the variables above, each tick will be one second.

In this way, you do not have to sleep the main thread and will still check for input every frame.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.