<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Multiple views/cameras</title>
		<link>http://www.allegro.cc/forums/view/612235</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 17 Mar 2013 20:07:42 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hello everybody,</p><p>I&#39;m quite new to Allegro and just registered to the forums.</p><p>Here I come with my first question: In my 2D platformer game I would like to have multiple cameras (e.g. for split screens). I already got this working, but I&#39;m not sure if my solution is good, so I&#39;m asking for your advice.</p><p>I implemented the cameras like this (simplified):
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">class</span> Camera <span class="k2">{</span>
    Vector2 position, origin, scale<span class="k2">;</span>
    <span class="k1">float</span> rotation<span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a> transform<span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/ALLEGRO_BITMAP"><span class="a">ALLEGRO_BITMAP</span></a> <span class="k3">*</span>bitmap<span class="k2">;</span> <span class="c">// the render target for the camera</span>
<span class="k2">}</span><span class="k2">;</span>
</pre></div></div><p>

So basically each camera has its own bitmap where it renders to. All these camera bitmaps are rendered to the display backbuffer in a final step.</p><p>Is this a good approach? One minor drawback of this approach is that I have to resize/recreate all camera bitmaps (almost) whenever the display resolution changes. I also have to recreate the bitmaps in order to change the size (width/height) of a camera. Is there any better way of doing this?</p><p>Looking forward for your answers.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Endgegner)</author>
		<pubDate>Sun, 17 Mar 2013 07:16:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You could turn those bitmaps into subbitmaps, and skip the final `al_draw_bitmap`s.</p><p>Probably have lower overhead. Not sure you&#39;ll notice a change.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 17 Mar 2013 07:47:19 +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/612235/978633#target">Endgegner</a> said:</div><div class="quote"><p>One minor drawback of this approach is that I have to resize/recreate all camera bitmaps (almost) whenever the display resolution changes. I also have to recreate the bitmaps in order to change the size (width/height) of a camera.</p></div></div><p>
One thought would be to have a couple XY values for the bounding box of each camera, then when it comes time to render a camera, simply render to the backbuffer after making a call to al_set_clipping_rectangle(). That way, you only have to recreate the backbuffer on each display change and not every camera&#39;s bitmaps, since they would no longer require one.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kris Asick)</author>
		<pubDate>Sun, 17 Mar 2013 10:47:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you both for your input!</p><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/612235/978641#target">Kris Asick</a> said:</div><div class="quote"><p>One thought would be to have a couple XY values for the bounding box of each camera, then when it comes time to render a camera, simply render to the backbuffer after making a call to al_set_clipping_rectangle(). That way, you only have to recreate the backbuffer on each display change and not every camera&#39;s bitmaps, since they would no longer require one.</p></div></div><p>
This works great, thanks. I have tried that before, but unfortunately I forgot to reset the clipping rectangle to the target bitmap size and this caused some rendering glitches. That was a rookie mistake, I guess. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><p>Just for the record, here is the solution:</p><p>These are the modified camera members:
</p><div class="source-code snippet"><div class="inner"><pre>Vector2 position, origin, scale<span class="k2">;</span>
<span class="k1">float</span> rotation<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a> transform<span class="k2">;</span>
IntRectangle clipping<span class="k2">;</span>
</pre></div></div><p>

And this is the rendering code:
</p><div class="source-code snippet"><div class="inner"><pre>IntRectangle clipping<span class="k2">;</span>

<a href="http://www.allegro.cc/manual/al_get_clipping_rectangle"><span class="a">al_get_clipping_rectangle</span></a><span class="k2">(</span><span class="k3">&amp;</span>clipping.x, <span class="k3">&amp;</span>clipping.y, <span class="k3">&amp;</span>clipping.width, <span class="k3">&amp;</span>clipping.height<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>

<span class="k1">for</span> <span class="k2">(</span><span class="k1">auto</span> camera <span class="k2">:</span> cameras<span class="k2">)</span>
<span class="k2">{</span>
    <a href="http://www.allegro.cc/manual/al_use_transform"><span class="a">al_use_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>camera.transform<span class="k2">)</span><span class="k2">;</span>
    <a href="http://www.allegro.cc/manual/al_set_clipping_rectangle"><span class="a">al_set_clipping_rectangle</span></a><span class="k2">(</span>camera.clipping.x, camera.clipping.y,
                              camera.clipping.width, camera.clipping.height<span class="k2">)</span><span class="k2">;</span>
    <span class="c">// Draw the bitmaps ...</span>
<span class="k2">}</span>

<a href="http://www.allegro.cc/manual/al_set_clipping_rectangle"><span class="a">al_set_clipping_rectangle</span></a><span class="k2">(</span>clipping.x, clipping.y, clipping.width, clipping.height<span class="k2">)</span><span class="k2">:</span>
</pre></div></div><p>

Problem solved.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Endgegner)</author>
		<pubDate>Sun, 17 Mar 2013 20:07:42 +0000</pubDate>
	</item>
</rss>
