<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>getpixel() how to use it?</title>
		<link>http://www.allegro.cc/forums/view/574279</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 24 Mar 2006 15:09:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In short, i have searched high and low, far and wide, for documentation on getpixel().</p><p>I have a simple question which i&#39;m assuming has a simple answer. how do i use it properly.</p><p>I&#39;m working on a breakout clone, i have to test what color the block is. i created an integer named color. // color = getpixel(buffer, 2,0)<br />x/y coordinates are chosen b/c i know logically what their color is. I am just testing to see how i use getpixel() properly. logically, the color is black. however, my question finally is....<br />if getpixel returns an integer, how is that translated into RGB color?<br />I must have overlooked this and am at the point where the answer is right in front of me, but i&#39;m completely blind to it :-)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Snake Snakes)</author>
		<pubDate>Wed, 22 Mar 2006 10:23:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Use getpixel then do this for anything othern than 8 bit:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">if</span><span class="k2">(</span> color <span class="k3">=</span> <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>
  its black<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

If you don&#39;t know how to use makecol, just click on the word.</p><p>Or you can just do what ML posted below me.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (SonShadowCat)</author>
		<pubDate>Wed, 22 Mar 2006 10:26:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><a href="http://www.allegro.cc/manual/getpixel">http://www.allegro.cc/manual/getpixel</a><br /><a href="http://www.allegro.cc/manual/getr">http://www.allegro.cc/manual/getr</a><br /><a href="http://www.allegro.cc/manual/getg">http://www.allegro.cc/manual/getg</a><br /><a href="http://www.allegro.cc/manual/getb">http://www.allegro.cc/manual/getb</a><br /><a href="http://www.allegro.cc/manual/geta">http://www.allegro.cc/manual/geta</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matthew Leverton)</author>
		<pubDate>Wed, 22 Mar 2006 10:27:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>thank you VERY much. i can&#39;t tell you how many times i&#39;ve read the allegro documentation on getpixel() and it&#39;s variants. but they mean nothing as i&#39;ve read them in frustration. thanks again for the help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Snake Snakes)</author>
		<pubDate>Wed, 22 Mar 2006 10:29:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I don&#39;t mean to be telling you how to do your game, but I think you might get better performance by storing it as an INT on creation and using defines. I think I heard somewhere that getpixel is a relatively slow function, plus doing it this way seems cleaner to me. If you are only doing this for the testing purposes, then go ahead and do that, otherwise, consider 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>&#160;</td></tr><tr><td class="number">2</td><td><span class="c">// object junk</span></td></tr><tr><td class="number">3</td><td><span class="p">#define BLOCKCOLOR_RED     0</span></td></tr><tr><td class="number">4</td><td><span class="p">#define BLOCKCOLOR_BLUE    1</span></td></tr><tr><td class="number">5</td><td><span class="p">#define BLOCKCOLOR_GREEN   2</span></td></tr><tr><td class="number">6</td><td>&#160;</td></tr><tr><td class="number">7</td><td>CBlock::CBlock<span class="k2">(</span><span class="k1">int</span> col<span class="k2">)</span></td></tr><tr><td class="number">8</td><td><span class="k2">{</span></td></tr><tr><td class="number">9</td><td>  color <span class="k3">=</span> col<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>&#160;</td></tr><tr><td class="number">12</td><td><span class="k1">void</span> CBlock::Collision<span class="k2">(</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>  <span class="k1">if</span><span class="k2">(</span> collision<span class="k2">(</span>ball.x, ball.y, x, y<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="k1">switch</span><span class="k2">(</span>color<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>      <span class="c">// red blocks = 1,000 points</span></td></tr><tr><td class="number">19</td><td>      <span class="k1">case</span> BLOCKCOLOR_RED:</td></tr><tr><td class="number">20</td><td>        playerscore <span class="k3">+</span><span class="k3">=</span> <span class="n">1000</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>      <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">22</td><td>      </td></tr><tr><td class="number">23</td><td>      <span class="c">// blue blocks = 1,250 points</span></td></tr><tr><td class="number">24</td><td>      <span class="k1">case</span> BLOCKCOLOR_BLUE:</td></tr><tr><td class="number">25</td><td>        playerscore <span class="k3">+</span><span class="k3">=</span> <span class="n">1250</span><span class="k2">;</span></td></tr><tr><td class="number">26</td><td>      <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">27</td><td>      </td></tr><tr><td class="number">28</td><td>      <span class="c">// green blocks = 1,325 points</span></td></tr><tr><td class="number">29</td><td>      <span class="k1">case</span> BLOCKCOLOR_GREEN:</td></tr><tr><td class="number">30</td><td>        playerscore <span class="k3">+</span><span class="k3">=</span> <span class="n">1325</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>      <span class="k1">break</span><span class="k2">;</span></td></tr><tr><td class="number">32</td><td>    <span class="k2">}</span></td></tr><tr><td class="number">33</td><td>&#160;</td></tr><tr><td class="number">34</td><td>  <span class="c">/* make your particles (if you would like) that make</span></td></tr><tr><td class="number">35</td><td><span class="c">       it look like it goes pop */</span></td></tr><tr><td class="number">36</td><td>&#160;</td></tr><tr><td class="number">37</td><td>  <span class="c">/* remove it from the vector or whatever here */</span></td></tr><tr><td class="number">38</td><td>  <span class="k2">}</span></td></tr><tr><td class="number">39</td><td>&#160;</td></tr><tr><td class="number">40</td><td><span class="k2">}</span></td></tr><tr><td class="number">41</td><td>&#160;</td></tr><tr><td class="number">42</td><td>&#160;</td></tr><tr><td class="number">43</td><td><span class="c">// in some other file...</span></td></tr><tr><td class="number">44</td><td>&#160;</td></tr><tr><td class="number">45</td><td>CBlock <span class="k3">*</span>newblock<span class="k2">(</span>BLOCKCOLOR_RED<span class="k2">)</span><span class="k2">;</span> <span class="c">// make a new red block</span></td></tr><tr><td class="number">46</td><td>blockvec.push_back<span class="k2">(</span>newblock<span class="k2">)</span><span class="k2">;</span> <span class="c">// push it onto our block vector</span></td></tr></tbody></table></div></div><p>

I recommend this because making sure all of your blocks have exactly the right color at exactly the right position is tedious work on your part, and because multiple blocks could have the same pixel at the same positions yet look different or act different. Again, this is just my two-cents. If you prefer doing it the way you already are, feel free to do so.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Elverion)</author>
		<pubDate>Wed, 22 Mar 2006 13:10:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
i can&#39;t tell you how many times i&#39;ve read the allegro documentation on getpixel() and it&#39;s variants. but they mean nothing as i&#39;ve read them in frustration
</p></div></div><p>
Care to explain what&#39;s wrong with it?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Wed, 22 Mar 2006 14:13:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think his problem, Evert, was the link between the value that getpixel returns and the actual colour it corresponds to. From a newbie point of view you can understand this because getpixel is in a completely different section (primitives) to the section that deals with colours (true-colour and paletted). Maybe what the manual needs is more cross-references and linkage, e.g. colour-&gt;primitives-&gt;transparency/patterned.</p><p>Of course, if he just uses 8bit then that isn&#39;t a problem as you can just use the palette index and ignore colour conversions <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Wed, 22 Mar 2006 16:12:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>@Neil: Allegro represents colors in a fairly standard way (using a palette for 8bpp and packed rgb values for everything else). Explaining such a standard way of doing things would probably go beyond the scope of a programming library documentation, and I think it&#39;s safe to assume that everybody who wants to use allegro either<br />a) is familiar with basic (graphics) programming concepts and procedures, or<br />b) is willing to read up on those</p><p>@OP: While getpixel() can do what you want it to, it is probably not wise to use it for this. IIUC, you want to use getpixel() to perform collision detection. While to the beginner this seems an intuitive and straightforward way to do things, it is not a &quot;clean&quot; nor a &quot;fool-proof&quot; way. Generally, you want to separate logic (objects and their interaction, movement, collision detection and response, physics etc.) from rendering (displaying objects on screen), so that the logic code never touches any graphics-related code, and the graphics code never changes any object data. So what you&#39;d need is:<br />1. A tile grid to hold the blocks; this can be a simple 2d array of ints, 0 representing an empty spot, and any other value representing pre-defined constants (which may represent colors directly, or translate through a block type list, allowing for &quot;special&quot; blocks).<br />2. Functions to transform &quot;world&quot; coordinates (the ball and paddle positions) into &quot;tile&quot; coordinates and back. This is usually a matter of integer division and multiplication; if your tiles are power-of-2-sized, you can use bit shifts instead.<br />3. Whenever the ball moves, check if it crosses a tile boundary. If not, you don&#39;t need any collision detection at all (except for screen edges and paddle). If it does, check the type of the &quot;new&quot; tile position, and handle accordingly (destroy tile, create powerup, change ball velocity, etc.)<br />This is a lot faster than your original method, I&#39;d estimate by a magnitude of several hundreds.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 22 Mar 2006 16:31:38 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I wasn&#39;t saying that, I was simply saying if the manual was structured in such a way as linking together logical areas (e.g. pixels with colour) then more the better.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Wed, 22 Mar 2006 16:58:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You have a point there.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Wed, 22 Mar 2006 17:10:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>thank you all for the help. I was unaware of how &quot;slow&quot; getpixel() would have been for the purpose i was intending to use if for. I have taken the advice of all and am rewriting code to accomodate. i&#39;m so glad Allegro has a such a knowledged community willing to help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Snake Snakes)</author>
		<pubDate>Thu, 23 Mar 2006 05:38:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>for faster access you can use <span class="source-code"><a href="http://www.allegro.cc/manual/_getpixel" target="_blank"><span class="a">_getpixel</span></a><span class="k2">(</span><span class="k2">)</span></span> these bypass a lot of the protective code and stuff.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Walker)</author>
		<pubDate>Thu, 23 Mar 2006 05:43:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>_getpixel and _putpixel always crashes my putey.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (CosmicR)</author>
		<pubDate>Thu, 23 Mar 2006 09:32:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I was simply saying if the manual was structured in such a way as linking together logical areas (e.g. pixels with colour) then more the better.
</p></div></div><p>
Can add a `See also:&#39; reference to the colour formats description to getpixel, I suppose. I&#39;ll probably forget, so if someone wants to do this and post a patch on the AD mailing list (or the Development section of the forum), cheers (and credits) to them.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
_getpixel and _putpixel always crashes my putey.
</p></div></div><p>
Post the smallest sample code that reproduces the crash.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Thu, 23 Mar 2006 13:57:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Something like this?</p><p>[EDIT]<br />Submitted the diff to [AD] as well.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (gnolam)</author>
		<pubDate>Thu, 23 Mar 2006 21:08:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
_getpixel and _putpixel always crashes my putey.
</p></div></div><p>
They don&#39;t accommodate for color depth. If your bitmap is anything but 8-bpp, you&#39;re doomed. Use _getpixel15(), _getpixel16() etc. instead, depending on bitmap color depth.<br />And they don&#39;t accommodate for clipping either, so be sure to check you&#39;re not trying to draw outside the bitmap.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Fri, 24 Mar 2006 15:09:54 +0000</pubDate>
	</item>
</rss>
