<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>[C++] al_put_pixel() problem? Bitmap not rendering correctly.</title>
		<link>http://www.allegro.cc/forums/view/611949</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 31 Jan 2013 00:08:48 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The attached file shows the output. It is constant. It doesn&#39;t change at all, always rendering the exact pixel-for-pixel output each time it is run with different parameters.</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_set_target_bitmap"><span class="a">al_set_target_bitmap</span></a><span class="k2">(</span>bitmap<span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.allegro.cc/manual/al_lock_bitmap"><span class="a">al_lock_bitmap</span></a><span class="k2">(</span>bitmap, <a href="http://www.allegro.cc/manual/al_get_bitmap_format"><span class="a">al_get_bitmap_format</span></a><span class="k2">(</span>bitmap<span class="k2">)</span>, ALLEGRO_LOCK_READWRITE<span class="k2">)</span><span class="k2">;</span>

  <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> ycounter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> ycounter <span class="k3">&lt;</span> w<span class="k2">;</span> ycounter<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
    <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> xcounter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> xcounter <span class="k3">&lt;</span> h<span class="k2">;</span> xcounter<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
      <a href="http://www.allegro.cc/manual/al_put_pixel"><span class="a">al_put_pixel</span></a><span class="k2">(</span>xcounter, ycounter, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span> <span class="n">0</span>, <span class="n">0</span>, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>topograph.at<span class="k2">(</span>xcounter, ycounter<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>

  <a href="http://www.allegro.cc/manual/al_unlock_bitmap"><span class="a">al_unlock_bitmap</span></a><span class="k2">(</span>bitmap<span class="k2">)</span><span class="k2">;</span>

<span class="c">////</span>
<a href="http://www.allegro.cc/manual/al_set_target_backbuffer"><span class="a">al_set_target_backbuffer</span></a><span class="k2">(</span>root<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_clear_to_color"><span class="a">al_clear_to_color</span></a><span class="k2">(</span><a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</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><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>thisRegion-&gt;bitmap, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_flip_display"><span class="a">al_flip_display</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

The topograph.at() is a std::vector. The contents of the vector vary, from the scaled output of rand(), to a libnoise Perlin noise module, to full zeros. All values are [0,255]. Yet it always outputs those bars. Am I doing anything obviously wrong? If any more information is needed, I&#39;m happy to provide. <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Skyrunner)</author>
		<pubDate>Tue, 29 Jan 2013 22:04:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><span class="source-code">topograph.at<span class="k2">(</span>xcounter, ycounter<span class="k2">)</span></span> <br />This looks fishy; std::vector::at only takes one argument, so it shouldn&#39;t even compile like this. The loop looks fine other than that, so if you fix getting the topograph value and it still looks the same, I would suspect that the way topograph is being filled with values is at fault.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (someone972)</author>
		<pubDate>Wed, 30 Jan 2013 00:48:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/611949/975760#target">someone972</a> said:</div><div class="quote"><p> This looks fishy; std::vector::at only takes one argument, so it shouldn&#39;t even compile like this.</p></div></div><p>(Assuming <span class="source-code">topograph</span> is populated correctly.) Probably the comma operator is being used, which doesn&#39;t make sense, the compiler should have thrown an error. So, what&#39;s actually happening is, <span class="source-code">xcounter</span> just keeps getting re-used and <span class="source-code">ycounter</span> is ignored.:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> ycounter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> ycounter <span class="k3">&lt;</span> w<span class="k2">;</span> ycounter<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
    <span class="k1">for</span> <span class="k2">(</span><span class="k1">int</span> xcounter <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> xcounter <span class="k3">&lt;</span> h<span class="k2">;</span> xcounter<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span>
      <a href="http://www.allegro.cc/manual/al_put_pixel"><span class="a">al_put_pixel</span></a><span class="k2">(</span>xcounter, ycounter, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span> <span class="n">0</span>, <span class="n">0</span>, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>topograph.at<span class="k2">(</span>xcounter<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

What you probably wanted:</p><p><span class="source-code"><a href="http://www.allegro.cc/manual/al_put_pixel"><span class="a">al_put_pixel</span></a><span class="k2">(</span>xcounter, ycounter, <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span> <span class="n">0</span>, <span class="n">0</span>, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span>topograph.at<span class="k2">(</span>xcounter<span class="k3">+</span>ycounter<span class="k3">*</span>h<span class="k2">)</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span></span></p><p>Is there a reason you compare <span class="source-code">ycounter</span> against <span class="source-code">w</span> (width) and <span class="source-code">xcounter</span> against <span class="source-code">h</span> (height)? Normally those two are the other way around.</p><p>(Assuming otherwise.) Finally, are you calling <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_739.html" target="_blank">srand</a></span>? Otherwise, there&#39;s a reason why you always get the same output.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeff Bernard)</author>
		<pubDate>Wed, 30 Jan 2013 08:57:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Okay, for starters, I should apologize sincerely and hope I don&#39;t get sent to programmer hell D:</p><p>I posted incorrect information: <tt>topograph</tt> is <i>not</i> a <tt>std::vector</tt>, but rather a class that encapsulates a <tt>vector</tt>. The vector is used to store a heightmap, and access is done by multiplying y by h and adding x. </p><p>I conducted a few tests, and I&#39;ve determined that the issue is most likely  in that very function. </p><p>When the entire populating stage of <tt>topograph</tt> was skipped, and <tt>rand()%256</tt> inserted in its place, the screen showed random noise as it should.<br />When <tt>topograph</tt> was populated via Perlin, scaled, and displayed using the code posted in the OP, it showed the bars.<br />When <tt>topograph</tt> was populated via rand(), scaled, and displayed, it showed the bars.<br />When <tt>topograph</tt> was populated via rand()%256, not scaled, and displayed, it showed bars.</p><p><tt>(int)topograph.at(xcounter, ycounter)</tt> is the culprit, or perhaps <tt>scale()</tt> is, too. </p><p>Sorry for causing consternation over the wrong information &gt;_&lt;
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Skyrunner)</author>
		<pubDate>Wed, 30 Jan 2013 19:24:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Those &quot;bars&quot; are exactly what you&#39;d get if you put X directly into the blue component of the pixels (wraparound at 256 starts a new &quot;bar&quot;).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Arthur Kalliokoski)</author>
		<pubDate>Wed, 30 Jan 2013 22:40:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The exact problem was this:</p><p>topograph.at() returns double*. <br />A vector normally has memory addresses that are sequential.<br />I didn&#39;t dereference the at()&#39;s result, so the address value was plugged in and it wrapped around in bars like that xD<br />Oh, the stupid mistakes.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Skyrunner)</author>
		<pubDate>Thu, 31 Jan 2013 00:08:48 +0000</pubDate>
	</item>
</rss>
