<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Is drawing to the buffer still part of game logic?</title>
		<link>http://www.allegro.cc/forums/view/590753</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 29 Mar 2007 11:39:40 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was wondering for quite sometime if I&#39;ve been doing it wrong. The standard way of timing your game according to the FAQ is</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="k2">}</span></td></tr><tr><td class="number">25</td><td>      <span class="k2">}</span></td></tr></tbody></table></div></div><p>


but with update_game_logic() for example. I have to iterate through a List of enemies. Obviously, they all have a draw method which draws to the buffer so when I terate through the list I would also immedtiately call their draw method.</p><p>pseudocode 1
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> enemy.size<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="k2">{</span>
    enemy<span class="k3">&lt;</span>i&gt;.update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
    enemy<span class="k3">&lt;</span>i&gt;.draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">//draw to buffer</span>
<span class="k2">}</span>

speed_counter--<span class="k2">;</span>

update_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

then eventually, the inner while loop would exit and render everything to the screen. I did this so I wouldn&#39;t have to make another iteration such as</p><p>pseudocode 2
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> enemy.size<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="k2">{</span>
    enemy<span class="k3">&lt;</span>i&gt;.update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

speed_counter--<span class="k2">;</span>

<span class="k1">for</span> <span class="k2">(</span>i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> enemy.size<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
<span class="k2">{</span>
    enemy<span class="k3">&lt;</span>i&gt;.draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>

update_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

which do you think is the right way? Pseudocode1 is working just fine with me but working doesn&#39;t mean it&#39;s right, right? Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (armond)</author>
		<pubDate>Wed, 28 Mar 2007 08:36:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Do it the second way. Queue long rambling advice on why its better, suffice to say its better to separate logic and drawing. Or you can find out the hard way I suppose.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kazzmir)</author>
		<pubDate>Wed, 28 Mar 2007 08:38:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not very experienced, but my tired mind is giving me only one explanation for the rule. Consider collision detection. You obviously can&#39;t test collision detection until all objects are in their new position. If you simply move one and draw it, move another one, draw it, etc., you could end up drawing an object and re-moving it because of collision and having to re-draw <s>it</s> <i>the entire buffer</i>. So doing game logic before drawing could theoretically eliminate redundant operations.</p><p>It&#39;s also probably more maintainable if game logic and drawing are separate. Code to draw everything is in one place so if changes are being implemented for many objects&#39; drawing the code is all located nearby so you can easily fix them, etc... I think.</p><p>Make sense?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (bamccaig)</author>
		<pubDate>Wed, 28 Mar 2007 08:44:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi guys! Thanks for your advices! I followed your suggestions and I&#39;m getting the feeling that my game would actually run faster now when I test it on slower machines. Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (armond)</author>
		<pubDate>Wed, 28 Mar 2007 10:00:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The main point for separating logic from drawing is (imo) that you can skip frames (i.e. drawing) if drawing is slow, so you do logic twice before drawing again. On slow drawing machines of course, and dynamically when your framerate would drop otherwise.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tobing)</author>
		<pubDate>Wed, 28 Mar 2007 10:55:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Separating logic from drawing is the most basic thing you can do to bring structure to your spaghetti code. It is not only better for technical reasons, but also because it makes you think in a certain way that has proven to work and help you understand things more clearly.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 28 Mar 2007 18:16:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Separating logic from drawing is the most basic thing you can do to bring structure to your spaghetti code. It is not only better for technical reasons, but also because it makes you think in a certain way that has proven to work and help you understand things more clearly.
</p></div></div><p>
Seconded and Thirded by me and myself.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Onewing)</author>
		<pubDate>Wed, 28 Mar 2007 20:21:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You obviously can&#39;t test collision detection until all objects are in their new position.
</p></div></div><p>
Maybe for getting &#39;hurt&#39; or for colleting items, but not for actually colliding with impassible objects - else you might have to backtrack and that&#39;ll add overhead. Then objects may backtrack onto objects that have now taken its spot.</p><p>You may want to also subdivide the operations further:
</p><div class="source-code snippet"><div class="inner"><pre>player: player_input<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
all ai: object_ai_logic_and_stuff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
all objects: object_update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
all objects: check_delete<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
controller: check_game_state<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Wed, 28 Mar 2007 20:28:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I get lazy sometimes and implement more things in the drawing code (usually animation type stuff, but sometimes stuff that is really pure logic <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />).  This is bad because you&#39;re hurting your ability to run the game on slower computers. <img src="http://www.allegro.cc/forums/smileys/embarassed.gif" alt=":-[" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Thu, 29 Mar 2007 00:21:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi guys! It worked! It does drop some frames(the game would lag) but then it&#39;s better than being able to display everything smoothly but slow. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (armond)</author>
		<pubDate>Thu, 29 Mar 2007 11:39:40 +0000</pubDate>
	</item>
</rss>
