<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>timer</title>
		<link>http://www.allegro.cc/forums/view/561470</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 25 Jan 2006 13:37:00 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>how can i implement a count timer from 10 to 0 second ?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (chin jiunn gai)</author>
		<pubDate>Tue, 24 Jan 2006 16:19:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why don&#39;t you try to use the allegro timer routines:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">volatile</span> <span class="k1">int</span> counter<span class="k2">;</span>

<span class="k1">void</span> my_timer_handler<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
   counter<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="k2">}</span>
<a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>my_timer_handler<span class="k2">)</span>
</pre></div></div><p>
and in your initialisation code you should lock the memory: </p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>counter<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>my_timer_handler<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
then you should 
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/install_timer" target="_blank"><span class="a">install_timer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>my_timer_handler, SECS_TO_TIMER<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
then wherever you want your timer function</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>counter <span class="k3">&gt;</span> <span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
    <span class="c">// this will be done every second</span>
    counter--<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Remember when you are not using the above, the timer will constantly increase which can cause some problems. call remove_int() after the loop and then install_int_ex() again next time you want it, or within the function place</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span> <span class="k2">(</span>counter <span class="k3">&gt;</span> <span class="n">1</span><span class="k2">)</span>
    counter <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (waldermort)</author>
		<pubDate>Tue, 24 Jan 2006 16:55:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey its the first person I&#39;ve seen explain the timer useage well. Thanks waldermort. I&#39;ve asked about this a bunch and no one&#39;s ever really told me the details I wanted =]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Tue, 24 Jan 2006 22:27:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Welcome <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Correct timer usage can be confusing as hell at first, but play around with them long enough and they become as easy is &#39;int x&#39;.</p><p>To others who wish to use a timer in their main loop (mainly used to regulate game speed) then break your code into 2 parts.  The first for doing all the calculating and thinking &#39;logic&#39; and the other for drawing all your bitmaps.  Then in the main loop your code should look like this:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/install_timer" target="_blank"><span class="a">install_timer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>my_timer_handler,MSEC_TO_TIMER<span class="k2">(</span><span class="n">80</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">while</span> <span class="k2">(</span>counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
   <span class="c">// do all your logic code here</span>
counter--<span class="k2">;</span>
<span class="k2">}</span>
<span class="c">// do all your drawing here</span>
</pre></div></div><p>

the counter will increase to 1 after every 80 miliseconds causing it to do the logic, eventually decreasing it back to 0, resulting in the bitmaps being drawn.  Changing the comparison of counter can bring extra benefits like &#39;only do once&#39; etc.</p><p>On another note, while loops will eat away at the CPU and can cause other programs to act slowly or stop responding, I recomend inserting &#39;wait(1)&#39; inside the loop which will give some of the CPU speed back to other programs.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (waldermort)</author>
		<pubDate>Wed, 25 Jan 2006 12:44:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>And I recommend not touching allegro timers at all, unless you absolutely have to. If you&#39;re on windows, use QueryPerformanceCounter, on linux, use gettimeofday. Both are very accurate, less resource-hungry and even easier to use than allegro timers. Only downside is that they are platform-specific, so if you plan on distributing for several platforms, you need a wrapper.<br />The problem with allegro timers is that they are implemented using a dedicated thread, and therefor they depend on the OS to schedule them nicely. The timer can&#39;t do anything while its thread is inactive, resulting in a so-called &quot;granularity&quot; or ~10 ms in windows. This means that your timer tick may actually be up to 10 ms off the correct time - even if you specify 1 ms precision (which will often result in 10 ticks at once, then 10ms of nothing, then another burst of 10 ticks, and so on).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 25 Jan 2006 13:37:00 +0000</pubDate>
	</item>
</rss>
