<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[A5] iOS - Touch Events non-functonal when game resumes from hibernation</title>
		<link>http://www.allegro.cc/forums/view/611286</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 22 Oct 2012 21:07:00 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I notice that if my iOS game hibernates for more than a few minutes (closed from the Home button) that when I resume, it seems like the event queue is not always notified of new touch events.  Seems the longer I leave it in hibernation, the more likely it is to not respond to touches when I resume.</p><p>As far as I can tell, I am properly handling the switch in/out, acknowledging, etc (as well as handling display lost/found events.)</p><p>Background audio stream always resumes fine, and the screen displays the proper &quot;paused game&quot; graphics.  Since I am not manually refiling my stream buffer, I&#39;m not sure if the audio playing is evidence that the event queue is actually functioning or not.  And since the paused graphic is drawn before the game actually yields to iOS, I&#39;m not sure if that&#39;s just buffered graphics or if its actually processing redraw events either.   Tonight I&#39;ll try drawing something dynamic to the screen to see if this is the case... but meantime...</p><p><b>When the display is switched out/in, should I detroy/recreate my event queue/sources?  Are there any pitfalls in doing so?</b></p><p>I haven&#39;t played with code yet, just brainstorming what the problem might be and any potential solutions.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Fri, 19 Oct 2012 20:32:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I had this problem at one point. You solution might work. What turned out to be the problem for me was timer events still ticking, so on resume the game pumped through thousands of them causing a long delay. You could either use <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>, or stop and restart the timers on pause/resume.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Fri, 19 Oct 2012 22:06:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;d suggest stopping anything and everything on pause. shouldn&#39;t need to destroy stuff, unless you want to be super memory friendly. or just free temp stuff on a low memory warning.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Fri, 19 Oct 2012 23:33:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ahhh Trent I definitely think you&#39;re onto something because sometimes if I just wait a while, it seems to come back (if I don&#39;t get impatient and end the task first.)</p><p>I guess my assumption was that events wouldnt be generated while it was in background. DOH!</p><p>Thomas: What do you mean by stopping?  Unregistering the event sources?</p><p>Everything as far as my game&#39;s drawing/update processing is stopped on pause.  I just wasn&#39;t aware that events would still be firing after the switch out.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (thebignic)</author>
		<pubDate>Sat, 20 Oct 2012 00:13:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I agree with Thomas, you should stop anything that might generate repeated events, mainly timers. I was just giving some options. I bet it&#39;s your timers though. Stop them on pause (al_stop_timer) and restart them after (al_start_timer.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sat, 20 Oct 2012 00:25:30 +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/611286/968508#target">thebignic</a> said:</div><div class="quote"><p>Thomas: What do you mean by stopping? Unregistering the event sources?</p></div></div><p>What Trent said. stop the timers. And if you have any secondary threads or other async tasks, try and pause or stop those as well. Unless its some background download you want to keep going.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 20 Oct 2012 00:34:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Also consider al_pause_event_queue
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Sun, 21 Oct 2012 11:16:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m a big confused about what&#39;s happening here. I thought the app still functioned when minimized, it just couldn&#39;t draw?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 22 Oct 2012 02:59:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There is no minimization... but you can do stuff in the background if you really want, but you generally don&#39;t want to. people will complain about power use.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 22 Oct 2012 03:34:03 +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/611286/968616#target">Thomas Fjellstrom</a> said:</div><div class="quote"><p> but you can do stuff in the background if you really want
</p></div></div><p>Hence my question. Timer events should be firing off in the background, and, unless you purposefully do something, should be handled as normal. I don&#39;t get why they are backing up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 22 Oct 2012 03:35:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It depends how you write your program. You can do it so that what you said is true, but then you&#39;re wasting resources on a limited device. Here&#39;s an example of how this can happen:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_DISPLAY_HALT_DRAWING<span class="k2">)</span> <span class="k2">{</span>
    <span class="c">// some cleanup code here</span>
    al_acknowledge_drawing_halt<span class="k2">(</span>display<span class="k2">)</span><span class="k2">;</span>
    <span class="k1">while</span> <span class="k2">(</span><span class="k1">true</span><span class="k2">)</span> <span class="k2">{</span>
        <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> e<span class="k2">;</span>
        <a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>display_event_queue, <span class="k3">&amp;</span>e<span class="k2">)</span><span class="k2">;</span>
        <span class="k1">if</span> <span class="k2">(</span>e.event_type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING<span class="k2">)</span> <span class="k2">{</span>
            <span class="k1">break</span><span class="k2">;</span>
        <span class="k2">}</span>
    <span class="k2">}</span>
    al_acknowledge_drawing_resume<span class="k2">(</span>display, NULL<span class="k2">)</span><span class="k2">;</span>
    <span class="c">// reset as needed and continue</span>
<span class="k2">}</span>
</pre></div></div><p>

If you need to process stuff in the background this doesn&#39;t work, but it spares the (possibly insignificant, but not necessarily) processing of your logic. NOTE: I intentionally left the stop/start timer out to show the potential problem. Also I used a separate event queue for display events only so you&#39;re not processing the other events.</p><p>Which brings up a good point, why not just skip those events in that loop? You could do that and it wouldn&#39;t cost much of anything.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Mon, 22 Oct 2012 04:49:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I see. I just make the timer events a nop in my app when it is switched out, so this issue didn&#39;t hit me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 22 Oct 2012 08:04:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Of course you can even save whatever cpu time that takes by just stopping the timer. The only events that you&#39;ll get after that are resume events. So your loop shouldn&#39;t even run till you get switched back to.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 22 Oct 2012 10:08:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think it&#39;s better to stop the timer. The app really shouldn&#39;t do <b>anything at all</b> besides wake up on the switch-in. Otherwise if you have 100 apps thinking like that (and since no app ever quits that is easily reached) you drain your batter just handling 6000 timer handles / second (each of them 60). It doesn&#39;t sound much, but it means the kernel has to do 6000 context switches and keep/reload code into memory just to handle the NOP timer events.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Mon, 22 Oct 2012 14:44:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Maybe for a future app. Going through the pain of sending out a new version just to fix this minor issue isn&#39;t worth my time <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 22 Oct 2012 17:41:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s exactly the attitude that makes me want to try your app, don&#39;t you ever change it <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (torhu)</author>
		<pubDate>Mon, 22 Oct 2012 20:04:33 +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/611286/968650#target">torhu</a> said:</div><div class="quote"><p> That&#39;s exactly the attitude that makes me want to try your app, don&#39;t you ever change it <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /></p></div></div><p>Not doing iOS development out of my own volition, so see if I care <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />.</p><p>Seriously, I was just curious why my app didn&#39;t hit this bug, then explained why... and everybody ganged up on me because I&#39;m this evil person who decreases their battery life by 0.1%. Obviously I didn&#39;t do it on purpose, but it&#39;s obvious that this isn&#39;t really an issue either. If you care that much, amend the bloody iOS readme and documentation for the display halted event to explain the best practices for it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 22 Oct 2012 21:07:00 +0000</pubDate>
	</item>
</rss>
