<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>The age-old 100% CPU usage problem - but still no solution</title>
		<link>http://www.allegro.cc/forums/view/590932</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 15 Apr 2007 20:57:21 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi all,</p><p>I&#39;m back on the forum after a long absence due to (ahem!) illness, this time with an  age old question which I see posted on this forum dozens of times but never really explained in a way I could understand. I&#39;ve managed to fix the speed of my game, but performance is choppy because, for whatever reason, the CPU is taking up 100% of the time. I&#39;ve made the game logic updates event-based, so they are called by an interrupt at regular intervals, and I&#39;ve tried cutting out the game_display() (the function that contains all the code dealing with drawing the screen) completely to see if the drawing was taking too much time (it throws around 50-60 sprites at a time so it would be understandable, especially under Linux with no GFX hardware acceleration). Without any drawing functions, the game still swallows up 100% CPU time.</p><p>This is the main.cc as it stands:</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="c">/* Open Invaders</span></td></tr><tr><td class="number">2</td><td><span class="c"> * (c) 2006 Darryl LeCount</span></td></tr><tr><td class="number">3</td><td><span class="c"> *</span></td></tr><tr><td class="number">4</td><td><span class="c"> *</span></td></tr><tr><td class="number">5</td><td><span class="c"> *  This program is free software; you can redistribute it and/or modify</span></td></tr><tr><td class="number">6</td><td><span class="c"> *  it under the terms of the GNU General Public License as published by</span></td></tr><tr><td class="number">7</td><td><span class="c"> *  the Free Software Foundation; either version 2 of the License, or</span></td></tr><tr><td class="number">8</td><td><span class="c"> *  (at your option) any later version.</span></td></tr><tr><td class="number">9</td><td><span class="c"> *</span></td></tr><tr><td class="number">10</td><td><span class="c"> *  This program is distributed in the hope that it will be useful,</span></td></tr><tr><td class="number">11</td><td><span class="c"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span></td></tr><tr><td class="number">12</td><td><span class="c"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></td></tr><tr><td class="number">13</td><td><span class="c"> *  GNU General Public License for more details.</span></td></tr><tr><td class="number">14</td><td><span class="c"> *</span></td></tr><tr><td class="number">15</td><td><span class="c"> *  You should have received a copy of the GNU General Public License</span></td></tr><tr><td class="number">16</td><td><span class="c"> *  along with this program; if not, write to the Free Software</span></td></tr><tr><td class="number">17</td><td><span class="c"> *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.</span></td></tr><tr><td class="number">18</td><td><span class="c"> */</span></td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td><span class="p">#include "allegro.h"</span></td></tr><tr><td class="number">21</td><td><span class="p">#include "functions.h"</span></td></tr><tr><td class="number">22</td><td><span class="c">//#include "./alogg/alogg.h"</span></td></tr><tr><td class="number">23</td><td><span class="p">#include &lt;iostream&gt;</span></td></tr><tr><td class="number">24</td><td> </td></tr><tr><td class="number">25</td><td><span class="k1">int</span> fullscreen_mode, frames_missed<span class="k2">;</span></td></tr><tr><td class="number">26</td><td>&#160;</td></tr><tr><td class="number">27</td><td><span class="k1">using</span> <span class="k1">namespace</span> std<span class="k2">;</span></td></tr><tr><td class="number">28</td><td>&#160;</td></tr><tr><td class="number">29</td><td><span class="k1">void</span> interrupt_time_control<span class="k2">(</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>  frames_missed<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>&#160;</td></tr><tr><td class="number">34</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>interrupt_time_control<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>&#160;</td></tr><tr><td class="number">36</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span>argv<span class="k2">[</span><span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">37</td><td><span class="k2">{</span></td></tr><tr><td class="number">38</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>interrupt_keys<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>interrupt_time_control<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">40</td><td>  </td></tr><tr><td class="number">41</td><td>  <a href="http://www.allegro.cc/manual/LOCK_VARIABLE" target="_blank"><span class="a">LOCK_VARIABLE</span></a><span class="k2">(</span>frames_missed<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">42</td><td>  </td></tr><tr><td class="number">43</td><td>  frames_missed<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">44</td><td>  fullscreen_mode<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">45</td><td>  </td></tr><tr><td class="number">46</td><td>  <span class="k1">if</span><span class="k2">(</span>argc&gt;1<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="k1">if</span><span class="k2">(</span>argv<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k3">=</span><span class="k3">=</span><span class="s">'-'</span><span class="k2">)</span></td></tr><tr><td class="number">49</td><td>    <span class="k2">{</span></td></tr><tr><td class="number">50</td><td>      fullscreen_mode<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">51</td><td>      </td></tr><tr><td class="number">52</td><td>      <span class="k1">switch</span><span class="k2">(</span>argv<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">53</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">54</td><td>        <span class="k1">case</span> <span class="s">'f'</span><span class="k2">:</span> fullscreen_mode<span class="k3">=</span><span class="n">1</span><span class="k2">;</span> <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">55</td><td>        <span class="k1">case</span> <span class="s">'w'</span><span class="k2">:</span> fullscreen_mode<span class="k3">=</span><span class="n">2</span><span class="k2">;</span> <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">56</td><td>      <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">57</td><td>    <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">58</td><td>  <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">59</td><td>  </td></tr><tr><td class="number">60</td><td>  initialise_game<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">61</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Allegro initialised...\n"</span><span class="k2">;</span></td></tr><tr><td class="number">62</td><td>  </td></tr><tr><td class="number">63</td><td>  display_setup<span class="k2">(</span>fullscreen_mode<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">64</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Allegro display established...\n"</span><span class="k2">;</span></td></tr><tr><td class="number">65</td><td>  </td></tr><tr><td class="number">66</td><td>  predefine_variables<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">67</td><td>  create_bitmasks<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">68</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Collision bitmasks initialised...\n"</span><span class="k2">;</span></td></tr><tr><td class="number">69</td><td>&#160;</td></tr><tr><td class="number">70</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Have fun!\n"</span><span class="k2">;</span></td></tr><tr><td class="number">71</td><td>  </td></tr><tr><td class="number">72</td><td>  intro_sequence<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">73</td><td>  </td></tr><tr><td class="number">74</td><td>  <span class="k1">while</span><span class="k2">(</span>program_still_active<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">75</td><td>  <span class="k2">{</span>  </td></tr><tr><td class="number">76</td><td>    title_screen<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">77</td><td>    predefine_variables<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">78</td><td>    reset_enemies_position<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">79</td><td>    reset_enemies_state<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">80</td><td>    initialise_ingame_music<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">81</td><td>    <a href="http://www.allegro.cc/manual/install_int" target="_blank"><span class="a">install_int</span></a><span class="k2">(</span>interrupt_time_control,<span class="n">4</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">82</td><td>    </td></tr><tr><td class="number">83</td><td>    <span class="k1">while</span><span class="k2">(</span>game_still_active<span class="k2">(</span><span class="k2">)</span><span class="k2">)</span></td></tr><tr><td class="number">84</td><td>    <span class="k2">{</span>    </td></tr><tr><td class="number">85</td><td>      <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> repeats<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>repeats<span class="k3">&lt;</span>frames_missed<span class="k2">;</span>repeats<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> </td></tr><tr><td class="number">86</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">87</td><td>        update_logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">88</td><td>      <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">89</td><td>      </td></tr><tr><td class="number">90</td><td>      game_display<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">91</td><td>      </td></tr><tr><td class="number">92</td><td>      frames_missed<span class="k3">=</span><span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">93</td><td>      </td></tr><tr><td class="number">94</td><td>      <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">95</td><td>      <a href="http://www.allegro.cc/manual/vsync" target="_blank"><span class="a">vsync</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">96</td><td>    <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">97</td><td>  <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">98</td><td>  </td></tr><tr><td class="number">99</td><td>  cout <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="s">"Thank you for playing!\n"</span><span class="k2">;</span></td></tr><tr><td class="number">100</td><td>  </td></tr><tr><td class="number">101</td><td>  <a href="http://www.allegro.cc/manual/allegro_exit" target="_blank"><span class="a">allegro_exit</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">102</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">103</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

Interrupt_keys() looks thus:</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> interrupt_keys<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="k1">char</span> nameext<span class="k2">[</span><span class="n">20</span><span class="k2">]</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>      </td></tr><tr><td class="number">5</td><td>  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_LCONTROL<span class="k2">]</span><span class="k3">&amp;</span><span class="k3">&amp;</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_S<span class="k2">]</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>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_737.html" target="_blank">sprintf</a><span class="k2">(</span>nameext,<span class="s">"oi_screen_%d.bmp"</span>,<span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_637.html" target="_blank">rand</a><span class="k2">(</span><span class="k2">)</span>%<span class="n">8998</span><span class="k2">)</span><span class="k3">+</span><span class="n">1000</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    <a href="http://www.allegro.cc/manual/save_bitmap" target="_blank"><span class="a">save_bitmap</span></a><span class="k2">(</span>nameext,<a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,gamepalette<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>  <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>  </td></tr><tr><td class="number">11</td><td>  <span class="k1">if</span><span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_LCONTROL<span class="k2">]</span><span class="k3">&amp;</span><span class="k3">&amp;</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_C<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">12</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">13</td><td>    program_active<span class="k3">=</span><span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>    game_active<span class="k3">=</span><span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>    <a href="http://www.delorie.com/djgpp/doc/libc/libc_37.html" target="_blank">abort</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>  <span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td><span class="k2">}</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>

Interrupt_time_control() is just this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">void</span> interrupt_time_control<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
  frames_missed<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

I&#39;ve been playing with this for months and am just about ready to chuck it in and delete the lot. Can anyone give me a hand as to why I can&#39;t stabilise the frame rate and drop the CPU usage? If there&#39;s anything else I should paste in let me know.</p><p>Thanks!</p><p>Darryl
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Tue, 10 Apr 2007 02:05:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thats because by default your program will use 100% of the cpu even if its just repeating a loop. Thats because it&#39;s not just repeating the loop, but repeating it as fast as possible.</p><p>To give up CPU cycles for other programs you need to call <b>rest</b> or you can also use <b>sleep</b> although I&#39;m not sure which header the latter is in, too much java programming :S</p><p>Anyway more on rest: <a href="http://alleg.sourceforge.net/stabledocs/en/alleg005.html#rest">http://alleg.sourceforge.net/stabledocs/en/alleg005.html#rest</a></p><p>[edit]</p><p>Woops just noticed you had a rest. Drop vysc. I believe some forum posters mentioned that vysnc would allow timing, but also consume 100% cpu usage while it did. As vsyc doesn&#39;t return until its done, rest literally only gets called 60 times per second, if that.</p><p>[edit2]<br />There was also just recently a post on sample game loops, though it&#39;s not 100% relevant, and looking through it I didn&#39;t really find any stellar examples (most of it is psuedo code) it might help. <a href="http://www.allegro.cc/forums/thread/590871">http://www.allegro.cc/forums/thread/590871</a></p><p>[edit3]<br />If neither of these helps, I&#39;ll take my GWARA Tins entry, and modify it so it doesn&#39;t use 100% cpu and post the updated code. Or try to at least. Just let me know.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Hard Rock)</author>
		<pubDate>Tue, 10 Apr 2007 02:35:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Your frame dropping algorithm fails to take into account if the logic processing is taking too long. Frame dropping cures when the rendering takes too long, but if the logic is taking to long, more logic updates will be requested while logic is being processed, resulting in potentially large amounts of logic loops per frame, which is possibly the source of your framerate troubles and your 100% CPU usage.</p><p>What you need to do is put a limit to how many logic updates you&#39;ll allow on one game loop. 4 is a good limit. This prevents your logic loop from over-processing, which will result in slow-down if your system can&#39;t handle the amount of logic that needs to be processed, but that will be true of any game using fixed-time logic.</p><p>You may also want to check and make sure you&#39;re not running 32-bit graphics. 16-bit graphics will run almost twice as fast as 32-bit on any system. Also, avoid strange colour depths such as 15-bit or 24-bit which may invoke compatability layers if your video card can&#39;t handle them by default.</p><p>Also, vsync() will <i>not</i> eat up so much CPU time that you&#39;ll get 100% usage so long as you have rest() or Sleep() statements to give time back to the OS. However, you should place your rest() statement immediately after rendering, not right before vsyncing.</p><p>--- Kris Asick (Gemini)<br />--- <a href="http://www.pixelships.com">http://www.pixelships.com</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Tue, 10 Apr 2007 02:41:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>While your program is running it counts towards using cpu&#39;s. Yielding/resting will tell the operating system to stop running your program for the time being. While its doing nothing it is using 0%.</p><p>Here is a fix that shouldn&#39;t affect any thing. It works exactly the same except that when the count is 0 it returns to the operating system. When the count goes back above 0 it wake the process up. Just replace sdl_semaphore with one from your operating system be it windows or linux (i can help you if you need).I used the evil sdl because allegro has nothing similar/portable IIRC.</p><div class="source-code snippet"><div class="inner"><pre><span class="c">//the timer function. basically SemPort will "increment" the tick variable. </span>
Uint32 SDLTimer_Callback<span class="k2">(</span>Uint32 interval, <span class="k1">void</span><span class="k3">*</span> param<span class="k2">)</span>
<span class="k2">{</span>
  SDL_SemPost<span class="k2">(</span>sleepSemaphore<span class="k2">)</span><span class="k2">;</span> <span class="c">//will wake up the other thread. "increments" the count</span>
  <span class="k1">return</span> interval<span class="k2">;</span>
<span class="k2">}</span>  

<span class="c">//the main loop</span>
<span class="k1">while</span> <span class="k2">(</span> <span class="k3">!</span>quitMe <span class="k2">)</span>
<span class="k2">{</span>
  SDL_SemWait<span class="k2">(</span>sleepSemaphore<span class="k2">)</span><span class="k2">;</span> <span class="c">//sleeps if count = 0, when it is no longer 0 it wakes up and decrements the count</span>
  gameLogic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

edit: i should explain that sleepSemaphore is basically just a counter. It is used for synchronizing between threads but this works perfectly fine in a situation like this. This uses &lt; 1% of my cpu in most cases (also opengl for hardware drawing helps!)</p><p>edit2: rest(1) doesn&#39;t sleep 1ms esp on windows. Windows is not a real-time operating system. You&#39;ll only get accuracy to ~ 10ms or so. SO like 10ms 20ms 30ms etc...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Tue, 10 Apr 2007 02:43:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I used the evil sdl because allegro has nothing similar/portable IIRC.
</p></div></div><p>
But there&#39;s aready a perfectly <a href="http://www.hmug.org/man/2/sem_post.php">valid</a> and <a href="http://www.hmug.org/man/2/sem_wait.php">portable</a> way to do it already...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 10 Apr 2007 02:50:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>And where might semaphore.h be in MinGW? In MSVS?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Tue, 10 Apr 2007 03:05:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://sourceware.org/pthreads-win32/">Right here</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 10 Apr 2007 03:44:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Actually that&#39;s pretty good thing. Windows is the only platform (other than embedded) that doesn&#39;t support pthreads). The only reason why i didn&#39;t use sem_wait was because almost all n00bs who post use windows. It&#39;s a fact! But I hope that works for vc (or whatever they used). </p><p>If it does... then its about time to make a wiki entry and end this line of threads once and for all!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Tue, 10 Apr 2007 03:57:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The only reason why i didn&#39;t use sem_wait was because almost everyone who posts uses windows.
</p></div></div><p>Fixed <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>A game with real-time performance requirements using 100% CPU time isn&#39;t a problem in itself. In fact, when your game yields its current timeslice to the OS, it must wait until the OS&#39;s next round of scheduling. That may be ok for tic-tac-toe. How demanding do you think your game will be on the CPU?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Steve++)</author>
		<pubDate>Tue, 10 Apr 2007 09:36:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Putting the thread to sleep and then waking it up is not a problem. In fact since timer is run in a seperate thread it gets woken up, then you have to reschedule the main loop thread anyways. When the sem_post occurs it changes the threads state to ready... the same as it would be if it was pre-empted because its time slice expired. </p><p>Windows is not real-time, not anything close... so you have to live with what you get i suppose. I guess this is why the thread about delta timing came up where you use getTimeOfDay() and find out how much time really elapsed (in milliseconds). But there are problems with pausing and scheduling (if you&#39;re relying upon small differences rather than averages then you&#39;d need to lock everything up during that computation sequence)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Tue, 10 Apr 2007 09:57:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
A game with real-time performance requirements using 100% CPU time isn&#39;t a problem in itself.
</p></div></div><p>
If your game is going to make my fans come on, it had better look like Half-Life 2 or I won&#39;t be loading it again.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Wed, 11 Apr 2007 02:15:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I sucked it in and wrote an allegro wiki entry under timers. <a href="http://wiki.allegro.cc/Timers#Yielding_the_CPU">http://wiki.allegro.cc/Timers#Yielding_the_CPU</a></p><p>edit: and some simple test code to measure performance. Here&#39;s what time outputs:<br />real    0m59.941s<br />user    0m0.040s<br />sys     0m0.012s</p><p>So it spends 1 minute of real time.. It spends 0.012 seconds doing system calls. It spends 0.040 seconds actually executing my code! I think that&#39;s a pretty good start <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>That gives a cpu usage of basically 0% (it uses 1.2% of a single second.. now divide that by 60 to get the actual rate)</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td><span class="p">#include &lt;allegro.h&gt;</span></td></tr><tr><td class="number">3</td><td><span class="p">#include &lt;semaphore.h&gt;</span></td></tr><tr><td class="number">4</td><td><span class="c">//for printing seconds since 1970 or whatever</span></td></tr><tr><td class="number">5</td><td><span class="p">#include &lt;stdlib.h&gt;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="c">//number of cycles per second</span></td></tr><tr><td class="number">8</td><td><span class="p">#define BPS 60</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td><span class="c">//create the mutex</span></td></tr><tr><td class="number">11</td><td>sem_t timer_sem<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>&#160;</td></tr><tr><td class="number">13</td><td><span class="k1">void</span> ticker<span class="k2">(</span><span class="k1">void</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>  sem_post<span class="k2">(</span><span class="k3">&amp;</span>timer_sem<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td><span class="k2">}</span></td></tr><tr><td class="number">17</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>ticker<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td>&#160;</td></tr><tr><td class="number">20</td><td><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span><span class="k3">*</span><span class="k3">*</span> argv<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>  sem_init<span class="k2">(</span><span class="k3">&amp;</span>timer_sem, <span class="n">0</span>, <span class="n">1</span><span class="k2">)</span><span class="k2">;</span> <span class="c">//initialize the semaphore, set the tick count to 1</span></td></tr><tr><td class="number">23</td><td>  <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>&#160;</td></tr><tr><td class="number">25</td><td>  <a href="http://www.allegro.cc/manual/LOCK_FUNCTION" target="_blank"><span class="a">LOCK_FUNCTION</span></a><span class="k2">(</span>ticker<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>&#160;</td></tr><tr><td class="number">27</td><td>  <a href="http://www.allegro.cc/manual/install_timer" target="_blank"><span class="a">install_timer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>  <a href="http://www.allegro.cc/manual/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>  <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span><span class="n">24</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>  <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED, <span class="n">640</span>, <span class="n">480</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</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>ticker, BPS_TO_TIMER<span class="k2">(</span>BPS<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>  <span class="k1">unsigned</span> <span class="k1">char</span> doLoop <span class="k3">=</span> <span class="n">0xFF</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>&#160;</td></tr><tr><td class="number">34</td><td>  <span class="k1">while</span><span class="k2">(</span>doLoop<span class="k2">)</span></td></tr><tr><td class="number">35</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">36</td><td>    sem_wait<span class="k2">(</span><span class="k3">&amp;</span>timer_sem<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td>    <span class="k1">if</span> <span class="k2">(</span><a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span><span class="k2">)</span></td></tr><tr><td class="number">38</td><td>      doLoop <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">39</td><td>    <span class="c">//do stuff here</span></td></tr><tr><td class="number">40</td><td>    <a href="http://www.allegro.cc/manual/rectfill" target="_blank"><span class="a">rectfill</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">1</span>, <span class="n">1</span>, <span class="n">200</span>, <span class="n">20</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">41</td><td>    <a href="http://www.allegro.cc/manual/textprintf_ex" target="_blank"><span class="a">textprintf_ex</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>, <span class="n">10</span>, <span class="n">10</span>, <a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>, <span class="n">100</span>, <span class="n">200</span><span class="k2">)</span>,<span class="k3">-</span><span class="n">1</span>, <span class="s">"Time: %d"</span>, <a href="http://www.delorie.com/djgpp/doc/libc/libc_821.html" target="_blank">time</a><span class="k2">(</span>NULL<span class="k2">)</span> <span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">42</td><td>    <span class="c">//end of loop!</span></td></tr><tr><td class="number">43</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">44</td><td>&#160;</td></tr><tr><td class="number">45</td><td>  <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">46</td><td><span class="k2">}</span></td></tr><tr><td class="number">47</td><td><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Wed, 11 Apr 2007 03:47:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The thing about sem_wait is that if it gets behind, you get really funny speedups as it plays catch-up.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Wed, 11 Apr 2007 17:57:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Like when you alt-tab back to Diablo 2 after leaving it minimized for a few seconds?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Wed, 11 Apr 2007 18:38:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Kibizor said:</div><div class="quote"><p>

Like when you alt-tab back to Diablo 2 after leaving it minimized for a few seconds?
</p></div></div><p>

Like that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (GullRaDriel)</author>
		<pubDate>Wed, 11 Apr 2007 18:40:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, this is the third time I&#39;ve typed this answer...Allegro.cc keeps logging me out for whatever reason and the answer gets lost...</p><p>Anyway, the only solution I&#39;ve found that effectively yields the CPU is to omit the vsync() and include rest(1). However, update_logic() was then executed so seldomly that frames_missed got really high and as a result I had frame rates of around 1 frame every five seconds. I tried limiting frames_missed to four, but that just made the game extremely slow and extremely choppy, although CPU usage was at about 80% there.</p><p>Edit: I&#39;ve just noticed that the title screen, which displays a large sprite in the middle of the screen, two textout_ex&#39;s and 500 pixel &quot;stars&quot; in the background is chomping up 90% CPU time on an Athlon 2400 - is Allegro&#39;s Linux graphics driver really that bad?</p><p>Edit 2: OK, I have the game running doing everything except blitting the backbuffer to the screen, and it runs at a fairly respectable 45% CPU usage. As soon as I do blit(display,screen,0,0,0,0,800,600) though, it just flats out to 90-95%...isn&#39;t there a faster way to blit display to screen?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Wed, 11 Apr 2007 23:16:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>clippy says</b> It looks like you are trying to profile a program. Would you like help?</p><p>a) use a profiler to actually determine how much time is spent in each function<br />b) understand how cpu time is measured and improve your yielding (rest doesn&#39;t cut it!)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Thu, 12 Apr 2007 00:16:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks - I&#39;ll give it a go, if only to bring the 45% down a bit. Still, I&#39;d love to know why a single Allegro function call is chomping up 50% of my CPU time. Just chopping out that single blit call brought it down from 95% to 45%...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Thu, 12 Apr 2007 01:00:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The thing about sem_wait is that if it gets behind, you get really funny speedups as it plays catch-up.
</p></div></div><p>
How so? Can&#39;t you do frame dropping?
</p><div class="source-code snippet"><div class="inner"><pre>sem_wait<span class="k2">(</span><span class="k3">&amp;</span>my_sem<span class="k2">)</span><span class="k2">;</span>
<span class="k1">do</span> <span class="k2">{</span>
    logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span> <span class="k1">while</span> <span class="k2">(</span>sem_trywait<span class="k2">(</span><span class="k3">&amp;</span>my_sem<span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>

draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 12 Apr 2007 01:46:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I&#39;ll give it a go, if only to bring the 45% down a bit. Still, I&#39;d love to know why a single Allegro function call is chomping up 50% of my CPU time. Just chopping out that single blit call brought it down from 95% to 45%..
</p></div></div><p>

What i actually mean is that how can you really be sure you&#39;re actually spending that much cpu time in a single blit. I really doubt it. </p><p>Rest is not an accurate function. Windows is not a real-time operating system (though linux has patches). This means that if you rest(1) it may take 20ms to return to the program or it may return right away, execute a loop, go to sleep, return right away, etc... This is all scheduling dependant.. so many adding a blit changes how windows schedules your process.</p><p>A profiler will actually tell you how much time you spend in each function. In my example code above. i have a simple rectfill.. When i change rectfill to cover the entire screen (with a gray colour) my cpu usage becomes:</p><pre>real    1m0.350s
user    0m0.884s
sys     0m0.052s
</pre><p>

So.. there&#39;s some expense in rectfill. Blitting should be comparable in runtime.</p><p>Now gprof (my profiler!) outputs: Clearly you can see that after just a 
</p><pre>  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00     4149     0.00     0.00  rectFill
  0.00      0.00     0.00     4149     0.00     0.00  textFill
  0.00      0.00     0.00        1     0.00     0.00  mainLoop
</pre><p>

Now that&#39;s funny. The time spent is too small to accurately measure. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>edit: there must be a way to do fixed width fonts in this forum!</p><p>edit2: thanks baf! and weird.. the post-preview doesn&#39;t use the same font.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Thu, 12 Apr 2007 02:05:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can use the [pre][/pre] tags.</p><pre>
Pretty fixed widthness!
</pre><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (BAF)</author>
		<pubDate>Thu, 12 Apr 2007 03:05:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
How so? Can&#39;t you do frame dropping?
</p></div></div><p>No, there&#39;s no way to set a semaphore to 0 again (aside from repeatedly polling it, so this would work, but looks ugly):</p><div class="source-code snippet"><div class="inner"><pre>sem_wait<span class="k2">(</span><span class="k3">&amp;</span>sem<span class="k2">)</span><span class="k2">;</span>
<span class="k1">while</span><span class="k2">(</span>sem_trywait<span class="k2">(</span><span class="k3">&amp;</span>sem<span class="k2">)</span> <span class="k3">!</span><span class="k3">=</span> <span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span>
logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Thu, 12 Apr 2007 09:29:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Frame dropping doesn&#39;t mean dropping logic frames, it means dropping rendered frames <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /> If you want to implement a maximum-allowed skip:
</p><div class="source-code snippet"><div class="inner"><pre>sem_wait<span class="k2">(</span><span class="k3">&amp;</span>my_sem<span class="k2">)</span><span class="k2">;</span>
<span class="k1">do</span> <span class="k2">{</span>
    logic<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">while</span> <span class="k2">(</span>sem_trywait<span class="k2">(</span><span class="k3">&amp;</span>my_sem<span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span> <span class="k3">&amp;</span><span class="k3">&amp;</span> <span class="k3">+</span><span class="k3">+</span>skip <span class="k3">&lt;</span> MAX_SKIP<span class="k2">)</span><span class="k2">;</span>

<span class="k1">if</span> <span class="k2">(</span>skip <span class="k3">&gt;</span><span class="k3">=</span> MAX_SKIP<span class="k2">)</span>
    <span class="k1">while</span> <span class="k2">(</span>sem_trywait<span class="k2">(</span><span class="k3">&amp;</span>my_sem<span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span> <span class="c">/* do nothing */</span><span class="k2">;</span>
skip <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>

draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
Alternatively, instead of the empty sem_trywait loop, you could destroy and re-init the semaphore, but that&#39;s likely not efficient.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Thu, 12 Apr 2007 09:41:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, I&#39;ve profiled it, and I&#39;m still trying to make head or tail of the output. The interesting bit seems to be:</p><pre>
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  us/call  us/call  name    
 81.27      0.13     0.13                             pmask_load_func
  6.25      0.14     0.01     1762     5.68     5.68  check_for_next_level()
  6.25      0.15     0.01      875    11.43    11.43  display_background()
  6.25      0.16     0.01                             main
  0.00      0.16     0.00     1762     0.00     0.00  read_input()
  0.00      0.16     0.00     1762     0.00     0.00  process_ufo()
  0.00      0.16     0.00     1762     0.00     5.68  update_logic()
  0.00      0.16     0.00     1762     0.00     0.00  check_if_game_over()
  0.00      0.16     0.00     1762     0.00     0.00  collision_detection()
  0.00      0.16     0.00     1762     0.00     0.00  move_automatic_items()
  0.00      0.16     0.00     1762     0.00     0.00  check_if_extra_life_due()
  0.00      0.16     0.00     1762     0.00     0.00  process_enemy_projectiles()
  0.00      0.16     0.00      876     0.00     0.00  game_still_active()
  0.00      0.16     0.00      875     0.00    11.43  game_display()
</pre><p>

pmask_load_func obviously belongs to pmask although I can&#39;t imagine why it reports it as taking up 81.27% of the processing time. Commenting out the call to collision_detection() (which contains the only references to pmask.h in the entire loop) does nothing for the performance, commenting out the blit call reduces usage by around 50%.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Thu, 12 Apr 2007 21:04:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It looks like you just started the game and quit it and called it &quot;profiling&quot;. You have to actually run the code for a while.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Fri, 13 Apr 2007 00:36:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I started the game as usual, played it through three levels, lost, went back to the title screen and then exited the game cleanly. I know the profiler needs to be able to have used all of the functions available and it did get the chance...</p><p>Edit: I&#39;ve attached a newly created profile which I created from playing the game for 15 minutes straight.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Fri, 13 Apr 2007 00:47:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well i&#39;m not exactly sure what all those functions do and how the code is structured.. but it appears to me like there really is no bottleneck per say in your code. Just look at the cumulative time. If your program ran for 15 minutes none of those functions really ate all that much time.</p><pre>
 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this 
	   function is profiled, else blank.
</pre><p>

So yours is actually listed in microseconds (us) per call. So if you add up the drawing commands that is not accounting for 7 minutes of total time (giving roughly 50% cpu usage). </p><p>Now i&#39;m assuming your game loop is in the main function. If you took it out of main and put it in its own function then you could easily use the first table and fill in some of the gaps.</p><p>But it doesn&#39;t look like your code it slow at all. The &quot;reported&quot; cpu usage is probably coming from system scheduling.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Fri, 13 Apr 2007 02:35:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>OK, I found out by using &quot;top&quot; the CPU usage of each process, and I found out something rather interesting - the game itself never takes up more than 60% of the CPU, even at the most intense moment. Xorg, on the other hand, shoots up when the game is running for whatever reason. I&#39;m not sure how I can create a log which would test the same thing running in fullscreen mode. Anyway, I don&#39;t think I can hope for much better performance at this time, short of switching the whole thing to OpenGL (which I&#39;d rather avoid given that it shouldn&#39;t really be necessary).</p><p>Anyway, thanks to all those that contributed with tips and help!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Fri, 13 Apr 2007 12:16:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would just do something like:</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">int</span> main<span class="k2">(</span><span class="k1">int</span> argc, <span class="k1">char</span> <span class="k3">*</span>argv<span class="k2">[</span><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>    <a href="http://www.allegro.cc/manual/allegro_init" target="_blank"><span class="a">allegro_init</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">4</td><td>    <a href="http://www.allegro.cc/manual/set_color_depth" target="_blank"><span class="a">set_color_depth</span></a><span class="k2">(</span><span class="n">32</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>    <a href="http://www.allegro.cc/manual/set_gfx_mode" target="_blank"><span class="a">set_gfx_mode</span></a><span class="k2">(</span>GFX_AUTODETECT_WINDOWED,<span class="n">640</span>,<span class="n">480</span>,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>    <a href="http://www.allegro.cc/manual/set_color_conversion" target="_blank"><span class="a">set_color_conversion</span></a><span class="k2">(</span>COLORCONV_TOTAL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>    text_mode<span class="k2">(</span><span class="k3">-</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    <a href="http://www.allegro.cc/manual/install_timer" target="_blank"><span class="a">install_timer</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>    <a href="http://www.allegro.cc/manual/install_keyboard" target="_blank"><span class="a">install_keyboard</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">10</td><td>    <a href="http://www.allegro.cc/manual/install_mouse" target="_blank"><span class="a">install_mouse</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    textprintf<span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,<a href="http://www.allegro.cc/manual/font" target="_blank"><span class="a">font</span></a>,<span class="n">0</span>,<span class="n">0</span>,<a href="http://www.allegro.cc/manual/makecol" target="_blank"><span class="a">makecol</span></a><span class="k2">(</span><span class="n">255</span>,<span class="n">255</span>,<span class="n">255</span><span class="k2">)</span>,<span class="s">"Press ESC to exit..."</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>    <a href="http://www.allegro.cc/manual/show_mouse" target="_blank"><span class="a">show_mouse</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>    </td></tr><tr><td class="number">14</td><td>    <span class="k1">bool</span> quit <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">15</td><td>    <span class="k1">bool</span> redraw <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>    </td></tr><tr><td class="number">17</td><td>    <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>imageBuffer <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_bitmap" target="_blank"><span class="a">create_bitmap</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/SCREEN_W" target="_blank"><span class="a">SCREEN_W</span></a>,<a href="http://www.allegro.cc/manual/SCREEN_H" target="_blank"><span class="a">SCREEN_H</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>    <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span>imageBuffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>    </td></tr><tr><td class="number">20</td><td>    <span class="k1">while</span> <span class="k2">(</span> <span class="k3">!</span>quit <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>  <span class="k1">if</span> <span class="k2">(</span> <a href="http://www.allegro.cc/manual/keypressed" target="_blank"><span class="a">keypressed</span></a><span class="k2">(</span><span class="k2">)</span> <span class="k2">)</span></td></tr><tr><td class="number">23</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">24</td><td>      <span class="k1">if</span> <span class="k2">(</span> <a href="http://www.allegro.cc/manual/key" target="_blank"><span class="a">key</span></a><span class="k2">[</span>KEY_ESC<span class="k2">]</span> <span class="k2">)</span></td></tr><tr><td class="number">25</td><td>      <span class="k2">{</span></td></tr><tr><td class="number">26</td><td>    quit <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>    <a href="http://www.allegro.cc/manual/clear_keybuf" target="_blank"><span class="a">clear_keybuf</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">29</td><td>      redraw <span class="k3">=</span> <span class="k1">true</span><span class="k2">;</span></td></tr><tr><td class="number">30</td><td>      <a href="http://www.allegro.cc/manual/clear_keybuf" target="_blank"><span class="a">clear_keybuf</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">32</td><td>  <span class="k1">if</span> <span class="k2">(</span> redraw <span class="k2">)</span></td></tr><tr><td class="number">33</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">34</td><td>      redraw <span class="k3">=</span> <span class="k1">false</span><span class="k2">;</span></td></tr><tr><td class="number">35</td><td>      <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span>imageBuffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">36</td><td>      <span class="c">// Do drawing...</span></td></tr><tr><td class="number">37</td><td>      </td></tr><tr><td class="number">38</td><td>      <span class="c">// End drawing...</span></td></tr><tr><td class="number">39</td><td>      <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>imageBuffer,<a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,imageBuffer-&gt;w,imageBuffer-&gt;h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">40</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">41</td><td>  <span class="k1">else</span></td></tr><tr><td class="number">42</td><td>  <span class="k2">{</span></td></tr><tr><td class="number">43</td><td>      <span class="c">// Update ai and yield unused time to system...</span></td></tr><tr><td class="number">44</td><td>      <a href="http://www.allegro.cc/manual/rest" target="_blank"><span class="a">rest</span></a><span class="k2">(</span><span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">45</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">46</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">47</td><td>    <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>imageBuffer<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">48</td><td>    <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">49</td><td><span class="k2">}</span><a href="http://www.allegro.cc/manual/END_OF_MAIN" target="_blank"><span class="a">END_OF_MAIN</span></a><span class="k2">(</span><span class="k2">)</span></td></tr></tbody></table></div></div><p>

This tells the system to yield until some event(redraw), which is in this case a key pressed, has occurred. It is up to you and your game logic to set this variable when you need to take control of the processing again. rest(0) and yield_timeslice never has seemed to work for me in windows or linux (several different flavors)...just pass rest(1) and I get the cpu to go to 0%...even during game play!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Sat, 14 Apr 2007 23:08:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>i havent done any game programming in a while, so take my question with a grain of salt.</p><p>Wouldn&#39;t it be better to use threads instead of loop+timer/frame rate counter?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (le_y_mistar)</author>
		<pubDate>Sat, 14 Apr 2007 23:36:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t think there IS a right or wrong answer to most of game programming...It is what is right for YOU. I have finally decided this after reading posts and books all of which contradict each other. It is ultimately up to you, and what you like. YOU are the one who has to code it and probably maintain it. <br />Each choice you have asked about has it&#39;s weakness and strengths. It is up to you to decide which problems and hurdles you wish to face...I DO know that threads can get you into a LOT of trouble if you are not careful...but you can do a lot of stuff you might not be able to do (at least no easily) without them...I would also imagine threads would be harder to debug as well...but I am certainly not a master of the art...:)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Sat, 14 Apr 2007 23:49:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Don Freeman said:</div><div class="quote"><p>
rest(0) and yield_timeslice never has seemed to work for me in windows or linux (several different flavors)...
</p></div></div><p>
That&#39;s because they&#39;re not <i>supposed</i> to reduce CPU usage. They yield to other processes. That&#39;s it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sat, 14 Apr 2007 23:57:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Interesting though.. goalie&#39;s method of using semaphores seems to work quite well. If you need finer grain control implement your own semaphore class by wrapping one.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goalie Ca)</author>
		<pubDate>Sun, 15 Apr 2007 00:09:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, as I understand it, there have been three ways here suggested to reduce CPU usage:</p><p>(a) Use rest(1) somewhere in the game loop. The least effective, but the easiest to implement.</p><p>(b) Use the Semaphore library. More effective, but a bit more difficult to use. I may tackle this when I&#39;m a little more confident with my programming.</p><p>(c) Get straight down to the grit of it and use threads. The most efficient method possible, but can be extremely unstable if you don&#39;t know what you&#39;re doing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (jamyskis)</author>
		<pubDate>Sun, 15 Apr 2007 20:01:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
(b) Use the Semaphore library. More effective, but a bit more difficult to use. I may tackle this when I&#39;m a little more confident with my programming.
</p></div></div><p>What&#39;s this &quot;Semaphore library&quot; you&#39;re talking about? You mean pthreads.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
(c) Get straight down to the grit of it and use threads. The most efficient method possible, but can be extremely unstable if you don&#39;t know what you&#39;re doing.
</p></div></div><p>I fail to see how this reduces CPU usage. In fact, it increases CPU usage, as now you have two busy loops running around <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CGamesPlay)</author>
		<pubDate>Sun, 15 Apr 2007 20:39:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well, as I understand it, there have been three ways here suggested to reduce CPU usage:
</p></div></div><p>
My method is, as it has always been:
</p><ul><li><p>do a frame draw</p></li><li><p>read a counter, compute number of milliseconds/whatever since last here in loop</p></li><li><p>if now one or more logic ticks behind, do that many logic ticks</p></li><li><p>otherwise sleep for the number of milliseconds/whatever left before next update</p></li></ul><p>
In SDL I use SDL_GetTicks which returns the number of milliseconds since the app started. In Allegro I have a second thread that updates a timer that is much coarser than milliseconds but gets the job done. My ChristmasHack &#39;05 entry, Nuclear Attack! (<a href="http://members.allegro.cc/ThomasHarte/files/christmashack/NuclearAttackWin32.zip">Windows</a>, <a href="http://members.allegro.cc/ThomasHarte/files/christmashack/NuclearAttack.dmg">OS X</a>) is one example. On a virtualised copy of Windows 2000 on my MacBook Pro, it uses less than 5% of one CPU core.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Harte)</author>
		<pubDate>Sun, 15 Apr 2007 20:57:21 +0000</pubDate>
	</item>
</rss>
