Re: [AD] [patch] is_trans_font()

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Milan Mimica wrote:
I think font_has_alpha() would be just enough. It is assumed that you had to determine the font type some way after load_font(). So the complete float loading & conversion code can look like:

FONT *fnt = load_font(...);
FONT *agl_fnt;

if (font_has_alpha(fnt))
{
	make_trans_font(fnt);
	agl_fnt = allegro_gl_convert_allegro_font_ex(..., GL_RGBA);
}
else
{
	agl_fnt = allegro_gl_convert_allegro_font_ex(..., GL_ALPHA8);
}

make_trans_font() triggers an assertion when passed a font that already uses the trans vtable. This becomes a problem when I want to keep the Allegro font in memory after creating the AllegroGL copy so I can restore the latter faster after texture memory has been lost. Of course, I can use a variable to keep track of whether make_trans_font() has been called on the font, but that's a bit ugly, isn't it? I'd prefer either to have is_trans_font() or patch make_trans_font() to do nothing if the font is already using the trans vtable. I suppose the latter is the way to go since it only introduces one new API function. Then again, it seems natural to add is_trans_font() to complement the existing is_mono_font, is_color_font and is_compatible_font functions.

It would be the best if load_font could determine the font type itself and convert before returning, just like it does for monochrome and color fonts. Too bad that trans font would crash on textout_ex() if trans blender hasn't been set.

I'll leave it to someone more knowledgeable than me to determine if that is fixable. :)

Attached is a patch to add font_has_alpha().

--
Daniel Schlyder
Index: include/allegro/font.h
===================================================================
--- include/allegro/font.h	(revision 7545)
+++ include/allegro/font.h	(working copy)
@@ -41,6 +41,7 @@
    struct FONT_VTABLE *vtable;
 } FONT;
 
+AL_FUNC(int, font_has_alpha, (FONT *f));
 AL_FUNC(void, make_trans_font, (FONT *f));
 
 AL_FUNC(int, is_color_font, (FONT *f));
Index: src/font.c
===================================================================
--- src/font.c	(revision 7545)
+++ src/font.c	(working copy)
@@ -1445,6 +1445,33 @@
 
 
 
+/* font_has_alpha:
+ *  Returns TRUE if a color font has an alpha channel.
+ */
+int font_has_alpha(FONT *fnt)
+{
+   FONT_COLOR_DATA *data;
+   int ch;
+
+   ASSERT(fnt);
+
+   if (!is_color_font(fnt))
+      return FALSE;
+
+   data = (FONT_COLOR_DATA *)(fnt->data);
+
+   while (data) {
+      for (ch = data->begin; ch != data->end; ++ch)
+         if (_bitmap_has_alpha(data->bitmaps[ch - data->begin]))
+            return TRUE;
+      data = data->next;
+   }
+
+   return FALSE;
+}
+
+
+
 /* make_trans_font:
  *  Modifes a font so glyphs are drawn with draw_trans_sprite.
  */


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/