Changeset 57:9e1f05fbbcef

Show
Ignore:
Timestamp:
06/14/08 08:09:03 (7 months ago)
Author:
Diggory Hardy <diggory.hardy@gmail.com>
branch:
default
Message:

Coloured and alpha-blended text is now supported.

TextWidgets? get text colour from argument.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • codeDoc/jobs.txt

    r56 r57  
    5252 
    5353 
    54 Done (for git log message): 
    55 Implemented gl.texture module to load textures from file (untested)
    56 Fixed log level/option setting in Init. 
     54Done (for mercurial log message): 
     55Coloured and alpha-blended text is now supported
     56TextWidgets get text colour from argument. 
  • codeDoc/mergetag/file-format-binary.txt

    r26 r57  
    11Copyright © 2007-2008 Diggory Hardy 
    22License: GNU General Public License version 2 or later (see COPYING) 
     3 
     4 
     5No file format is set yet; this basically includes possibilities. The file format may or may not be compatible across platforms; if not it may just be used as a cache (i.e. open .mtt/.mtb, whichever is newest, and if it's .mtt then save a .mtb version). 
    36 
    47 
  • data/conf/fonts.mtt

    r48 r57  
    11{MT01} 
    22<char[]|fallback="default"> 
     3!{Lists available fonts. This data may be moved to options for more generic handling.} 
    34{default} 
    45<char[]|path="/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf"> 
  • data/conf/gui.mtt

    r47 r57  
    88<int|x=150> 
    99<int|y=200> 
    10 <int[][int]|widgetData=[0:[0xB004,5,5,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2],1:[0x3001],2:[0x2]]> 
     10<int[][int]|widgetData=[0:[0xB004,5,5,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2,1,1,1,1,1,2,1,2,1,2],1:[0x3001],2:[0x2,0xFFFF00]]> 
    1111{WEmbedded} 
    1212<int|x=20> 
  • mde/gl/basic.d

    r48 r57  
    2525//BEGIN GL & window setup 
    2626void glSetup () { 
     27    glDisable(GL_LIGHTING); 
    2728    glDisable(GL_DEPTH_TEST); 
    2829    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 
     
    3031    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 
    3132    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 
    32     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
    3333    glEnable(GL_TEXTURE_2D); 
    34     glShadeModel(GL_FLAT); 
     34    glShadeModel(GL_SMOOTH); 
    3535     
    3636    glClearColor (0.0f, 0.0f, 0.0f, 0.0f); 
  • mde/gui/widget/miscWidgets.d

    r49 r57  
    107107{ 
    108108    this (IWindow wind, int[] data) { 
    109         if (data.length != 1) throw new WidgetDataException; 
     109        if (data.length != 2) throw new WidgetDataException; 
    110110        if (font is null) font = FontStyle.get("default"); 
    111111        font.updateBlock (str, textCache); 
    112112        mw = textCache.w; 
    113113        mh = textCache.h; 
     114        colour = Colour (cast(ubyte) (data[1] >> 16u), 
     115                         cast(ubyte) (data[1] >> 8u), 
     116                         cast(ubyte) data[1] ); 
    114117        super (wind,data); 
    115118    } 
     
    117120    void draw () { 
    118121        super.draw(); 
    119         font.textBlock (x,y, str, textCache); // test new-lines and unicode characters 
     122        font.textBlock (x,y, str, textCache, colour); // test new-lines and unicode characters 
    120123    } 
    121124     
     
    123126    const str = "Text Widget\nαβγ − ΑΒΓ"; 
    124127    TextBlock textCache; 
     128    Colour colour; 
    125129    static FontStyle font; 
    126130} 
  • mde/resource/FontTexture.d

    r53 r57  
    2525module mde.resource.FontTexture; 
    2626 
     27import mde.types.basic; // Colour 
    2728import mde.Options; 
    2829import mde.resource.exception; 
     
    161162    } 
    162163     
    163     /** Render a block of text using a cache. Updates the cache if necessary. */ 
    164     void drawTextCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y) { 
     164    /** Render a block of text using a cache. Updates the cache if necessary. 
     165     * 
     166     * Params: 
     167     *  face =  Current typeface pointer; must be passed from font.d (only needed if the cache is 
     168     *      invalid) 
     169     *  str =   Text to render (only needed if the cache is invalid) 
     170     *  cache = Cache used to speed up CPU-side rendering code 
     171     *  x = Smaller x-coordinate of position 
     172     *  y = Smaller y-coordinate of position 
     173     *  col =   Text colour (note: currently limited to black or white) */ 
     174    void drawCache (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col ) { 
    165175        updateCache (face, str, cache); // update if necessary 
    166176        debug scope (failure) 
    167177                logger.error ("drawTextCache failed"); 
    168178         
     179        // opaque (GL_DECAL would be equivalent) 
     180        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
     181         
     182        drawCacheImpl (cache, x, y, col); 
     183    } 
     184    /** A variation of drawCache, for transparent text. 
     185     * 
     186     * Instead of passing the alpha value(s) as arguments, set the openGL colour prior to calling: 
     187     * --- 
     188     * glColor3f (.5f, .5f, .5f);   // set alpha to half 
     189     * drawCacheA (face, ...); 
     190     *  
     191     * glColor3ub (0, 255, 127);    // alpha 0 for red, 1 for green, half for blue 
     192     * drawCacheA (face, ...); 
     193     * --- 
     194     * 
     195     * The overhead of the transparency is minimal. */ 
     196    void drawCacheA (FT_Face face, char[] str, ref TextBlock cache, int x, int y, Colour col/+ = Colour.WHITE+/) { 
     197        updateCache (face, str, cache); // update if necessary 
     198        debug scope (failure) 
     199                logger.error ("drawTextCache failed"); 
     200         
     201        // transparency alpha 
     202        // alpha is current colour, per component 
     203        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
     204         
     205        drawCacheImpl (cache, x, y, col); 
     206    } 
     207    
     208    private void drawCacheImpl (ref TextBlock cache, int x, int y, Colour col) { 
     209        if (DerelictGL.availableVersion() >= GLVersion.Version14) { 
     210            glBlendFunc (GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR); 
     211            glBlendColor(col.r, col.g, col.b, 1f);  // text colour 
     212        } else 
     213            glBlendFunc (col.nearestGLConst, GL_ONE_MINUS_SRC_COLOR); 
     214         
    169215        glEnable (GL_TEXTURE_2D); 
    170216        glEnable(GL_BLEND); 
    171         glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR); 
    172217         
    173218        foreach (chr; cache.chars) { 
     
    282327    void drawTexture () {   // temp func 
    283328        if (tex.length == 0) return; 
     329        glEnable (GL_TEXTURE_2D); 
    284330        glBindTexture(GL_TEXTURE_2D, tex[0].texID); 
    285         glEnable (GL_TEXTURE_2D); 
    286331        glEnable(GL_BLEND); 
    287332        glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_COLOR); 
     333        float[4] Cc = [ 1.0f, 1f, 1f, 1f ]; 
     334        glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Cc.ptr); 
     335        glColor3f (1f, 0f, 0f); 
    288336         
    289337        glBegin (GL_QUADS); 
     
    313361    static LinePacker create () { 
    314362        LinePacker p; 
    315         //FIXME: check for error
     363        //FIXME: why do I get a blank texture when binding to non-0
    316364        //glGenTextures (1, &(p.texID)); 
    317365        p.texID = 0; 
    318         //FIXME: why do I get a blank texture when using bind? 
    319         glBindTexture(GL_TEXTURE_2D, p.texID); 
    320366         
    321367        // add a pretty background to the texture 
    322         static if (false) { 
     368        static if (true) { 
    323369            glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    324370            glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); 
     
    336382         
    337383        // Create a texture without initialising values. 
    338         glTexImage2D(GL_TEXTURE_2D, 0, 3, 
     384        glBindTexture(GL_TEXTURE_2D, p.texID); 
     385        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 
    339386                     dimW, dimH, 0, 
    340387                     GL_RGB, GL_UNSIGNED_BYTE, ptr); 
  • mde/resource/font.d

    r53 r57  
    1717module mde.resource.font; 
    1818 
     19public import mde.types.basic;  // Colour 
    1920import mde.Options; 
    2021import mde.resource.FontTexture; 
     
    229230     * The text block is drawn with top-left corner at x,y. To put the text's baseline at a given 
    230231     * y coordinate would require some changes. Line height is currently variable, depending on the 
    231      * highest glyph in the line. 
     232     * highest glyph in the line (should probably be fixed: FIXME). 
     233     * 
     234     * Specify the text's colour with col; currently this is only Colour.WHITE or Colour.BLACK 
     235     * (FIXME). FIXME: add alpha support. 
    232236     * 
    233237     * As a CPU-side code optimisation, store a TextBlock (unique to str) and pass a reference as 
     
    247251     * of a text block is to use a TextBlock cache and update it, either with this function or with 
    248252     * the updateBlock function. */ 
    249     void textBlock (int x, int y, char[] str, ref TextBlock cache) { 
     253    void textBlock (int x, int y, char[] str, ref TextBlock cache, Colour col) { 
    250254        try { 
    251             fontTex.drawTextCache (face, str, cache, x, y); 
     255            fontTex.drawCache (face, str, cache, x, y, col); 
    252256        } catch (Exception e) { 
    253257            logger.warn ("Exception while drawing text: "~e.msg); 
     
    255259    } 
    256260    /** ditto */ 
    257     void textBlock (int x, int y, char[] str) { 
     261    void textBlock (int x, int y, char[] str, Colour col) { 
    258262        try { 
    259263            // Using the cache method for one-time use is slightly less than optimal, but doing so 
     
    261265            // would be horrible). 
    262266            TextBlock cache; 
    263             fontTex.drawTextCache (face, str, cache, x, y); 
     267            fontTex.drawCache (face, str, cache, x, y, col); 
    264268        } catch (Exception e) { 
    265269            logger.warn ("Exception while drawing text: "~e.msg); 
     
    267271    } 
    268272     
     273    /** A variation of textBlock for transparency. 
     274     * 
     275     * Set the alpha by calling glColor*() first. See FontTexture.drawCacheA()'s documentation for 
     276     * details. */ 
     277    void textBlockA (int x, int y, char[] str, ref TextBlock cache, Colour col) { 
     278        try { 
     279            fontTex.drawCacheA (face, str, cache, x, y, col); 
     280        } catch (Exception e) { 
     281            logger.warn ("Exception while drawing text: "~e.msg); 
     282        } 
     283    } 
    269284     
    270285    /** The font-specified vertical distance between the baseline of consecutive lines. */