<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>How to stop events from being detected?</title>
		<link>http://www.allegro.cc/forums/view/615111</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 24 Feb 2015 01:12:55 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello allegro masters!</p><p>I have been creating small and fun games using allegro 5.0.10 since 5 months.<br />I can&#39;t say that i haven&#39;t had some difficulties with it, but i got used to it.</p><p>Now I&#39;m currently writing a game that includes a pause option. So when you click a button on the screen, it pauses the game. </p><p>The way that i set it up is that when i get a click on the cordinates on my button, the current timer gets stopped, and a pause menu function gets called which starts another event queue and displays some buttons to keep playing or get back to the main menu. After the person clicks the button to keep playing, the timer of the game loop gets started again.</p><p>The part of my code where i describe what is happening is this:<br />[code]if(events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){<br />			if(mouseX&lt;=width-10 &amp;&amp; mouseX&gt;=width-35 &amp;&amp; mouseY&gt;=10 &amp;&amp; mouseY&lt;=40){ //if the click is on the pause button<br />				al_stop_timer(timer);<br />				PauseMenu(); //call the pause function<br />				al_start_timer(timer);<br />			}<br />		}[/code]</p><p>My problem: When the pause menu gets displayed, any actions i do ( like clicking on the display ) get registered as events in my main game loop. So when the player un-pauses the game, all clicks that were made in the pause menu take effect. I do not want that. Is there any way to avoid events getting detected by my main game loop where i use this pause function?</p><p>I didn&#39;t show you my whole code because it is 1100+ lines
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SomeoneConfused)</author>
		<pubDate>Mon, 23 Feb 2015 02:07:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can either use <span class="source-code"><a href="http://www.allegro.cc/manual/al_unregister_event_source"><span class="a">al_unregister_event_source</span></a></span> or you can create another event queue that subscribes to the relevant sources in the second. Actually, you should pass an ALLEGRO_EVENT_QUEUE* (your queue) into your PauseMenu() function and have it monitor the events instead. That way you can still ignore the ones you don&#39;t need and you can use the ones that you do.</p><p>Edit<br />Use <a href="http://alleg.sourceforge.net/a5docs/refman/events.html#al_pause_event_queue">al_pause_event_queue</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Mon, 23 Feb 2015 02:27:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I might be lazy and just tell the main game event handling to ignore events <span class="source-code"><span class="k1">if</span><span class="k2">(</span>paused<span class="k2">)</span> <span class="k1">continue</span><span class="k2">;</span></span> kind of thing.</p><p>But that <span class="source-code">al_pause_event_queue</span> function looks handy.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 23 Feb 2015 02:31:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the suggestions about the al_pause_event_queue function. But I&#39;m using the latest recommended build of allegro ( 5.0.10 ) and this function is included in 5.1. I do not know if i should update it or insert my pause function in my main game loop, and not as additional function creating additional events loop.</p><p>EDIT: i did my code that way:<br />else if(events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN){<br />			if(mouseX&lt;=width-10 &amp;&amp; mouseX&gt;=width-35 &amp;&amp; mouseY&gt;=10 &amp;&amp; mouseY&lt;=40){<br />				al_stop_timer(timer);<br />				al_unregister_event_source(event_queue,al_get_keyboard_event_source());<br />				al_unregister_event_source(event_queue,al_get_mouse_event_source());<br />				PauseMenu();<br />				al_register_event_source(event_queue,al_get_keyboard_event_source());<br />				al_register_event_source(event_queue,al_get_mouse_event_source());<br />				al_start_timer(timer);<br />			}<br />		}</p><p>I do not know however if constantly unregistering and registering sources again always when a person pauses the game is a good and very efficient process.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SomeoneConfused)</author>
		<pubDate>Mon, 23 Feb 2015 02:50:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t think its too inefficient. I think it&#39;ll work fine. It&#39;s a nice quick solution to the problem.</p><p>Though, I would probably slap each game mode into its own event handler, so the main event loop just dispatches to the event handlers for each active mode (say the game, and the menu). In pause mode the game mode would know not to actually handle events related to updating certain aspects of the game.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 23 Feb 2015 03:46:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There&#39;s also <span class="source-code"><a href="http://www.allegro.cc/manual/al_flush_event_queue"><span class="a">al_flush_event_queue</span></a></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Mon, 23 Feb 2015 03:52:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>the al_flush_event_queue() seems really good function. I can use it instead of removing and adding back event sources. Currently my code is this:</p><p>if(mouseX&lt;=width-10 &amp;&amp; mouseX&gt;=width-35 &amp;&amp; mouseY&gt;=10 &amp;&amp; mouseY&lt;=40){<br />				al_stop_timer(timer);<br />				PauseMenu();				<br />				al_flush_event_queue(event_queue);<br />				al_start_timer(timer);<br />			}</p><p>If this metodh is hardware efficient and does not take much processing to complete, i will leave it that way. But I&#39;m always open for more suggestions. Thanks for the ones above me ^</p><p>Also, do i need to stop my game loop&#39;s timer before and start it after the pause menu if i flush the event queue? Or it will flush all timer events since the pause menu? Because if the timer isn&#39;t stopped, the code that runs when there is timer event happening, runs about 100-200 times at once, depending on how long I paused the game, so that is why i stop the timer. Is there al_pause_timer or the only way to pause a timer is to stop it and restart it?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SomeoneConfused)</author>
		<pubDate>Mon, 23 Feb 2015 12:35:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I agree with Thomas. I think that the solution here is neither to stop nor flush the main loop event queue, but rather to restructure the program so that the main loop handles the different program <i>modes</i> efficiently. I&#39;m confused though. Is the &quot;pause menu&quot; running on a different thread? It seems if that call blocked then you should be more or less fine. I still think restructuring the code is better. Have a single timer, a single main loop, and use a game &quot;mode&quot; and optionally function pointers to control the logic at runtime.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Mon, 23 Feb 2015 14:20:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615111/1010650#target">SomeoneConfused</a> said:</div><div class="quote"><p>the al_flush_event_queue() seems really good function. I can use it instead of removing and adding back event sources. Currently my code is this:</p></div></div><p>Assuming you can use <span class="source-code">al_pause_event_queue</span> that might be an even better solution. Don&#39;t think you even have to start/stop the timer in that case, it will keep firing in the background, but it won&#39;t insert events into your queue. One function call instead of three.</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615111/1010652#target">bamccaig</a> said:</div><div class="quote"><p>It seems if that call blocked then you should be more or less fine. I still think restructuring the code is better. </p></div></div><p>Allegro uses a thread to pump timer events onto your queue. so it&#39;ll fill your queue with events even if your main thread blocks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 23 Feb 2015 21:03:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="https://www.allegro.cc/forums/thread/615111/1010655#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p>
Allegro uses a thread to pump timer events onto your queue. so it&#39;ll fill your queue with events even if your main thread blocks.
</p></div></div><p>

Ah, yes, that hadn&#39;t occurred to me. Seems he has already reached that solution by flushing the queue. Mostly I say restructure.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Mon, 23 Feb 2015 21:25:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Like i said above Thomas, i use allegro 5.0.10, the latest stable build of allegro. The al_pause_event_queue is included in allegro 5.1.</p><p>So you&#39;re saying that i don&#39;t need to stop and start the timer again, if i just flush the event_queue after my pause menu?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SomeoneConfused)</author>
		<pubDate>Tue, 24 Feb 2015 01:08:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/615111/1010667#target">SomeoneConfused</a> said:</div><div class="quote"><p>Like i said above Thomas, i use allegro 5.0.10, the latest stable build of allegro. The al_pause_event_queue is included in allegro 5.1.</p></div></div><p>Doh. Sorry about that.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>So you&#39;re saying that i don&#39;t need to stop and start the timer again, if i just flush the event_queue after my pause menu?</p></div></div><p>I think its better to stop the timer and flush the queue if you can&#39;t pause the queue. Could cause some excess memory use when paused if it is paused for a long time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Tue, 24 Feb 2015 01:12:55 +0000</pubDate>
	</item>
</rss>
