<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>fps and timer stuff</title>
		<link>http://www.allegro.cc/forums/view/450605</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 26 Jan 2005 06:08:13 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I tested a game sketch with a powerful PC, and it was completly unplayeable because it rans too fast!.</p><p>1-How can force the game to run no more than 60fps?<br />2-Installing more than one timer will slow the program too much?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 18 Jan 2005 02:10:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1. <a href="http://alleg.sourceforge.net/faq.html#How_can_I_make_my_game_run_at_the_same_speed_on_any_computer_">It&#39;s in the FAQ</a></p><p>2. I think timers are implemented in a thread, so they shouldn&#39;t slow down your system too much.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rampage)</author>
		<pubDate>Tue, 18 Jan 2005 02:38:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>1.-<br />Define timer and time rate:
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// fps counter</span>
<span class="p">#define FPS_RATE BPS_TO_TIMER(60)</span>
<span class="k1">volatile</span> <span class="k1">unsigned</span> <span class="k1">int</span> fpsTimer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="k1">void</span> FPS_FUNC<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
  fpsTimer<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>FPS_FUNC<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Use the timer to run the stuff you want 60 times per second:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>  <span class="c">// Reset vars  </span></td></tr><tr><td class="number">2</td><td>  <span class="k1">unsigned</span> <span class="k1">int</span> lastFrame <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  fpsTimer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td>  <span class="c">// Install timers</span></td></tr><tr><td class="number">6</td><td>  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>SEC_FUNC, SEC_RATE<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>FPS_FUNC, FPS_RATE<span class="k2">)</span><span class="k2">;</span>  </td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>  <span class="k1">bool</span> run <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  <span class="k1">do</span> <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td>    <span class="k1">if</span><span class="k2">(</span>lastFrame <span class="k3">&lt;</span> fpsTimer <span class="k3">|</span><span class="k3">|</span> lastFrame <span class="k3">=</span><span class="k3">=</span> UINT_MAX<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">13</td><td>      lastFrame <span class="k3">=</span> fpsTimer<span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>      <span class="c">// STUFF (logic + render + input?)</span></td></tr><tr><td class="number">16</td><td>                        run <span class="k3">=</span> do_stuff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>    </td></tr><tr><td class="number">18</td><td>    <span class="k2">}</span>    </td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td>    yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// Save CPU (timer need it)</span></td></tr><tr><td class="number">21</td><td>&#160;</td></tr><tr><td class="number">22</td><td>  <span class="k2">}</span> <span class="k1">while</span><span class="k2">(</span>run<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>&#160;</td></tr><tr><td class="number">24</td><td>  <span class="c">// Remove timers</span></td></tr><tr><td class="number">25</td><td>  <a href="http://www.allegro.cc/manual/remove_int" target="_blank"><span class="a">remove_int</span></a><span class="k2">(</span>SEC_FUNC<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>  <a href="http://www.allegro.cc/manual/remove_int" target="_blank"><span class="a">remove_int</span></a><span class="k2">(</span>FPS_FUNC<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>...</td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 04:08:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Isn&#39;t better to let all the logic update at 60 fps and render at the best speed the computer can? I think human eye can still feel the difference between 60fps and 75fps, so if I can run at 75fps why not?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (umperio)</author>
		<pubDate>Tue, 18 Jan 2005 04:53:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>i agree with umperio
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (flares)</author>
		<pubDate>Tue, 18 Jan 2005 05:13:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Umperio said:</div><div class="quote"><p>Isn&#39;t better to let all the logic update at 60 fps and render at the best speed the computer can? I think human eye can still feel the difference between 60fps and 75fps, so if I can run at 75fps why not?</p></div></div><p>I&#39;m not sure of this but if you only update the logic 60 times per second there is no point (and no graphical improvement) in reblitting the <b>same</b> image over and over... better use this time to yield to the OS or whatever
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Tue, 18 Jan 2005 05:17:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Mithrandir said:</div><div class="quote"><p>
I&#39;m not sure of this but if you only update the logic 60 times per second there is no point (and no graphical improvment) in reblitting the <b>same</b> image over and over... better use this time to yield to the OS or whatever
</p></div></div><p>
Well if there&#39;s no logic update there&#39;s no changes between two frames, so yielding would be nice, but I would replace <span class="source-code">yield_timeslice<span class="k2">(</span><span class="k2">)</span></span> with <span class="source-code"><a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Marco Radaelli)</author>
		<pubDate>Tue, 18 Jan 2005 05:26:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yea, Marco and Mithrandir got the answer. No sense in drawing if no logic changes.</p><p>For sprite animation its better if you know that you will display 60fps and no more than that, so you can set your animations to the movement rate you want, and test them the same rate that they will be drawn in every computer (every computer that can do your 60fps) IMO.</p><p>But it depends on the game. You can also set considerations about the game time if you are making a real time game (time lost due to drawing, ...).</p><p>You can read <a href="http://www.iguanademos.com/Jare/Articles.php?view=FixLoop">this article</a> from Javier Arévalo (Commandos saga) for nice example.</p><p>---<br />rest(1) didnt work for me, so i moved to yield.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 06:03:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>No sense in drawing if no logic changes.</p></div></div><p>There&#39;s no down side, from a performance or visual perspective. Use page flipping.</p><p>I would highly not recommend using rest(1). On a windows machine, this means it&#39;s going to rest for approximately 10ms (and do nothing in that time). If the machine is a slower one, this time is valuable and can lead to loss of logic cycles and/or loss of video frames.</p><p>The best solution is to use page flipping with a lock on the fps of your logic. You wont get tearing, and your game will run at a decent speed on all systems. On slower systems, you&#39;ll get choppy video but not choppy or slow logic.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Tue, 18 Jan 2005 10:10:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you rerender the same screen again(not just blit or flip it) it may still be rendering when the next logic step should have been running.  This leads to small skips in the game that make fast moving objects look jerky.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Erkle)</author>
		<pubDate>Tue, 18 Jan 2005 10:29:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I changed your example a little, but I don&#39;t understand how it works <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><div class="source-code snippet"><div class="inner"><pre> <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">if</span><span class="k2">(</span>lastFrame <span class="k3">&gt;</span><span class="k3">=</span> fpsTimer <span class="k3">&amp;</span><span class="k3">&amp;</span> lastFrame <span class="k3">!</span><span class="k3">=</span> UINT_MAX<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
       yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
       <span class="k1">continue</span><span class="k2">;</span>
    <span class="k2">}</span>
    lastFrame <span class="k3">=</span> fpsTimer<span class="k2">;</span>
    <span class="c">// stuff</span>
 <span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 18 Jan 2005 11:08:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>you can lock the fps this way :</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>      <span class="k1">volatile</span> <span class="k1">int</span> speed_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td>      <span class="k1">void</span> increment_speed_counter<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">4</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>         speed_counter<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td>      <a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>increment_speed_counter<span class="k2">)</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td>      <span class="k1">void</span> play_the_game<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">11</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>         <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>speed_counter<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>         <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>increment_speed_counter<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>         <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>increment_speed_counter, BPS_TO_TIMER<span class="k2">(</span><span class="n">60</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>         <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>game_over<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">18</td><td>            <span class="k1">while</span> <span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">19</td><td>               update_game_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td>               speed_counter--<span class="k2">;</span></td></tr><tr><td class="number">21</td><td>            <span class="k2">}</span></td></tr><tr><td class="number">22</td><td>&#160;</td></tr><tr><td class="number">23</td><td>            update_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>            <span class="c">// no need to begin a cycle logic drawing if speed_counter=0 </span></td></tr><tr><td class="number">25</td><td>            <span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span><span class="c">/*lock the FPS*/</span><span class="k2">}</span></td></tr><tr><td class="number">26</td><td>         <span class="k2">}</span></td></tr><tr><td class="number">27</td><td>      <span class="k2">}</span></td></tr></tbody></table></div></div><p>

I didn&#39;t know about the yield_timeslice function, but don&#39;t understand what it does, so i will continue with this stuff instead
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamal)</author>
		<pubDate>Tue, 18 Jan 2005 15:16:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I would highly not recommend using rest(1). On a windows machine, this means it&#39;s going to rest for approximately 10ms (and do nothing in that time). If the machine is a slower one, this time is valuable and can lead to loss of logic cycles and/or loss of video frames.
</p></div></div><p>

On a windows machine you schould use Sleep() and timeBeginPeriod(). Don&#39;t forget timeEndPeriod().
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 18 Jan 2005 15:17:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>On Windows rest() is implemented using Sleep(). </p><p>And why should rest(1) wait for 10ms? My measurements indicate that rest(1) waits fpr 1ms average - slight variations possible, so that&#39;s not too exact. You have approx. 1000 rest(1) calls within a second.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 18 Jan 2005 15:24:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Gering said:</div><div class="quote"><p>On a windows machine you schould use Sleep()</p></div></div><p>This is what, AFAIK, <span class="source-code">yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span> does under windows.</p><p>Tobing, when people say that rest waits for 10ms it&#39;s because (i don&#39;t know where i read it) allegro&#39;s(or it&#39;s windows&#39;s fault?) implementation of the timer isn&#39;t precise to the millisecond.</p><p>BTW what Marco is saying is valid for a WIP (i don&#39;t recall which one) where yield_timeslice, under windows, didn&#39;t work as expected.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Tue, 18 Jan 2005 15:55:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
I don&#39;t see why use Rest would resolve the problem because, regardless of<br />the computer speed, sometimes you&#39;ll have to rest 10 ms, sometimes 0, ...<br />I think the only thing you have to do is to look at if logic has to be implemented, so just do nothing while timer_counter==0<br />just add one line after the drawing should do the trick :</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>game_over<span class="k2">)</span> <span class="k2">{</span>
            <span class="k1">while</span> <span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
               update_game_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
               speed_counter--<span class="k2">;</span>
            <span class="k2">}</span>

            update_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
     
            <span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span><span class="c">/*do nothing*/</span><span class="k2">}</span>
         <span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamal)</author>
		<pubDate>Tue, 18 Jan 2005 16:38:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">lucaz said:</div><div class="quote"><p>
I changed your example a little, but I don&#39;t understand how it works <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /><br />while(!key[KEY_ESC]) {<br />    if(lastFrame &lt; fpsTimer &amp;&amp; lastFrame != UINT_MAX)) {<br />       yield_timeslice();<br />       continue;<br />    }<br />    lastFrame = fpsTimer;<br />    // stuff<br />}
</p></div></div><p>

Not the right way. You should use:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span>
    <span class="k1">if</span><span class="k2">(</span>lastFrame <span class="k3">&gt;</span><span class="k3">=</span> fpsTimer <span class="k3">&amp;</span><span class="k3">&amp;</span> lastFrame <span class="k3">!</span><span class="k3">=</span> UINT_MAX<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span>
       lastFrame <span class="k3">=</span> fpsTimer<span class="k2">;</span>
       <span class="c">// stuff</span>
       
    <span class="k2">}</span>
    yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
Explanation:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">// Dont stop till user press key_esc</span></td></tr><tr><td class="number">2</td><td><span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">3</td><td>    <span class="c">// First time lastframe = 0 and fpstimer = 0</span></td></tr><tr><td class="number">4</td><td>    <span class="c">// (60 ticks per second = fps rate you have set for the fpstimer)</span></td></tr><tr><td class="number">5</td><td>    <span class="c">// Every tick fpsTimer goes ++</span></td></tr><tr><td class="number">6</td><td>    <span class="c">// so lastframe will be fpstimer - 1</span></td></tr><tr><td class="number">7</td><td>    <span class="c">// in that case you enter the if sentence to do the stuff</span></td></tr><tr><td class="number">8</td><td>    <span class="c">// this situation takes place 60 times in a second, so you paint 60frames</span></td></tr><tr><td class="number">9</td><td>    <span class="c">// If computer is slow and cannot paint 60 frames, pending frames are discarded</span></td></tr><tr><td class="number">10</td><td>    <span class="c">// lastframe would be &lt; fpstimer 'n' times (where n is the frames lost)</span></td></tr><tr><td class="number">11</td><td>    <span class="c">// the UINT_MAX condition is to avoid uint overflow (fpstimer would be 0 and</span></td></tr><tr><td class="number">12</td><td>    <span class="c">// lastframe would be equal to UINT_MAX, so the '&lt;' condition wouldn't work</span></td></tr><tr><td class="number">13</td><td>    <span class="c">// So the condition must be to paint 60 times</span></td></tr><tr><td class="number">14</td><td>    <span class="k1">if</span><span class="k2">(</span>lastFrame <span class="k3">&lt;</span> fpsTimer <span class="k3">&amp;</span><span class="k3">&amp;</span> lastFrame <span class="k3">!</span><span class="k3">=</span> UINT_MAX<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>       lastFrame <span class="k3">=</span> fpsTimer<span class="k2">;</span></td></tr><tr><td class="number">16</td><td>       <span class="c">// stuff</span></td></tr><tr><td class="number">17</td><td>       </td></tr><tr><td class="number">18</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">19</td><td>    <span class="c">// this is needed to prevent cpu from tax death</span></td></tr><tr><td class="number">20</td><td>    yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>


Using draw out of the if condition for painting more than 60 times can be used to scroll, page flipping or this kind of stuff they say, but u really dont need it (maybe my way make the minimum requiriments higher). In my game (in progress 40%) input phase includes logic and scroll calculation, so i dont need to paint the same frame (no tick, no changes). This will probably change when i add the AI, so ai can use the free time to improve its performance.</p><p>@lucaz <br />You can use:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k2">(</span>lastframe <span class="k3">&lt;</span> fpstimer<span class="k2">)</span> <span class="k2">{</span>
    lastframe <span class="k3">=</span> fpstimer<span class="k2">;</span>
    <span class="c">// stuff</span>
<span class="k2">}</span>
<span class="k1">or</span>
<span class="k2">(</span>fpstimer <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
    fpstimer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
    <span class="c">// stuff</span>
<span class="k2">}</span>
</pre></div></div><p>
I think you can control time better using the first solution.</p><div class="quote_container"><div class="title">jamal said:</div><div class="quote"><p>

while (speed_counter &gt; 0) {<br />               update_game_logic();<br />               speed_counter--;<br />         }</p></div></div><p>

If computer is slow at a certain moment speed_counter will grow a lot. If cpu gets free later, cpu will try to do this amount of counter that couldn&#39;t do, so you will have a really fast moment on the game. (Dont know if i have explained it well; i mean, that you have to control lost frames in a moment, and no extend them to later; if so, later will have more than 60fps, what can be a bad effect for animations).</p><div class="quote_container"><div class="title">jamal said:</div><div class="quote"><p>
        // yield vs<br />        while(speed_counter == 0){/*lock the FPS*/}
</p></div></div><p>

are you sure this works? this will block the cpu to max usage, so even the timer wont be take in consideration: speed_counter will be 0 forever. IMO. Think i tried it the first time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 16:50:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Nunca Ese said:</p><p>   jamal said:</p><p>   while (speed_counter &gt; 0) {<br />      update_game_logic();<br />      speed_counter--;<br />   }</p><p>If computer is slow at a certain moment speed_counter will grow a lot. If cpu gets free later, cpu will try to do this amount of counter that couldn&#39;t do, so you will have a really fast moment on the game. (Dont know if i have explained it well; i mean, that you have to control lost frames in a moment, and no extend them to later; if so, later will have more than 60fps, what can be a bad effect for animations).
</p></div></div><p>

I know about this but I don&#39;t think it&#39;s a good solution to have the logic and the drawing dependant. Imagine you have a computer playing your game at 30 fps. Another computer will play it at 60 fps (which is what you want). if logic and drawing are dependant, the game will be two times slower in the first computer ( I mean here the moving of characters,...), so this won&#39;t be the same game. In the other hand (&quot;my way&quot;) the game will always be the same, exept a bad drawing if the computer is too slow.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Nunca Ese said :<br />jamal said:<br />// yield vs<br />while(speed_counter == 0){/*lock the FPS*/}</p><p>are you sure this works? this will block the cpu to max usage, so even the timer wont be take in consideration: speed_counter will be 0 forever. IMO. Think i tried it the first time.
</p></div></div><p>
I don&#39;t think so because the incrementation of speed_counter is done via a thread so is independant of the main loop</p><p>[EDIT] <i>*lock the FPS*</i> means do nothing as I corrected it the second post [/EDIT]
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamal)</author>
		<pubDate>Tue, 18 Jan 2005 17:13:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The timer precision is given by a certain constant (forgot the name in the moment), and back in Windows 3.1 the accuracy of a timer was very low, something like 18 updates with a second or so. All that has improved, so the timer accuracy should be at least 0.1ms if not better (Windows XP, I do not know about any of the other systems). </p><p>I have never used any other function than rest() resp. Sleep() to give time back to the cpu, so I don&#39;t know any of the other functions mentioned above - timeBeginPeriod() or yield_timeslice(). </p><p>This form of waiting: 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span><span class="c">/*lock the FPS*/</span><span class="k2">}</span>
</pre></div></div><p>
is something I do not like, because it uses 100% cpu to do just nothing. It is not nice, and uses a lot of resources on a notebook, which I believe it shouldn&#39;t. So I would use something like 
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span><a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><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>
to wait without wasting resources like cpu usage.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 18 Jan 2005 17:36:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">jamal said:</div><div class="quote"><p>
Imagine you have a computer playing your game at 30 fps. Another computer will play it at 60 fps (which is what you want). if logic and drawing are dependant, the game will be two times slower in the first computer
</p></div></div><p>
This only happens if computer is too slow, which is the same performance in your case. Its normal that it will be two times slower, thats in the case that cpu and video card do the stuff at half the speed from the minimum requirements.<br />If computer is fast enough to run 60fps it will (thats the concept of minimum requirements).</p><div class="quote_container"><div class="title">jamal said:</div><div class="quote"><p>
In the other hand (&quot;my way&quot;) the game will always be the same, exept a bad drawing if the computer is too slow.
</p></div></div><p>
Hope the comment above applies to this sentence. Mine will have reduced performance only if it cannot draw 60fps, which is equal to &quot;slow computer&quot;.</p><p>What i mean is that its better using speed_counter = 0; instead of speed_counter--; if you are not going to consider the lost frames.</p><p>If computer is slow at a certain moment (54 fps):<br />your code will do spped_counter to reach 6 (value) (60 ticks - 54 times)<br />When computer restores its normal speed your code will have speed_counter = 6, so next time it will try to do 66fps, what is not they right vehaviour (you want the computer to run 60fps all the time, not more than that).<br />6 frames is not a problem, but imagine the case that antivirus suddenly starts to scan (slow computer) or p2p running, or any kind of background program, (downloading  tasks, etc.) that can make your game to lose 60 frames (this would make your game to try 120fps next second, animations at double speed). Frames sould be discarded, so speed_counter should be set to 0, losing the 6 frames.</p><p>The other point is a behaviour for the 6 frames (look at the javier arevalos&#39; article (link some messages above), there they have a good way to control (using interpolation) the lost frames):</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Easy but not very useful way</span>
<span class="k1">if</span><span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
   speed_counter--<span class="k2">;</span>
    <span class="c">// losts</span>
   <span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
     <span class="c">// frame lost, do X</span>
     speed_counter--<span class="k2">;</span>
   <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">tobing said:</div><div class="quote"><p>
I have never used any other function than rest() resp. Sleep() to give time back to the cpu, so I don&#39;t know any of the other functions mentioned above - timeBeginPeriod() or yield_timeslice().
</p></div></div><p>
Neither <br />- while(speed_counter == 0){/*lock the FPS*/}<br />nor<br />- while(speed_counter == 0){rest(1);}<br />worked for me. speed_counter was always 0 on the first case, and dont remember what, but bad behaviour with rest()<br />yield_timeslice() gives up the rest of the current scheduler timeslice. The problem is that timeslice size changes from so version, so its like rest(variable) IMO.</p><p>Maybe the problem with rest is:<br />***** start manual info</p><p>void rest(unsigned int time);</p><p>This function waits for the specified number of milliseconds.</p><p>Passing 0 as parameter will not wait, but just yield. This can be useful in order to &quot;play nice&quot; with other processes. Other values will cause CPU time to be dropped on most platforms. This will look better to users, and also does things like saving battery power and making fans less noisy.</p><p><b>Note that calling this inside your active game loop is a bad idea, as you never know when the OS will give you the CPU back, so you could end up missing the vertical retrace and skipping frames.</b> On the other hand, on multitasking operating systems it is good form to give up the CPU for a while if you will not be using it. <br />****** end manual info
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 17:40:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Its normal that it will be two times slower, thats in the case that cpu and video card do the stuff at half the speed from the minimum requirements.</p></div></div><p>
Having your game run at half the speed it is meant to run because the computer can&#39;t draw all frames is poor design.<br />It completely changes the dynamics of the game if what was a fast-paced jump&#39;n run game suddenly runs at half speed. Imagine what would happen if you tried to run an RTS over a network where not all computers have the same speed.</p><p>It&#39;s more important that the logic runs at a constant rate on all computers (in sofar as that is possible) than that you don&#39;t skip animation frames because otherwise the animation will be less smooth.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Neither<br />- while(speed_counter == 0){/*lock the FPS*/}<br />nor<br />- while(speed_counter == 0){rest(1);}<br />worked for me. speed_counter was always 0 on the first case</p></div></div><p>
Did you remember to declare speed_counter as volatile?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>The problem is that timeslice size changes from so version, so its like rest(variable) IMO.</p></div></div><p>
I&#39;m not sure what you mean?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Note that calling this inside your active game loop is a bad idea, as you never know when the OS will give you the CPU back, so you could end up missing the vertical retrace and skipping frames.</p></div></div><p>
Look at the code. They&#39;re not calling it from the active (meaning logic) loop.<br />The way I do this myself is to run the logic until it is up to date, then draw a frame if that is nescessary, otherwise just yield/rest.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 18 Jan 2005 18:02:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">jamal said:</div><div class="quote"><p>I don&#39;t see why use Rest would resolve the problem because, regardless of<br />the computer speed, sometimes you&#39;ll have to rest 10 ms, sometimes 0, ...</p></div></div><p>I never said to use rest to keep framerate constant; what i was suggesting was to use yield_timeslice to play nice with multitasking. Since in a previous allegro WIP there was a bug that didn&#39;t make yield_timeslice work under windows, Marco introduced rest, but only as a mean to yield, not for fps.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FMC)</author>
		<pubDate>Tue, 18 Jan 2005 18:04:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is my game loop as I use it currently:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="c">//----------------------------------------------------------------------------</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k1">static</span> <span class="k1">volatile</span> <span class="k1">int</span> timer_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="c">// set to 60 times per second - desired fps</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">static</span> <span class="k1">void</span> timerCounterUpdater<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">6</td><td><span class="k2">{</span></td></tr><tr><td class="number">7</td><td>  <span class="k3">+</span><span class="k3">+</span>timer_counter<span class="k2">;</span></td></tr><tr><td class="number">8</td><td><span class="k2">}</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td><span class="k1">static</span> <span class="k1">volatile</span> <span class="k1">int</span> fps_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="c">// set to 1 per second - count fps</span></td></tr><tr><td class="number">11</td><td>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">static</span> <span class="k1">void</span> fpsCounterUpdater<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">13</td><td><span class="k2">{</span></td></tr><tr><td class="number">14</td><td>  <span class="k3">+</span><span class="k3">+</span>fps_counter<span class="k2">;</span></td></tr><tr><td class="number">15</td><td><span class="k2">}</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td><span class="k1">static</span> <span class="k1">volatile</span> <span class="k1">int</span> perf_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> <span class="c">// set to 1000 per second - measure time used by a certain code fragment</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td><span class="k1">static</span> <span class="k1">void</span> perfCounterUpdater<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">20</td><td><span class="k2">{</span></td></tr><tr><td class="number">21</td><td>  <span class="k3">+</span><span class="k3">+</span>perf_counter<span class="k2">;</span></td></tr><tr><td class="number">22</td><td><span class="k2">}</span></td></tr><tr><td class="number">23</td><td>&#160;</td></tr><tr><td class="number">24</td><td><span class="c">//----------------------------------------------------------------------------</span></td></tr><tr><td class="number">25</td><td>&#160;</td></tr><tr><td class="number">26</td><td><span class="k1">struct</span> FPSInfo</td></tr><tr><td class="number">27</td><td><span class="k2">{</span></td></tr><tr><td class="number">28</td><td>  <span class="k1">int</span> logic<span class="k2">;</span></td></tr><tr><td class="number">29</td><td>  <span class="k1">int</span> displayed<span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  <span class="k1">int</span> iteration<span class="k2">;</span></td></tr><tr><td class="number">31</td><td>  <span class="k1">int</span> advance_cputime<span class="k2">;</span></td></tr><tr><td class="number">32</td><td>  <span class="k1">int</span> display_cputime<span class="k2">;</span></td></tr><tr><td class="number">33</td><td>  <span class="k1">int</span> show_cputime<span class="k2">;</span></td></tr><tr><td class="number">34</td><td>  <span class="k1">int</span> perf_counter_<span class="k2">;</span></td></tr><tr><td class="number">35</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">36</td><td>&#160;</td></tr><tr><td class="number">37</td><td>&#160;</td></tr><tr><td class="number">38</td><td><span class="c">//----------------------------------------------------------------------------</span></td></tr><tr><td class="number">39</td><td>&#160;</td></tr><tr><td class="number">40</td><td><span class="c">// frame counter</span></td></tr><tr><td class="number">41</td><td><span class="k1">static</span> FPSInfo cur        <span class="k3">=</span> <span class="k2">{</span> <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span> <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">42</td><td><span class="k1">static</span> FPSInfo displayed  <span class="k3">=</span> <span class="k2">{</span> <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span> <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">43</td><td>&#160;</td></tr><tr><td class="number">44</td><td>&#160;</td></tr><tr><td class="number">45</td><td>&#160;</td></tr><tr><td class="number">46</td><td><span class="k1">void</span> GameLoop::loop<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">47</td><td><span class="k2">{</span></td></tr><tr><td class="number">48</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k1">true</span><span class="k2">)</span></td></tr><tr><td class="number">49</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">50</td><td>    <span class="k1">bool</span> needsRefresh <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>    cur_skip <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">52</td><td>    <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span><span class="k1">const</span> draw_buffer <span class="k3">=</span> display_engine.draw_buffer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// what I draw onto</span></td></tr><tr><td class="number">53</td><td>    <span class="k1">while</span><span class="k2">(</span> timer_counter <span class="k3">&gt;</span> <span class="n">0</span> <span class="k2">)</span></td></tr><tr><td class="number">54</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">55</td><td>      <span class="k1">if</span><span class="k2">(</span> <a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span> <span class="k2">)</span></td></tr><tr><td class="number">56</td><td>        <a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">57</td><td>&#160;</td></tr><tr><td class="number">58</td><td>      <span class="c">// handle user input</span></td></tr><tr><td class="number">59</td><td>&#160;</td></tr><tr><td class="number">60</td><td>      <span class="c">// Book keeping</span></td></tr><tr><td class="number">61</td><td>      <span class="k3">+</span><span class="k3">+</span>cur.logic<span class="k2">;</span></td></tr><tr><td class="number">62</td><td>      <span class="k3">-</span><span class="k3">-</span>timer_counter<span class="k2">;</span></td></tr><tr><td class="number">63</td><td>      <span class="k3">+</span><span class="k3">+</span>cur_skip<span class="k2">;</span></td></tr><tr><td class="number">64</td><td>      cur.advance_cputime <span class="k3">-</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">65</td><td>      advance_game_time<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// advance logic game time</span></td></tr><tr><td class="number">66</td><td>      cur.advance_cputime <span class="k3">+</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">67</td><td>      <span class="k1">if</span><span class="k2">(</span> cur_skip <span class="k3">&gt;</span><span class="k3">=</span> max_skip <span class="k2">)</span> <span class="c">// max_skip is constant, e.g. 3 - max 3 frames may be skipped before the logical fps goes down</span></td></tr><tr><td class="number">68</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">69</td><td>        timer_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">70</td><td>        <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">71</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">72</td><td>      needsRefresh <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">73</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">74</td><td>    <span class="k1">if</span><span class="k2">(</span> needsRefresh <span class="k2">)</span></td></tr><tr><td class="number">75</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">76</td><td>      cur.display_cputime <span class="k3">-</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">77</td><td>      <span class="c">// draw screen</span></td></tr><tr><td class="number">78</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">79</td><td>        <a href="http://www.allegro.cc/manual/acquire_bitmap" target="_blank"><span class="a">acquire_bitmap</span></a><span class="k2">(</span> draw_buffer <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">80</td><td>        <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span> draw_buffer <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">81</td><td>        <span class="c">// do the drawing here</span></td></tr><tr><td class="number">82</td><td>        <a href="http://www.allegro.cc/manual/release_bitmap" target="_blank"><span class="a">release_bitmap</span></a><span class="k2">(</span> draw_buffer <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">83</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">84</td><td>      cur.display_cputime <span class="k3">+</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">85</td><td>&#160;</td></tr><tr><td class="number">86</td><td>      cur.show_cputime <span class="k3">-</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">87</td><td>      <span class="c">// Display double_buffer</span></td></tr><tr><td class="number">88</td><td>      needsRefresh <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">89</td><td>      display_engine.display_draw_buffer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">90</td><td>      cur.displayed<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">91</td><td>      cur.show_cputime <span class="k3">+</span><span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">92</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">93</td><td>    <span class="k1">else</span></td></tr><tr><td class="number">94</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">95</td><td>      <span class="k1">if</span><span class="k2">(</span> rest_dur_ms <span class="k3">&gt;</span> <span class="n">0</span> <span class="k2">)</span></td></tr><tr><td class="number">96</td><td>        <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span> rest_dur_ms <span class="k2">)</span><span class="k2">;</span> <span class="c">// rest for given amount of milliseconds if non-zero</span></td></tr><tr><td class="number">97</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">98</td><td>    cur.iteration<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">99</td><td>    <span class="k1">if</span><span class="k2">(</span> fps_counter <span class="k3">&gt;</span> <span class="n">0</span> <span class="k2">)</span></td></tr><tr><td class="number">100</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">101</td><td>      displayed  <span class="k3">=</span> cur<span class="k2">;</span></td></tr><tr><td class="number">102</td><td>      displayed.perf_counter_ <span class="k3">=</span> perf_counter<span class="k2">;</span></td></tr><tr><td class="number">103</td><td>      cur.logic  <span class="k3">=</span> cur.iteration <span class="k3">=</span> cur.displayed <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">104</td><td>      cur.advance_cputime <span class="k3">=</span> cur.display_cputime <span class="k3">=</span> cur.show_cputime <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">105</td><td>      fps_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">106</td><td>      perf_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">107</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">108</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">109</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
including performance measurements. The basic structure is from the german book I had recommended some days ago, the important things I added are rest() and performance measure. </p><p>Feel free to comment on this code, I would be glad to improve it where I can.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 18 Jan 2005 18:09:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Mithrandir said :<br />I never said to use rest to keep framerate constant; what i was suggesting was to use yield_timeslice to play nice with multitasking. Since in a previous allegro WIP there was a bug that didn&#39;t make yield_timeslice work under windows, Marco introduced rest, but only as a mean to yield, not for fps.
</p></div></div><p>
sorry Mithrandir I misunderstood you <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Nunca Ese :<br /> <br />I think Evert explained it well : Logic has to run at a constant rate on every computer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamal)</author>
		<pubDate>Tue, 18 Jan 2005 18:19:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Isn&#39;t better to let all the logic update at 60 fps and render at the best speed the computer can? I think human eye can still feel the difference between 60fps and 75fps, so if I can run at 75fps why not?</p></div></div><p>

One reason why not is beat frequencies.  Your example would give an artifact at 15 Hz, which is acceptable IMO, but an extreme example would be logic at 60Hz with display at 61Hz, which in a slow scroll would produce a visible jerkiness at 1Hz intevals.  Other less obvious artifacts  can cause trouble at the epilepsy-inducing frequencies (around 3-7Hz) which are uncomfortable for most viewers.</p><p>The best policy would probably be to allow only certain fixed frequencies for displaying. You could even make it explicit by coding each display freq as follows (this is for the 4:5 relationship of 60 and 75Hz)</p><div class="source-code snippet"><div class="inner"><pre>     loop:update_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  <span class="c">//with vsync</span>
         update_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         update_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
         jmp loop
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Smith)</author>
		<pubDate>Tue, 18 Jan 2005 18:49:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It completely changes the dynamics of the game if what was a fast-paced jump&#39;n run game suddenly runs at half speed. Imagine what would happen if you tried to run an RTS over a network where not all computers have the same speed.<br />It&#39;s more important that the logic runs at a constant rate on all computers (in sofar as that is possible) than that you don&#39;t skip animation frames because otherwise the animation will be less smooth.
</p></div></div><p>

Agree. I was talking about my turn based game and also solo games.<br />When computer is fast enough, both mine and yours works perfectly.<br />When computer is too slow for the fps rate:<br />- mine: skips logic and frames -&gt; slow game = entire timeline rendered<br />- yours: skip frames -&gt; normal game = timeline complete, but only pieces of the timeline shown<br />Think mine option is better for turn based and solo games.</p><div class="quote_container"><div class="title">evert said:</div><div class="quote"><p>
Did you remember to declare speed_counter as volatile?
</p></div></div><p>
could be, dunno.</p><div class="quote_container"><div class="title">evert said:</div><div class="quote"><p>
The problem is that timeslice size changes from so version, so its like rest(variable) IMO.<br />I&#39;m not sure what you mean?
</p></div></div><p>
1st so = os (sorry, spanish SO -&gt; english OS (Operating System))<br />It should read as: &quot;The problem is that timeslice size changes from O.S. version, so its like rest(variable) IMO.&quot; you will rest un uncertain time, you dont know timeslice size and how far you are from the end of the timeslice.</p><div class="quote_container"><div class="title">evert said:</div><div class="quote"><p>
Look at the code. They&#39;re not calling it from the active (meaning logic) loop.<br />The way I do this myself is to run the logic until it is up to date, then draw a frame if that is nescessary, otherwise just yield/rest. 
</p></div></div><p>
Dont see why &#39;active&#39; means &#39;inside logic&#39;, explain please <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 19:45:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I was talking about my turn based game and also solo games.<br />[...]Think mine option is better for turn based and solo games.</p></div></div><p>
It could a viable alternative in those cases. I wouldn&#39;t say it&#39;s better.<br />I&#39;d hate for spiffy special effects to take up too much of my time while playing a game, especially if I know they will not eat so much time when playing on a faster computer.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>you will rest un uncertain time, you dont know timeslice size and how far you are from the end of the timeslice.</p></div></div><p>
If your program is designed well enough, then you shouldn&#39;t care about this. As is said, rest(1) rests on average 1 ms.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Dont see why &#39;active&#39; means &#39;inside logic&#39;, explain please</p></div></div><p>
Nothing to explain, really. That&#39;s just what the docs mean by active.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 18 Jan 2005 20:29:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">evert said:</div><div class="quote"><p>
It could a viable alternative in those cases. I wouldn&#39;t say it&#39;s better.
</p></div></div><p>
Yea, depends on who consumes the time, render or logic, and what you want to show.</p><div class="quote_container"><div class="title">evert said:</div><div class="quote"><p>
you will rest un uncertain time, you dont know timeslice size and how far you are from the end of the timeslice.<br />If your program is designed well enough, then you shouldn&#39;t care about this. As is said, rest(1) rests on average 1 ms.
</p></div></div><p>
Agree. Im using yield_timeslace.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Dont see why &#39;active&#39; means &#39;inside logic&#39;, explain please<br />Nothing to explain, really. That&#39;s just what the docs mean by active. 
</p></div></div><p>
Ok. My mistake then.</p><p>Nice thread by the way! <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Tue, 18 Jan 2005 21:15:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The official guide to the use of Allegro timers.<br /><a href="http://pixwiki.bafsoft.com/wiki/index.php/Timers">Timers</a></p><p>It&#39;s far from complete, please improve it if you have things to add or correct.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trezker)</author>
		<pubDate>Tue, 18 Jan 2005 23:39:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
One reason why not is beat frequencies. Your example would give an artifact at 15 Hz, which is acceptable IMO, but an extreme example would be logic at 60Hz with display at 61Hz, which in a slow scroll would produce a visible jerkiness at 1Hz intevals
</p></div></div><p>
Actually that&#39;s the big problem with hardcoding the ticks per second in the game. Playing a game coded at 60fps on a 85Hz display mode looks very jerky (this is most noticeable in 3D games or 2D games with fast scrolling). One solution is to force display frequency, but not all video drivers allow it, or the user may have disallowed it, so it&#39;s not a perfect solution.</p><p>That&#39;s why most comercial games use a variable timing interval (they check how much time has passed since the last logic, using WinAPI&#39;s GetPerformanceTimer()). I haven&#39;t tried this method yet, but I&#39;ll certainly do. The general algorithm to move an object this way would be something like this:</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>myGame<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>end<span class="k2">)</span></td></tr><tr><td class="number">4</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>    do_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>    draw_stuff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">8</td><td><span class="k2">}</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td><span class="k1">int</span> last_update <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>do_logic<span class="k2">(</span><span class="k2">)</span></td></tr><tr><td class="number">12</td><td><span class="k2">{</span></td></tr><tr><td class="number">13</td><td>  <span class="k1">int</span> cur_time <span class="k3">=</span> CurrentTime<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>  <span class="k1">int</span> ellapsed_time <span class="k3">=</span> cur_time <span class="k3">-</span> last_update<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td>  ball_position_x <span class="k3">+</span><span class="k3">=</span> speed <span class="k3">*</span> ellapsed_time<span class="k2">;</span></td></tr><tr><td class="number">17</td><td>&#160;</td></tr><tr><td class="number">18</td><td>  last_update <span class="k3">=</span> cur_time<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Wed, 19 Jan 2005 00:22:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I suggested rest(1), but probably I should have said rest(0). Right now I think the latter is the correct. IIRC there was a bug somewhere about yield_timeslice() and people suggested me to use rest(), probably with 0, not with 1.</p><p>Using 0, IIRC will release the timeslice, without any wait.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Marco Radaelli)</author>
		<pubDate>Wed, 19 Jan 2005 03:07:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well I&#39;m more confused now.</p><p>For example, to show a circle that change it color per loop.<br />If I want 60 different colors per second, how is the code?.</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">volatile</span> <span class="k1">int</span> timer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td><span class="k1">void</span> timer_handle<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> timer<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span> <span class="k2">}</span> END_FUNCION<span class="k2">(</span>timer_handle<span class="k2">)</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>  <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT,<span class="n">680</span>,<span class="n">400</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>timer_handle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>timer,BPS_TO_TIMER<span class="k2">(</span><span class="n">60</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>  <span class="k1">int</span> color<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">13</td><td>    ???</td></tr><tr><td class="number">14</td><td>    color<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>    <a href="http://www.allegro.cc/manual/circle" target="_blank"><span class="a">circle</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,screen-&gt;w<span class="k3">/</span><span class="n">2</span>,screen-&gt;h<span class="k3">/</span><span class="n">2</span>,<span class="n">50</span>,color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr><tr><td class="number">18</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Wed, 19 Jan 2005 07:41:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">volatile</span> <span class="k1">int</span> timer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td><span class="k1">void</span> timer_handle<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> timer<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span> <span class="k2">}</span> END_FUNCION<span class="k2">(</span>timer_handle<span class="k2">)</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span> <span class="k3">!</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT,<span class="n">680</span>,<span class="n">480</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span> <span class="k3">!</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_298.html" target="_blank">exit</a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>timer_handle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>timer_handle,BPS_TO_TIMER<span class="k2">(</span><span class="n">60</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>  <span class="k1">unsigned</span> <span class="k1">int</span> color <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">13</td><td>    <span class="k1">if</span><span class="k2">(</span>timer<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">14</td><td>      timer--<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>      color<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>      <a href="http://www.allegro.cc/manual/circle" target="_blank"><span class="a">circle</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,screen-&gt;w<span class="k3">/</span><span class="n">2</span>,screen-&gt;h<span class="k3">/</span><span class="n">2</span>,<span class="n">50</span>,color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">18</td><td><span class="k2">}</span></td></tr><tr><td class="number">19</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 19 Jan 2005 07:51:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@ImLeftFooted
</p><div class="source-code snippet"><div class="inner"><pre>...
<span class="k1">if</span><span class="k2">(</span>timer<span class="k2">)</span> <span class="k2">{</span>
timer-- <span class="c">// vs ++ </span>
...
</pre></div></div><p>
<img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">volatile</span> <span class="k1">int</span> timer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td><span class="k1">void</span> timer_handle<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span> timer<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>timer_handle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">3</td><td>&#160;</td></tr><tr><td class="number">4</td><td><span class="p">#define RATE 1</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span><span class="k3">*</span> args<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">7</td><td>  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT,<span class="n">640</span>,<span class="n">480</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// 640x480 , not 680x400 ;)</span></td></tr><tr><td class="number">9</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>timer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>timer_handle<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>  <a href="http://www.allegro.cc/manual/install_int_ex" target="_blank"><span class="a">install_int_ex</span></a><span class="k2">(</span>timer_handle,BPS_TO_TIMER<span class="k2">(</span>RATE<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>  <a href="http://www.allegro.cc/manual/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  <span class="c">// Would key[] work without this?</span></td></tr><tr><td class="number">13</td><td>&#160;</td></tr><tr><td class="number">14</td><td>  <span class="k1">int</span> color <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>    <span class="k1">if</span><span class="k2">(</span>timer <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">17</td><td>      color<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>      <a href="http://www.allegro.cc/manual/circlefill" target="_blank"><span class="a">circlefill</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,screen-&gt;w<span class="k3">/</span><span class="n">2</span>,screen-&gt;h<span class="k3">/</span><span class="n">2</span>,<span class="n">50</span>,color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>      <a href="http://www.allegro.cc/manual/rectfill" target="_blank"><span class="a">rectfill</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">10</span>, <span class="n">10</span>, <span class="n">50</span>, <span class="n">20</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td>      textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>, <span class="s">"Frame %d"</span>, color<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>      timer--<span class="k2">;</span></td></tr><tr><td class="number">22</td><td>    <span class="k2">}</span>    </td></tr><tr><td class="number">23</td><td>    <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">25</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td><span class="k2">}</span></td></tr><tr><td class="number">27</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

This code works (some mistakes on lucaz code). Take it and play with RATE param (set to 60 or whatever u want).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Wed, 19 Jan 2005 08:01:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeesssssssssssssss, I hope Ive understood the idea.<br />When &#39;timer&#39; becomes &lt;0 it inmediatly turns to 60 again?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Wed, 19 Jan 2005 08:07:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">lucaz said:</div><div class="quote"><p>
Yeesssssssssssssss, I hope Ive understood the idea.<br />When &#39;timer&#39; becomes &lt;0 it inmediatly turns to 60 again?
</p></div></div><p>

Not exactly. Timers are events thrown &#39;n&#39; times in a sec. Those &#39;n&#39; times are also known as ticks. What the program is doing is firing 60 ticks per second.<br />Every tick (=every 16,6ms) you call timer++.<br />Every &#39;while&#39; iteration is supposed to run faster than 16,6, so you get timer == 1 every 16ms, process it (thats if(timer) which means if(timer!=0)) and set timer to 0 again (timer--) inside the condition.<br />You wont enter the if(timer) condition again till a new tick is fired (and timer++ is done) -&gt; you will enter it 60 times in a sec.</p><p>change
</p><div class="source-code snippet"><div class="inner"><pre>textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>, <span class="s">"Frame %d"</span>, color<span class="k2">)</span><span class="k2">;</span>
with
textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>, <span class="s">"Timer %d"</span>, timer<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
you will notice that timer is always 1.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Nunca Ese)</author>
		<pubDate>Wed, 19 Jan 2005 08:36:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>thanks a lot people!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Wed, 19 Jan 2005 09:09:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Don&#39;t draw in the logic loop.
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span><span class="k2">(</span>playing<span class="k2">)</span> <span class="k2">{</span>
     <span class="k1">while</span><span class="k2">(</span>timer <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span> <span class="k2">{</span>
            color<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
            timer--<span class="k2">;</span>
      <span class="k2">}</span>      
     <a href="http://www.allegro.cc/manual/circlefill" target="_blank"><span class="a">circlefill</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,screen-&gt;w<span class="k3">/</span><span class="n">2</span>,screen-&gt;h<span class="k3">/</span><span class="n">2</span>,<span class="n">50</span>,color<span class="k2">)</span><span class="k2">;</span>
     <a href="http://www.allegro.cc/manual/rectfill" target="_blank"><span class="a">rectfill</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">10</span>, <span class="n">10</span>, <span class="n">50</span>, <span class="n">20</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
     textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>, <span class="s">"Frame %d"</span>, color<span class="k2">)</span><span class="k2">;</span>
     <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>timer<span class="k2">)</span>
            yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 20 Jan 2005 08:10:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not? :S
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Thu, 20 Jan 2005 11:25:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Because the rendering is usually the most expensive (system-wise) in your program. You want it seperated out so you can skip it if you fall behind (ie. frame skipping). Even you absolutely have to draw for your logic to properly run (which is rare), you still don&#39;t need to draw to the screen (unless you&#39;re using direct dirty rectangles like Allegro&#39;s GUI). It also helps the CPU cache when you access larger buffers, since the CPU wouldn&#39;t have to try to cache the buffer everytime you draw something more after doing a bit more logic. As well, WIndows and X don&#39;t really like mixing drawing to VRAM and using other input functions at the same time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 20 Jan 2005 12:41:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>read this - about timing</p><p><a href="http://www.geisswerks.com/ryan/FAQS/timing.html">http://www.geisswerks.com/ryan/FAQS/timing.html</a></p><p>    Average duration of Sleep(1)<br />    Gemini:   10 ms  (10 calls to Sleep(1) took exactly 100 ms)<br />    Vaio:     ~4 ms  (10 calls to Sleep(1) took 35-45 ms)<br />    HP:       10 ms  (10 calls to Sleep(1) took exactly 100 ms)</p><p>---</p><p>timeBeginPeriod() solves the problem:<br />it lowers the granularity of Sleep() to whatever parameter you give it.<br />So if you&#39;re on windows 2000 and you call timeBeginPeriod(1) and then<br />Sleep(1), it will truly sleep for just 1 millisecond, rather than the <br />default 10!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Fri, 21 Jan 2005 18:49:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>So if you&#39;re on windows 2000 and you call timeBeginPeriod(1) and then Sleep(1), it will truly sleep for just 1 millisecond, rather than the default 10!</p></div></div><p>
I bet it doesn&#39;t do a thing for CPU usage that way either.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 21 Jan 2005 18:54:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thx for this really interesting article.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Fri, 21 Jan 2005 19:39:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Kitty Cat said:</div><div class="quote"><p>
I bet it doesn&#39;t do a thing for CPU usage that way either.
</p></div></div><p>

It does! <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Fri, 21 Jan 2005 20:31:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If it really works it should be added to Allegro...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Fri, 21 Jan 2005 21:02:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, this is one of the best threads I ever read.<br />I love you ALL!!</p><p>My code currently looks like this and it works very good!</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>  <span class="k1">if</span> <span class="k2">(</span>no_skip <span class="k3">=</span><span class="k3">=</span> <span class="n">1</span><span class="k2">)</span> speed_counter <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td>  <span class="k1">while</span><span class="k2">(</span>exityn_game <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">4</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td>    <span class="k1">while</span> <span class="k2">(</span>speed_counter <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">7</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">8</td><td>      input<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>  </td></tr><tr><td class="number">9</td><td>      speed_counter--<span class="k2">;</span></td></tr><tr><td class="number">10</td><td>      fps_counter<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">12</td><td>    draw_map<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>    </td></tr><tr><td class="number">14</td><td>    <span class="c">/* added this line after I read this topic */</span></td></tr><tr><td class="number">15</td><td>    <span class="k1">if</span> <span class="k2">(</span>no_skip <span class="k3">!</span><span class="k3">=</span> <span class="n">1</span><span class="k2">)</span> <span class="k2">{</span><span class="k1">while</span><span class="k2">(</span>speed_counter <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span><a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span><span class="k2">}</span><span class="k2">}</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>  <span class="k2">}</span></td></tr></tbody></table></div></div><p>

THX!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (count)</author>
		<pubDate>Fri, 21 Jan 2005 23:59:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>timeBeginPeriod() solves the problem:<br />it lowers the granularity of Sleep() to whatever parameter you give it.<br />So if you&#39;re on windows 2000 and you call timeBeginPeriod(1) and then<br />Sleep(1), it will truly sleep for just 1 millisecond, rather than the<br />default 10!</p></div></div><p>That&#39;s some interesting information <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />Will definitely keep that in mind.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I bet it doesn&#39;t do a thing for CPU usage that way either.</p></div></div><p>Why wouldn&#39;t it?<br />Normally when there is nothing to update, your CPU uses all of it&#39;s time to check if there is a need to update, and loop back. It does it over and over again, because that&#39;s what the loop tells it to do. Which means millions of &quot;if (x)&quot; checks and looping mechanisms (actual amount depends on the computer). <br />If you&#39;re averaging a wait of 16ms (~60fps) in 1ms increments, you&#39;re only making a maximum 16 &quot;if (x)&quot; checks each logic update. So naturally you&#39;re using far less CPU time.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Sat, 22 Jan 2005 07:07:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I wonder how much confused can I be! <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Sat, 22 Jan 2005 09:05:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>If you&#39;re averaging a wait of 16ms (~60fps) in 1ms increments, you&#39;re only making a maximum 16 &quot;if (x)&quot; checks each logic update. So naturally you&#39;re using far less CPU time.</p></div></div><p>
But something needs to constantly check if that 1ms time period has passed or not. The scheduler granularity in Windows is 10ms, so the process that does the Sleep checking will only run approximately once every 10ms (unless you give it a higher priority, which will cause it to take more CPU). To get more than that, you&#39;d need to busy loop, which doesn&#39;t do anything for CPU usage.</p><p>More than that though, even if you can get more Sleep granularity while still sleeping the CPU, the efficiency of Sleep reducing CPU usage will deteriorate since it will have to return to the program and check if another frame is ready more often, before going back to sleep. It&#39;s like going to bed then waking up every hour to check if 8 hours have passed, or sleeping at least 8 hours and not waking up before you need to. A game with 100 logic frames per second (or less) will do just fine with a Sleep granularity of ~10ms.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Sat, 22 Jan 2005 09:14:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have done some experiments with timeBeginPeriod() and rest() and such. First, thanks to all for these really interesting infos. </p><p>So now I can understand my measurements much better, which relate internal CPU counter and the CPU usage indicated in the task manager. Now, using timeBeginPeriod(1) in the start of my program changes the confusing behaviour I saw before into some understandable behaviour now. </p><p>Well, I was forced to include winmm.dll and stuff into my main program, so I tried to move timeBeginPeriod() into the implementation of rest(). That was a good idea! Works perfectly well for my program, and also better than changing the main program only. </p><p>So how can I propose this change for consideration for the next allegro change? </p><p>Next I have again analysed the behaviour of dlg, which uses 100% CPU on doing nothing, but not all the time. I think I should write the results in an extra thread, but again I have two little changes to propose here, one for allegro and one for dlg. How do I do that?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Mon, 24 Jan 2005 14:26:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>So how can I propose this change for consideration for the next allegro change?</p></div></div><p>
You can send a patch (or post manually how the code should be changed) on the mailinglist, this forum, or through private message/email to one of the developers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Mon, 24 Jan 2005 14:38:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks. I think I&#39;ll post the instructions how to change based on 4.1.17 into a new thread this evening. Same for dlg.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Mon, 24 Jan 2005 14:48:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This may be useful too</p><p><a href="http://www.bitbanksoftware.com/Programmers/code4.html">http://www.bitbanksoftware.com/Programmers/code4.html</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Mon, 24 Jan 2005 19:18:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That look a bit similar to what I&#39;m doing in my game loop (which I posted in some thread last week, forgot which it was). Essentially I call rest(1) if I have some time to waste. As for the fps, I only want about 60fps (or another number, as this is configurable), I don&#39;t care for the very exact number here as long as it is about the requested number in the long run.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Mon, 24 Jan 2005 19:30:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Have you tested timeBeginPeriod() in your gameloop?<br />FPS should get more accurate, but what about the CPU time (better or worse)? </p><p>Or are you using Linux?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Mon, 24 Jan 2005 20:58:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m using Windows XP, and the fps got more accurate. Actually, that&#39;s not the whole truth. The code is using timers to control the loop. One timer is at 60/sec, one is at 1/sec to count the actual frame rate, and I use a third timer at 10000/sec to measure times of code sections at 0.1ms precision. The point was: If I use rest(1) within my game loop to give time back when I don&#39;t need that time for my current frame, I got different measures between my internal measurement and the task manager. Now that&#39;s pretty close, and overall cpu usage is reduced. Some secrets remain, though. Maybe I should report these in a separate thread, so you could try my latest executable (which is not online yet).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Mon, 24 Jan 2005 21:20:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The code is using timers to control the loop.
</p></div></div><p>

Are you using Allegro&#39;s Timers? Because they are not accurate, you schould use QueryPerformanceCounter() to get exactly the fps you want. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>[edit]<br />and use the highest resolution possible - in ticks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 17:29:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, I&#39;m using allegro&#39;s timers. I think that using timeBeginPeriod() and timeEndPeriod() also might increase precision of those timers. </p><p>Well, I might use any Windows specific functions, but that would mean I decide to not port the game to any other platform in future. Somehow I hesitate to make such a decision, so I would really prefer to stick to allegro&#39;s features.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 25 Jan 2005 17:45:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Yes, I&#39;m using allegro&#39;s timers. I think that using timeBeginPeriod() and timeEndPeriod() also might increase precision of those timers. 
</p></div></div><p>
could be, <br />but resolution of 1ms isn&#39;t good enough, if you want fixed FPS and give CPU time back.</p><p>e.g.</p><p>You want to run your game at 60 FPS<br />Then one Frame schould take 16.6667 ms.</p><p>With allegro&#39;s timers you can get 16 or 17 (if timeBeginPeriod() increase precision on that timer)</p><p>if 16 then your FPS will be at 62.5<br />if 17 then your FPS will be at 58.8</p><p>The higher the FPS the less accurate the result.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well, I might use any Windows specific functions, but that would mean I decide to not port the game to any other platform in future. Somehow I hesitate to make such a decision, so I would really prefer to stick to allegro&#39;s features.
</p></div></div><p>

I would write an timer class and use #ifdef _WIN32 for QueryPerformanceCounter() and timeBeginPeriod()<br />and #else for Linux. It has an High Resolution Timer too, something like getdaytime()?! <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 18:10:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Actually I&#39;m using allegro timers which call a function incrementing a variable at a given speed. I don&#39;t know that these are unprecise the way you suggest here, instead I found these timers quite reliable. So I have Timers working in a separate thread (I assume) and my game loop just looks at the values of the variables being incremented by these timers. So I can show the next frame when the variable becomes nonzero. Maybe my frames are not precisely 16.66666...ms apart, but in average they are, because some take 16ms where others would take 17ms.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 25 Jan 2005 18:16:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK +- 2 FPS is not that big deal for most games. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /><br />How do you move objects? Is motion smooth? Do you use delta_time?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 18:26:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Cutrrently I&#39;m not moving objects smoothly. Internally I distinuish between physical and logical time, because I want to be able to adjust the game speed in a wide range. So essentially this means that I put several logical frames into a physical frame (when game speed is high), and I can skip displaying physical frames also to avoid slowdown. </p><p>For later versions I want to implement smooth movement also, but currently I&#39;m working on my A-Train clone (see depot) which did not have smooth movement in original, so I decided to do it the same way for the first version. Also I don&#39;t have animated sprites or tiles yet, that&#39;s also planned for the future.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 25 Jan 2005 18:31:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What I am doing is:</p><p>I have a FPS (Frames Per Second) set to maximum 62 -&gt; rest goes to CPU<br />and I have a LPS (Logic(Frames) Per Second) set to exactly 100</p><p>The FPS can drop as low as 1 Frame Per Second, but the Logic Per Second stays always at 100 (it takes 2 ms on my computer, while rendering takes ~9-11ms). I can spent ~3-4 ms pro Frame to the CPU, which is ~25% (depends on CPU).</p><p>I have a counter (64Bit) that counts the ticks since program start. <br />1 Tick  = ~8.500.000 ms (If I remember correctly)</p><p>I do not use ms to calculate my movement, but ticks.</p><p>For that I have 2 helper function<br />long long ms2ticks(float ms); <br />float ticks2ms(long long ticks);</p><p>This way movement gets very precise on fast an also slow systems.</p><p>BTW the Logic is fixed to 100 updates because of my network code, which results in better ping times. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 18:46:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sounds good. </p><p>Somehow I get the impression here, that almost everybody starting to program a game re-invents the game loop again and again. It might be a good idea to create a small collection of &#39;good&#39; or &#39;state-of-the-art&#39; game loops somewhere on this site, what do you think? </p><p>Anyway, I would like to see your game loop to learn how you do these things...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 25 Jan 2005 19:00:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Somehow I get the impression here, that almost everybody starting to program a game re-invents the game loop again and again. It might be a good idea to create a small collection of &#39;good&#39; or &#39;state-of-the-art&#39; game loops somewhere on this site, what do you think? 
</p></div></div><p>

this is a good idea<br />1) easy game loops - like the one in the faq - but not that accurate<br />   can have more than one timer and use those for easy animation<br />2) synchron (fps = lps) needs delta or fixed speed<br />3) asynchron (fps &lt;&gt; lps) uses only one timer - <br />   2 kinds of movement: pixels/logic or pixel/time</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Anyway, I would like to see your game loop to learn how you do these things...
</p></div></div><p>
OK, kann ich später posten (da ich grad im Büro bin). Muss erst ein bissl suchen, da ich lange nicht mehr an dem Projekt gearbeitet habe, kaum Zeit gehabt... <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 19:11:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Kein Problem. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /> und Dank im Voraus! </p><p>Edit: </p><p>The right place to collect game loops would be the Resources -&gt; Code Gallery, which by now only has 3 entries.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Tue, 25 Jan 2005 19:20:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, I found a gameloop, but this one is without my c++ timer class.</p><p>m2t() converts ms to ticks<br />t2m() converts ticks to ms</p><p>This was written a while ago - next step is port all to c++ and make code cleaner... </p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">void</span> main_loop<span class="k2">(</span><span class="k1">void</span><span class="k2">)</span></td></tr><tr><td class="number">2</td><td><span class="k2">{</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">long</span> <span class="k1">long</span> logic_delta<span class="k2">;</span></td></tr><tr><td class="number">4</td><td>  <span class="k1">long</span> <span class="k1">long</span> render_delta<span class="k2">;</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td>  <span class="k1">long</span> <span class="k1">long</span> logic_wait <span class="k3">=</span> timer_ticks<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> m2t<span class="k2">(</span><span class="n">1000</span><span class="k3">/</span>LPS_wanted<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <span class="k1">long</span> <span class="k1">long</span> render_wait <span class="k3">=</span> timer_ticks<span class="k2">(</span><span class="k2">)</span> <span class="k3">+</span> m2t<span class="k2">(</span><span class="n">1000</span><span class="k3">/</span>FPS_wanted<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>  <span class="c">// game loop</span></td></tr><tr><td class="number">10</td><td>  <span class="k1">while</span> <span class="k2">(</span>isLoop<span class="k2">)</span></td></tr><tr><td class="number">11</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>    <span class="c">// logic skip</span></td></tr><tr><td class="number">13</td><td>    logic_delta <span class="k3">=</span> logic_wait <span class="k3">-</span> timer_ticks<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>    <span class="k1">if</span> <span class="k2">(</span>logic_delta <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">15</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>      <span class="c">// calculate new logic_wait  </span></td></tr><tr><td class="number">17</td><td>      logic_wait <span class="k3">+</span><span class="k3">=</span> m2t<span class="k2">(</span><span class="n">1000</span><span class="k3">/</span>LPS_wanted<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td>      <span class="c">// update lps and gtpl</span></td></tr><tr><td class="number">20</td><td>      timer_update<span class="k2">(</span>LPS<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>&#160;</td></tr><tr><td class="number">22</td><td>      <span class="c">// update logic</span></td></tr><tr><td class="number">23</td><td>      main_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">25</td><td>&#160;</td></tr><tr><td class="number">26</td><td>    <span class="c">// render skip</span></td></tr><tr><td class="number">27</td><td>    render_delta <span class="k3">=</span> render_wait <span class="k3">-</span> timer_ticks<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>    <span class="k1">if</span> <span class="k2">(</span>render_delta <span class="k3">&lt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">29</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">30</td><td>      <span class="c">// calculate new render_wait  </span></td></tr><tr><td class="number">31</td><td>      render_wait <span class="k3">+</span><span class="k3">=</span> m2t<span class="k2">(</span><span class="n">1000</span><span class="k3">/</span>FPS_wanted<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>&#160;</td></tr><tr><td class="number">33</td><td>      <span class="c">// update fps </span></td></tr><tr><td class="number">34</td><td>      timer_update<span class="k2">(</span>FPS<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>&#160;</td></tr><tr><td class="number">36</td><td>      <span class="c">// render frame</span></td></tr><tr><td class="number">37</td><td>      main_render<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">38</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">39</td><td>&#160;</td></tr><tr><td class="number">40</td><td>    <span class="c">// give os rest of cpu power &amp; save battery</span></td></tr><tr><td class="number">41</td><td>    <span class="k1">if</span> <span class="k2">(</span>t2m<span class="k2">(</span>min<span class="k2">(</span>logic_delta, render_delta<span class="k2">)</span><span class="k2">)</span> <span class="k3">&gt;</span> <span class="n">1</span><span class="k2">)</span></td></tr><tr><td class="number">42</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">43</td><td>      <span class="c">// update sleep</span></td></tr><tr><td class="number">44</td><td>      timer_update<span class="k2">(</span>SLEEP<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">45</td><td>      </td></tr><tr><td class="number">46</td><td>      <span class="c">// CPU Sleep for 1 msec</span></td></tr><tr><td class="number">47</td><td>      Sleep<span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">48</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">49</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">50</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Tue, 25 Jan 2005 22:13:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>I have a FPS (Frames Per Second) set to maximum 62 -&gt; rest goes to CPU<br />and I have a LPS (Logic(Frames) Per Second) set to exactly 100</p></div></div><p>
Why are these different? Your logic rate should be the same as your max FPS (one logic update per frame, with frame dropping), or keep them in sync (FPS = LPS) using time deltas.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Wed, 26 Jan 2005 05:57:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I want the network code (which is in a logic frame) highly responsive and I am scared of using threads. <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><p>Using 100 Logic updates gives me way better results than 62 logic updates. But I don&#39;t want to render more often than 62/60 because it&#39;s a waste of CPU time. Ping is also getting better by using rest() / Sleep() , because RakNet is running in threads. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gering)</author>
		<pubDate>Wed, 26 Jan 2005 06:08:13 +0000</pubDate>
	</item>
</rss>
