<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Best performance when loading Bitmaps</title>
		<link>http://www.allegro.cc/forums/view/608463</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 29 Sep 2011 02:34:56 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello everyone.</p><p>I am making a shot&#39;em up game, and now i am programming the weapons. I make a class for a weapon, and the class have an ALLEGRO_BITMAP for the sprite of the shoot. My question is if it&#39;s better to load the bitmap one time as a static const member, or loading and destroying it every time the bullet is fired and destroyed.</p><p>I had try first without the static member, and it was ok, but now i try the static const way and i get and error:</p><p>class Weapon1{<br />private:<br />	static const ALLEGRO_BITMAP *sprite = al_load_bitmap(&quot;sprites/bullet1.png&quot;);</p><p>......</p><p>&quot;error C2864: &#39;Weapon1::sprite&#39; : only static const integral data members can be initialized within a class&quot;</p><p>Then i create &quot;sprite&quot; as static, not const, and i initialized it before the main class but i get a memory error:<br />&quot;Unhandled exception at 0x00000000 in Thunder Force IV.exe: 0xC0000005: Access violation reading location 0x00000000.&quot;</p><p>class Weapon1{<br />private:<br />	static ALLEGRO_BITMAP *sprite;</p><p>.....</p><p>ALLEGRO_BITMAP *Weapon1::sprite = al_load_bitmap(&quot;sprites/bullet1.png&quot;);</p><p>int main(void)<br />{</p><p>....</p><p>Thanks for reading, i&#39;m awaiting your help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Tue, 27 Sep 2011 19:13:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>#1 is bad, since you want to store your resources earlier in the program cycle. When you launch the level, you load your bullet resources. If you have multiple guns, preload them all; not when you&#39;ve picked up your new gun.</p><p>#2 is going to be impractical, since it takes longer to access the hard drive and load the bitmap of the bullet than it takes for the bullet to be fired AND &quot;die&quot;.</p><p>So preload it.  In your main <span class="source-code">load_level<span class="k2">(</span><span class="k1">int</span> which_level<span class="k2">)</span></span> function, determine which bullets/weapons you&#39;ll find in this level. Preload the images there. Once the level is over and the player has died or passed it, unload the images.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (OnlineCop)</author>
		<pubDate>Tue, 27 Sep 2011 20:29:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You cannot load an image before you initialize Allegro.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Tue, 27 Sep 2011 22:23:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your answers. I understand what you said and i think you are right, that&#39;s what i going to do.</p><p><img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=";D" border="0" /> i can&#39;t load images before load Allegro...i dind&#39;t realize that, what a fail, thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Tue, 27 Sep 2011 22:26:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I like to do one more thing: Only load each image file once, and then have many ALLEGRO_BITMAP pointers point to the same image.</p><p>In other words, only load the ALLEGRO_BITMAP for your bullet once, and make every bullet object use the same ALLEGRO_BITMAP.</p><p>I discovered many years ago that doing it this way gave me much better performance. I assume it&#39;s still true. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (David Couzelis)</author>
		<pubDate>Tue, 27 Sep 2011 23:18:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>With your advice, i create in &quot;level&quot; class a sprite cache, in this case i try a matrix of ALLEGRO_BITMAP because i want to have the sprites grouped, where every row represents a weapon and the columns are the sprites of that weapon. Now i have a problem, is a C++ problem not an Allegro one, but i can&#39;t find a solution in my books or in google, so i need your help again to create and destroy a matrix of ALLEGRO_BITMAP:</p><p>class Level{<br />private:<br />ALLEGRO_BITMAP ***weapon_sprites;<br />public:<br />Level();<br />~Level(){ ? ? ? ? ? }</p><p>.....</p><p>Level::Level(){<br />weapon_sprites = ? ? ? ? ? //I need a 7x5 matrix</p><p>Thanks again for your help
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Wed, 28 Sep 2011 19:22:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;d use <span class="source-code"><span class="k1">new</span></span> and a loop to allocate your array of bitmaps, something like:</p><div class="source-code snippet"><div class="inner"><pre>weapon_sprites <span class="k3">=</span> <span class="k1">new</span> <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span><span class="k2">[</span>number_of_sprites<span class="k2">]</span><span class="k2">;</span>
<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> number_of_sprites<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span> weapon_sprites <span class="k3">=</span> load_sprite<span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span> <span class="k2">}</span>
</pre></div></div><p>


If you find performance isn&#39;t as good as you like it, you may want to consider storing as many sprites as possible in a single bitmap, and use the <span class="source-code"><a href="http://www.allegro.cc/manual/al_hold_bitmap_drawing"><span class="a">al_hold_bitmap_drawing</span></a></span> function to group all sprites from the same bitmap together.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 29 Sep 2011 00:18:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for your help, but what i want to do is a matrix of ALLEGRO_BITMAP, not an array. I know how to do an array, but i don&#39;t know how to create and initialize an array of arrays of ALLEGRO_BITMAP, and how it has to be deleted.</p><p>I find the description of the function &quot;al_hold_bitmap_drawing&quot; very usefull, but i&#39;m still learning c++ and Allegro and my project is in an early stage, althought i will use this function very soon, thanks for it.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Thu, 29 Sep 2011 01:56:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A matrix is a 2D array, or if you&#39;re allocating it dynamically, you have to do the y * width + x thing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Thu, 29 Sep 2011 01:59:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I would appreciate if you put me a simple example filling my example above, thanks for your help.</p><p>EDIT: Oh i do know now what you mean. I had thought that way, but i think it is less intuitive than a matrix, but if it is easier or simply better i will do that way.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Thu, 29 Sep 2011 02:21:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>A matrix just adds an extra *...</p><p>something like:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span><span class="k3">*</span><span class="k3">*</span>weapon_sprites <span class="k3">=</span> <span class="k1">new</span> <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span><span class="k3">*</span><span class="k2">[</span>rows<span class="k2">]</span><span class="k2">;</span>
<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> rows<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
   weapon_sprites<span class="k2">[</span>i<span class="k2">]</span> <span class="k3">=</span> <span class="k1">new</span> <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a><span class="k3">*</span><span class="k2">[</span>columns<span class="k2">]</span><span class="k2">;</span>
   <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> j <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> j <span class="k3">&lt;</span> columns<span class="k2">;</span> j<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
      weapon_sprites<span class="k2">[</span>i<span class="k2">]</span><span class="k2">[</span>j<span class="k2">]</span> <span class="k3">=</span> load_sprite<span class="k2">(</span>...<span class="k2">)</span><span class="k2">;</span>
   <span class="k2">}</span>
<span class="k2">}</span>
</pre></div></div><p>

Thats just pseudo code. You&#39;ll have to figure out how to fit it into your own setup.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Thu, 29 Sep 2011 02:24:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>For better performance, you should put as many of your sprite images as you can on large bitmaps, and then use <span class="source-code"><a href="http://www.allegro.cc/manual/al_create_sub_bitmap"><span class="a">al_create_sub_bitmap</span></a></span> and/or <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_bitmap_region"><span class="a">al_draw_bitmap_region</span></a></span> to handle drawing. This allows you to use <span class="source-code"><a href="http://www.allegro.cc/manual/al_hold_bitmap_drawing"><span class="a">al_hold_bitmap_drawing</span></a></span> quite effectively, which really improves performance. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (amber)</author>
		<pubDate>Thu, 29 Sep 2011 02:34:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for the code. I discover what i was doing wrong and why it returned a costructor error. That confused me and it makes me crazy. I&#39;m very grateful for your help.</p><p>I will try that functions you suggest me, but i feel a bit overwhelmed because i&#39;m still beggining with Allegro, but i promise to learn and use these functions.</p><p>Thanks to all people that help begginers like me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Manveru)</author>
		<pubDate>Thu, 29 Sep 2011 02:34:56 +0000</pubDate>
	</item>
</rss>
