Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 08 Feb 2013
Offline Last Active Feb 24 2013 12:47 PM

Topics I've Started

How to handle mouse input events (from the OS)

09 February 2013 - 09:19 AM

So it's been a while since the last time I created an abstraction for the mouse, so I have a few questions about good practices to mouse handling.


The way it used to be done "back in the day" was to, each iteration of the game loop, do something like so:


1. Calculate the mouse position delta by tracking its distance from the center of the window (e.g. mouse.position-window.center).

2. Force the mouse.position back to window.center.


I'm not sure all the reasons why it was handled this way, but this solution at least allows both the ability to move the mouse in any direction indefinitely and it nearly eliminates the possibility of the mouse moving outside of the window.  One negative side effect of this approach is that anytime you switch from first-person-mode to a menu requiring absolute mouse position, the cursor starts up at in the center position (0.5, 0.5).  


But in modern games, it appears that the mouse's input is handled differently, as if it's "locked in place" when needing relative position (first-person mode) and preserves its position when switching back to "absolute position" (menu mode). (For instance, in WoW, you can hold down the right mouse button to rotate the camera, but once you let go, the mouse cursor continues from where it was before you had clicked.)


So the first question is, what's the best way to implement this behavior?  Is this "faked" by saving the position and resetting to that point, or is it locked in place using an OS call?  If the latter, what are the relevant OS calls (for win32, x11, and/or os x)?


Or maybe, is input handling preferably handled a different way altogether?


Another question is, how do you lock a mouse within the bounds of a window?  I know you can at least force the position of the mouse cursor, but is there an OS call that achieves this effect (again, for win32, x11, and/or os x)


Thanks for any guidance!