<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Unwanted pixels showing up using masked_blit()</title>
		<link>http://www.allegro.cc/forums/view/588362</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Mon, 06 Nov 2006 06:03:12 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I am using masked_blit() to draw tiles to the screen.  When I draw a transparent tile to the screen it leaves some unwanted pixels as you can see in the image below.  There is only 9 tiles being draw from the coord (0,0).  Each tile is 64x64 pixels.<br /><span class="remote-thumbnail"><span class="json">{"name":"whatiseeof6.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/3\/a36aa3f504b072678e266eb48151ad85.png","w":646,"h":512,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/3\/a36aa3f504b072678e266eb48151ad85"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/3/a36aa3f504b072678e266eb48151ad85-240.jpg" alt="whatiseeof6.png" width="240" height="190" /></span><br />The tileset I am using is this:<br />http://img139.imageshack.us/img139/767/tiledu3.png<br />I am drawing the bottom right one to the screen. (TILE: 3 AKA the 4th one coord: 1,1 or 2,2 however you look at it)  (FYI: The file is actually a bitmap... imageshack just converted it to a png)<br />I was wondering how I can get rid of those pixels.  I am also wondering what I should be drawing to the back of the screen.  Right now I have it drawing the background to the transparent color.<br />clear_to_color(BackBuffer, bitmap_mask_color(BackBuffer));<br />(I then swap the BackBuffer to the screen when I am ready)<br />I can set the BackBuffer to any color, but when I do a masked_blit() with a transparent color, the transparent color shows up as black.  Why is this and how can I fix it?<br />Last question:  How come I cannot use other things to draw to the screen other than the first tool I use.  i.e. If I use masked_blit() to draw something to the screen...  If I then try and draw something with blit() or draw_sprite it will not be drawn to the screen.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sat, 04 Nov 2006 14:16:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Can you show some minimal code? Something like the function that draws a tile and the loop that draws them (or whatever you used in this particular example). It seems like either draw too much, or clear your buffer twice or something.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ron Ofir)</author>
		<pubDate>Sat, 04 Nov 2006 16:15:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah here are some bits of my code
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>...</td></tr><tr><td class="number">2</td><td>cTile Tile<span class="k2">;</span></td></tr><tr><td class="number">3</td><td>...</td></tr><tr><td class="number">4</td><td><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> tile<span class="k2">;</span></td></tr><tr><td class="number">5</td><td>tile <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><span class="n">64</span>, <span class="n">64</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>tile <span class="k3">=</span> Tile.GetTile<span class="k2">(</span><span class="n">3</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>...</td></tr><tr><td class="number">8</td><td><span class="k1">while</span><span class="k2">(</span><span class="k3">!</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">9</td><td><span class="k2">{</span></td></tr><tr><td class="number">10</td><td>     <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> y <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> y <span class="k3">&lt;</span> <span class="n">3</span><span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">11</td><td>     <span class="k2">{</span></td></tr><tr><td class="number">12</td><td>          <span class="k1">for</span><span class="k2">(</span><span class="k1">int</span> x <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> x <span class="k3">&lt;</span> <span class="n">3</span><span class="k2">;</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">13</td><td>          <span class="k2">{</span></td></tr><tr><td class="number">14</td><td>               thescreen.DrawToBuffer<span class="k2">(</span>tile, <span class="n">0</span>, <span class="n">0</span>, tile-&gt;w <span class="k3">*</span> x, tile-&gt;h <span class="k3">*</span> y<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>     <span class="k2">}</span></td></tr><tr><td class="number">17</td><td>     thescreen.Swap<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</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">19</td><td><span class="k2">}</span></td></tr><tr><td class="number">20</td><td>...</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>tile<span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>
Here is the DrawToBuffer and Swap methods...
</p><div class="source-code snippet"><div class="inner"><pre>...
protected:
     <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> BackBuffer
...
BOOL cScreen::DrawToBuffer<span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> backbuf, <span class="k1">int</span> xpos, <span class="k1">int</span> ypos, <span class="k1">int</span> xdes <span class="k3">=</span> <span class="n">0</span>, <span class="k1">int</span> ydes <span class="k3">=</span> <span class="n">0</span><span class="k2">)</span>
<span class="k2">{</span>
     <a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>backbuf, BackBuffer, xpos, ypos, xdes, ydes, backbuf-&gt;w, backbuf-&gt;h<span class="k2">)</span><span class="k2">;</span>
     <span class="k1">return</span> TRUE<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre>BOOL cScreen::Swap<span class="k2">(</span><span class="k2">)</span>
<span class="k2">{</span>
     <a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>BackBuffer, <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>, BackBuffer-&gt;w, BackBuffer-&gt;h<span class="k2">)</span><span class="k2">;</span>
     ClearBuffer<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
     <span class="k1">return</span> TRUE<span class="k2">;</span>
<span class="k2">}</span>

BOOL cScreen::ClearBuffer<span class="k2">(</span><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>BackBuffer, <a href="http://www.allegro.cc/manual/bitmap_mask_color" target="_blank"><span class="a">bitmap_mask_color</span></a><span class="k2">(</span>BackBuffer<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
     <span class="k1">return</span> TRUE<span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>
If this may be helpful to you... here is some information from my tile class:
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> cTile::GetTile<span class="k2">(</span><span class="k1">int</span> TileNum<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="c">// Is the Tile Number in range?</span></td></tr><tr><td class="number">4</td><td>     <span class="k1">if</span><span class="k2">(</span>TileNum <span class="k3">&gt;</span> <span class="k2">(</span><span class="k2">(</span>m_Cols <span class="k3">*</span> m_Rows<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">5</td><td>     <span class="k2">{</span></td></tr><tr><td class="number">6</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">"TileSet: %s\nTile: %d\nDoes not exhist!"</span>, m_Filename, TileNum<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>          TileNum <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>m_Cols <span class="k3">*</span> m_Rows<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/allegro_message" target="_blank"><span class="a">allegro_message</span></a><span class="k2">(</span><span class="s">"Using Tile: %d"</span>, TileNum<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>     <span class="k2">}</span></td></tr><tr><td class="number">10</td><td>     <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span>   tile<span class="k2">;</span></td></tr><tr><td class="number">11</td><td>     tile      <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>m_TileWidth, m_TileHeight<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">12</td><td>     <span class="k1">int</span>       tilexpos <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">13</td><td>     <span class="k1">int</span>       tileypos <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">14</td><td>&#160;</td></tr><tr><td class="number">15</td><td>     tileypos  <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>TileNum <span class="k3">/</span> m_Cols<span class="k2">)</span> <span class="k3">*</span> m_TileHeight<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>     tilexpos  <span class="k3">=</span> <span class="k2">(</span><span class="k2">(</span>TileNum % m_Cols<span class="k2">)</span> <span class="k3">*</span> m_TileWidth<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">17</td><td>     <a href="http://www.allegro.cc/manual/masked_blit" target="_blank"><span class="a">masked_blit</span></a><span class="k2">(</span>m_TileSet, tile, tilexpos, tileypos, <span class="n">0</span>, <span class="n">0</span>, m_TileWidth, m_TileHeight<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">18</td><td>     <span class="k1">return</span> tile<span class="k2">;</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sun, 05 Nov 2006 01:20:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You must not use masked_blit() when you copy the back buffer to the screen. What do you want to achieve by this anyway? Also it doesn&#39;t make sense to call vsync() after drawing the back buffer to the screen.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Sun, 05 Nov 2006 01:26:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well what should I use instead of masked blit?  And I&#39;m new to Allegro so I was playing with the vsync(), I didn&#39;t notice I copied that too.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sun, 05 Nov 2006 01:33:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Well what should I use instead of masked blit?
</p></div></div><p>
<span class="source-code"><a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span><span class="k2">)</span></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Sun, 05 Nov 2006 01:33:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Alright, well that was changed but had no effect on what is being seen on the screen.  I heard there might be problems with making my graphics in MS Paint (Which that tile set was made in)  What is another program I could use?  Link? :-D
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sun, 05 Nov 2006 01:39:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I heard there might be problems with making my graphics in MS Paint
</p></div></div><p>
Depends on which version you have, what format you save to and how you setup the gfx mode in Allegro and load the bitmap.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
What is another program I could use?
</p></div></div><p>
GIMP.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
Link?
</p></div></div><p>
<a href="http://www.gimp.org/">http://www.gimp.org/</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Sun, 05 Nov 2006 01:47:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Recreating the graphic in another editor didn&#39;t help.  I&#39;m still seeing red dots.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Sun, 05 Nov 2006 02:12:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Then post a simple but complete and compilable 20 line program that shows the error and we&#39;ll tell you what you&#39;re doing wrong.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Sun, 05 Nov 2006 13:47:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Is it saved in a true bitmap format? If it is a JPEG then even though you draw magic pink from one pixel to the next, you end up with nearly magic pink. Trust me. I&#39;ve done this myself. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Mon, 06 Nov 2006 01:14:05 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Matthew, I want you to take a very close look at the screenshot you&#39;ve provided by using any graphics editor and filling the black area with white. You&#39;re going to notice that not only are the leftover colours not the same as your tile, you&#39;re also going to notice FOUR different colours being left over. Each bright pixel has another one beside them. The ones beside the green pixels are a dark blue and the ones beside the red ones are very slightly green.</p><p>Those colours are NOT coming from your bitmaps. So where are they coming from?</p><p>First, you really need to change your naming conventions. Naming a global object &quot;tile&quot;, another global object &quot;Tile&quot; and a local object in CTile &quot;tile&quot; is extremely confusing.</p><p>Secondly, you&#39;ve got a memory leak. You&#39;re calling:<br /><span class="source-code">tile <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><span class="n">64</span>, <span class="n">64</span><span class="k2">)</span><span class="k2">;</span></span><br />...and then immediately afterwards in the CTile::GetTile() routine you&#39;re changing that freshly created bitmap to point to:<br /><span class="source-code">tile      <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>m_TileWidth, m_TileHeight<span class="k2">)</span><span class="k2">;</span></span></p><p>So that&#39;s definitely something you need to fix, but it&#39;s NOT the cause of the pixels you&#39;re seeing.</p><p>Your problem has to do with how you&#39;re grabbing your tiles. Miran said you should be using blit() instead of masked_blit(). I agree, but not just for the back buffer, EVERYWHERE, except cScreen :: DrawToBuffer(), you want to leave that one as masked_blit(), or even draw_sprite() to simplify things.</p><p>Think about this: When you allocate some memory nothing&#39;s in it, except possibly remnants from other things previously in memory. That&#39;s where those coloured pixels are coming from. So why aren&#39;t they disappearing when you grab your tile with CTile::GetTile()? Because you&#39;re grabbing your tile with masked_blit()! Because the tile you&#39;re trying to grab is completely transparent, masked_blit() doesn&#39;t paste anything onto your bitmap, thus it STILL has memory remnants in it!</p><p>blit() ignores transparent pixels, blitting them exactly as they are.<br />masked_blit() pays attention to transparent pixels by NOT blitting them.</p><p>So, you have three changes you need to make:<br />1. Change all masked_blit() commands to blit() except for the one in cScreen :: DrawToBuffer().<br />2. REMOVE: tile = create_bitmap(64, 64);<br />3. Call vsync() BEFORE drawing to the screen, not after.</p><p>And you really should have a unique name for absolutely everything global to minimize confusion.</p><p>That should solve your problems.</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>Mon, 06 Nov 2006 02:35:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Excellent analysis Kris.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Mon, 06 Nov 2006 04:30:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah I fixed this problem yesterday by changing all my masked_blit()&#39;s to regular blit()&#39;s except for the one in my DrawToBuffer() method.  This solved the problem.  I will work on my naming method, I normally just call them whatever I want when I&#39;m trying to test them out, sorry about the confusion.  I need to rename my whole screen class because I&#39;ve already created and fixed an error where I did this:
</p><div class="source-code snippet"><div class="inner"><pre>cScreen <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a><span class="k2">;</span>
...
<a href="http://www.allegro.cc/manual/blit" target="_blank"><span class="a">blit</span></a><span class="k2">(</span>randombitmap, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, ...<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
That tried to blit to my cScreen object instead of the screen &gt;.&lt; <b>dope</b></p><p>Oh and to this:
</p><div class="quote_container"><div class="title">Richard Phipps said:</div><div class="quote"><p>
Excellent analysis Kris.
</p></div></div><p>
It was an &quot;Excellent analysis&quot; very indepth and more people should think like that.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Dalrymple)</author>
		<pubDate>Mon, 06 Nov 2006 06:03:12 +0000</pubDate>
	</item>
</rss>
