<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Vector questions</title>
		<link>http://www.allegro.cc/forums/view/590186</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 23 Feb 2007 11:51:51 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, this one&#39;s pretty simple. I&#39;m going to reduce this example to a very minor part of my project.</p><p>In my game I have a bullet struct followed by a vector with that struct as it&#39;s elements. (See code below) The problem occurs after I push_back around 8,000 elements of this vector. Things run great until then, but at somewhere around this point in the application it hangs up (only graphically it would seem.) Once the bullets hit something and tell the vector to remove themselves, everything resumes.</p><p>Not sure if this is a code problem or I just don&#39;t understand something about vectors. Here&#39;s the code I have for this problem:</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">struct</span> bullets<span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="k1">float</span> x,y<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  <span class="k1">float</span> velX,velY<span class="k2">;</span></td></tr><tr><td class="number">4</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">5</td><td>vector<span class="k3">&lt;</span>bullets&gt;bullet<span class="k2">;</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td><span class="k1">void</span> update_bullets<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">8</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">&lt;</span>bullet.size<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">9</td><td>    <span class="k1">if</span><span class="k2">(</span>bullet.at<span class="k2">(</span>i<span class="k2">)</span><span class="k2">)</span> <span class="k2">{</span> <span class="c">//check to see if it's there</span></td></tr><tr><td class="number">10</td><td>      bullet<span class="k3">&lt;</span>i&gt;.x<span class="k3">+</span><span class="k3">=</span>bullet<span class="k3">&lt;</span>i&gt;.velX<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>      bullet<span class="k3">&lt;</span>i&gt;.y<span class="k3">+</span><span class="k3">=</span>bullet<span class="k3">&lt;</span>i&gt;.velY<span class="k2">;</span></td></tr><tr><td class="number">12</td><td>      <span class="k1">if</span><span class="k2">(</span>collided_with_object<span class="k2">)</span></td></tr><tr><td class="number">13</td><td>        bullet.erase<span class="k2">(</span>bullet.begin<span class="k2">(</span><span class="k2">)</span><span class="k3">+</span>i<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>&#160;</td></tr><tr><td class="number">17</td><td><span class="k1">void</span> draw_bullets<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">18</td><td>  <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> i<span class="k3">=</span><span class="n">0</span><span class="k2">;</span> i<span class="k3">&lt;</span>bullet.size<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">19</td><td>    <span class="k1">if</span><span class="k2">(</span>bullet.at<span class="k2">(</span>i<span class="k2">)</span><span class="k2">)</span> <span class="c">//check to see if it's there</span></td></tr><tr><td class="number">20</td><td>      <a href="http://www.allegro.cc/manual/putpixel" target="_blank"><span class="a">putpixel</span></a><span class="k2">(</span>buffer,bullet<span class="k3">&lt;</span>i&gt;.x,bullet<span class="k3">&lt;</span>i&gt;.y,<span class="n">3</span><span class="k2">)</span><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>main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">24</td><td>  <span class="k1">while</span><span class="k2">(</span>game_loop<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">25</td><td>    <span class="c">//in the real application I have the timer setup correctly =P</span></td></tr><tr><td class="number">26</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_SPACE<span class="k2">]</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">27</td><td>      bullets b<span class="k3">=</span><span class="k2">{</span><span class="n">0</span>,<span class="n">0</span>,<span class="n">10</span>,<span class="n">0</span><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">28</td><td>      bullet.push_back<span class="k2">(</span>b<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">30</td><td>    update_bullets<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>    draw_bullets<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>    <span class="c">//buffer the screen</span></td></tr><tr><td class="number">33</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">34</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Wed, 21 Feb 2007 22:56:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It might be better to use a pointer to the struct, like so:<br /><span class="source-code">vector<span class="k3">&lt;</span>bullets<span class="k3">*</span><span class="k3">&gt;</span>bullet<span class="k2">;</span></span><br />Because a vector would need to keep copying that struct over when it needs to resize - instead, a vector of pointers would be smaller and quicker to organise.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
for(int i=0; i&lt;bullet.size(); i++)
</p></div></div><p>
You should use an iterator, I think that it&#39;s like (someone should correct this):
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span>vector<span class="k3">&lt;</span>bullets&gt;::iterator i <span class="k3">=</span> bullet.start<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> i <span class="k3">!</span><span class="k3">=</span> bullet.end<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span> <span class="c">/*nothing here*/</span><span class="k2">)</span>
</pre></div></div><p>
and when you erase a bullet, it&#39;s like
</p><div class="source-code snippet"><div class="inner"><pre>    <span class="k1">if</span><span class="k2">(</span>collided_with_object<span class="k2">)</span>
    <span class="k2">{</span>
        bullet.erase<span class="k2">(</span><span class="k3">*</span><span class="k2">(</span><span class="k3">&amp;</span>i<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
        i <span class="k3">=</span> bullet.delete<span class="k2">(</span>i<span class="k2">)</span><span class="k2">;</span>
    <span class="k2">}</span>
    <span class="k1">else</span>
    <span class="k2">{</span>
        i<span class="k3">+</span><span class="k3">+</span><span class="k2">;</span>
    <span class="k2">}</span>
</pre></div></div><p>
Something like that ^.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Wed, 21 Feb 2007 23:11:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Add your timer and screen buffering to the code you provided. There could be a problem there.</p><p>Also, something to keep in mind here is that vectors are not efficient for deleting elements in the middle of them, only for pushing and popping. I recommend you change your bullet.erase() line with the following:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>collided_with_object<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>i <span class="k3">=</span><span class="k3">=</span> bullet.size<span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span>
    bullet.pop_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <span class="k1">else</span> <span class="k2">{</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_566.html" target="_blank">memcpy</a><span class="k2">(</span><span class="k3">&amp;</span>bullet<span class="k3">&lt;</span>i&gt;,<span class="k3">&amp;</span>bullet.back<span class="k2">(</span><span class="k2">)</span>,<span class="k1">sizeof</span><span class="k2">(</span>bullets<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
    bullet.pop_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Granted, this is not an OOP compliant solution, but if you want to keep using vector objects, it&#39;s a fast way to speed up the erasing of your bullets.</p><p>When you have 8000 objects in your vector and you delete, say, the 5th one, there&#39;s about 7994 reallocations of memory which must occur. (If I understand how vectors work.)</p><p>It&#39;s a lot more efficient to set a limit, allocate the memory ahead of time, then work within that limit so you&#39;re not constantly allocating and deallocating memory.</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>Wed, 21 Feb 2007 23:15:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I recommend you change your bullet.erase() line with the following
</p></div></div><p>
If you don&#39;t know what he&#39;s doing, he&#39;s swapping the last vector item with a &#39;dead&#39; one in the middle so that the vector doesn&#39;t need to shift down all of the items towards the end. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>But you still need to delete the item at the end...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Wed, 21 Feb 2007 23:22:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay thanks a lot, guys. This is helping a lot. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /></p><p>I have a question concerning Kris&#39;s snippet. Were you declaring <tt>i</tt> as an <tt>int</tt> or <tt>vector&lt;bullets*&gt;::iterator</tt>?
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>collided_with_object<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>i <span class="k3">=</span><span class="k3">=</span> bullet.size<span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span>
</pre></div></div><p>

</p><div class="quote_container"><div class="title">Kris Asick said:</div><div class="quote"><p>
but if you want to keep using vector objects, it&#39;s a fast way to speed up the erasing of your bullets.
</p></div></div><p>
Is there a better way? I&#39;m definitely up to changing my code completely at this point. Is it better to go with a predelcared size so that you&#39;re not resizing a ton every game cycle?</p><p>Here&#39;s the code for my timer, though it&#39;s not easily readable and I can&#39;t imagine it being very helpful.
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="c">//blah blah all this junk.</span></td></tr><tr><td class="number">3</td><td>  <span class="c">//I've already declared the variable and function of course</span></td></tr><tr><td class="number">4</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">5</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">6</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">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>exit_now<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">9</td><td>    <span class="k1">while</span><span class="k2">(</span>speed_counter&gt;0<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">10</td><td>      game_logic.update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>   <span class="c">//all the logic</span></td></tr><tr><td class="number">11</td><td>      speed_counter--<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>    game_logic.draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//all the drawing</span></td></tr><tr><td class="number">14</td><td>    game_logic.buff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//buffer it (see below)</span></td></tr><tr><td class="number">15</td><td>    yield_timeslice<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></td></tr><tr><td class="number">17</td><td><span class="k2">}</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td><span class="k1">void</span> game_logic::buff<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">20</td><td>  <a href="http://www.allegro.cc/manual/acquire_screen" target="_blank"><span class="a">acquire_screen</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</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">22</td><td>  <a href="http://www.allegro.cc/manual/stretch_blit" target="_blank"><span class="a">stretch_blit</span></a><span class="k2">(</span>buffer,<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>,buffer-&gt;w,buffer-&gt;h,<span class="n">0</span>,<span class="n">0</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">23</td><td>  </td></tr><tr><td class="number">24</td><td>  <span class="k1">if</span><span class="k2">(</span>draw_mouse<span class="k2">)</span></td></tr><tr><td class="number">25</td><td>    <a href="http://www.allegro.cc/manual/draw_sprite" target="_blank"><span class="a">draw_sprite</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/mouse_sprite" target="_blank"><span class="a">mouse_sprite</span></a>,<a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a>,<a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>  </td></tr><tr><td class="number">27</td><td>  <a href="http://www.allegro.cc/manual/release_screen" target="_blank"><span class="a">release_screen</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/clear_to_color" target="_blank"><span class="a">clear_to_color</span></a><span class="k2">(</span>buffer,<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">29</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

[edit] Gosh I edit a lot
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Thu, 22 Feb 2007 01:12:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>EDIT: I am only commenting on what people were saying on how to structure the list and not the main issue with the graphics freezing -- I didn&#39;t see anything wrong with the posted code.</p><p>---</p><p>I see conversations head this way all of the time, so for the bullet idea I will bring back what I did research on in the past with particles (bullets are the same thing practically). copy-on-death with a vector is the fastest way if bullets are small (yours are). You don&#39;t need to memcpy, you can just use assignment.</p><p><a href="http://www.gillius.org/articles/partmem.htm">http://www.gillius.org/articles/partmem.htm</a> -- VectorAlgor is what I&#39;m referring to -- and there is code.</p><p>Most people (and I used to all the time) use lists of pointers, but lists of pointers can be 4-5x slower than vectors with copy-on-death with 36 bytes. As the object size goes down the vector gets faster; your size is 16. As the object size increases, eventually pointer list and vector will match speed. In terms of memory usage, the vector version uses less as the object peak, but if you use it intelligently it will increase to the peak and stay there while the list version goes up and down, but at the peak the list uses more memory, therefore the vector solution has a lower upper-bound.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Thu, 22 Feb 2007 02:44:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><i>Get vsync() out of the area between acquire and release!</i></p><p>That could be the source of all your problems. Calling vsync() while a video bitmap is locked with the acquire commands can cause bad things to happen!</p><p>On my computer, that action would cause a program lock-up within the first split second of operation.</p><p>I also noticed I forgot a } in the code I posted, and I also noticed a way to improve it:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span>collided_with_object<span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">if</span> <span class="k2">(</span>i <span class="k3">&lt;</span> bullet.size<span class="k2">(</span><span class="k2">)</span> <span class="k3">-</span> <span class="n">1</span><span class="k2">)</span>
    <a href="http://www.delorie.com/djgpp/doc/libc/libc_566.html" target="_blank">memcpy</a><span class="k2">(</span><span class="k3">&amp;</span>bullet<span class="k3">&lt;</span>i&gt;,<span class="k3">&amp;</span>bullet.back<span class="k2">(</span><span class="k2">)</span>,<span class="k1">sizeof</span><span class="k2">(</span>bullets<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
  bullet.pop_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

<img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></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>Thu, 22 Feb 2007 03:20:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okee doke. <tt>vsync()</tt> before <tt>acquire_screen()</tt> then? If so, that didn&#39;t solve anything for me, but it&#39;s nice to know for later projects. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Thu, 22 Feb 2007 03:45:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;d draw mouse to buffer, not to screen.<br />Also, have you tried to drop the aquire_screen and/or vsync completely?</p><p>I haven&#39;t done any windows development for quite a few years but I don&#39;t remember not having any problems when not using them.</p><p>[edit]</p><p>Doh, you are using stretching. You might want to ignore this post. I haven&#39;t got enough sleep <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Thu, 22 Feb 2007 03:51:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, using iterators may have sped it up a little. The biggest help was Kris Asick&#39;s code. Thanks <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> I can handle around 40,000 instances now, which is a huge step up from 8,000!</p><p>I&#39;ll have to spend some extra time reading up on the link you provided, gillius.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Thu, 22 Feb 2007 05:38:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, I think we can assume something here...</p><p>Your code was working fine before, up until about 8,000 bullets, then died.</p><p>Now it works up to about 40,000 then dies.</p><p>Either way, <i>it still dies</i>, but improving the bullet removing code improved how many you could handle.</p><p>This suggests that the problem is in the way you are handling framerates below the target framerate. I would look at how you handle lower than normal framerates and see if that&#39;s the problem.</p><p><b>EDIT:</b> I figured the problem out. If your timer increments while the game logic is still being processed, it never runs the drawing code.</p><p>Try this...</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="k1">int</span> speed_counter_copy<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  <span class="c">//blah blah all this junk.</span></td></tr><tr><td class="number">4</td><td>  <span class="c">//I've already declared the variable and function of course</span></td></tr><tr><td class="number">5</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">6</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">7</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">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>exit_now<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">10</td><td>    speed_counter_copy <span class="k3">=</span> speed_counter<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    <span class="k1">while</span><span class="k2">(</span>speed_counter_copy&gt;0<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>      game_logic.update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>   <span class="c">//all the logic</span></td></tr><tr><td class="number">13</td><td>      speed_counter_copy--<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>    game_logic.draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//all the drawing</span></td></tr><tr><td class="number">16</td><td>    game_logic.buff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//buffer it (see below)</span></td></tr><tr><td class="number">17</td><td>    yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><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>Thu, 22 Feb 2007 08:06:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>My program doesn&#39;t really <b>die</b> either way. Like there&#39;s no errors or forcing the closing of the program. Pretty much just becomes a extremely laggy (like, up to 20 seconds of no visible updates... Just hard number crunching) at around 40,000 bullets.</p><p>About your counter code. It doesn&#39;t work, and I don&#39;t see why it would. What was it you were trying to do?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Thu, 22 Feb 2007 09:26:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;re right, the code I provided doesn&#39;t work... the problem is almost the same with it.</p><p>Here&#39;s what&#39;s happening with your original code. Your variable &quot;speed_counter&quot; is incremented by your 60 FPS timer, right? And you&#39;ve designed your second while loop to run while speed_counter is greater than 0.</p><p>So what happens when you&#39;re processing so much data that speed_counter increments while in the middle of processing logic? The while loop continues for another logic pass, which again takes so long that speed_counter increments, so it processes even more logic, and this whole time, ends up skipping your drawing routine which is outside the while loop.</p><p>My code, which I thought would work, doesn&#39;t because each time it processes the logic, it ends up with more of it to process each new frame and just stockpiles exponentially when the framerate drops.</p><p>I don&#39;t actually understand proper frame dropping techniques because I switched to doing realtime programming before I learned any, but that&#39;s what you need to look up because your frame dropping technique is where the problem is. Though frame dropping is usually for handling when <i>rendering</i> takes too long. If it&#39;s the game logic that&#39;s taking too long, you need to do something to decrease how much logic is processed.</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>Thu, 22 Feb 2007 22:42:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Kris,</p><p>Would you code work if you add the following?</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">2</td><td>  <span class="k1">int</span> speed_counter_copy<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>  <span class="c">//blah blah all this junk.</span></td></tr><tr><td class="number">4</td><td>  <span class="c">//I've already declared the variable and function of course</span></td></tr><tr><td class="number">5</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">6</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">7</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">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td>  <span class="k1">while</span><span class="k2">(</span><span class="k3">!</span>exit_now<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">10</td><td>    speed_counter_copy <span class="k3">=</span> speed_counter<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>    speed_counter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span>                   <span class="c">// &lt;&lt;&lt;----------------Line added </span></td></tr><tr><td class="number">12</td><td>    <span class="k1">while</span><span class="k2">(</span>speed_counter_copy&gt;0<span class="k2">)</span> <span class="k2">{</span></td></tr><tr><td class="number">13</td><td>      game_logic.update<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>   <span class="c">//all the logic</span></td></tr><tr><td class="number">14</td><td>      speed_counter_copy--<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>    game_logic.draw<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//all the drawing</span></td></tr><tr><td class="number">17</td><td>    game_logic.buff<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>       <span class="c">//buffer it (see below)</span></td></tr><tr><td class="number">18</td><td>    yield_timeslice<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">19</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">20</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HardTranceFan)</author>
		<pubDate>Fri, 23 Feb 2007 02:37:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yes, Kris is right frame dropping only works if the logic loop runs fast enough that the computer can keep up with logic alone. Since rendering takes a long time (especially with Allegro games), usually this assumption is that the logic is much less demanding than graphics.</p><p>If the logic loop can&#39;t keep up then you have problems because if rendering takes a lot more effort, then your game is going to run too slow.</p><p>The only solution if your game is fixed-rate logic is to have a maximum frame skip and run your game <i>slower than real-time</i> -- the computer just can&#39;t keep up with your game.</p><p>What HardTranceFan showed does something similar except that it isn&#39;t predictable. What will happen is that the frames will get farther and farther spaced out because every time it runs, speed_counter and therefore speed_counter_copy will get higher.</p><p>I&#39;ve published on here and also mentioned in several posts something to deal with it if you have dynamic rate logic, that had several ranges (dt being the logic frame duration for a single step)
</p><ol><li><p>If dt &gt; maxDt, dt = maxDt (run less than real-time)</p></li><li><p>If dt &gt; frameSkipDt (frame skipping)</p></li><li><p>If dt &gt; minDt (1:1 frame drawing, dynamic dt rate)</p></li><li><p>else, dt is &lt; minDt (1:1 frame drawing, fixed dt rate, CPU sleeping to reduce CPU usage</p></li></ol><p>

EDIT: BTW, having code to run less than real-time is especially important even for fast computers and fast games -- if your process is temporarily suspended because your user switched away from your app (alt-tab), suspended/slept/hibernated PC, a process like virus scan or firewall paused your PC, or a heavy disk app like defrag paused you, you don&#39;t want the game to go into super-fast zoom mode when it returns from the pause... you want it to stop, pause, and resume where it left off. If it&#39;s a network game, you can&#39;t do this, though.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Fri, 23 Feb 2007 02:46:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What HardTranceFan showed does something similar except that it isn&#39;t predictable. What will happen is that the frames will get farther and farther spaced out because every time it runs, speed_counter and therefore speed_counter_copy will get higher.
</p></div></div><p>

No, <i>my</i> code does that. HardTraceFan&#39;s code fixes the bug in mine, but the problem persists if the logic is overloaded. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></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>Fri, 23 Feb 2007 03:31:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, it helped! <img src="http://www.allegro.cc/forums/smileys/cool.gif" alt="8-)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Fri, 23 Feb 2007 04:57:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ha sorry Kris I get mixed up who posted what sometimes <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gillius)</author>
		<pubDate>Fri, 23 Feb 2007 11:51:51 +0000</pubDate>
	</item>
</rss>
