Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Mouse Cursor Jumping

This thread is locked; no one can reply to it. rss feed Print
Mouse Cursor Jumping
Zortrox
Member #15,399
November 2013
avatar

This code runs every time the system registers the mouse moving. There is a small lag sometimes when I move my mouse quickly though, so I was wondering if this code caused it. I recently moved this part of my code from my main loop into a class, and also recently updated my Allegro from 5.0.10 to 5.1.7.

If I keep my on-screen cursor set equal to my mouse cursor, both update at the same time, whenever I use "al_set_mouse_xy()" though, the lag starts again.

#SelectExpand
1void Game::event_mouse_axes(ALLEGRO_EVENT &ev) 2{ 3 cursor_x += ev.mouse.x - screen_width/2; 4 cursor_y += ev.mouse.y - screen_height/2; 5 if (cursor_x < 0) cursor_x = 0; 6 else if (cursor_x > screen_width) cursor_x = screen_width; 7 if (cursor_y < 0) cursor_y = 0; 8 else if (cursor_y > screen_height) cursor_y = screen_height; 9 al_set_mouse_xy(display, screen_width / 2, screen_height / 2); 10}

I can find another way to fix my cursor position, but I'm just wondering is "al_set_mouse_xy()" causes common problems or if I've done something wrong with this code. Thanks!

EDIT: Okay, so I've been messing around with this code and my actual cursor_x and cursor_y seems to skip to a nearby location whenever I move my mouse quickly. I've looked thought my code and this is the only place where I'm doing anything with setting the cursor coordinates too.

Peter Wang
Member #23
April 2000

Why are you setting the mouse cursor position anyway? If it's emulate an infinite space, then you probably want to warp it to the middle of the screen and not to (cursor_x, cursor_y). Also, ev.mouse.dx, ev.mouse.dy might be useful to you.

Mouse cursor lag is usually caused by drawing after each received event. The drawing can't keep up with the incoming mouse movement events so the event queue grows longer and longer. One solution is only to draw when the event queue is empty.

ph03nix
Member #15,028
April 2013
avatar

He appears to be limiting the mouse's position within the game's window.

I don't know how to fix the jumpiness, but you only need to set the mouse's position if it's out of bounds instead of always setting it.

pkrcel
Member #14,001
February 2012

Isn't there a native way to constrain the cursor in the active window anyhow?

It is unlikely that Google shares your distaste for capitalism. - Derezo
If one had the eternity of time, one would do things later. - Johan Halmén

Gabriel Campos
Member #12,034
June 2010
avatar

Kris Asick
Member #1,424
July 2001

Lag with the mouse cursor (or I/O in general) can also be attributed to improper display settings when using a TV for your computer display instead of an actual computer screen. This happens when a full-screen game puts the TV into a refresh rate it isn't normally in for computer usage because a particular refresh rate is higher in the auto-detection list than the one that's normally used. Plus, modern TVs need to be turned into a special "Game Mode" for the specific refresh rate they're using in order to not encounter huge delays. (And despite this, some still do.)

If you're not using a TV as your computer monitor then this is almost certainly not the issue, but I thought I'd mention it all the same.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Zortrox
Member #15,399
November 2013
avatar

Sorry, I've been gone all day.

Yes, I was originally setting it to the middle of the screen, but I guess I copied the code before I had a chance to look over my changes. It is supposed to be al_set_mouse_xy(display, screen_width/2, screen_height/2) (I'll edit the original post).

As to why I don't use al_grab_mouse(): it seems to keep the mouse constrained the the entire window, and not to the drawing area. This means that the game won't update my mouse location when I'm hovering over the title bar (kinda tricky for and RTS game or something).

I'll set the mouse position whenever it goes out of the drawing area and see if that fixes my graphical lag. Thanks for the replies, guys!

P.S. I'm not using a TV for a monitor, but I'll keep that in mind for any future games I create. Thanks, Kris!

EDIT: I changed

#SelectExpand
1cursor_x = ev.mouse.x; 2cursor_y = ev.mouse.y;

to

#SelectExpand
1cursor_x += ev.mouse.dx; 2cursor_y += ev.mouse.dy;

in my code and then set my mouse x,y to the screen center and it worked! Thanks, Peter!

Go to: