<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>get and set pixel in Allegro 5</title>
		<link>http://www.allegro.cc/forums/view/612561</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 12 May 2013 03:21:45 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m trying to program the rendering of a 3D horizontal layer from a 2D image, like the &quot;mode 7&quot; effect from many SNES games.<br />I don&#39;t have any problem with the math, the problem I&#39;m having is with Allegro&#39;s al_get_pixel and al_put_pixel methods. They seem to be extremely slow, way too slow for real time. It&#39;s slow regardless of whether the bitmap is locked or not.</p><p>My question is: is there a way to get and set pixels from a bitmap faster than al_get_pixel and al_put_pixel, like accessing the data directly?</p><p>Help is much appreciated!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Fri, 10 May 2013 01:34:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Pixel by pixel this is going to be extremely slow. However, all you need to do is create a &quot;cache&quot; bitmap, render your tiles to it in regular 2D, then use a perspective transform (al_perspective_transform in 5.1) and then draw the cache bitmap. It&#39;s basically free mode 7 using the 3d hardware.</p><p>Here&#39;s an example where I do it in my game:</p><p><span class="remote-thumbnail"><span class="json">{"name":"Ss72.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/0793e42e0f5013f966f31a301dc1e58e.png","w":720,"h":480,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/0793e42e0f5013f966f31a301dc1e58e"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/0/7/0793e42e0f5013f966f31a301dc1e58e-240.jpg" alt="Ss72.png" width="240" height="160" /></span></p><p>EDIT: Forgot to mention, I do a rotation along the X axis as well as the perspective transform.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Fri, 10 May 2013 01:43:17 +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/612561/982416#target">Trent Gamblin</a> said:</div><div class="quote"><p>al_perspective_transform in 5.1</p></div></div><p>

I&#39;m using the windows binaries, which is only 5.0.8. According to the manual, 5.1 is unstable.<br />al_perspective_transform does indeed seem to what I want, however. Would you recommend switching to 5.1 despite this?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Fri, 10 May 2013 01:56:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>5.1 isn&#39;t really unstable. The API may change a bit and that&#39;s what &quot;unstable&quot; means. I&#39;d recommend it, I&#39;ve been using 5.1 in my games for years now.</p><p>EDIT: You could also just lift the code from 5.1 into your game.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Fri, 10 May 2013 02:00:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I looked at the 5.1 code, and it seems to set the values of a 4x4 matrix to a projection matrix. When al_use_transform is called, it applies the 3D transformation to the image. It doesn&#39;t seem to do this in 5.0.8, instead only using the 2D components of the 4x4 matrix. al_use_transform has the same code in 5.0.8 and 5.1, so one of the functions it calls must be different.</p><p>Is there a way I can still use 5.0.8 but still have those nice 3D transformations <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Fri, 10 May 2013 02:54:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think al_compose_transform is different in 5.1 compared to 5.0. It&#39;s exactly as you stated, 5.1 does a &quot;3D&quot; multiplication and I think 5.0 still only does 2D (since it didn&#39;t get any of the 3D transforms.) I didn&#39;t look at the code but I think you can just copy al_compose_transform from 5.1 and use that to combine your matrices.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Fri, 10 May 2013 05:44:11 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>al_compose_transform only modifies the matrix, and the matrix itself isn&#39;t the problem. The function that actually performs the transformations on the bitmap (which I believe is <i>display-&gt;vt-&gt;update_transformation</i> from al_use_transform) seems to ignore the 3D components of the matrix in 5.0.8. I tested this by manually modifying the matrix to a set that I know would produce a 3d perspective, but this only effects the bitmap in 2D.</p><p>I&#39;m not very familiar with allegro and I can&#39;t find where <i>display-&gt;vt-&gt;update_transformation</i> is in the source code, although it seems to be a lost cause. I&#39;m probably better off switching to 5.1, or finding a different way to produce the 3D effect.</p><p>Edit: I&#39;ve figured out a trick to do it in 5.0.8 without getting or setting pixels <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" /><br />However, I would still like to know if there&#39;s a faster way to get/set pixels in general, especially get.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sat, 11 May 2013 06:47:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>There are two ways basically. 1) lock the bitmap first, then unlock it when you&#39;re done. 2) lock it and don&#39;t use get/put pixel but use direct access to the lock buffer which can be slightly faster.</p><p>In general, try to make the format you create your bitmaps with the one you lock them with, otherwise a conversion has to be done (on both lock and unlock.)
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sat, 11 May 2013 07:16:07 +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/612561/982526#target">Trent Gamblin</a> said:</div><div class="quote"><p>use direct access to the lock buffer which can be slightly faster.</p></div></div><p>
I&#39;ve been trying to figure out how to do this with no success. Would you be willing to explain how this is done?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sat, 11 May 2013 07:59:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><a href="http://www.allegro.cc/manual/al_set_new_bitmap_format"><span class="a">al_set_new_bitmap_format</span></a><span class="k2">(</span>ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE<span class="k2">)</span><span class="k2">;</span>
<span class="number">  2</span><a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span>bmp <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_bitmap"><span class="a">al_load_bitmap</span></a><span class="k2">(</span><span class="s">"something.png"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number">  3</span><a href="http://www.allegro.cc/manual/ALLEGRO_LOCKED_REGION"><span class="a">ALLEGRO_LOCKED_REGION</span></a> <span class="k3">*</span>lr <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_lock_bitmap"><span class="a">al_lock_bitmap</span></a><span class="k2">(</span>bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READWRITE<span class="k2">)</span><span class="k2">;</span>
<span class="number">  4</span><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> <a href="http://www.allegro.cc/manual/al_get_bitmap_height"><span class="a">al_get_bitmap_height</span></a><span class="k2">(</span>bmp<span class="k2">)</span><span class="k2">;</span> y<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  5</span>   <span class="k1">uint8_t</span> <span class="k3">*</span>ptr <span class="k3">=</span> <span class="k2">(</span><span class="k1">uint8_t</span> <span class="k3">*</span><span class="k2">)</span>lr-&gt;data <span class="k3">+</span> y <span class="k3">*</span> lr-&gt;pitch<span class="k2">;</span>
<span class="number">  6</span>   <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> <a href="http://www.allegro.cc/manual/al_get_bitmap_width"><span class="a">al_get_bitmap_width</span></a><span class="k2">(</span>bmp<span class="k2">)</span><span class="k2">;</span> x<span class="k3">+</span><span class="k3">+</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number">  7</span>      <span class="k1">int</span> r <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">0</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="number">  8</span>      <span class="k1">int</span> g <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">1</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="number">  9</span>      <span class="k1">int</span> b <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">2</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="number"> 10</span>      <span class="k1">int</span> a <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">3</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>      <span class="k3">*</span><span class="k2">(</span><span class="k2">(</span><span class="k1">uint32_t</span> <span class="k3">*</span><span class="k2">)</span>ptr<span class="k2">)</span> <span class="k3">=</span> <span class="k2">(</span>r <span class="k3">|</span> <span class="k2">(</span>g <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="n">8</span><span class="k2">)</span> <span class="k3">|</span> <span class="k2">(</span>b <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="n">16</span><span class="k2">)</span> <span class="k3">|</span> <span class="k2">(</span>a <span class="k3">&lt;</span><span class="k3">&lt;</span> <span class="n">24</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>      ptr <span class="k3">+</span><span class="k3">=</span> <span class="n">4</span><span class="k2">;</span>
<span class="number"> 13</span>   <span class="k2">}</span>
<span class="number"> 14</span><span class="k2">}</span>
<span class="number"> 15</span><a href="http://www.allegro.cc/manual/al_unlock_bitmap"><span class="a">al_unlock_bitmap</span></a><span class="k2">(</span>bmp<span class="k2">)</span><span class="k2">;</span>
</div></div><p>

Something like that. I couldn&#39;t remember if ABGR_8888_LE is alpha first or red first... but just experiment with that.</p><p>Things to take note of: you don&#39;t have to use that bitmap format. It&#39;s a good format if you&#39;re using OpenGL and 32 bit bitmaps. Otherwise use something else. Second: the ALLEGRO_PIXEL_FORMAT_ANY means the lr-&gt;data block will be formatted in the same format as you created the bitmap. You CAN specify any format there, but if it&#39;s not the same as the bitmap format you get two conversions (thus slower.) Last: If you only need to read or write use ALLEGRO_LOCK_READONLY or ALLEGRO_LOCK_WRITEONLY. They&#39;re faster if you don&#39;t need to read and write.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sat, 11 May 2013 08:26:01 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks for all the help! I got your code to work. It should be faster, plus I do prefer getting an integer from 0-255 rather than a float (checking float equality makes me paranoid). Not sure what the +10 was for, but I didn&#39;t seem to need it. The other tips you gave were quite helpful too.</p><p>The only problem is that in debug mode I get this error in the console and program crashes:<br />Assertion failed: bitmap != dest &amp;&amp; bitmap != dest-&gt;parent, file allegro-git\src\bitmap.c, line 210</p><p>In release I have no such error and it runs fine.</p><p>Edit: I was drawing a bitmap region to itself, causing the error. Thank you for all the help Trent!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sat, 11 May 2013 16:56:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> r <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">0</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="k1">int</span> g <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">1</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="k1">int</span> b <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>ptr<span class="k3">+</span><span class="n">2</span><span class="k2">)</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
</pre></div></div><p>

Can&#39;t you just simply write this as:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> r <span class="k3">=</span> ptr<span class="k2">[</span><span class="n">0</span><span class="k2">]</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="k1">int</span> g <span class="k3">=</span> ptr<span class="k2">[</span><span class="n">1</span><span class="k2">]</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
<span class="k1">int</span> b <span class="k3">=</span> ptr<span class="k2">[</span><span class="n">2</span><span class="k2">]</span> <span class="k3">+</span> <span class="n">10</span><span class="k2">;</span>
</pre></div></div><p>

Or is there something I&#39;m missing here?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (l j)</author>
		<pubDate>Sat, 11 May 2013 17:02:53 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You can do it either way.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sat, 11 May 2013 17:05:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s compiled to exactly the same object code, so no difference whatsoever. But if you have void pointer, you have to cast it to right one first (before you add offset).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Raidho36)</author>
		<pubDate>Sat, 11 May 2013 21:47:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The 10 was not important and I forgot to mention why I put it there. I was just going to say this is a way to add brightness to the pixels (though IRL you would check that the value didn&#39;t go over 255 with MIN(255, p+10)).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 12 May 2013 03:21:45 +0000</pubDate>
	</item>
</rss>
