<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>create_bitmap and classes</title>
		<link>http://www.allegro.cc/forums/view/590578</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 17 Mar 2007 06:15:22 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello,</p><p>I am new to the Allegro library.  I recently began putting together my first Tetris clone, and I set up a class for the shapes.  One of the (currently public) data members of the class is BITMAP *images[4][2], which holds all possible rotations and a corresponding &quot;eraser&quot; image.  I designed a public member function to set up bitmaps for the rotations of the shape.  This is so that custom images can be put into the game.  When I try to &quot;create&quot; bitmaps for the other elements of the images array to place a roated sprite in, the program crashes.</p><p>I do not understand a great deal about memory allocation and other related subjects, but I assume that there this is due to something with the compiler not being happy about a class trying to mess around with memory after the variables were already initialized.  Of course, I could be completely wrong here.</p><p>Here are sections of the problematic code (again, I am somewhat new to this, so this may be very ugly):
</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">class</span> shape:<span class="k2">{</span></td></tr><tr><td class="number">2</td><td>public:</td></tr><tr><td class="number">3</td><td>   <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>images<span class="k2">[</span><span class="n">4</span><span class="k2">]</span><span class="k2">[</span><span class="n">2</span><span class="k2">]</span><span class="k2">;</span>   </td></tr><tr><td class="number">4</td><td>   <span class="c">//...other variables and method...</span></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td>   <span class="k1">int</span> setup<span class="k2">(</span><span class="k1">char</span> <span class="k3">*</span>image, <span class="k1">char</span> <span class="k3">*</span>eraser, <span class="k1">int</span> rotations<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td><span class="k2">}</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>  </td></tr><tr><td class="number">9</td><td><span class="k1">int</span> shape::setup<span class="k2">(</span><span class="k1">char</span> <span class="k3">*</span>image, <span class="k1">char</span> <span class="k3">*</span>eraser, <span class="k1">int</span> rotations<span class="k2">)</span></td></tr><tr><td class="number">10</td><td><span class="k2">{</span></td></tr><tr><td class="number">11</td><td>   <span class="c">//load the initial images</span></td></tr><tr><td class="number">12</td><td>   images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_bitmap" target="_blank"><span class="a">load_bitmap</span></a><span class="k2">(</span>image, NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>   images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span> <span class="k3">=</span> <a href="http://www.allegro.cc/manual/load_bitmap" target="_blank"><span class="a">load_bitmap</span></a><span class="k2">(</span>eraser, NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>   <span class="c">//error checking</span></td></tr><tr><td class="number">16</td><td>   <span class="k1">if</span> <span class="k2">(</span><span class="k3">!</span>images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">)</span><span class="k2">{</span>   </td></tr><tr><td class="number">17</td><td>      <a href="http://www.allegro.cc/manual/allegro_message" target="_blank"><span class="a">allegro_message</span></a><span class="k2">(</span><span class="s">"Error loading shape."</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>      <span class="k1">return</span> <span class="n">1</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="k1">if</span> <span class="k2">(</span><span class="k3">!</span>images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><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/allegro_message" target="_blank"><span class="a">allegro_message</span></a><span class="k2">(</span><span class="s">"Error loading eraser."</span><span class="k2">)</span><span class="k2">;</span>     </td></tr><tr><td class="number">22</td><td>      <span class="k1">return</span> <span class="n">1</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>&#160;</td></tr><tr><td class="number">25</td><td>   <span class="c">//setup all of the rotated images</span></td></tr><tr><td class="number">26</td><td>   <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> i <span class="k3">=</span> <span class="n">1</span><span class="k2">;</span> i <span class="k3">&lt;</span> rotations<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">27</td><td>      <span class="k1">if</span> <span class="k2">(</span>i % <span class="n">2</span> <span class="k3">=</span><span class="k3">=</span> <span class="n">0</span><span class="k2">)</span><span class="k2">{</span></td></tr><tr><td class="number">28</td><td>         images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <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>images<span class="k2">[</span><span class="n">0</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">&gt;</span>w, images<span class="k2">[</span><span class="n">0</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">&gt;</span>h<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">29</td><td>         images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">1</span><span class="k2">]</span> <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>images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>w, images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>h<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>      <span class="k1">else</span><span class="k2">{</span></td></tr><tr><td class="number">32</td><td>         images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <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>images<span class="k2">[</span><span class="n">0</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">&gt;</span>h, images<span class="k2">[</span><span class="n">0</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">&gt;</span>w<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">33</td><td>         images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">1</span><span class="k2">]</span> <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>images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>h, images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k3">-</span><span class="k3">&gt;</span>w<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>      <span class="k2">}</span></td></tr><tr><td class="number">35</td><td>      </td></tr><tr><td class="number">36</td><td>      <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span>images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">37</td><td>      <a href="http://www.allegro.cc/manual/rotate_sprite" target="_blank"><span class="a">rotate_sprite</span></a><span class="k2">(</span>images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">0</span><span class="k2">]</span>, <span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span><span class="n">64</span> <span class="k3">*</span> i<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">38</td><td>&#160;</td></tr><tr><td class="number">39</td><td>      <a href="http://www.allegro.cc/manual/clear_bitmap" target="_blank"><span class="a">clear_bitmap</span></a><span class="k2">(</span>images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">1</span><span class="k2">]</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">40</td><td>      <a href="http://www.allegro.cc/manual/rotate_sprite" target="_blank"><span class="a">rotate_sprite</span></a><span class="k2">(</span>images<span class="k3">&lt;</span>i&gt;<span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, images<span class="k2">[</span><span class="n">0</span><span class="k2">]</span><span class="k2">[</span><span class="n">1</span><span class="k2">]</span>, <span class="n">0</span>, <span class="n">0</span>, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span><span class="n">64</span> <span class="k3">*</span> i<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">41</td><td>   <span class="k2">}</span><span class="c">//end if</span></td></tr><tr><td class="number">42</td><td>   <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">43</td><td><span class="k2">}</span><span class="c">//end setup</span></td></tr></tbody></table></div></div><p>

Are there any suggestions for keeping this sort of function as part of the class, or should I just setup another function outside of the class to take a shape object as a parameter and do all of the bitmap functions there?</p><p>Thank you for any advice, criticism, etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Justin Hamilton)</author>
		<pubDate>Fri, 16 Mar 2007 15:05:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Rather than answer your question, I&#39;d more like to question your design concept. Why exactly do you need this &quot;eraser&quot; image? I have a feeling you aren&#39;t familiar with the concept of double buffering.</p><p>And why don&#39;t you simply rotate the images as needed during runtime? Since the rotation angles are multiples of 90 degrees, there will be almost no performance hit.</p><p>As for the actual code you posted, I can see only one problem. In the rotation for loop there are no curly braces around the if/else bodies. (does that even compile?)</p><p>Also when you do images[0][0]-&gt;w, I&#39;d rather put some brackets in there to make sure it&#39;s obvious what exactly you are dereferencing, but I don&#39;t think that&#39;s actually a problem in your code.</p><p>Another thing you might want to do is initialize all pointers to 0 somewhere in the beginning. Just in case...</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Thank you for any advice, criticism, etc.
</p></div></div><p>
No need to give thanks in advance. When you&#39;re satisfied with the answers, just send some money. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Fri, 16 Mar 2007 15:39:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">miran said:</div><div class="quote"><p>
I have a feeling you aren&#39;t familiar with the concept of double buffering.
</p></div></div><p>Not terribly so, no <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />  I mean, I use a buffer to draw to the screen, but I don&#39;t understand a lot of the techniques related to it.</p><div class="quote_container"><div class="title">miran said:</div><div class="quote"><p>
And why don&#39;t you simply rotate the images as needed during runtime? Since the rotation angles are multiples of 90 degrees, there will be almost no performance hit.
</p></div></div><p>Hmm, I could do that.  I was just under the impression that it was more efficient to assign the rotations to bitmaps.</p><div class="quote_container"><div class="title">miran said:</div><div class="quote"><p>
As for the actual code you posted, I can see only one problem. In the rotation for loop there are no curly braces around the if/else bodies. (does that even compile?)
</p></div></div><p>You&#39;re right, that wouldn&#39;t compile.  I actually cleaned it up a little bit when copying over from my IDE, and ended up forgetting to brace the code together.  I&#39;ll edit my original post, thank you.</p><div class="quote_container"><div class="title">miran said:</div><div class="quote"><p>
Another thing you might want to do is initialize all pointers to 0 somewhere in the beginning. Just in case...
</p></div></div><p>Good idea, thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Justin Hamilton)</author>
		<pubDate>Fri, 16 Mar 2007 16:19:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Not terribly so, no  I mean, I use a buffer to draw to the screen, but I don&#39;t understand a lot of the techniques related to it.
</p></div></div><p>
OK, then make sure you are familiar with the concept; it&#39;s one of the very basic techniques you need for game development.<br />The idea goes something like this. Say you want to display something on screen; the most straightforward way is to just draw it to the screen bitmap directly. This is all fine and dandy, but you get into trouble when you need to remove things from the screen, or things overlap - because the user can see everything you draw and erase.<br />The solution is to render your entire scene not directly to screen, but to a memory buffer (just an ordinary memory bitmap with the exact same size as the screen). When drawing is complete, you blit() the entire buffer to the screen in one go, overwriting everything that was there before. The process of scene composition (including the initial buffer clear) is hidden from the user, and there won&#39;t be any flicker.
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I set up a class for the shapes. One of the (currently public) data members of the class is BITMAP *images[4][2], which holds all possible rotations and a corresponding &quot;eraser&quot; image.
</p></div></div><p>
That doesn&#39;t sound like good design to me.<br />Each shape should have 4 4x4 array of ints or bools, one for each possible rotation. You should draw the actual blocks based on that array at run-time, that is, iterate through the array and draw a block where it contains a &quot;1&quot; or &quot;true&quot;. The bitmaps for the blocks should be stored somewhere else.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 16 Mar 2007 18:20:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m surprised you&#39;re not getting a compiler error for failing to return a value at the end of the routine.</p><p>But, your problem has nothing to do with the way you&#39;re doing it, and everything to do with the fact that it&#39;s a constructor, which means if that constructor is run <i>before</i> you&#39;ve initialized Allegro, bad things will happen. (IE: Crashes.)</p><p>Calling Allegro functions before Allegro is initialized is never a good thing. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>And yes, doing all the rotations ahead of time would be faster, but uses more memory. A lot of speed considerations are like that, where the faster you want something the more memory you need to use.</p><p>And double buffering is an important thing to learn. I highly recommend you do so.</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, 16 Mar 2007 20:34:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And yes, doing all the rotations ahead of time would be faster, but uses more memory.
</p></div></div><p>
This is tetris we&#39;re talking about here.<br />The amount of data here, if we use 4x4 grids of 32-bit ints (very inefficient, we could cram all that information into a single 16-bit int), each rotation takes up 7 * 4 * 4 * 4 bytes = 224 bytes. So the pre-calculation will cost us the immense amount of 672 bytes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 16 Mar 2007 20:49:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Actually he caches the rotated bitmaps, not the shapes themselves. I&#39;m not sure how big the bitmaps are but I don&#39;t think they would take several megs of RAM. </p><p>Also I don&#39;t think that kind of optimizations are really needed in Tetris. Having a single variable showing the rotation in allegro degrees (0, 64, 128, 192) and using rotate_sprite everywhere would probably be much easier.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (HoHo)</author>
		<pubDate>Fri, 16 Mar 2007 20:57:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t like rotate_sprite for extremely simple rotations, but that&#39;s just me. <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Fri, 16 Mar 2007 21:16:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><b>Tobias Dammers:</b> I know that. 672 bytes in your example is <i>still</i> more memory, isn&#39;t it? <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, 16 Mar 2007 23:52:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Tobias Dammers said:</div><div class="quote"><p>
OK, then make sure you are familiar with the concept; it&#39;s one of the very basic techniques you need for game development.<br />The idea goes something like this. Say you want to display something on screen; the most straightforward way is to just draw it to the screen bitmap directly. This is all fine and dandy, but you get into trouble when you need to remove things from the screen, or things overlap - because the user can see everything you draw and erase.<br />The solution is to render your entire scene not directly to screen, but to a memory buffer (just an ordinary memory bitmap with the exact same size as the screen). When drawing is complete, you blit() the entire buffer to the screen in one go, overwriting everything that was there before. The process of scene composition (including the initial buffer clear) is hidden from the user, and there won&#39;t be any flicker.
</p></div></div><p>This much I understand.  The problem that I am having with that is keeping the &quot;resting&quot; blocks on the screen, but being able to erase the active block, which is why I was using a corresponding &quot;eraser&quot; image with exactly the same dimensions as the active block.</p><p>Would it be more effective to draw inactive blocks on the buffer, blit the buffer to the screen (erasing the active), and then blit the active block to the screen instead of the buffer?</p><p>Thank you for the replies, everyone.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Justin Hamilton)</author>
		<pubDate>Sat, 17 Mar 2007 02:13:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Clear the buffer, and draw <i>all</i> of the blocks, active and inactive alike, to the buffer before it goes to the screen. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p><tt>std::vector</tt> may be of some use to you in this project. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kikaru)</author>
		<pubDate>Sat, 17 Mar 2007 06:15:22 +0000</pubDate>
	</item>
</rss>
