As the title says I'm trying to accomplish an overlay menu in my SDL2 application. I have a window where I want to show an overlay menu if the mouse starts moving and show it as long the mouse moves. If the mouse stops moving a timer should start with a specific timeout and should hide the menu after the timeout has passed.

I tried using mouse events like SDL_MOUSEMOTION, but that doesn't work. I would rather need something like "mouse motion stopped" events, where I then would start the timer.

I then thought I could combine SDL_MOUSEMOTION with SDL_GetRelativeMouseState() and compare the mouse position deltas and start the timer if the deltas are 0. But that kind of seems too complicated. Is the latter the way to go, or is there a simpler way?

有帮助吗?

解决方案

There are several ways you could approach this:

  • SDL doesn't send 'mouse motion stopped' events but conceptually, a 'mouse motion stopped' event is a frame where you haven't received a mouse motion event. If you have a frame update loop, keep track of whether you've received a mouse motion event in the previous frame and update your menu timer accordingly.

  • Simply reset your menu's timer every time you receive a SDL_MouseMotion event. It's not elegant but it should work. Run the menu timer as soon as you receive the first motion event and just reset the timer each time you receive a subsequent one until it expires and you hide the menu.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top