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.

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.

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.

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.

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!