<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Allegro fonts in AllegroGL</title>
		<link>http://www.allegro.cc/forums/view/405734</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sat, 11 Sep 2004 21:09:52 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve just finished writing a few useful functions for displaying allegro fonts in AllegroGL. (AllegroGL&#39;s font drawing routines are not working for displaying textured allegro fonts in anything other than monochrome).</p><p>If anyone else is having the same problem, you may find this very useful.</p><p>Here are the function prototypes:</p><p><span class="source-code">JFONT <span class="k3">*</span>create_jfont<span class="k2">(</span><a href="http://www.allegro.cc/manual/FONT" target="_blank"><span class="a">FONT</span></a> <span class="k3">*</span>fnt<span class="k2">)</span><span class="k2">;</span></span>This generates a font from an allegro font. FONT *fnt must be an allegro font.</p><p><span class="source-code"><span class="k1">void</span> destroy_jfont<span class="k2">(</span>JFONT <span class="k3">*</span>jfont<span class="k2">)</span><span class="k2">;</span></span>Destroys the generated font, deleting OpenGL textures.</p><p><span class="source-code"><span class="k1">void</span> jprintf<span class="k2">(</span>JFONT <span class="k3">*</span>f, <span class="k1">float</span> x, <span class="k1">float</span> y, <span class="k1">float</span> z,<span class="k1">float</span> scale, <span class="k1">char</span> <span class="k3">*</span>format, ...<span class="k2">)</span><span class="k2">;</span></span>Draws the text as a series of quads (printf style). OpenGL&#39;s matrix transformations must be set appropriately before the text is drawn.</p><p>There <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />, nice and simple.</p><p>(note: the blurring around the edges is &#39;cause this is a .jpg screenshot.)<br /><span class="remote-thumbnail"><span class="json">{"name":"fnt1.JPG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d473963939496b4f9a2581af789f644.jpg","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/d\/0d473963939496b4f9a2581af789f644"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/0/d/0d473963939496b4f9a2581af789f644-240.jpg" alt="fnt1.JPG" width="240" height="180" /></span></p><p>JFONT *create_jfont(FONT *fnt) is actually quite complicated.  It uses a packing algorithm to fit the glyphs into an appropriately sized texture:</p><p>Allegro font: (before packing)<br /><span class="remote-thumbnail"><span class="json">{"name":"fnt2.JPG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8f965402b786972e9c4327c7b8daa05.jpg","w":958,"h":284,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/8\/a8f965402b786972e9c4327c7b8daa05"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/a/8/a8f965402b786972e9c4327c7b8daa05-240.jpg" alt="fnt2.JPG" width="240" height="71" /></span></p><p>OpenGL (2^n,2^m) texture after packing:<br /><span class="remote-thumbnail"><span class="json">{"name":"fnt3.JPG","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b9bfb84e4b992bd3b635099981ed6c44.jpg","w":513,"h":256,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/9\/b9bfb84e4b992bd3b635099981ed6c44"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/b/9/b9bfb84e4b992bd3b635099981ed6c44-240.jpg" alt="fnt3.JPG" width="240" height="119" /></span></p><p>I won&#39;t post the code now, but if anyone wants it, I&#39;ll neaten it up a bit. (I can&#39;t be bothered at the moment &#39;cause, no doubt, Bob&#39;ll fix the problem tomorrow and all this will be a waste of time <img src="http://www.allegro.cc/forums/smileys/rolleyes.gif" alt="::)" /> )</p><p>Gnatinator:  Thanks for your code, I found it useful for writing my own functions.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Fri, 10 Sep 2004 04:46:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The other option would have been to send me a patch for AllegroGL...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Fri, 10 Sep 2004 04:55:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>That&#39;s what I spent the better part of today trying to do, but I just couldn&#39;t get my head around all your code let alone actually find the problem.</p><p>I&#39;ll give it another go over the weekend if you like <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />. Or maybe you could fix it <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />?  Seriously I&#39;m sure it would take you a tenth of the time it would take me...</p><p>Well if it is fixable (I gave up earlier today mainly because I was worried that it was an OpenGL limitation and not a bug) I don&#39;t mind trying again.  It&#39;d do me a lot of good to go through it all in detail, I&#39;m sure <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Fri, 10 Sep 2004 05:07:43 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It looks good to me.. although you may want to, when creating the OpenGL texture, make it have as few vertical rows as possible and put a transparant pixel or two in between the glyphs. This will help when using linear filtering. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Though I would think it&#39;d be better to just fix AllegroGL.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 10 Sep 2004 06:29:45 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>make it have as few vertical rows as possible</p></div></div><p> I don&#39;t understand <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>put a transparant pixel or two in between the glyphs</p></div></div><p> Already done.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Fri, 10 Sep 2004 13:42:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Put as many glyphs on a single vertical row as possible. The fewer vertical rows you have, the less padding there&#39;ll be. More space-efficient.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Fri, 10 Sep 2004 14:51:18 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The packing algorith I&#39;m using doesn&#39;t work like that.  If you look closely you&#39;ll see that there are no columns (vertical rows).  The reason why there is padding at the bottom is because the OpenGL texture must be (2^n,2^m).</p><p>[EDIT] Implemented gradients for monochrone fonts.  Look&#39;n&#39; pretty <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />.</p><p>These are drawn with the same monochrone font:<br /><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/f/9/f9e0b84cfcd6e81598143c63ce8c0c33.png" alt="fnt1.bmp" width="139" height="58" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Fri, 10 Sep 2004 16:00:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Looking good! Im glad to finally see a complete solution to the problem. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Gnatinator: Thanks for your code, I found it useful for writing my own functions.</p></div></div><p>
Hey hey, no problem</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>These are drawn with the same monochrone font...</p></div></div><p>
Cant see the picture
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnatinator)</author>
		<pubDate>Sat, 11 Sep 2004 13:38:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Gnatinator: I wonder if your texture packing code is better than AllegroGL&#39;s. If it is, and is also simpler, then it might be a good replacement.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Sat, 11 Sep 2004 13:48:49 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Gnatinator: I wonder if your texture packing code is better than AllegroGL&#39;s. If it is, and is also simpler, then it might be a good replacement.</p></div></div><p>

Heh, the texture isnt packed or unpacked. The code just loads a tga texture, cuts it up and displays characters according to the parameters you give the function.</p><p>If you want my code check out my post <a href="http://www.allegro.cc/forums/view_thread.php?_id=405531">here.</a></p><p>The disadvantage to my system is that the font is displayed in quads of all the same size. So unless all your characters are the same width, your going to have a slight change of distance between each char. </p><p>At the moment I have a parameter that will adjust character distance and for now I just use that which is kind of a temporary fix.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnatinator)</author>
		<pubDate>Sat, 11 Sep 2004 15:52:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The texture you posted shows characters of varying width, just use those widths. no?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 11 Sep 2004 15:58:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>In that font (the parameters on the function could accomidate any char w/h as long as all are constant) the chars have 16 pixel hieght and 16 width box surrounding them. The function just blits that 16x16 box for whatever char you need, sometimes creating a little extra space.</p><p>EDIT:<br />To see the layout better, open it in PS or some other graphics program that support grids and turn on a 16x16 grid over the image then you will see.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnatinator)</author>
		<pubDate>Sat, 11 Sep 2004 16:02:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Actually, You&#39;ll have to excuse me <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" /> I was thinking about the one James posted <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /> the one all packed together.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sat, 11 Sep 2004 16:12:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is the second time I&#39;ve been confused with someone else also using a greyscale avatar in the same thread as me <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />.</p><p>My code stores character offsets during packing so that the text is drawn exactly as it would look if drawn with allegro&#39;s text functions.  It also means that it is faster because the quads are only as big as they need to be.</p><p>For example here&#39;s some text with texturing disabled to give you an idea of the quad size:</p><p><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/8/8/8837d82e7dd49f8164629a0b19a9f5dc.png" alt="fnt1.PNG" width="227" height="57" /></p><p>[Edit]<br />Oh, and another thing I just added: When loading the font, there is now the option to enable anti-aliasing.  This means that the font texture (the packed one shown in my first post) includes an alpha channel which is set so that when the font is drawn, it blends smoothly onto whatever it is drawn upon.  This is pointless for small fonts (because of OpenGLs automatic masked texture anti-aliasing) but looks great for larger fonts where you need a more gradual gradient from opaque to transparent.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sat, 11 Sep 2004 19:50:23 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey James maybe you could upload your code? <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Gnatinator)</author>
		<pubDate>Sat, 11 Sep 2004 20:05:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, just need some time to neaten it up a bit (add indents and some comments etc. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" />).</p><p>[EDIT]</p><div class="quote_container"><div class="title">Bob said:</div><div class="quote"><p>I wonder if your texture packing code is better than AllegroGL&#39;s. If it is, and is also simpler, then it might be a good replacement.</p></div></div><p>

Pahahahah.  I&#39;m almost too embarrassed to show my code it&#39;s such a mess.  Sorry, there&#39;s not a hope in hell of anything I&#39;ve written replacing your professional code.</p><p>All this is meant to be is something hacky that works until someone fixes AllegrGL. I wish I had the ability to fix it, but unfortunately it&#39;s beyond me.</p><p>So here it is:</p><p>jfont.h
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td>&#160;</td></tr><tr><td class="number">3</td><td><span class="k1">typedef</span> <span class="k1">struct</span> JFONT_GLYPH <span class="k2">{</span>           <span class="c">//each character width,height,texture offset and size offset</span></td></tr><tr><td class="number">4</td><td>    <span class="k1">float</span> w, h<span class="k2">;</span>  <span class="c">//store as floats to avoid conversion during rendering</span></td></tr><tr><td class="number">5</td><td>    <span class="k1">float</span> offx,offy<span class="k2">;</span> <span class="c">//position on the font texture   (top left)</span></td></tr><tr><td class="number">6</td><td>    <span class="k1">float</span> offxx,offyy<span class="k2">;</span> <span class="c">//position on the font texture (bottem right) - could be calculated from others, but faster to store it here</span></td></tr><tr><td class="number">7</td><td>    <span class="k1">float</span> px,py<span class="k2">;</span> <span class="c">//character postion modifier (e.g. so that _ is drawn at the bottem and not in the middle</span></td></tr><tr><td class="number">8</td><td><span class="k2">}</span> JFONT_GLYPH<span class="k2">;</span></td></tr><tr><td class="number">9</td><td>&#160;</td></tr><tr><td class="number">10</td><td><span class="k1">typedef</span> <span class="k1">struct</span> JFONT <span class="k2">{</span></td></tr><tr><td class="number">11</td><td>    GLuint texture<span class="k2">;</span>  <span class="c">//opengl texture with all font characters</span></td></tr><tr><td class="number">12</td><td>    JFONT_GLYPH glyph<span class="k2">[</span><span class="n">96</span><span class="k2">]</span><span class="k2">;</span>  <span class="c">//character offsets, sizes etc.    </span></td></tr><tr><td class="number">13</td><td>    <span class="k1">float</span> font_height<span class="k2">;</span> <span class="c">//height of tallest character in the font.</span></td></tr><tr><td class="number">14</td><td><span class="k2">}</span> JFONT<span class="k2">;</span></td></tr><tr><td class="number">15</td><td>&#160;</td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td><span class="c">//prototypes</span></td></tr><tr><td class="number">18</td><td>&#160;</td></tr><tr><td class="number">19</td><td>JFONT <span class="k3">*</span>create_jfont<span class="k2">(</span><a href="http://www.allegro.cc/manual/FONT" target="_blank"><span class="a">FONT</span></a> <span class="k3">*</span>fnt<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">20</td><td><span class="k1">void</span> destroy_jfont<span class="k2">(</span>JFONT <span class="k3">*</span>jfont<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">21</td><td>&#160;</td></tr><tr><td class="number">22</td><td><span class="c">//pre font loading, these are parameters set prior to loading the font: </span></td></tr><tr><td class="number">23</td><td><span class="k1">int</span> enable_jfont_alpha<span class="k2">(</span><span class="k1">int</span> x<span class="k2">)</span><span class="k2">;</span>  <span class="c">//for anti-aliased font </span></td></tr><tr><td class="number">24</td><td><span class="k1">int</span> enable_jfont_monochrome<span class="k2">(</span><span class="k1">int</span> x<span class="k2">)</span><span class="k2">;</span> <span class="c">/* loads a colour allegro font as monochrome (could maybe do with some optimizing </span></td></tr><tr><td class="number">25</td><td><span class="c">                                       - change the internal format of the OpenGL texture or something. I dunno :S ) </span></td></tr><tr><td class="number">26</td><td><span class="c">                                       Also, if optimisation is possible, it needs to be made automatic for allegro fonts</span></td></tr><tr><td class="number">27</td><td><span class="c">                                       that are already monochrome.*/</span></td></tr><tr><td class="number">28</td><td>&#160;</td></tr><tr><td class="number">29</td><td>&#160;</td></tr><tr><td class="number">30</td><td><span class="k1">void</span> jprintf<span class="k2">(</span>JFONT <span class="k3">*</span>f, <span class="k1">float</span> px, <span class="k1">float</span> py, <span class="k1">float</span> pz,<span class="k1">float</span> scale, <span class="k1">char</span> <span class="k3">*</span>format, ...<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">31</td><td>&#160;</td></tr><tr><td class="number">32</td><td><span class="c">//Pre text drawing:</span></td></tr><tr><td class="number">33</td><td><span class="k1">int</span> enable_jfont_gradient<span class="k2">(</span><span class="k1">int</span> x<span class="k2">)</span><span class="k2">;</span></td></tr><tr><td class="number">34</td><td>&#160;</td></tr><tr><td class="number">35</td><td><span class="c">/* This function is very cumbersome at the moment :(. </span></td></tr><tr><td class="number">36</td><td><span class="c"></span></td></tr><tr><td class="number">37</td><td><span class="c">0: top left of font</span></td></tr><tr><td class="number">38</td><td><span class="c">1: bottom left</span></td></tr><tr><td class="number">39</td><td><span class="c">2: bottem right</span></td></tr><tr><td class="number">40</td><td><span class="c">3: top right</span></td></tr><tr><td class="number">41</td><td><span class="c"></span></td></tr><tr><td class="number">42</td><td><span class="c">*/</span></td></tr><tr><td class="number">43</td><td><span class="k1">void</span> set_jfont_gradient<span class="k2">(</span><span class="k1">float</span> R0,<span class="k1">float</span> G0,<span class="k1">float</span> B0,<span class="k1">float</span> A0,</td></tr><tr><td class="number">44</td><td>                        <span class="k1">float</span> R1,<span class="k1">float</span> G1,<span class="k1">float</span> B1,<span class="k1">float</span> A1,</td></tr><tr><td class="number">45</td><td>                        <span class="k1">float</span> R2,<span class="k1">float</span> G2,<span class="k1">float</span> B2,<span class="k1">float</span> A2,</td></tr><tr><td class="number">46</td><td>                        <span class="k1">float</span> R3,<span class="k1">float</span> G3,<span class="k1">float</span> B3,<span class="k1">float</span> A3<span class="k2">)</span><span class="k2">;</span></td></tr></tbody></table></div></div><p>



jfont.c is attatched &#39;cause it&#39;s too big. </p><p>[EDIT 2] - fixed bug: create_jfont() was not working if allegro_gl_flip_texture() was set incorrectly.  Now it works whatever allegro_gl_flip_texture() is set to and restores original value.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (james_lohr)</author>
		<pubDate>Sat, 11 Sep 2004 21:09:52 +0000</pubDate>
	</item>
</rss>
