Jump to content
  • Advertisement
Sign in to follow this  
Finalspace

Caching the operating system events

This topic is 498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have seen a few guys (Shawn McGrath, Johnaton Blow) storing each operating system event into its own structure.
Unfortunatly i never have seen how they internally store and process that events, so i wanted to ask if someones knows how to do that?

Do i use some sort of a ring buffer or something like that? Or do i reset the cached events each frame?
Storing it forever simply does not work, because they will eventually lock your game loop because it may not catch up...

Also how does this work for mouse and gamepad input - normally i would poll this in the game loop.

The hole point of this idea is to simply abstract the entire input handling, so its up to the application how to deal with input. Edited by Finalspace

Share this post


Link to post
Share on other sites
Advertisement

In my engine, I have a separate thread that deals with the OS GUI's event queue. I listen for events in the WinProc (on Windows, OS X uses a NSWindow subclass), and then convert the OS-specific event format into one that is platform independent. The events are then pushed to window-specific delegate callback methods (std::function objects) that respond to things like mouse motion, mouse wheel, mouse button, keyboard, etc. This is all done on the OS thread.

Eventually, after passing through the GUI hierarchy the events are queued in a atomically double-buffered array within the engine's input system. On each update of the engine's main thread, the previously queued events from the OS thread are used by the input system, the array is cleared, and the buffers are swapped atomically. Events have an associated time stamp relative to the epoch so that I can maintain ordering and detect stale events.

Share this post


Link to post
Share on other sites

Input is captured from the OS, timestamped, stored in your custom format (as mentioned above, you convert each OS format to your own proprietary format), eventually consumed by your game thread in its main loop, and sent to a fixed-sized log of inputs (in case you need to check for sequences of button presses for special attacks etc.)

I've written at-length about this:
https://www.gamedev.net/topic/650875-input-handling-design-frustration/#entry5115033
https://www.gamedev.net/topic/650640-is-using-static-variables-for-input-engine-evil/#entry5113267
https://www.gamedev.net/topic/664831-handling-input-via-windows-messages-feedback-requested/#entry5204705
And an article written based on those (and others):
https://www.gamedev.net/resources/_/technical/general-programming/asynchronous-keyboard-input-for-fixed-time-step-games-r3959

 

L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!