Game Loops and Synchronous Input

Started by
4 comments, last by ApochPiQ 14 years, 11 months ago
Hey everyone, So lately I've been looking into lower-level game programming (meaning the Win32 API, C/C++, etc.). Up to this point, I've done all of my game programming in either Java or C#. Anyways, I'm having a bit of trouble with gathering player input in the same thread as updating the game world and rendering it to the screen. Since I/O is rather asynchronous, some kind of timeout seems necessary (all other things being equal). On the other hand, maybe I/O is handled in (what amounts to) a separate thread even on the OS level, through interrupts and the like. Can any of you help me out here? I apologize if the above is confusing -- just looking for some clarification. Thanks!
Advertisement
Assuming you're working in Win32 with C++ - the OS actually queues up input data for you. When you want to obtain user input, you pull a list of the input events that have occurred since the last time you read the input queue. You then respond to all of the input in the queue, and go about doing the game simulation update and graphics rendering.

Typically nowadays this is done via WM_INPUT which you can handle from your window's message procedure.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Thanks for your reply! Sounds like my suspicion has been confirmed -- the OS does most of the heavy lifting. I can only imagine how it must have been when that was not true, and you had to write your own interrupt routines for capturing player input... :P

One question: is using WM_INPUT faster (or otherwise more advantageous) than using e.g. WM_KEYUP and WM_KEYDOWN?
Using raw input (via WM_INPUT) is much more powerful and flexible than the alternatives. You'll get access to more devices than just the keyboard and mouse, and better information about what exactly is going on. (For example, WM_KEYUP doesn't work as you might expect if the player holds down the Alt key.)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

To be honest, I don't see myself making any games for a while that would not rely solely on the standard input devices -- i.e., keyboard and mouse. Do you think that reading WM_KEY* and WM_MOUSE* messages would be sufficient for what I want to do?
Sufficient yes. A good idea, no. WM_INPUT was created to help make input processing simpler, and if you ignore that opportunity, you're in for a lot of really annoying bugs and "quirks" in your input code.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

This topic is closed to new replies.

Advertisement