0
\$\begingroup\$

A mouse might have a frequency of 125Hz, meaning that the system will read off the mouses position every 8ms. Keyboards work in a similar manner.

If a key is pressed and held down, events are continuously raised.

Does every one of these events make it to the game loop?

I imagine that if the game loop does not complete fast enough, the user interactions will buffer, causing a noticeable interaction latency.

I can think of ways to deal with this:

  • Add a maximum size to the input buffer so that as new events arrive, older ones are pushed from the front of the queue as the new events are pushed to the back of the queue (enqueued).

  • When the key is pressed, send a "key-down" message and let the game loop repeat the received message until a "key-up" message is received.

Can anyone think of other ways to deal with user input, and do any of my suggestions have obvious flaws?

UPDATE I am not developing a game, I am doing research and so my above questions are purely academic. I am not using any API.

\$\endgroup\$
  • 1
    \$\begingroup\$ "If a key is pressed and held down, events are continuously raised." actually, not necessarily. Many APIs will just send one event the moment the key is pressed down and a second event when it is released. \$\endgroup\$ – Philipp Feb 12 '17 at 20:59
  • 1
    \$\begingroup\$ "I am not using any API." Of course you are: I guess that unless you're writing a driver, you'll at least have to use the OS' API ;) \$\endgroup\$ – Alexandre Vaillancourt Feb 13 '17 at 3:13
3
\$\begingroup\$

It depends on what kind of API you are using to read user input.

There are polling-based APIs where you read the current state of input devices in your game when it is convenient (like Unity). When that happens too infrequently, it is possible to miss input events. To avoid that from happening, the loop which handles input should run faster than the graphics loop.

There are event-based APIs where your program has an event queue it needs to process (like WinAPI). In such APIs, the event queue can theoretically overflow, but practical this is rarely a problem, unless you need a very long time to handle some events.

There are also event-based APIs where you assign event handler functions to certain input events (like in browser-based Javascript). These functions are called the moment the event happens. These usually do not miss events. But the drawback is that you often have little control over when these event handlers are called, so you don't know what state your application is currently in when it happens. It can often be smart to turn these into a queue-based API by having those event handlers push the events to a global event queue which you then process at a controlled moment of your game loop. That way you have better control over when events are processed.

You did not mention what platform and API you are using to develop your game, so you need to check for yourself what options you have available.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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