ecere/gfx/NewFonts: Outline color support
[sdk] / ecere / src / gfx / newFonts / fontRenderer.ec
index 00f361b..3e7daf4 100644 (file)
@@ -10,12 +10,13 @@ import "Color"
 #if defined(_GLES)
    #define ES1_1
 #else
-   //#define SHADERS
+   #define SHADERS
 #endif
 
 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
 #  if defined(SHADERS)
-#     include "gl_core_3_3.h"
+//#     include "gl_core_3_3.h"
+#     include "gl_compat_4_4.h"     // FIXME: no glPushAttrib() in core profile
 #  else
 #     include "gl_compat_4_4.h"
 #  endif
@@ -28,17 +29,21 @@ import "fontManager"
 import "textureManager"
 import "drawManager"
 
+#define DM_ENABLE_EXT_COLOR (1)
+
 public class FontRenderer : FontManagerRenderer
 {
    DrawManager dm;
    Texture texture;
    int textureWidth, textureHeight;
+   int channelcount;
 
    int imagecount;
    int imageAlloc;
    DMImage *imageList;
 
    ColorAlpha stateColor;
+   ColorAlpha stateExtColor;
    ColorAlpha stateCursorColor;
    uint32 stateLayer;
 
@@ -53,6 +58,12 @@ public:
 
    property DrawManager drawManager { set { dm = value; } }
 
+   bool init(int channelCount)
+   {
+      this.channelcount = channelCount;
+      return true;
+   }
+
    ~FontRenderer()
    {
       delete texture;
@@ -63,7 +74,7 @@ public:
    {
       IMGImage image
       {
-         format = { width = width, height = height, type = grayScale, bytesPerPixel = 1, bytesPerLine = width };
+         format = { width = width, height = height, type = grayScale, bytesPerPixel = channelcount, bytesPerLine = width };
       };
 
       delete texture;
@@ -94,26 +105,41 @@ public:
    {
      if(texture)
      {
+        int glformat = GL_RED;
         int w = rect[2] - rect[0];
         int h = rect[3] - rect[1];
 
+        if( channelcount == 1 )
+          glformat = GL_RED;
+        else if( channelcount == 2 )
+          glformat = GL_RG;
+        else if( channelcount == 3 )
+          glformat = GL_RGB;
+        else if( channelcount == 4 )
+          glformat = GL_RGBA;
+
+        // FIXME: no glPushAttrib() in core profile
+//#ifndef SHADERS
         glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
         glPushAttrib( GL_TEXTURE_BIT );
+//#endif
         glBindTexture( GL_TEXTURE_2D, texture.glTex );
         glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
         glPixelStorei( GL_UNPACK_ROW_LENGTH, textureWidth );
         glPixelStorei( GL_UNPACK_SKIP_PIXELS, rect[0] );
         glPixelStorei( GL_UNPACK_SKIP_ROWS, rect[1] );
-        glTexSubImage2D( GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, GL_RED, GL_UNSIGNED_BYTE, data );
+        glTexSubImage2D( GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, glformat, GL_UNSIGNED_BYTE, data );
+//#ifndef SHADERS
         glPopAttrib();
         glPopClientAttrib();
+//#endif
 
       #if 0
         IMGImage image;
         image.format.width = textureWidth;
         image.format.height = textureHeight;
-        image.format.type = IMG_FORMAT_TYPE_GRAYSCALE;
-        image.format.bytesPerPixel = 1;
+        image.format.type = channelcount == 4 ? IMG_FORMAT_TYPE_RGBA32 : IMG_FORMAT_TYPE_GRAYSCALE;
+        image.format.bytesPerPixel = channelcount;
         image.format.bytesPerLine = image.format.width * image.format.bytesPerPixel;
         image.data = data;
         imgWritePngFile( "zzz2.png", &image, 1.0 );
@@ -140,6 +166,10 @@ public:
 
       image = &imageList[ imageindex ];
    #if 1
+      image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR, stateLayer );
+   #elif 1
+      image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY, stateLayer );
+   #elif 1
       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND, stateLayer );
    #else
       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_NORMAL, stateLayer );
@@ -151,13 +181,21 @@ public:
    void drawImage( int targetx, int targety, int imageindex )
    {
       DMImage *image = &imageList[ imageindex ];
+   #if DM_ENABLE_EXT_COLOR
+      dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateColor, stateExtColor );
+   #else
       dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateColor );
+   #endif
    }
 
    void drawImageCursor( int targetx, int targety, int imageindex )
    {
       DMImage *image = &imageList[ imageindex ];
+   #if DM_ENABLE_EXT_COLOR
+      dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateCursorColor, stateExtColor );
+   #else
       dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateCursorColor );
+   #endif
    }
 
    void drawImageAlt( byte *texdata, int targetx, int targety, int offsetx, int offsety, int width, int height )
@@ -171,7 +209,11 @@ public:
 
       /* 0.2588190451, 0.965925826289 */
 
+   #if DM_ENABLE_EXT_COLOR
+      dm.drawImageFloatExtColor( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor, stateExtColor );
+   #else
       dm.drawImageFloat( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor );
+   #endif
    }
 
    void resetImages( )
@@ -184,6 +226,11 @@ public:
       stateColor = { color.a, { color.color.b, color.color.g, color.color.r } };
    }
 
+   void setExtColor( ColorAlpha color )
+   {
+      stateExtColor = { color.a, { color.color.b, color.color.g, color.color.r } };
+   }
+
    void setCursorColor( ColorAlpha color )
    {
       stateCursorColor = { color.a, { color.color.b, color.color.g, color.color.r } };