A timing problem

I users!
I'm a programmer, I'm tryng to create a game which works in windows. The libraries that I'm using are:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <cstdlib>
#include <windows.h>
#include <string>

Now the problem. The game is an arcade game and the user scope is to press the key which the computer tells the user to press. You know, in this type of games the time you have to press the key should not be infinite.

So the program is structured in a one cycle that check what key the user press. Now if I would implement the time I might use the Sleep() function which will slow all the cycle.

So How can I do?
Thanks for the understanding!
Make a note of the time
Tell user to press key
Check the time
Check if user pressed key
Check the time
Check if user pressed key
Check the time
Check if user pressed key
Check the time
Check if user pressed key
...

Eventually it will be too much time, or the user pressed the key.
You should use proper timer events instead of a busy loop, but yeah that's the jist of it.

I'd search for code examples for terms presented in
https://docs.microsoft.com/en-us/windows/desktop/multimedia/timer-operations
or
https://msdn.microsoft.com/en-us/library/windows/desktop/ms632592(v=vs.85).aspx

Edit: I mean, timer events are an option, but periodic checking is okay, too. (but a waste of processor unless something else is happening within that loop).
Last edited on
Most games run on a simple loop. It might look something like this.

1
2
3
4
5
6
while(true) {
  processInput();
  updateGameState();
  renderGame();
  presentAndWait();
}


Typically this game loop will run 60 times a second, either presentAndWait includes a wait for vsync or there is an API call in there to wait an appropriate time. In this case, your "game state" would merely be the key the user is expected to press, and after you gather input in processInput you see if the user pressed that key on that frame, and if they did you an API call to get the current time and calculate when the user pressed the key.

However, it looks like you're not using a typical game library, you're just using the Windows API. If you're not already running a game loop and only using the win32 API, this is solved even more simply by the wndproc event handler you likely already have. When you display the key the user wants to press, store the time. When the event for the correct keypress comes in, subtract that time from the current time and you get the time it took the user to press the key.
Topic archived. No new replies allowed.