Question

I am confused as to how applications handle the mouse input with Direct2D.

Consider the case:

  1. Say I painted a ball on the window. If I move the cursor over the ball, it will change color.
  2. Does this mean that the application have to poll the mouse and check if the cursor is over the ball pretty much every second of the application running time.
  3. Doesn't that decrease performance when you have more items? Is there another way than what polling which in other words is a bunch of if-statements.
Was it helpful?

Solution

  1. You need to perform the hit test when the mouse moves, or when the ball moves.
  2. Efficient hit test / collision detection is a major concern in game development. If performance becomes an issue, collision detection is usually performed in two phases: A "broad phase" and a "narrow phase". One approach for the broad phase are quad trees (for two dimensions): The space (the window) is divided into "sections"; each ball and the mouse are assigned to a section according to their position. Only balls being in the same section as the mouse are candidates for the narrow phase. In the narrow phase, you just test the candidates that survived the broad phase.

OTHER TIPS

Usually applications will listen for the WM_MOUSEMOVE event and update any affected objects in the event handler (or signal the objects of the change event in case it is an expensive operation). The render code shouldn't need to poll where the mouse cursor is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top