<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Question about sprites and masking</title>
		<link>http://www.allegro.cc/forums/view/587389</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 03 Sep 2006 14:49:42 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>After setting the display mode and so on, I load a bitmap with the following code:<br /><span class="source-code"><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> tmp <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><span class="s">"sprite.bmp"</span>,NULL<span class="k2">)</span><span class="k2">;</span></span><br />This bitmap is a very simple one, which is just a red circle with the magic pink all around. If I try to draw this image through a normal blit I see the whole bitmap as expected on my screen (also with the magic pink).<br />But when I try to draw this as a sprite I get just a grey box instead of my circle. <br />Here is the code for it ( I also tried it with masked_blit):<br /><span class="source-code"><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>,tmp,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span></span></p><p>Then I did a forum search and found a thread with a similar problem. So I changed my code to something like this:
</p><div class="source-code snippet"><div class="inner"><pre><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="k3">|</span> COLORCONV_KEEP_TRANS<span class="k2">)</span><span class="k2">;</span>

<a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> tmp <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><span class="s">"sprite.bmp"</span>,NULL<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> image <span class="k3">=</span> <a href="http://www.allegro.cc/manual/create_video_bitmap" target="_blank"><span class="a">create_video_bitmap</span></a><span class="k2">(</span>tmp-&gt;w,tmp-&gt;h<span class="k2">)</span><span class="k2">;</span>
<span class="k1">int</span> c <span class="k3">=</span> <a href="http://www.allegro.cc/manual/bitmap_mask_color" target="_blank"><span class="a">bitmap_mask_color</span></a><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/clear_to_color" target="_blank"><span class="a">clear_to_color</span></a><span class="k2">(</span>image,c<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>tmp,image,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,<span class="n">0</span>,tmp-&gt;w,tmp-&gt;h<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>tmp<span class="k2">)</span><span class="k2">;</span>

<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>,image,<span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

With exactly these lines it works as it should. But I want to know why ? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /> I used Allegro time ago and I remember that I didn&#39;t have to do something like this at that time. Maybe there are also other ways ?</p><p>Another Question: Is it good to create the sprites in video memory as I did in the above code or should I create them in normal memory ?</p><p>P.S. Do you know why I can&#39;t reply on my other started topic ? <a href="http://www.allegro.cc/forums/thread/587370">http://www.allegro.cc/forums/thread/587370</a><br />I wanted to add something but I can&#39;t <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FreeCastle)</author>
		<pubDate>Sat, 02 Sep 2006 19:55:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
The reason that video memory is being used is that some gfx cards can blit from video memory to video memory very fast, gaining a considerable speed boost over normal memory. From the manual:</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
If the GFX_HW_VRAM_BLIT_MASKED bit in the gfx_capabilities flag is set, the current driver supports hardware accelerated sprite drawing when the source image is a video memory bitmap or a sub-bitmap of the screen. This is extremely fast, so when this flag is set it may be worth storing some of your more frequently used sprites in an offscreen portion of the video memory.</p><p>Warning: if the hardware acceleration flag is not set, draw_sprite() will not work correctly when used with a sprite image in system or video memory so the latter must be a memory bitmap.
</p></div></div><p>

It sounds like your grey problems were due to the sprite and the screen being different colour depths. draw_sprite doesn&#39;t do proper colour conversion, whereas the blit in the code you posted above does. See the manual for draw_sprite:</p><p><span class="source-code"><a href="http://www.allegro.cc/manual/draw_sprite" target="_blank"><span class="a">draw_sprite</span></a></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sat, 02 Sep 2006 20:11:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>When do you call set_color_depth()? I use the following and it works fine for me:
</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">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr><tr><td class="number">2</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">3</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">4</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_DIRECTX_WIN,<span class="n">500</span>,<span class="n">500</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">5</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">6</td><td>  <span class="c">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr><tr><td class="number">7</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">8</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">9</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">10</td><td>  <span class="c">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr><tr><td class="number">11</td><td>  <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>bmp     <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">12</td><td>  <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>sprite  <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><span class="s">"Sprite.bmp"</span>,NULL<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>  <span class="c">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr><tr><td class="number">14</td><td>        <span class="k1">while</span> <span class="k2">(</span> <span class="k3">!</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">15</td><td>        <span class="k2">{</span></td></tr><tr><td class="number">16</td><td>             <a href="http://www.allegro.cc/manual/draw_sprite" target="_blank"><span class="a">draw_sprite</span></a><span class="k2">(</span>bmp,sprite,x,y<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>             <a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>bmp,<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>,bmp-&gt;w,bmp-&gt;h<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>bmp<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="c">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr><tr><td class="number">21</td><td>        <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>bmp<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>        <a href="http://www.allegro.cc/manual/destroy_bitmap" target="_blank"><span class="a">destroy_bitmap</span></a><span class="k2">(</span>sprite<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">23</td><td>        <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">24</td><td>  <span class="c">//////////////////////////////////////////////////////////////////////////////////////////////</span></td></tr></tbody></table></div></div><p>

About memory/video bitmaps:<br />   Depending on you harware, memory bitmaps may even be faster than video bitmaps...also, you can&#39;t do some things with video bitmaps like with memory bitmaps.</p><p>About replying:<br />   You where the last one to post...so it won&#39;t let you. I will &#39;bump&#39; so you can add something else...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Sat, 02 Sep 2006 20:16:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
set_gfx_mode(GFX_DIRECTX_WIN,500,500,0,0);
</p></div></div><p>
Not only are you unnecessarily using a platform-dependant driver, you are trying to set a 500x500 resolution... without error checking. Yikes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Sat, 02 Sep 2006 20:24:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
It sounds like your grey problems were due to the sprite and the screen being different colour depths. draw_sprite doesn&#39;t do proper colour conversion, whereas the blit in the code you posted above does. See the manual for draw_sprite:
</p></div></div><p>

Yes I think this is the reason. But I don&#39;t know how to handle it. I created e.g. a simple bitmap with MS-Paint and then saved it as a 24-bit bitmap (thats the only true-color option). Then in my allegro app I also set the color depth to 24 (later 32). But I don&#39;t get the correct image represented <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /><br />I already looked at the manual, but I didn&#39;t find a solution for that...</p><p>About that video/memory bitmap thing:<br />In the code that I posted I use a video bitmap to store the sprite. And thats my ONLY option! If I use a normal memory bitmap I get the same problem again. That&#39;s really <span class="cuss"><span><span class="cuss"><span>shit</span></span></span></span>. Maybe it&#39;s no problem on my system to put all sprites in video bitmaps, but maybe there are other systems with much lesser memory...</p><p>@Freeman<br />Thanks for replying
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FreeCastle)</author>
		<pubDate>Sat, 02 Sep 2006 20:49:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>gnolam:<br />&#39;You&#39;re bustin&#39; my balls here....&#39;, I was just trying to show a quick and simple way to do it...OF COURSE you would add error checking...I left it out for clarity. Have you ever read programming books? I believe they are the same way... And considering <u>I</u> ONLY use windows, it would be fair to assume that I would only know about WINDOWS stuff, which would be fine in this case considering HE is using windows as well... <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><p>FreeCastle:<br />Try my code sample above and see if it works for you...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Don Freeman)</author>
		<pubDate>Sat, 02 Sep 2006 21:19:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>sorry to hijack the thread but i&#39;ve read it and it made me wonder what the difference between a system bitmap and a memory bitmap is ?</p><p>maybe others might learn seomething here too
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (William Labbett)</author>
		<pubDate>Sat, 02 Sep 2006 21:34:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://www.allegro.cc/manual/api/bitmap-objects/">RTFM</a> <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Derezo)</author>
		<pubDate>Sat, 02 Sep 2006 21:56:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Arrrr...<br />I changed my code according to yours Freeman.<br />The only difference to mine was, that I called the set_color_depth()-function after the set_gfx_mode() function and not before, like in your code. Now it works as it should <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> <br />Maybe I am just too stupid to read the manual correctly, but I didn&#39;t see it <b>clearly</b> that the set_color_depth() function should be called always before the set_gfx_mode() function.<br />Anyway... thank you very much for your help
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (FreeCastle)</author>
		<pubDate>Sat, 02 Sep 2006 22:16:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And considering <u>I</u> ONLY use windows
</p></div></div><p>
You&#39;re aware, I hope, that GFX_AUTODETECT_WINDOWED gives you GFX_DIRECTX_WIN in Windows by default? SO even if you&#39;re only interested in Windows, there is no valid reason for not using GFX_AUTODETECT_WINDOWED.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I didn&#39;t see it <b>clearly</b> that the set_color_depth() function should be called always before the set_gfx_mode() function.
</p></div></div><p>
</p><div class="quote_container"><div class="title">man set_gfx_mode said:</div><div class="quote"><p>
The color depth of the graphic mode has to be specified <b>before</b> calling this function with set_color_depth().
</p></div></div><p>
</p><div class="quote_container"><div class="title">man set_color_depth said:</div><div class="quote"><p>
 Sets the pixel format to be used by <b>subsequent</b> calls to  set_gfx_mode() and  create_bitmap().
</p></div></div><p>
Seems pretty clear to me... <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /><br />It&#39;s also fairly obvious when you stop to think about it: set_gfx_mode() creates the window given some colour depth. To change that, you have to tell it to use a different one from whatever its current default is.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Sun, 03 Sep 2006 14:49:42 +0000</pubDate>
	</item>
</rss>
