diff mde/resource/FontTexture.d @ 57:9e1f05fbbcef

Coloured and alpha-blended text is now supported. TextWidgets get text colour from argument.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 14 Jun 2008 13:09:03 +0100
parents f000d6cd0f74
children 7cab2af4ba21
line wrap: on
line diff
--- a/mde/resource/FontTexture.d	Sat Jun 14 12:04:25 2008 +0100
+++ b/mde/resource/FontTexture.d	Sat Jun 14 13:09:03 2008 +0100
@@ -24,6 +24,7 @@
  * matter. However, for the model/world coords, y increases downwards. */
 module mde.resource.FontTexture;
 
+import mde.types.basic;	// Colour
 import mde.Options;
 import mde.resource.exception;
 
@@ -160,15 +161,59 @@
         }
     }
     
-    /** Render a block of text using a cache. Updates the cache if necessary. */
-    void drawTextCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y) {
+    /** Render a block of text using a cache. Updates the cache if necessary.
+     *
+     * Params:
+     *  face =	Current typeface pointer; must be passed from font.d (only needed if the cache is
+     *  	invalid)
+     *  str =	Text to render (only needed if the cache is invalid)
+     *  cache =	Cache used to speed up CPU-side rendering code
+     *  x =	Smaller x-coordinate of position
+     *  y =	Smaller y-coordinate of position
+     *  col =	Text colour (note: currently limited to black or white) */
+    void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col ) {
         updateCache (face, str, cache);	// update if necessary
         debug scope (failure)
                 logger.error ("drawTextCache failed");
         
+        // opaque (GL_DECAL would be equivalent)
+        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
+        
+        drawCacheImpl (cache, x, y, col);
+    }
+    /** A variation of drawCache, for transparent text.
+     *
+     * Instead of passing the alpha value(s) as arguments, set the openGL colour prior to calling:
+     * ---
+     * glColor3f (.5f, .5f, .5f);	// set alpha to half
+     * drawCacheA (face, ...);
+     * 
+     * glColor3ub (0, 255, 127);	// alpha 0 for red, 1 for green, half for blue
+     * drawCacheA (face, ...);
+     * ---
+     *
+     * The overhead of the transparency is minimal. */
+    void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col/+ = Colour.WHITE+/) {
+        updateCache (face, str, cache);	// update if necessary
+        debug scope (failure)
+                logger.error ("drawTextCache failed");
+        
+        // transparency alpha
+        // alpha is current colour, per component
+        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+        
+        drawCacheImpl (cache, x, y, col);
+    }
+   
+    private void drawCacheImpl (ref TextBlock cache, int x, int y, Colour col) {
+        if (DerelictGL.availableVersion() >= GLVersion.Version14) {
+            glBlendFunc (GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR);
+            glBlendColor(col.r, col.g, col.b, 1f);	// text colour
+        } else
+            glBlendFunc (col.nearestGLConst, GL_ONE_MINUS_SRC_COLOR);
+        
         glEnable (GL_TEXTURE_2D);
         glEnable(GL_BLEND);
-        glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR);
         
         foreach (chr; cache.chars) {
             GlyphAttribs* ga = chr.ga;
@@ -281,10 +326,13 @@
     
     void drawTexture () {	// temp func
         if (tex.length == 0) return;
+        glEnable (GL_TEXTURE_2D);
         glBindTexture(GL_TEXTURE_2D, tex[0].texID);
-        glEnable (GL_TEXTURE_2D);
         glEnable(GL_BLEND);
         glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR);
+        float[4] Cc = [ 1.0f, 1f, 1f, 1f ];
+        glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Cc.ptr);
+        glColor3f (1f, 0f, 0f);
         
         glBegin (GL_QUADS);
         glTexCoord2f (0f, 0f);	glVertex2i (0, 0);
@@ -312,14 +360,12 @@
     // create a new texture
     static LinePacker create () {
         LinePacker p;
-        //FIXME: check for error?
+        //FIXME: why do I get a blank texture when binding to non-0?
         //glGenTextures (1, &(p.texID));
         p.texID = 0;
-        //FIXME: why do I get a blank texture when using bind?
-        glBindTexture(GL_TEXTURE_2D, p.texID);
         
         // add a pretty background to the texture
-        static if (false) {
+        static if (true) {
             glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
             glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
             ubyte[3][dimH][dimW] testTex;
@@ -335,7 +381,8 @@
             const void* ptr = null;
         
         // Create a texture without initialising values.
-        glTexImage2D(GL_TEXTURE_2D, 0, 3,
+        glBindTexture(GL_TEXTURE_2D, p.texID);
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
                      dimW, dimH, 0,
                      GL_RGB, GL_UNSIGNED_BYTE, ptr);
         return p;