<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Caching texts</title>
		<link>http://www.allegro.cc/forums/view/610883</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 25 Aug 2012 10:21:03 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m thinking about caching static text which are getting drawn on screen. I.e. just draw such text into a bitmap one time and never again call al_draw_text for it, unless something changes.</p><p>But I ran into two problems.</p><p>1) How to determine size of caching bitmap and string position inside it? For now, I do this:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">int</span> x<span class="k3">=</span><span class="n">0</span>,y<span class="k3">=</span><span class="n">0</span>,w<span class="k3">=</span><span class="n">0</span>,h<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_get_text_dimensions"><span class="a">al_get_text_dimensions</span></a><span class="k2">(</span> <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>, text, <span class="k3">&amp;</span>x, <span class="k3">&amp;</span>y, <span class="k3">&amp;</span>w, <span class="k3">&amp;</span>h <span class="k2">)</span><span class="k2">;</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_create_bitmap"><span class="a">al_create_bitmap</span></a><span class="k2">(</span> w, h <span class="k2">)</span><span class="k2">;</span>
ScopedTarget t<span class="k2">(</span> bmp <span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span> <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>, color, <span class="k3">-</span>x, <span class="k3">-</span>y, ALLEGRO_ALIGN_LEFT, text <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Then, I draw a cached/non-cached text to compare the results:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span> <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>, color, x, y, ALLEGRO_ALIGN_LEFT, text <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> bmp, x, y, <span class="n">0</span> <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

And the cached text ends up is off by TWO pixels to the left! So, obviously, I&#39;m doing something wrong.</p><p>2) Even worse things happen, when I want to draw RIGHT or CENTER-aligned text. The cached bitmap stays the same, of course (it shouldn&#39;t be affected by alignment, should it?). But how to draw it, so the result would be the same? For now, I do this:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span> <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>, color, x, y, ALLEGRO_ALIGN_RIGHT, text <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> bmp, x <span class="k3">-</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>, y, <span class="n">0</span> <span class="k2">)</span><span class="k2">;</span>

<a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a><span class="k2">(</span> <a href="http://www.allegro.cc/manual/font"><span class="a">font</span></a>, color, x, y, ALLEGRO_ALIGN_CENTER, text <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> bmp, x <span class="k3">-</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="k3">/</span><span class="n">2</span>,<span class="n">0f</span>, y, <span class="n">0</span> <span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

But in both cases, cached text is off by a different amount of pixels, so I think I&#39;m missing something here too.</p><p>Can anyone help me with this? I think I&#39;m missing something very simple, or either very complex <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Max Savenkov)</author>
		<pubDate>Fri, 24 Aug 2012 11:54:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In point 1) you draw the text using -x and -y, so if y was 1 you would draw the text at -1 on the bitmap and then at 1 on the display?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Dizzy Egg)</author>
		<pubDate>Fri, 24 Aug 2012 19:18:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I think that&#39;s it, too.  Take the minuses out of your coordinates for <span class="source-code"><a href="http://www.allegro.cc/manual/al_draw_text"><span class="a">al_draw_text</span></a></span> and see what happens.</p><p>Also, you don&#39;t get any compiler warnings about the implicit cast to float?  It&#39;s been too long...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gideon Weems)</author>
		<pubDate>Sat, 25 Aug 2012 08:07:31 +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/610883/963943#target">Dizzy Egg</a> said:</div><div class="quote"><p>
In point 1) you draw the text using -x and -y, so if y was 1 you would draw the text at -1 on the bitmap and then at 1 on the display?
</p></div></div><p>

That helps with point 1), thanks! And I looked at the source at solved 2). I just have to use al_get_text_length instead of bbw return parameter of al_al_get_text_dimensions to adjust X of al_draw_bitmap, because they differ quite a lot.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Max Savenkov)</author>
		<pubDate>Sat, 25 Aug 2012 10:21:03 +0000</pubDate>
	</item>
</rss>
