<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Difference between my time loop and Allegro</title>
		<link>http://www.allegro.cc/forums/view/617364</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 19 Apr 2018 22:50:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I created two time loops, one mine using deltaTime and one with Allegro event timer.<br />The first one looks something like this:
</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>timeFrameCurrent <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">  2</span>deltaTime <span class="k3">=</span> timeFrameCurrent <span class="k3">-</span> timeFrameLast<span class="k2">;</span>
<span class="number">  3</span>p_time-&gt;OnDeltaTime<span class="k2">(</span>deltaTime<span class="k2">)</span><span class="k2">;</span>
<span class="number">  4</span>...
<span class="number">  5</span><span class="k1">if</span><span class="k2">(</span><span class="k3">!</span>al_event_queue_is_empty<span class="k2">(</span>p_queue<span class="k2">)</span><span class="k2">)</span>
<span class="number">  6</span><span class="k2">{</span>
<span class="number">  7</span><a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>p_queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span><span class="c">// Code for input here</span>
<span class="number">  9</span><span class="k2">}</span>
<span class="number"> 10</span>...
<span class="number"> 11</span>Update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span><span class="k1">if</span><span class="k2">(</span>al_event_queue_is_empty<span class="k2">(</span>p_queue<span class="k2">)</span><span class="k2">)</span>
<span class="number"> 13</span><span class="k2">{</span>
<span class="number"> 14</span><span class="c">// Render code here</span>
<span class="number"> 15</span><span class="k2">}</span>
<span class="number"> 16</span>timeFrameLast <span class="k3">=</span> timeFrameCurrent<span class="k2">;</span>
</div></div><p>
And the second like this:
</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><a href="http://www.allegro.cc/manual/al_wait_for_event"><span class="a">al_wait_for_event</span></a><span class="k2">(</span>p_queue, <span class="k3">&amp;</span>event<span class="k2">)</span><span class="k2">;</span>
<span class="number">  2</span>...
<span class="number">  3</span><span class="c">// Code for input here</span>
<span class="number">  4</span>...
<span class="number">  5</span><span class="k1">if</span><span class="k2">(</span>event.type <span class="k3">=</span><span class="k3">=</span> ALLEGRO_EVENT_TIMER<span class="k2">)</span>
<span class="number">  6</span><span class="k2">{</span>
<span class="number">  7</span>Update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span><span class="k1">if</span><span class="k2">(</span>al_event_queue_is_empty<span class="k2">(</span>p_queue<span class="k2">)</span><span class="k2">)</span>
<span class="number">  9</span><span class="k2">{</span>
<span class="number"> 10</span><span class="c">// Render code here</span>
<span class="number"> 11</span><span class="k2">}</span>
<span class="number"> 12</span><span class="k2">}</span>
</div></div><p>

Second code works fine and I don&#39;t have any errors while in the first one everything works fine until I move a mouse. If I move it the game looks like it&#39;s in speed mode and everything moves faster.<br />I could not figure it why is this happening. I just created a logic for deltaTime and set to skip waiting for event if there are no currently any events to let the engine update itself and render.<br />Do you have any clue why would this happen?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tibor698)</author>
		<pubDate>Thu, 12 Apr 2018 02:32:16 +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/617364/1036725#target">tibor698</a> said:</div><div class="quote"><p>
if(!al_event_queue_is_empty(p_queue))<br />{<br />al_wait_for_event(p_queue, &amp;event); <br />// Code for input here <br />}
</p></div></div><p>

<s>What are you trying to accomplish there, logically? If the event queue is not empty why would you wait for an event (you already know there is one...)?</s></p><p>[edit:] Actually, it makes sense, if the queue is not empty you just retrieve the first event.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elias)</author>
		<pubDate>Thu, 12 Apr 2018 06:26:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It is just my guess since you posted your code incomplete, but it looks like you do Update(); every event in your code. You should update only when ALLEGRO_EVENT_TIMER occurs, like in the second code.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Izual)</author>
		<pubDate>Thu, 12 Apr 2018 11:15:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Izual is right. You&#39;re calling Update() for every event, including mouse axes events, which makes it super speedy when you move the mouse.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 12 Apr 2018 15:59:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If using delta time, the &quot;logic update&quot; itself is supposed to take into account how much time has passed since the last &quot;logic update&quot;.<br />In other words, you will not see <span class="source-code">ship.x <span class="k3">+</span><span class="k3">=</span> <span class="n">4</span></span>, but rather <span class="source-code">ship.x <span class="k3">+</span><span class="k3">=</span> delta_t <span class="k3">*</span> ship.speed</span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 12 Apr 2018 17:01:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Izual This was my intent, to not have a fixed FPS but to use a max possible one.<br />@Edgar This is exactly what is happening but I don&#39;t understand why. Shouldn&#39;t it be possible to call Update for every event but with deltaTime as @Audric noted like ship.x += deltaTime * speed?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tibor698)</author>
		<pubDate>Wed, 18 Apr 2018 01:02:22 +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/617364/1036881#target">tibor698</a> said:</div><div class="quote"><p>
@Edgar This is exactly what is happening but I don&#39;t understand why. Shouldn&#39;t it be possible to call Update for every event but with deltaTime as @Audric noted like ship.x += deltaTime * speed? 
</p></div></div><p>
Update is equivalent to passing time in your world. Why would you want to make time pass for every mouse event? It doesn&#39;t make sense. Update() gets called on every TIMER event.</p><p>For example here is one of my typical game loops :
</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="number">  2</span><span class="k1">int</span> Game::Run<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  3</span>
<span class="number">  4</span>   <span class="k1">bool</span> paused <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number">  5</span>   <span class="k1">while</span> <span class="k2">(</span>state <span class="k3">!</span><span class="k3">=</span> QUIT<span class="k2">)</span> <span class="k2">{</span>
<span class="number">  6</span>      Display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  7</span>      <span class="k1">int</span> nevents <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>
<span class="number">  8</span>      <span class="k1">do</span> <span class="k2">{</span>
<span class="number">  9</span>         EagleEvent ee <span class="k3">=</span> sys-&gt;WaitForSystemEventAndUpdateState<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>         <span class="k1">if</span> <span class="k2">(</span>ee.type <span class="k3">=</span><span class="k3">=</span> EAGLE_EVENT_DISPLAY_CLOSE<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 11</span>            state <span class="k3">=</span> QUIT<span class="k2">;</span>
<span class="number"> 12</span>         <span class="k2">}</span>
<span class="number"> 13</span>         <span class="k1">if</span> <span class="k2">(</span>ee.type <span class="k3">=</span><span class="k3">=</span> EAGLE_EVENT_DISPLAY_SWITCH_OUT<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 14</span>            paused <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span>
<span class="number"> 15</span>         <span class="k2">}</span>
<span class="number"> 16</span>         <span class="k1">if</span> <span class="k2">(</span>ee.type <span class="k3">=</span><span class="k3">=</span> EAGLE_EVENT_DISPLAY_SWITCH_IN<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 17</span>            paused <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span>
<span class="number"> 18</span>         <span class="k2">}</span>
<span class="number"> 19</span>         <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>paused<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 20</span>            <span class="k1">if</span> <span class="k2">(</span>ee.type <span class="k3">=</span><span class="k3">=</span> EAGLE_EVENT_TIMER<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 21</span>               <span class="k3">+</span><span class="k3">+</span>nevents<span class="k2">;</span>
<span class="number"> 22</span>               <span class="k1">if</span> <span class="k2">(</span>nevents <span class="k3">=</span><span class="k3">=</span> <span class="n">1</span><span class="k2">)</span> <span class="k2">{</span><span class="c">/// Slow down gracefully if the gpu or cpu can't handle it</span>
<span class="number"> 23</span>                  Update<span class="k2">(</span>ee.timer.eagle_timer_source-&gt;SPT<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 24</span>               <span class="k2">}</span>
<span class="number"> 25</span>            <span class="k2">}</span>
<span class="number"> 26</span>            CheckInputs<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 27</span>            state <span class="k3">=</span> HandleEvent<span class="k2">(</span>ee<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 28</span>         <span class="k2">}</span>
<span class="number"> 29</span>      <span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span>sys-&gt;GetSystemQueue<span class="k2">(</span><span class="k2">)</span><span class="k3">-</span><span class="k3">&gt;</span>HasEvent<span class="k2">(</span><span class="n">0</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 30</span>   <span class="k2">}</span>
<span class="number"> 31</span>   <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 32</span><span class="k2">}</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 18 Apr 2018 17:12:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://">Edgar Reynaldo</a> said:</div><div class="quote"><p>Why would you want to make time pass for every mouse event? It doesn&#39;t make sense.</p></div></div><p>

I thought that I was calling update for every event, including mouse events and others, even if there where no events (timeDelta should be smaller in this case). So generally I thought I was updateing/rendering as fast as possible and timeDelta giving me the exact frame time.<br />Is there maybe some error in my code? How could I make it independent of TIMER and other events and render as fast as possible?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tibor698)</author>
		<pubDate>Thu, 19 Apr 2018 00:51:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your monitor can only display so many frames per second. This is known as your monitor&#39;s refresh rate. If you try to display more than this number of frames per second, then you will have partial frames. One problem that can come from this is screen tearing, where a horizontal line appears across your display as it updates.</p><p>Most monitors have a refresh rate of 60 or more Hz. Trying to display more frames than this in a second is pointless, as they won&#39;t be shown, or they will be shown partially, causing screen tearing.</p><p>I think your cause of confusion comes from trying to mash all your logic into an Update function. I understand you want to handle every event, that&#39;s good, you don&#39;t want to miss events. But calling update on every event is wrong. It should only be called when time passes in your game. To handle the other events, you should have a HandleEvent function. All events go to this function, and when it receives a timer event, then it calls Update. Other events get processed accordingly.</p><p>Your logic can run at any rate, but your display is locked to the refresh rate.</p><p>If you need perfectly smooth animation, then you can use interpolation to find the exact position of an object between frames based on delta time, but your logic should still run at a fixed rate.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 19 Apr 2018 01:12:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I disagree, delta time system is valid. It can be considerably harder to implement than fixed frequency, but it can have the following advantages over it :
</p><ul><li><p>When there is a performance hit, delta time may be able to catch up quicker than if you have to call a fixed frequency Update() a dozen times in a row.
</p></li><li><p>For competitive games, it makes it possible to accept inputs immediately, rather than waiting for the next &quot;tick&quot; : The player with better reflexes will act first
</p></li><li><p>It makes it possible to take into account every mouse position that the system detects (precision of position/trajectory). Even non-gamer mice can have a reliable 200 Hz precision, and for a painting program for example you will draw much smoother curves if you can process them all. &quot;Fruit ninja&quot; games will also benefit from faithful mouse movement.
</p></li></ul></div>]]>
		</description>
		<author>no-reply@allegro.cc (Audric)</author>
		<pubDate>Thu, 19 Apr 2018 15:07:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Variable delta timing is super tricky, and mostly non-deterministic. When dt gets too high, you start getting things like teleportation and moving through walls. Most amateur collision systems aren&#39;t robust enough to handle high dt. If they dealt with intercept times, it could be different, but that just makes it harder. At that point, you&#39;re probably better off using a physics engine, unless you&#39;re a masochist.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Thu, 19 Apr 2018 22:50:54 +0000</pubDate>
	</item>
</rss>
