<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Is there anything wrong with this type of game loop?</title>
		<link>http://www.allegro.cc/forums/view/612378</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 13 Apr 2013 14:03:22 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My game loop has always been one timer for logic, and one timer for drawing.<br />But I just read this:</p><div class="quote_container"><div class="title">SiegeLord said:</div><div class="quote"><p>
</p><div class="quote_container"><div class="title">adamk kromm said:</div><div class="quote"><p> <br />The way I understand it is you set up a bunch of timers. One for rendering, one for updating the logic, and one/some for getting input.
</p></div></div><p>
This seems insane, I don&#39;t know if anybody really does it like that. The event driven loops generally seen for A5 involve a single timer and are fundamentally the same to the game loop you posted. Once you add interpolation, they become practically identical:
</p></div></div><p>

Thread: <a href="https://www.allegro.cc/forums/thread/612179">https://www.allegro.cc/forums/thread/612179</a></p><p>The thread is locked so I couldn&#39;t reply there, but why is using one timer for logic and one for drawing &quot;insane&quot;? Am I misunderstanding SiegeLord?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Mon, 08 Apr 2013 04:08:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The thing is you really only need a single timer to track total elapsed time, then run your game logic and rendering independetly off of that in a single loop. What I basically do is:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">do</span> <span class="k2">{</span>
    <span class="c">// 1. Update timer and calculate how many logic frames to process</span>
    <span class="c">// 2. Handle All Allegro Events in Queue</span>
    <span class="k1">while</span> <span class="k2">(</span>logicframes <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span>
    <span class="k2">{</span>
        <span class="c">// 3a. Process Keyboard/Mouse/Joystick I/O</span>
        <span class="c">// 3b. Process x Frames of Game Logic (Based on time passed)</span>
        logicframes--<span class="k2">;</span>
    <span class="k2">}</span>
    <span class="c">// 4. Interpolate Object Positions for Rendering</span>
    <span class="c">// 5. VSync and Render Frame</span>
<span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>done<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Mon, 08 Apr 2013 10:51:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;d say it&#39;s insane because the drawing timer seems completely unnecessary. There are two things that are of interest in the game loop as far as the drawing goes.  First, you don&#39;t want to draw too often: if you have interpolation, drawing more often than the refresh rate of the monitor is wasteful; if you don&#39;t have interpolation, then drawing multiple identical frames is wasteful (i.e. drawing two frames in a row without a timer event between them). Second, you want to somewhat align your drawing rate to the refresh rate (to prevent tearing and it&#39;s useful for interpolation). Both are admirably well served by the combination of a logic timer and some vsync control.</p><p>The two reasons I can think of having a separate timer for drawing, are 1) you want to draw consistently slower than refresh rate; 2) you are trying to dogmatically use events for everything.</p><p>I really don&#39;t see the reasoning behind #1. If the idea is to provide a better user experience by keeping the FPS consistent, then I think interpolation will provide a better solution than artificially limiting the FPS. If interpolation is not an option, I&#39;d explicitly drop frames instead of using a separate timer.</p><p>#2 is where the insanity starts to creep in. The reality of providing graphical output in a videogame is that there is an external hardware &quot;timer&quot; (the temporal sequence of monitor refreshes) that governs when you should draw. The way you &quot;listen&quot; to this event is by using a vsync&#39;ing mechanism. To ignore that reality and to create an entirely unrelated source of events (a drawing timer) is nonsensical.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 08 Apr 2013 11:59:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s not insane. There are good reasons to limit drawing to less than refresh rate, such as conserving batteries on portables and keeping laptops from heating up or desktop fans from spinning madly. That&#39;s why I use two timers with configurable draw rate.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Mon, 08 Apr 2013 12:11:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok. I&#39;d still do it by explicitly dropping frames instead of adding the complexity of an additional timer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 08 Apr 2013 12:41:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Uh, wouldn&#39;t an handheld device consume more batteries for keeping the screen lit, instead of computing drawings, I mean in at least one order of magnitude difference.</p><p>I think drawing at a slower pace while the screen is still lit would consume roughly the same amount of energy as when actively refreshing the screen....if not for the calculations on the GPU and so on...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Mon, 08 Apr 2013 19:04:02 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>No, it definitely makes a big difference...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Mon, 08 Apr 2013 21:28:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Would this be the Money Drop I&#39;d be losing my million against it no doubt, back in the days the main concern of my fellow customers was mostly to reduce power consumption modulating the screnn backlight usage <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (pkrcel)</author>
		<pubDate>Tue, 09 Apr 2013 01:17:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So what&#39;s actually the best way to implement a game loop? I&#39;ve read the previous thread, I&#39;ve read this thread, I&#39;ve read various A5 tutorials. Which is the best way?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (axilmar)</author>
		<pubDate>Wed, 10 Apr 2013 21:06:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t think there&#39;s 1 best way. There are a few variations that are pretty much the same. I think as long as you use al_wait_for_event and not poll the event queue primarily, it&#39;s good.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Wed, 10 Apr 2013 21:16:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612378/980430#target">axilmar</a> said:</div><div class="quote"><p> Which is the best way?</p></div></div><p>You need to state what you need out of it first. Depending on what you need, you will get different loops. The only critical A5 specific bit is that you need to be careful to drain the event queue and not pick off one event per frame. Otherwise anything goes, pretty much. I, personally, kind of like the latest game loop I came up with (it&#39;s in the thread linked to in the OP... reproduced below):</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">const</span> <span class="k1">int</span> MAX_FRAMESKIP <span class="k3">=</span> <span class="n">5</span><span class="k2">;</span>
<span class="number">  2</span><span class="k1">const</span> <span class="k1">float</span> FPS <span class="k3">=</span> <span class="n">25</span><span class="k2">;</span>
<span class="number">  3</span><span class="k1">const</span> <span class="k1">float</span> dt <span class="k3">=</span> <span class="n">1</span>.<span class="n">0</span> <span class="k3">/</span> FPS<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span><a href="http://www.allegro.cc/manual/ALLEGRO_TIMER"><span class="a">ALLEGRO_TIMER</span></a><span class="k3">*</span> t <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_create_timer"><span class="a">al_create_timer</span></a><span class="k2">(</span>dt<span class="k2">)</span><span class="k2">;</span>
<span class="number">  6</span><a href="http://www.allegro.cc/manual/al_register_event_source"><span class="a">al_register_event_source</span></a><span class="k2">(</span>q, <a href="http://www.allegro.cc/manual/al_get_timer_event_source"><span class="a">al_get_timer_event_source</span></a><span class="k2">(</span>t<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span><a href="http://www.allegro.cc/manual/al_start_timer"><span class="a">al_start_timer</span></a><span class="k2">(</span>t<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span><span class="k1">float</span> game_time <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 10</span><span class="k1">float</span> offset <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>
<span class="number"> 12</span><span class="k1">bool</span> game_is_running <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span><span class="k1">while</span><span class="k2">(</span>game_is_running<span class="k2">)</span>
<span class="number"> 15</span><span class="k2">{</span>
<span class="number"> 16</span>  <span class="k1">int</span> loops <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 17</span>  <a href="http://www.allegro.cc/manual/ALLEGRO_EVENT"><span class="a">ALLEGRO_EVENT</span></a> e<span class="k2">;</span>
<span class="number"> 18</span>
<span class="number"> 19</span>  <span class="k1">while</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_next_event"><span class="a">al_get_next_event</span></a><span class="k2">(</span>q, <span class="k3">&amp;</span>e<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 20</span>  <span class="k2">{</span>
<span class="number"> 21</span>    <span class="k1">if</span><span class="k2">(</span>e.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_TIMER<span class="k2">)</span>
<span class="number"> 22</span>    <span class="k2">{</span>
<span class="number"> 23</span>      game_time <span class="k3">=</span> e.timer.count <span class="k3">*</span> dt<span class="k2">;</span>
<span class="number"> 24</span>      update_game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 25</span>      loops<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="number"> 26</span>
<span class="number"> 27</span>      <span class="k1">if</span><span class="k2">(</span>loops <span class="k3">&gt;</span><span class="k3">=</span> MAX_FRAMESKIP<span class="k2">)</span>
<span class="number"> 28</span>        <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 29</span>    <span class="k2">}</span>
<span class="number"> 30</span>  <span class="k2">}</span>
<span class="number"> 31</span>
<span class="number"> 32</span>  <span class="k1">float</span> interpolation <span class="k3">=</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/al_get_time"><span class="a">al_get_time</span></a><span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> offset <span class="k3">-</span> game_time<span class="k2">)</span> <span class="k3">/</span> dt<span class="k2">;</span>
<span class="number"> 33</span>  display_game<span class="k2">(</span>interpolation<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 34</span><span class="k2">}</span>
</div></div><p>

The frameskip bit is optional, as is interpolation. The critical bit here is that the FPS is limited by the vsync (which is (still!) a tiny bit buggy on A5) instead of using <span class="source-code"><a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a></span> like in the more classical A5 game loops. You can also add a mechanism to skip frames if you want to keep the FPS lower than the refresh rate.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Wed, 10 Apr 2013 23:33:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Siege&#39;s loop format is actually very similar to mine, except I don&#39;t use Allegro for timing as I have my own timing system in place, so I process Allegro events first, then handle game logic.</p><p>...I also just noticed some errors in how I explained my loop format. *goes to fix them*</p><p>...meh, it won&#39;t let me because of the age of the post, so I&#39;ll just repost my pseudo-code with the fixes applied:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">do</span> <span class="k2">{</span>
    <span class="c">// 1. Update timer and calculate how many logic frames to process (if any)</span>
    <span class="c">// 2. Handle All Allegro Events in Queue</span>
    <span class="k1">while</span> <span class="k2">(</span>logicframes <span class="k3">&gt;</span> <span class="n">0</span><span class="k2">)</span>
    <span class="k2">{</span>
        <span class="c">// 3a. Process Keyboard/Mouse/Joystick I/O</span>
        <span class="c">// 3b. Process 1 frame of Game Logic</span>
        logicframes--<span class="k2">;</span>
    <span class="k2">}</span>
    <span class="c">// 4. Interpolate Object Positions for Rendering</span>
    <span class="c">// 5. VSync and Render Frame</span>
    Sleep<span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">;</span> <span class="c">// For sake of Windows multi-tasking</span>
<span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span><span class="k3">!</span>done<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Thu, 11 Apr 2013 01:01:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you for all the examples and the explanations. </p><p>I am currently making a physics engine for my high school physics exam, and it is also the first time I&#39;ve really tried to use time steps and I&#39;m astonished. It has made all collision detection so much better, and the movement of objects is so much smoother. I think I&#39;ll be using one timer from now on, and two timers in portables.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnamra)</author>
		<pubDate>Sat, 13 Apr 2013 14:03:22 +0000</pubDate>
	</item>
</rss>
