SDL: Haywire mouse motion events

Started by
6 comments, last by Haytal 14 years, 5 months ago
Hi, I'm having trouble with mouse motion events using SDL 1.2.13 on Ubuntu Linux 9.10, x64 version. I'm getting mouse motion events with inexplicably large values for apparently no reason. The part of the code grabbing events looks as follows:

while(!m_game_over)
{
  SDL_Event e;
  while(SDL_PollEvent(&e))
  {
    switch(e.type)
    {
    //...snip...
    case SDL_MOUSEMOTION:
      m_log << "(" << e.motion.xrel << "," << e.motion.yrel << ")\n";
      break;
    //..snip..
    }
  }
  draw_stuff();
  SDL_Delay(1);
}



Running this code, if I slowly move the mouse left across the screen, the log is filled with the following:

(-9,-1)
(-7,0)
(-2,0)
(-1,0)
(521,471)
(521,471)
(521,471)
(-3,1)
(-2,1)
(521,471)
(-1,2)
(-2,0)
(-1,1)
(-2,0)
(0,2)
(-1,1)
(521,471)
(-2,0)
(521,471)
(-1,0)
(-2,0)
(-1,0)
(-2,0)
(521,471)
(-1,0)
(-5,0)
(-3,0)
(-4,0)
(-5,0)
(-3,0)
(-1,0)
(-3,0)
(-2,0)
(521,471)
(521,471)
(-3,0)
(-3,0)
(-3,0)
(-4,0)
(-5,0)
(-4,0)
(-5,0)
(-3,0)
(-3,0)



and I'm not sure why. Has anyone else run into this before? Right now I can sort of deal with it by making sure that xrel and yrel are between -100 and 100, but I'd prefer to fix the problem rather than build a hack to deal with it.
Advertisement
Check the case above SDL_MOUSEMOTION, ensue it has a break statement on it.
It's definitely something up with the mouse motion stuff SDL is doing, my code is fine. (Good thinking though!)
If this is purely a problem with SDL mouse events, then you should be able to come up with a minimal test app that demonstrates the problem that we can test? If not, that hints that the problem is with your code, and not SDL.
Are you doing SDL_PumpEvents anywhere? You need to do that before you poll so that SDL can gather all events from input devices.
Okay, here's a program that demonstrates what's going on. Compile with

g++ `sdl-config --cflags --libs`


Press space to switch between showing the cursor and hiding the cursor - this appears to be the problem on my system. When the cursor is showing, everything works as expected. When the cursor is hidden, I get the random strange events. Press any other key to quit.

#include <SDL/SDL.h>#include <iostream>#define WIDTH  640#define HEIGHT 480int main(){  SDL_Init(SDL_INIT_VIDEO);  SDL_WM_GrabInput(SDL_GRAB_ON);  SDL_ShowCursor(false);  bool cs = false;  SDL_Surface* s = SDL_SetVideoMode(WIDTH,HEIGHT,0,SDL_ANYFORMAT);  bool done = false;  while(!done)  {    SDL_PumpEvents();    SDL_Event e;    while(SDL_PollEvent(&e))    {      switch(e.type)      {        case SDL_KEYDOWN:          if(e.key.keysym.sym == SDLK_SPACE)          {            cs = !cs;            SDL_ShowCursor(cs);            break;          }        case SDL_QUIT:          done = true;          break;        case SDL_MOUSEMOTION:          std::cout << "(" << e.motion.xrel << "," << e.motion.yrel << ")" << std::endl;          break;      }    }    SDL_Delay(1);  }  return 0;}
Perhaps this may be of help:

Quote:
Simply put, a SDL_MOUSEMOTION type event occurs when a user moves the mouse within the application window or when SDL_WarpMouse is called. Both the absolute (x and y) and relative (xrel and yrel) coordinates are reported along with the current button states (state). The button state can be interpreted using the SDL_BUTTON macro (see SDL_GetMouseState).

If the cursor is hidden (SDL_ShowCursor(0)) and the input is grabbed (SDL_WM_GrabInput(SDL_GRAB_ON)), then the mouse will give relative motion events even when the cursor reaches the edge of the screen. This is currently only implemented on Windows and Linux/Unix-alikes.


Try warping the pointer to the center of the window when the cursor is hidden.
I tried your code and it worked fine.
The only time I get strange values is when I switch between hidden and visible cursor:
(0,1)
(0,1)
(-1,0)
(0,1)
(0,1)
(0,1)
(0,1)
(0,1)
(-164,-68) <-- I changed from hidden to visible
(0,1)
(0,1)
(0,1)
(0,2)
(0,1)

I think that happens because the cursor is reset to the center of the screen when you switch between hidden and visible.

Oh, and I'm on windows 7 using sdl 1.2.14 so maybe updating your sdl version helps.

This topic is closed to new replies.

Advertisement