<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[repost] Holding down a key makes the mouse laggy</title>
		<link>http://www.allegro.cc/forums/view/618090</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 11 Apr 2020 21:06:08 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My old thread was locked.  (A month seems like a short amount of time to autolock threads on such an inactive forum...)</p><p>Reaching out again since I spent a large chunk of time today trying to solve this with no luck.  Went from a simple timer-less loop I implemented quickly, to adding a timer and even attempting to workaround by implementing my own mouse-poller via events.  I get the same behavior either way.  See the attached video.  When holding down the spacebar to pan around my map editor, mouse input is pretty choppy, it&#39;s like the mouse is being polled every other frame.  In the terminal I&#39;m continuously printing the mouse motion, you can see that the mouse&#39;s movement is frequently coming in as 0,0 from the last frame, when a key is being held down.</p><p>&quot;Stuck&quot; on 5.2.3.  Avoiding revisiting the non-trivial task of compiling Allegro just to find out whether or not the issue is a problem with Allegro and if so whether it still exists.  (The precompiled ones don&#39;t work on either of my Forth systems SwiftForth and VFX.)  </p><p>Here&#39;s the video so you can see what&#39;s happening:<br /><a href="https://www.dropbox.com/s/4bt2k7bgom3orha/2020-04-10%2018-29-06.mkv?dl=0">https://www.dropbox.com/s/4bt2k7bgom3orha/2020-04-10%2018-29-06.mkv?dl=0</a></p><p>And here is my code:</p><pre>
: frame
    update  \ game rendering
    display al_flip_display
    kbs0 kbs1 /ALLEGRO_KEYBOARD_STATE move
    kbs0 al_get_keyboard_state
    ms0 ms1 /ALLEGRO_MOUSE_STATE move
    ms0 al_get_mouse_state
    step   \ game logic
 ;
: (go)
    begin
        queue alevt al_get_next_event if
            etype ALLEGRO_EVENT_TIMER = if
                frame
                kbs0 59 al_key_down if exit then
            then
            \ read-mouse  \ commented-out processing of mouse events
            pump  \ game events
        then
    again
;
: go
    kbs0 /ALLEGRO_KEYBOARD_STATE erase
    kbs1 /ALLEGRO_KEYBOARD_STATE erase
    al_uninstall_keyboard  al_install_keyboard drop
    queue al_get_keyboard_event_source al_register_event_source
    display al_flip_display
    1e 60e f/ al_create_timer dup al_start_timer to timer
    queue timer al_get_timer_event_source al_register_event_source
    (go)
    timer al_destroy_timer
;
</pre><p>

Edited to remove superfluous code and comment some things
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sat, 11 Apr 2020 03:37:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can&#39;t say I understand this programming language at all but I kind of like it not having &#39;;&#39; at the end of each command.<br />Not understanding the language I am only guessing here though I think your &#39;(go)&#39; block should actually be processing also the ALLEGRO_EVENT_MOUSE_AXES events as well (move the code for this from the &#39;frame&#39; block&#39; to &#39;(go)&#39; block).<br />I think your &#39;frame&#39; block also would need to have the &#39;al_flip_display&#39; moved to the end of it?</p><p>p.s. You can make it easier to read your source code if you (when posting on this forum) add <span class="source-code"> ...</span> tags around the code itself.</p><p>p.p.s. re:keyboard events, since you are using al_get_keyboard_state you can disregard the ALLEGRO_EVENT_KEY_DOWN and only process the ALLEGRO_EVENT_MOUSE_AXES in your &#39;(go)&#39; block - though if performance is an issue you will likely want to change your code from polling the entire keyboard state each frame to only responding to the actual key-presses and for this you would want to remove al_get_keyboard_state and replace it with processing of the keyboard events(ALLEGRO_EVENT_KEY_DOWN ) in your &#39;(go)&#39; block. </p><p>EDIT2: Also, are you missing something like &#39;queue al_get_mouse_event_source al_register_event_source&#39; in your code?</p><p><img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=";D" border="0" />.. this is way over my head.. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> sorry to waste your time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MikiZX)</author>
		<pubDate>Sat, 11 Apr 2020 11:57:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I left out the initialization code.  You can assume I&#39;m initializing all the subsystems and event queue, otherwise nothing would be working <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sat, 11 Apr 2020 12:25:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, you are right. You are also using the ALLEGRO_MOUSE_STATE so no need to process any of the mouse events (which now that I&#39;ve re-read your original post I see you have tried already). <br />Possibly try separating the code you have in the &#39;frame&#39; block so drawing is not executed every 1/60th of a second(or, as a test, comment out the drawing altogether and see if your printout values still contain the &#39;0 0&#39; values). If you do have the keyboard and mouse events handled in your &#39;(go)&#39; block then I would go with that rather than polling the keyboard and mouse state each time a timer ticks.<br />Anyway, I see I&#39;m no much help so I&#39;ll stop here and let others try to help you out. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (MikiZX)</author>
		<pubDate>Sat, 11 Apr 2020 15:15:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I commented out the drawing (<tt>update</tt>) and the <tt>al_flip_display</tt> and that fixed the 0,0&#39;s.  Uncommenting <tt>al_flip_display</tt> made them return.  So we&#39;re getting somewhere.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sat, 11 Apr 2020 18:27:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can you reproduce the issue in a small test case written in C++?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Sat, 11 Apr 2020 18:43:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello roger,</p><p>Sorry to see you&#39;re still having trouble.</p><p>Troubleshooting steps I can think of ;</p><p>1) Compile a minimal example in C and try to reproduce the behavior.</p><p>2) If commenting out your &#39;display&#39; function call removes the lag, then you should check your cpu usage. Constantly polling the keyboard and mouse involves polling Windows for the keyboard state, and that could certainly be a case of slowdown.</p><p>3) Don&#39;t poll. Use events the way they were supposed to. It is a trivial task to translate events into state. And events don&#39;t block like polling does.</p><p>4) Compile allegro yourself. No one wants to revert to 5.2.3 to test this for you when it might already be fixed. It&#39;s not that hard, honestly. Stop avoiding it.</p><p>5) Using al_get_next_event in a tight loop is wrong. You&#39;re burning out the cpu. Use al_wait_for_event like you&#39;re supposed to.
</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/618090/1045599#target">roger levy</a> said:</div><div class="quote"><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="k2">:</span> <span class="k2">(</span>go<span class="k2">)</span>
    begin
        queue alevt <a href="http://www.allegro.cc/manual/al_get_next_event"><span class="a">al_get_next_event</span></a> <span class="k1">if</span>
            etype ALLEGRO_EVENT_TIMER <span class="k3">=</span> <span class="k1">if</span>
                frame
                kbs0 <span class="n">59</span> <a href="http://www.allegro.cc/manual/al_key_down"><span class="a">al_key_down</span></a> <span class="k1">if</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a> then
            then
            \ read-mouse  \ commented-out processing of mouse events
            pump  \ game events
        then
    again
</pre></div></div><p>
</p></div></div><p>

I honestly can&#39;t think of anything else at this point. Hope this helps.</p><p>Edgar
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 11 Apr 2020 18:54:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>CPU usage was very low.  I tried your suggestions #2, #3, and #5.  Even though I&#39;m no longer calling the polling functions, the behavior is unchanged.  </p><p>Regarding #4 ... The last time I successfully compiled a DLL of Allegro that worked with Forth it took me an entire day - and that was years ago - come to think of it, I think the version I use (or a 5.2.4 one I ditched) was sent to me out of the kindness of someone&#39;s heart.  It&#39;s not a big deal for you because you&#39;re a C/++ programmer and you&#39;re used to it.  IIRC the last time I tried to compile Allegro I couldn&#39;t produce a DLL that Forth could recognize.  It&#39;s a complicated task with many options and dependencies ... I think that&#39;s pretty objectively accurate.  </p><p>On top of it, things aren&#39;t entirely &quot;working&quot;, there&#39;s weirdness regarding the audio codec addon and after much trial-and-error discovered I had to copy the dependency DLL&#39;s into both system&#39;s bin folders to be able to load ogg files during development.  With a 5.2.4 DLL I believe I compiled myself, I had horrible audio corruption playing certain files.  Neither have been solved, despite lengthy interaction with the forum on both.</p><p>So ... maybe you can see now why I&#39;m so hesitant to go down that rabbit hole, with so many potential pitfalls.</p><p>(BTW, thank you for your help so far.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sat, 11 Apr 2020 19:18:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you&#39;ll remember I offered to compile allegro for you, but you never said anything to me. You didn&#39;t answer my question as to whether it needed to be 32 or 64 bit.</p><p>I don&#39;t have any other ideas.</p><p>If 1) can&#39;t reproduce the problem, then it&#39;s not allegro. Check this before you continue.</p><p>My offer to build binaries for you still stands, but you have to work with me.</p><p><b>EDIT</b><br />Meet me on IRC on freenode on #allegro and we can discuss this real time.</p><p><b>EDIT2</b><br />My nick is Modulo5k
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 11 Apr 2020 19:22:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When was that?  I don&#39;t have any recollection of you offering to do that recently - and my memory is really hazy - I don&#39;t know why I might have decided to give up on upgrading without saying anything but it was probably that I&#39;d spent too much time on the problem already and found a workaround - but if you still don&#39;t mind, that would be great.  It&#39;s worth a shot.  Both Forths are 32-bit systems.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (roger levy)</author>
		<pubDate>Sat, 11 Apr 2020 19:28:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>roger and I managed to solve it. Turns out 64 bit libraries aren&#39;t compatible with VFX forth. He tried it with my binaries for 5.2.5 and they fixed the problem.</p><p>If there&#39;s any interest in binaries for 5.2.6 binaries for 32 bit windows I may be inclined to build them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Sat, 11 Apr 2020 21:06:08 +0000</pubDate>
	</item>
</rss>
