<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Jaggy/irregular Animation and main loop</title>
		<link>http://www.allegro.cc/forums/view/588783</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 29 Nov 2006 06:29:40 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello,</p><p>I need some advice on smooth animation. I tried two rendering techniques (double buffering and page flipping) and neither of them turned out to be smooth enough, even if I use vsync, the animation seems to be kind of &quot;jaggy&quot;.</p><p>I am using the following approach to a main game loop:</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> LogicTimer<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="c">//Timer for game logic</span></td></tr><tr><td class="number">4</td><td>  pending_cycles<span class="k3">+</span><span class="k3">+</span><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><a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>pnd_LogicTimer<span class="k2">)</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="k1">void</span> FramesTimer<span class="k2">(</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>  pending_frames<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><a href="http://www.allegro.cc/manual/END_OF_FUNCTION" target="_blank"><span class="a">END_OF_FUNCTION</span></a><span class="k2">(</span>pnd_FramesTimer<span class="k2">)</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> main<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><span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>game_over<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="k1">if</span> <span class="k2">(</span>blit_buffer<span class="k2">)</span> <span class="c">//Has the screen buffer changed, does it need to be blit?</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">20</td><td>    <span class="c">//Page flipping</span></td></tr><tr><td class="number">21</td><td>    <a href="http://www.allegro.cc/manual/show_video_bitmap" target="_blank"><span class="a">show_video_bitmap</span></a><span class="k2">(</span>screen_buffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>&#160;</td></tr><tr><td class="number">23</td><td>    <span class="k1">if</span> <span class="k2">(</span>screen_buffer <span class="k3">=</span><span class="k3">=</span> page1<span class="k2">)</span></td></tr><tr><td class="number">24</td><td>      screen_buffer <span class="k3">=</span> page2<span class="k2">;</span></td></tr><tr><td class="number">25</td><td>    <span class="k1">else</span></td></tr><tr><td class="number">26</td><td>      screen_buffer <span class="k3">=</span> page1<span class="k2">;</span></td></tr><tr><td class="number">27</td><td>      </td></tr><tr><td class="number">28</td><td>    <a href="http://www.allegro.cc/manual/clear_to_color" target="_blank"><span class="a">clear_to_color</span></a><span class="k2">(</span>screen_buffer, black<span class="k2">)</span><span class="k2">;</span> <span class="c">//Reset memory bitmap to blackness.</span></td></tr><tr><td class="number">29</td><td>    blit_buffer <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">31</td><td>&#160;</td></tr><tr><td class="number">32</td><td>  PollEvents<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">//User input polling</span></td></tr><tr><td class="number">33</td><td>&#160;</td></tr><tr><td class="number">34</td><td>  <span class="c">//Logic</span></td></tr><tr><td class="number">35</td><td>  <span class="k1">while</span> <span class="k2">(</span>pending_cycles <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">36</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">37</td><td>    compute_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">38</td><td>    pending_cycles--<span class="k2">;</span></td></tr><tr><td class="number">39</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">40</td><td>&#160;</td></tr><tr><td class="number">41</td><td>  <span class="c">//Rendering</span></td></tr><tr><td class="number">42</td><td>  <span class="k1">if</span> <span class="k2">(</span>pending_frames <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">43</td><td>        <span class="k2">{</span></td></tr><tr><td class="number">44</td><td>    render_stuff_to_screen_buffer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">45</td><td>    pending_frames--<span class="k2">;</span></td></tr><tr><td class="number">46</td><td>    blit_buffer <span class="k3">=</span> <span class="n">1</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="c">//rest(1);</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>

As you can see, I am using two different timers to do logic calculations and rendering stuff. That&#39;s because I want to be able to modify game speed and (desired) framerate seperately from each other.</p><p>I have put blitting functions on top of the loop, because I might want to be able to rest() at the end of the loop, handing over some CPU time to other applications. This rest() call being a waste of time for my game, I think it&#39;s most efficient to calculate all the game logic between blit() and rest().</p><p>When I use a much simpler approach without timers, like in the Allegro example, animation is smooth.</p><p>I am working under Windows where Allegro timers internally make use of threads, I guess. I am now suspecting that my two timers are interfering with each other in some way or that the timing code in Allegro is not very accurate.</p><p>Thanks for any suggestions.</p><p>Best regards,<br />Christoph
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Locutus266)</author>
		<pubDate>Tue, 28 Nov 2006 22:40:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your objects probably have velocities that make them move, right?<br />It might be the integration technique that you are using that makes the animation jaggy.<br />I&#39;m working under Windows too and don&#39;t recall having any trouble with the timers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mr. Big)</author>
		<pubDate>Tue, 28 Nov 2006 23:59:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>what speed are you setting your timers ? <br />Maybe if logic timer is faster than draw timer then animation can be irregular,<br />but not sure because I just use one timer in my main loop.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamal)</author>
		<pubDate>Wed, 29 Nov 2006 03:42:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The logic timer is triggered about 250 times per sec, while the frame timer is normally at 60 times per sec. I tried to remove the frame timer for testing purposes and it didn&#39;t really change anything. I suppose the positional calculations themselves are already &quot;jaggy&quot;.</p><p>I am using floating point values as speed settings (and for screen coordinates). My objects normally have speeds between 0.0f and 1.0f, so that they never move more than one pixel at a time. They are not allowed to skip any pixels, because collision detection depends on it. Else, I&#39;d have to work with vectors and that&#39;s complicated I guess. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Best regards and thanks so far,<br />Christoph
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Locutus266)</author>
		<pubDate>Wed, 29 Nov 2006 05:02:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Allegro timers sucks on windows, use a more accurate timer instead.<br />Look at <a href="http://www.reflectedgames.com/create.html">Phipps&#39;s articles</a>, he uses a better timer with double buffering and page flipping.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Paul whoknows)</author>
		<pubDate>Wed, 29 Nov 2006 06:25:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In addition to what&#39;s been mentioned so far, under Windows I get frequent &quot;blips&quot; in animation unless I set the process to run under a higher priority. BTW, does anyone know if there is a way for the program itself to set it&#39;s priority under Windows?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 29 Nov 2006 06:29:40 +0000</pubDate>
	</item>
</rss>
