<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Optimizing code: Hunters</title>
		<link>http://www.allegro.cc/forums/view/588995</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 11 Dec 2006 06:41:26 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, as I am developing my current game, <i>Hunters</i>, I have seen some freezing of the program. This is mostly due to only having half the memory I normally do, but it made me think: how can I optimize my code? I have included both the main file and the header with this post. I hope my code is well documented.</p><p>Cookies for help! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Sun, 10 Dec 2006 07:15:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My eyes are bleeding.</p><p>No offense, but you really need to split up that code a bit more before i will even try to look at it seriously.</p><p>Your main function is about 470 lines long.<br />You almost exclusively use globals.<br />You declare/define all your classes in one .h file.</p><p>But enough of that <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>At a glance;
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">while</span> <span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span> <span class="k3">&lt;</span> log_time<span class="k2">)</span>
<span class="k2">{</span><span class="k2">}</span>
</pre></div></div><p>
Use proper timing (search the forum), and see if the problems clear up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Sun, 10 Dec 2006 07:30:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use a profiler and isolate some functions for us or write some pseudo code.. i don&#39;t have time to go through code but if you posted snippets or ideas i and others could make some quick comments.</p><p>In terms of memory: pass by reference, mmap large files, use a memory pool if you have lots of allocation/deallocation. Make your contructors as simple as possible and use initialization syntax for variables rather than assignment. (some compilers still haven&#39;t figured this out in all cases).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Sun, 10 Dec 2006 09:14:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I really don&#39;t think you have anything that is overly time-/memory- consuming.<br />Remake your timing function. clock() has very (!) poor accuracy; it only changes value about 6-7 times per second (on winXP).</p><p>Try it yourself:
</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="p">#include &lt;stdio.h&gt;</span></td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;time.h&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include &lt;stdlib.h&gt;</span></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">6</td><td>  clock_t lastVal<span class="k2">;</span></td></tr><tr><td class="number">7</td><td>  <span class="k1">int</span> i<span class="k2">;</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>  lastVal <span class="k3">=</span> <span class="k3">-</span><span class="n">1</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  <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><span class="n">10000000</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>    <span class="k1">if</span><span class="k2">(</span> lastVal <span class="k3">!</span><span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span> <span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>      lastVal <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_103.html" target="_blank">clock</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_624.html" target="_blank">printf</a><span class="k2">(</span><span class="s">"lastVal: %i\n"</span>,lastVal<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>    <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>  <a href="http://www.delorie.com/djgpp/doc/libc/libc_802.html" target="_blank">system</a><span class="k2">(</span><span class="s">"PAUSE"</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Sun, 10 Dec 2006 20:22:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, I did this:
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">long</span> timer<span class="k2">;</span>
<span class="k1">void</span> plus_time<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>timer<span class="k3">+</span><span class="k3">+</span><span class="k2">}</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/install_int" target="_blank"><span class="a">install_int</span></a><span class="k2">(</span>plus_time, <span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
instead of <tt>clock()</tt>, and it runs a lot better now. Thanks! <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /><br />As for the code, it was in just 1 file for quite a while. I split it for a bit of convenience. I use globals out of habit, and it seems to work just fine. (I really don&#39;t like using non-global variables that much) <img src="http://www.allegro.cc/forums/smileys/embarassed.gif" alt=":-[" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 11 Dec 2006 02:30:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should try to think about the design before you start coding, so you don&#39;t have to write everything as a global &quot;just in case&quot;. But anyway; glad it worked better with allegro&#39;s timer <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Also, you should read the timer section in the manual fort he &quot;proper&quot; way to do it (volatile etc).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Mon, 11 Dec 2006 03:09:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have considered replacing the <tt>shot</tt> and <tt>enemy</tt> arrays with <tt>vectors</tt>. Think that would help at all? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 11 Dec 2006 03:31:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think it would. It would conveniently remove the cap on max number of enemies... <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SiegeLord)</author>
		<pubDate>Mon, 11 Dec 2006 03:33:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oh. Yeah. Cool. BTW, the actual max number is 80 right now... <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Hey, I think I found something: When I include &lt;vector&gt;, I get warning about includes with:<br /><tt>
&lt;ul&gt;
&lt;li&gt; windows.h&lt;/li&gt;
&lt;li&gt; c++io.h&lt;/li&gt;
&lt;li&gt; fpos.h&lt;/li&gt;
&lt;li&gt; gthr.h&lt;/li&gt;
&lt;li&gt; gthr-default.h&lt;/li&gt;
&lt;li&gt; wingdi.h&lt;/li&gt;
&lt;li&gt; iosfwd.h&lt;/li&gt;
&lt;li&gt; stl_algobase.h&lt;/li&gt;
&lt;li&gt; vector.h&lt;/li&gt;
&lt;/ul&gt;
</tt><br />Does anyone know what&#39;s wrong? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 11 Dec 2006 04:10:34 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
clock() has very (!) poor accuracy; it only changes value about 6-7 times per second (on winXP).
</p></div></div><p>

It does not do what you think it does...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Wang)</author>
		<pubDate>Mon, 11 Dec 2006 06:11:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, anyone know why <tt>&lt;vector&gt;</tt> is trying to include <tt>&quot;windows.h&quot;</tt>? Please? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 11 Dec 2006 06:16:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It returns the number of &quot;ticks&quot; (1000/second on my machine) since the process started. Only, it does so in rather large &quot;jumps&quot;.</p><p>Or am i wrong?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jonatan Hedborg)</author>
		<pubDate>Mon, 11 Dec 2006 06:16:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>kikaru, can you paste all of your include statements in order.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Mon, 11 Dec 2006 06:28:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, here:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;allegro.h&gt;</span>
<span class="p">#include &lt;math.h&gt;</span>
<span class="p">#include &lt;stdio.h&gt;</span>
<span class="p">#include "hunters.h"</span>
<span class="p">#include &lt;vector&gt;</span>
</pre></div></div><p>

hunters.h has this in the include:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;allegro.h&gt;</span>
<span class="p">#include &lt;math.h&gt;</span>
<span class="p">#include &lt;stdio.h&gt;</span>
<span class="p">#include &lt;vector&gt;</span>
</pre></div></div><p>

[EDIT]<br />Hey, isn&#39;t there like &quot;winalleg.h&quot; or something that I need to include? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" /></p><p>[EDIT 2]<br />I added &quot;winalleg.h&quot;, and it work <i>perfectly</i>! Thanks for your help guys! <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Mon, 11 Dec 2006 06:41:26 +0000</pubDate>
	</item>
</rss>
