<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Mouse Cursor Jumping</title>
		<link>http://www.allegro.cc/forums/view/613522</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 22 Nov 2013 06:31:20 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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.</p><p>If I keep my on-screen cursor set equal to my mouse cursor, both update at the same time, whenever I use &quot;al_set_mouse_xy()&quot; though, the lag starts again.</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">void</span> Game::event_mouse_axes<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> <span class="k3">&amp;</span>ev<span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>  cursor_x <span class="k3">+</span><span class="k3">=</span> ev.mouse.x <span class="k3">-</span> screen_width<span class="k3">/</span><span class="n">2</span><span class="k2">;</span>
<span class="number">  4</span>  cursor_y <span class="k3">+</span><span class="k3">=</span> ev.mouse.y <span class="k3">-</span> screen_height<span class="k3">/</span><span class="n">2</span><span class="k2">;</span>
<span class="number">  5</span>  <span class="k1">if</span> <span class="k2">(</span>cursor_x <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span> cursor_x <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  6</span>  <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>cursor_x <span class="k3">&gt;</span> screen_width<span class="k2">)</span> cursor_x <span class="k3">=</span> screen_width<span class="k2">;</span>
<span class="number">  7</span>  <span class="k1">if</span> <span class="k2">(</span>cursor_y <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span> cursor_y <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  8</span>  <span class="k1">else</span> <span class="k1">if</span> <span class="k2">(</span>cursor_y <span class="k3">&gt;</span> screen_height<span class="k2">)</span> cursor_y <span class="k3">=</span> screen_height<span class="k2">;</span>
<span class="number">  9</span>  <a href="http://www.allegro.cc/manual/al_set_mouse_xy"><span class="a">al_set_mouse_xy</span></a><span class="k2">(</span>display, screen_width <span class="k3">/</span> <span class="n">2</span>, screen_height <span class="k3">/</span> <span class="n">2</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span><span class="k2">}</span>
</div></div><p>

I can find another way to fix my cursor position, but I&#39;m just wondering is &quot;al_set_mouse_xy()&quot; causes common problems or if I&#39;ve done something wrong with this code.  Thanks!</p><p>EDIT:  Okay, so I&#39;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&#39;ve looked thought my code and this is the only place where I&#39;m doing anything with setting the cursor coordinates too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Zortrox)</author>
		<pubDate>Thu, 21 Nov 2013 04:49:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why are you setting the mouse cursor position anyway? If it&#39;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.</p><p>Mouse cursor lag is usually caused by drawing after each received event. The drawing can&#39;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.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Thu, 21 Nov 2013 13:14:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>He appears to be limiting the mouse&#39;s position within the game&#39;s window.</p><p>I don&#39;t know how to fix the jumpiness, but you only need to set the mouse&#39;s position if it&#39;s out of bounds instead of always setting it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Thu, 21 Nov 2013 19:00:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Isn&#39;t there a <i>native</i> way to constrain the cursor in the active window anyhow?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Thu, 21 Nov 2013 21:00:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="https://www.allegro.cc/manual/5/al_grab_mouse">https://www.allegro.cc/manual/5/al_grab_mouse</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gabriel Campos)</author>
		<pubDate>Thu, 21 Nov 2013 23:25:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>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&#39;t normally in for computer usage because a particular refresh rate is higher in the auto-detection list than the one that&#39;s normally used. Plus, modern TVs need to be turned into a special &quot;Game Mode&quot; for the specific refresh rate they&#39;re using in order to not encounter huge delays. (And despite this, some still do.)</p><p>If you&#39;re not using a TV as your computer monitor then this is almost certainly not the issue, but I thought I&#39;d mention it all the same.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Fri, 22 Nov 2013 01:38:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sorry, I&#39;ve been gone all day.</p><p>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&#39;ll edit the original post).</p><p>As to why I don&#39;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&#39;t update my mouse location when I&#39;m hovering over the title bar (kinda tricky for and RTS game or something).</p><p>I&#39;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><p>P.S. I&#39;m not using a TV for a monitor, but I&#39;ll keep that in mind for any future games I create.  Thanks, Kris!</p><p>EDIT: I changed
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>cursor_x <span class="k3">=</span> ev.mouse.x<span class="k2">;</span>
<span class="number"> 2</span>cursor_y <span class="k3">=</span> ev.mouse.y<span class="k2">;</span>
</div></div><p>
to
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number"> 1</span>cursor_x <span class="k3">+</span><span class="k3">=</span> ev.mouse.dx<span class="k2">;</span>
<span class="number"> 2</span>cursor_y <span class="k3">+</span><span class="k3">=</span> ev.mouse.dy<span class="k2">;</span>
</div></div><p>
in my code and then set my mouse x,y to the screen center and it worked!  Thanks, Peter!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Zortrox)</author>
		<pubDate>Fri, 22 Nov 2013 06:31:20 +0000</pubDate>
	</item>
</rss>
