<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>FPS-Locking with timer</title>
		<link>http://www.allegro.cc/forums/view/590940</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 11 Apr 2007 19:01:43 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi all!</p><p>I&#39;ve a problem with a simple Pong program:</p><p>In my main loop I work with like it is told at <a href="http://www.allegro.cc/manual/miscellaneous/frequently-asked-questions-(faq)/general-problems/bd35ed0706dad8b5c3aa3b3a91cdba2c">http://www.allegro.cc/manual/miscellaneous/frequently-asked-questions-(faq)/general-problems/bd35ed0706dad8b5c3aa3b3a91cdba2c</a>
</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">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">2</td><td>  <span class="k2">{</span>    </td></tr><tr><td class="number">3</td><td>    iSkip<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>    <span class="k1">while</span><span class="k2">(</span>speed_counter&gt;0<span class="k2">)</span></td></tr><tr><td class="number">5</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">6</td><td>      <span class="k1">if</span> <span class="k2">(</span>getKeyState<span class="k2">(</span>KEY_ESC<span class="k2">)</span><span class="k3">=</span><span class="k3">=</span><span class="n">1</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>        bEnd<span class="k3">=</span><span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">10</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_LEFT<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>        psBar-&gt;vMove<span class="k2">(</span>psBar-&gt;iGetSpeed<span class="k2">(</span><span class="k2">)</span><span class="k3">*</span><span class="k3">-</span><span class="n">1</span>,psDisplay<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="k1">else</span> <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_RIGHT<span class="k2">]</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>        psBar-&gt;vMove<span class="k2">(</span>psBar-&gt;iGetSpeed<span class="k2">(</span><span class="k2">)</span>,psDisplay<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>      psBall-&gt;vMove<span class="k2">(</span>psDisplay, psBar<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>&#160;</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>    <a href="http://www.allegro.cc/manual/acquire_bitmap" target="_blank"><span class="a">acquire_bitmap</span></a><span class="k2">(</span>psDisplay-&gt;m_pBSArena<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>    psBalr-&gt;vDelete<span class="k2">(</span>psDisplay-&gt;m_pBSArena,psDisplay-&gt;m_pBSArena_bkgnd<span class="k2">)</span><span class="k2">;</span>  </td></tr><tr><td class="number">24</td><td>    psBalr-&gt;vDraw<span class="k2">(</span>psDisplay-&gt;m_pBSArena<span class="k2">)</span><span class="k2">;</span>        </td></tr><tr><td class="number">25</td><td>    psBall-&gt;vDelete<span class="k2">(</span>psDisplay-&gt;m_pBSArena,psDisplay-&gt;m_pBSArena_bkgnd<span class="k2">)</span><span class="k2">;</span>      </td></tr><tr><td class="number">26</td><td>    psBall-&gt;vDraw<span class="k2">(</span>psDisplay-&gt;m_pBSArena<span class="k2">)</span><span class="k2">;</span>    </td></tr><tr><td class="number">27</td><td>    <a href="http://www.allegro.cc/manual/release_bitmap" target="_blank"><span class="a">release_bitmap</span></a><span class="k2">(</span>psDisplay-&gt;m_pBSArena<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>    <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>psDisplay-&gt;m_pBSArena,<a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,psDisplay-&gt;m_pBSArena-&gt;w,psDisplay-&gt;m_pBSArena-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>    <span class="k1">if</span> <span class="k2">(</span>bEnd<span class="k3">=</span><span class="k3">=</span><span class="k1">true</span><span class="k2">)</span></td></tr><tr><td class="number">30</td><td>      <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>  <span class="k2">}</span></td></tr></tbody></table></div></div><p>

My problem is, that the ball is jumping around the screen because there are many, many passes of the loop before the speed_counter is 0 and so the ball moves from (x,y) in one frame to (x+20,y+20) in the next frame. (Doesn&#39;t look very fine). <br />How have you solved such problems? </p><p>One idea is that I make a new loop. This loop match one second (With a framerate of 80 fps this loop will be executed 80 times a second) . So I need a timer that can tell me the durration of the loop (from the beginning to the end of drawing). After drawing I can then make a rest() of (1000/rramerate - duration of the loop). </p><p>Is this a good idea? - How do I make such a timer?</p><p>THX poschi
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (poschma)</author>
		<pubDate>Tue, 10 Apr 2007 16:04:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is the most confusing thread I&#39;ve read in a while, but that might just be me.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
there are many, many passes of the loop before the speed_counter is 0
</p></div></div><p>

You&#39;ve got something making expensive rendering calls, then. So when control goes back to the logic bit, it has to make up all those lost logic frames in one visual frame.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
This loop match one second
</p></div></div><p>

Wait, what? Does that mean you are moving it in one go of the while loop the amount that you want it to move in one second? Because if so, you need to divide it by your FPS.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So I need a timer that can tell me the durration of the loop (from the beginning to the end of drawing).
</p></div></div><p>

You can either hack an Allegro timer to do it, probably resulting in inaccurate data, or you can use <a href="http://www.cppreference.com/stddate/clock.html">Clock()</a> or similar. Just take the difference between the return value before and after.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
After drawing I can then make a rest() of (1000/rramerate - duration of the loop).
</p></div></div><p>

You can, but I don&#39;t know why you would. That&#39;ll just put you further behind.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Tue, 10 Apr 2007 16:59:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sorry confusing you!<br />Ok, I understand why you are so confused. I was thinking in the wrong direction. My problem has nothing todo with the timer. They work exactly like they should. I insertet a debug routine and the number of loop passes was exactly my frame rate of 80 FPS. Still visually it looks like the ball is sometimes faster a little bit than other times.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (poschma)</author>
		<pubDate>Wed, 11 Apr 2007 10:16:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you are rendering at 80 FPS, you can try enabling vsync. Shearing due to not using vsync may make the animation appear less smooth, although enabling vsync may make it look like it &quot;stutters&quot; if it causes the framerate to shift rapidly. If you have an LCD monitor, you could be having problems with response time. I know breakout style games like Sunny Ball can be almost unplayable at the faster/harder skill levels because the ball smears on an LCD screen. I thought I bought a fast monitor at 15ms (previously all &quot;normal&quot; LCDs were 20-25ms), but playing Sunny Ball on a CRT was a TON better as the frame rate could be more than 60, but mostly because of no smearing. If this is what&#39;s causing the ball animation to not look good for you, there&#39;s nothing you can do.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Wed, 11 Apr 2007 19:01:43 +0000</pubDate>
	</item>
</rss>
