1 /* *****************************************************************************
2 * Original Version Copyright (c) 2007-2014 Alexis Naveros.
4 * Ecere Corporation has unlimited/unrestricted rights.
5 * *****************************************************************************/
19 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
21 //# include "gl_core_3_3.h"
22 # include "gl_compat_4_4.h" // FIXME: no glPushAttrib() in core profile
24 # include "gl_compat_4_4.h"
28 #if defined(__EMSCRIPTEN__)
30 #include <GLES2/gl2.h>
36 import "fmFontManager"
37 import "textureManager"
40 #define DM_ENABLE_EXT_COLOR (1)
42 public class FontRenderer : FontManagerRenderer
46 int textureWidth, textureHeight;
53 ColorAlpha stateColor;
54 ColorAlpha stateExtColor;
55 ColorAlpha stateCursorColor;
59 imageList = new DMImage[imageAlloc];
62 stateCursorColor = white;
63 stateLayer = DM_LAYER_ABOVE;
67 property DrawManager drawManager { set { dm = value; } }
69 bool init(int channelCount)
71 this.channelcount = channelCount;
81 bool createTexture( int width, int height )
85 format = { width = width, height = height, type = grayScale, bytesPerPixel = channelcount, bytesPerLine = width };
90 texture = { 0 << DM_TEXTURE_ORDER_SHIFT };
91 texture.build(image, 0, 0.0, 0 );
94 textureHeight = height;
98 int resizeTexture( int width, int height )
102 // Reuse create to resize too.
106 imageList = new DMImage[imageAlloc];
108 retval = createTexture( width, height );
112 void updateTexture( int *rect, const byte* data )
116 #if defined(SHADERS) && !defined(__EMSCRIPTEN__)
117 int glformat = GL_RED;
119 int glformat = GL_ALPHA;
121 int w = rect[2] - rect[0];
122 int h = rect[3] - rect[1];
124 if( channelcount == 1 );
125 #if !defined(__EMSCRIPTEN__)
126 else if( channelcount == 2 )
129 else if( channelcount == 3 )
131 else if( channelcount == 4 )
134 // FIXME: no glPushAttrib() in core profile
135 #if !defined(__EMSCRIPTEN__)
136 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
137 glPushAttrib( GL_TEXTURE_BIT );
139 glBindTexture( GL_TEXTURE_2D, texture.glTex );
140 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
141 #if !defined(__EMSCRIPTEN__)
142 glPixelStorei( GL_UNPACK_ROW_LENGTH, textureWidth );
143 glPixelStorei( GL_UNPACK_SKIP_PIXELS, rect[0] );
144 glPixelStorei( GL_UNPACK_SKIP_ROWS, rect[1] );
146 glTexSubImage2D( GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, glformat, GL_UNSIGNED_BYTE, data );
147 #if !defined(__EMSCRIPTEN__)
154 image.format.width = textureWidth;
155 image.format.height = textureHeight;
156 image.format.type = channelcount == 4 ? IMG_FORMAT_TYPE_RGBA32 : IMG_FORMAT_TYPE_GRAYSCALE;
157 image.format.bytesPerPixel = channelcount;
158 image.format.bytesPerLine = image.format.width * image.format.bytesPerPixel;
160 imgWritePngFile( "zzz2.png", &image, 1.0 );
170 int registerImage( int offsetx, int offsety, int width, int height )
172 int imageindex = imagecount;
175 if( imagecount >= imageAlloc )
178 imageList = renew imageList DMImage[imageAlloc];
182 image = &imageList[ imageindex ];
184 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR, stateLayer );
186 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY, stateLayer );
188 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND, stateLayer );
190 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_NORMAL, stateLayer );
196 void drawImage( int targetx, int targety, int imageindex )
198 DMImage *image = &imageList[ imageindex ];
199 #if DM_ENABLE_EXT_COLOR
200 dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateColor, stateExtColor );
202 dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateColor );
206 void drawImageCursor( int targetx, int targety, int imageindex )
208 DMImage *image = &imageList[ imageindex ];
209 #if DM_ENABLE_EXT_COLOR
210 dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateCursorColor, stateExtColor );
212 dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateCursorColor );
216 void drawImageAlt( byte *texdata, int targetx, int targety, int offsetx, int offsety, int width, int height )
221 void drawImageFloat( float targetx, float targety, float angsin, float angcos, int imageindex )
223 DMImage *image = &imageList[ imageindex ];
225 /* 0.2588190451, 0.965925826289 */
227 #if DM_ENABLE_EXT_COLOR
228 dm.drawImageFloatExtColor( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor, stateExtColor );
230 dm.drawImageFloat( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor );
239 void setColor( ColorAlpha color )
241 stateColor = { color.a, { color.color.b, color.color.g, color.color.r } };
244 void setExtColor( ColorAlpha color )
246 stateExtColor = { color.a, { color.color.b, color.color.g, color.color.r } };
249 void setCursorColor( ColorAlpha color )
251 stateCursorColor = { color.a, { color.color.b, color.color.g, color.color.r } };
254 void setLayer( uint32 layerIndex )
256 stateLayer = layerIndex;