<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>matrix_sprite()</title>
		<link>http://www.allegro.cc/forums/view/591685</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 02 Jun 2007 01:40:12 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I thought I was doing myself a favor by providing a common interface for drawing, scaling, rotating, shearing, and allowing for hierarchical drawing by concatenating matrices.</p><p>Turns out, I&#39;ve created a monster instead.</p><p>Here&#39;s the code for matrix_sprite()
</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="k1">typedef</span> boost::numeric::ublas::matrix<span class="k3">&lt;</span>double&gt; Mat<span class="k2">;</span></td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k1">void</span> matrix_sprite<span class="k2">(</span><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> bmp, <a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a><span class="k3">*</span> sprite, Mat<span class="k3">&amp;</span> m<span class="k2">)</span></td></tr><tr><td class="number">4</td><td><span class="k2">{</span></td></tr><tr><td class="number">5</td><td>    Mat to<span class="k2">(</span><span class="n">3</span>, <span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">6</td><td>    to.clear<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">7</td><td>    Mat from<span class="k2">(</span><span class="n">3</span>, <span class="n">1</span><span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">8</td><td>    from<span class="k2">(</span><span class="n">1</span>, <span class="n">0</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></td></tr><tr><td class="number">9</td><td>    from<span class="k2">(</span><span class="n">2</span>, <span class="n">0</span><span class="k2">)</span> <span class="k3">=</span> <span class="n">1</span><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> i <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> i <span class="k3">&lt;</span> sprite-&gt;h<span class="k2">;</span> i<span class="k3">+</span><span class="k3">+</span>, from<span class="k2">(</span><span class="n">1</span>, <span class="n">0</span><span class="k2">)</span><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>        from<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span><span class="k2">)</span> <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">for</span> <span class="k2">(</span><span class="k1">int</span> j <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span> j <span class="k3">&lt;</span> sprite-&gt;w<span class="k2">;</span> j<span class="k3">+</span><span class="k3">+</span>, from<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k3">+</span><span class="k3">+</span><span class="k2">)</span></td></tr><tr><td class="number">14</td><td>        <span class="k2">{</span></td></tr><tr><td class="number">15</td><td>            to <span class="k3">=</span> prod<span class="k2">(</span>m, from<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">16</td><td>            <a href="http://www.allegro.cc/manual/_putpixel" target="_blank"><span class="a">_putpixel</span></a><span class="k2">(</span>bmp, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span> to<span class="k2">(</span><span class="n">0</span>, <span class="n">0</span><span class="k2">)</span>, <span class="k2">(</span><span class="k1">int</span><span class="k2">)</span> to<span class="k2">(</span><span class="n">1</span>, <span class="n">0</span><span class="k2">)</span>, <a href="http://www.allegro.cc/manual/_getpixel" target="_blank"><span class="a">_getpixel</span></a><span class="k2">(</span>sprite, j, i<span class="k2">)</span><span class="k2">)</span><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="k2">}</span></td></tr><tr><td class="number">19</td><td><span class="k2">}</span></td></tr></tbody></table></div></div><p>

I probably misused _putpixel and _getpixel; this is my first time doing this low-level stuff.</p><p>It takes a 3x3 matrix that represents 2d position, along with rotation, scaling, and shearing. It multiplies that matrix and the 3x1 from matrix, which grabs pixels from the sprite, to get a 3x1 matrix that represents the position on the buffer to draw the pixel to.</p><p>It is, however, quite slow.</p><p>1969ms for 100 calls to matrix_sprite()<br />vs<br />31ms for 100 calls to rotate_scaled_sprite()<br />(no optimization)</p><p>2 problems:</p><p> <b>1) The call to prod (a Boost/uBLAS function) is ridiculously expensive.</b></p><p>If I comment out just that line, it costs only 219ms to make the call. 9x faster.</p><p><b>2) Scaled sprites don&#39;t get interpolated at all, so they show up as just as many pixels</b></p><p><span class="remote-thumbnail"><span class="json">{"name":"524883949_0857a844fa_o.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/7\/d777046dce63597efff50b74cc17b66d.png","w":806,"h":632,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/7\/d777046dce63597efff50b74cc17b66d"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/d/7/d777046dce63597efff50b74cc17b66d-240.jpg" alt="524883949_0857a844fa_o.png" width="240" height="188" /></span></p><p>vs.</p><p><span class="remote-thumbnail"><span class="json">{"name":"524804670_0e5207cffb_o.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6a67b70ea224c0d1c7685222985bf77a.png","w":806,"h":632,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6a67b70ea224c0d1c7685222985bf77a"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/a/6a67b70ea224c0d1c7685222985bf77a-240.jpg" alt="524804670_0e5207cffb_o.png" width="240" height="188" /></span></p><p>So.
</p><ul><li><p>Find another matrix library?</p></li><li><p>Scrap the function and just use rotate_scaled_sprite and keep a bunch of extra data members handy?</p></li><li><p>Apply one of those magical algorithms that everyone but me knows about and achieve a call time of -1 ms?</p></li><li><p>Await the apocalypse and just forget about this &quot;programming&quot; stuff?</p></li></ul><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Fri, 01 Jun 2007 18:54:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Considering all the trouble you&#39;re going through, I think perhaps you should just switch to AllegroGL or OpenLayer. They provide hardware acceleration and thus will get around some of the issues you&#39;re describing without having to do all the work you&#39;re doing.</p><p>For what you want, OpenLayer is a better, easier choice. AllegroGL will give you much more control but you&#39;ll have to do much of it manually using the OpenGL state system.</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>Fri, 01 Jun 2007 19:21:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hm. I suppose OpenLayer looks good. I&#39;ve been wanting to give AllegroGL a try for a while, too...</p><p>The main thing I would like to accomplish by taking a leap like that, though, is to have some framework code I can plug my logic and rendering into. From the BerliOS description, it claims it&#39;s a framework, but I see nothing in the documentation to back that up. Perhaps I have a different expectation of what a &quot;framework&quot; is.</p><p>I expected something like</p><p>OLSetCallbackFrameMove(MyGame::OnUpdateGame);<br />OLSetCallbackFrameRender(MyGame::OnRender);</p><p>...to appear at some point.</p><p>Of course, most of the benefits to this kind of structure are psychological; thinking there&#39;s a library there to muscle through the menial labor and give your game the cue when it&#39;s time to do what you want to do.</p><p>Still, I thought OpenLayer would be to Allegro what the DirectX Framework is to DirectX.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kibiz0r)</author>
		<pubDate>Sat, 02 Jun 2007 01:40:12 +0000</pubDate>
	</item>
</rss>
