Re: [AD] translucent fonts

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


On Thu, 2006-01-05 at 13:52 +0100, Elias Pschernig wrote:
> One way around that would be to only use the top-left corner pixel as
> background color for 32-bit alpha fonts, and keep 0 for 8-bit and yellow
> for 15/16/24-bit and opaque 32-bit ones.
> 

Attached is a patch which does that. Since it was the 3rd location to
define a bitmap_has_alpha(), the patch makes it an API function. And the
API toget alpha font drawing is now to use make_trans_font() on a font
to switch its vtable, and no more state.

-- 
Elias Pschernig
Index: include/allegro/font.h
===================================================================
--- include/allegro/font.h	(revision 5626)
+++ include/allegro/font.h	(working copy)
@@ -41,6 +41,8 @@
    struct FONT_VTABLE *vtable;
 } FONT;
 
+AL_FUNC(void, make_trans_font, (FONT *f));
+
 AL_FUNC(int, is_color_font, (FONT *f));
 AL_FUNC(int, is_mono_font, (FONT *f));
 AL_FUNC(int, is_compatible_font, (FONT *f1, FONT *f2));
Index: include/allegro/internal/aintern.h
===================================================================
--- include/allegro/internal/aintern.h	(revision 5626)
+++ include/allegro/internal/aintern.h	(working copy)
@@ -262,6 +262,8 @@
 AL_VAR(FONT_VTABLE *, font_vtable_mono);
 AL_VAR(FONT_VTABLE, _font_vtable_color);
 AL_VAR(FONT_VTABLE *, font_vtable_color);
+AL_VAR(FONT_VTABLE, _font_vtable_trans);
+AL_VAR(FONT_VTABLE *, font_vtable_trans);
 
 AL_FUNC(FONT_GLYPH *, _mono_find_glyph, (AL_CONST FONT *f, int ch));
 AL_FUNC(BITMAP *, _color_find_glyph, (AL_CONST FONT *f, int ch));
Index: src/font.c
===================================================================
--- src/font.c	(revision 5626)
+++ src/font.c	(working copy)
@@ -921,7 +921,7 @@
         }
         else {
             if (bitmap_color_depth(g) == bitmap_color_depth(bmp)) {
-	       masked_blit(g, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
+                masked_blit(g, bmp, 0, 0, x, y + (h-g->h)/2, g->w, g->h);
             }
             else {
                int color_conv_mode;
@@ -952,6 +952,38 @@
 
 
 
+/* trans_render_char:
+ *  (trans vtable entry)
+ *
+ *  Renders a transparent character onto a bitmap, at the specified location,
+ *  using the specified colors. fg is ignored. if bg == -1, render as
+ *  transparent, else render as opaque. Returns the character width, in pixels.
+ */
+static int trans_render_char(AL_CONST FONT* f, int ch, int fg, int bg, BITMAP* bmp, int x, int y)
+{
+    int w = 0;
+    int h = f->vtable->font_height(f);
+    BITMAP *g = 0;
+
+    acquire_bitmap(bmp);
+
+    if(bg >= 0) {
+       rectfill(bmp, x, y, x + f->vtable->char_length(f, ch) - 1, y + h - 1, bg);
+    }
+
+    g = _color_find_glyph(f, ch);
+    if(g) {
+       draw_trans_sprite(bmp, g, x, y + (h-g->h)/2);
+       w = g->w;
+    }
+
+    release_bitmap(bmp);
+
+    return w;
+}
+
+
+
 /* color_render:
  *  (color vtable entry)
  *  Renders a color font onto a bitmap, at the specified location, using
@@ -1393,8 +1425,39 @@
 
 FONT_VTABLE* font_vtable_color = &_font_vtable_color;
 
+FONT_VTABLE _font_vtable_trans = {  
+    font_height,
+    color_char_length,
+    length,
+    trans_render_char,
+    color_render,
+    color_destroy,
 
+    color_get_font_ranges,
+    color_get_font_range_begin,
+    color_get_font_range_end,
+    color_extract_font_range,
+    color_merge_fonts,
+    color_transpose_font
+};
 
+FONT_VTABLE* font_vtable_trans = &_font_vtable_trans;
+
+
+
+/* make_trans_font:
+ *  Modifes a font so glyphs are drawn with draw_trans_sprite.
+ */
+void make_trans_font(FONT *f)
+{
+   ASSERT(f);
+   ASSERT(f->vtable == font_vtable_color);
+
+   f->vtable = font_vtable_trans;
+}
+
+
+
 /* is_color_font:
  *  returns non-zero if the font passed is a bitmapped colour font
  */
Index: src/fontbmp.c
===================================================================
--- src/fontbmp.c	(revision 5626)
+++ src/fontbmp.c	(working copy)
@@ -32,7 +32,10 @@
 {
    int c;
 
-   if (bitmap_color_depth(bmp) == 8) {
+   if (bitmap_has_alpha(bmp)) {
+      c = getpixel(bmp, 0, 0);
+   }
+   else if (bitmap_color_depth(bmp) == 8) {
       c = 255;
    }
    else {
Index: src/gfx.c
===================================================================
--- src/gfx.c	(revision 5626)
+++ src/gfx.c	(working copy)
@@ -163,6 +163,29 @@
 
 
 
+/* bitmap_has_alpha:
+ *  Checks whether this bitmap has an alpha channel.
+ */
+int bitmap_has_alpha(BITMAP *bmp)
+{
+   int x, y, c;
+
+   if (bitmap_color_depth(bmp) != 32)
+      return FALSE;
+
+   for (y = 0; y < bmp->h; y++) {
+      for (x = 0; x < bmp->w; x++) {
+	 c = getpixel(bmp, x, y);
+	 if (geta32(c))
+	    return TRUE;
+      }
+   }
+
+   return FALSE;
+}
+
+
+
 /* vsync:
  *  Waits for a retrace.
  */
Index: src/tga.c
===================================================================
--- src/tga.c	(revision 5626)
+++ src/tga.c	(working copy)
@@ -470,29 +470,6 @@
 
 
 
-/* bitmap_has_alpha:
- *  Checks whether this bitmap has an alpha channel.
- */
-static int bitmap_has_alpha(BITMAP *bmp)
-{
-   int x, y, c;
-
-   if (bitmap_color_depth(bmp) != 32)
-      return FALSE;
-
-   for (y = 0; y < bmp->h; y++) {
-      for (x = 0; x < bmp->w; x++) {
-	 c = getpixel(bmp, x, y);
-	 if (geta32(c))
-	    return TRUE;
-      }
-   }
-
-   return FALSE;
-}
-
-
-
 /* save_tga:
  *  Writes a bitmap into a TGA file, using the specified palette (this
  *  should be an array of at least 256 RGB structures).
Index: tools/plugins/datimage.c
===================================================================
--- tools/plugins/datimage.c	(revision 5626)
+++ tools/plugins/datimage.c	(working copy)
@@ -27,27 +27,6 @@
 
 
 
-/* checks whether this bitmap has an alpha channel */
-static int bitmap_has_alpha(BITMAP *bmp)
-{
-   int x, y, c;
-
-   if (bitmap_color_depth(bmp) != 32)
-      return FALSE;
-
-   for (y=0; y<bmp->h; y++) {
-      for (x=0; x<bmp->w; x++) {
-	 c = getpixel(bmp, x, y);
-	 if (geta32(c))
-	    return TRUE;
-      }
-   }
-
-   return FALSE;
-}
-
-
-
 /* checks whether this RLE sprite has an alpha channel */
 static int rle_has_alpha(AL_CONST RLE_SPRITE *spr)
 {


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