1 /* *****************************************************************************
2 * Original Version Copyright (c) 2007-2014 Alexis Naveros.
4 * Ecere Corporation has unlimited/unrestricted rights.
5 * *****************************************************************************/
17 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
19 //# include "gl_core_3_3.h"
20 # include "gl_compat_4_4.h" // FIXME: no glPushAttrib() in core profile
22 # include "gl_compat_4_4.h"
29 import "fmFontManager"
30 import "textureManager"
33 #define DM_ENABLE_EXT_COLOR (1)
35 public class FontRenderer : FontManagerRenderer
39 int textureWidth, textureHeight;
46 ColorAlpha stateColor;
47 ColorAlpha stateExtColor;
48 ColorAlpha stateCursorColor;
52 imageList = new DMImage[imageAlloc];
55 stateCursorColor = white;
56 stateLayer = DM_LAYER_ABOVE;
60 property DrawManager drawManager { set { dm = value; } }
62 bool init(int channelCount)
64 this.channelcount = channelCount;
74 bool createTexture( int width, int height )
78 format = { width = width, height = height, type = grayScale, bytesPerPixel = channelcount, bytesPerLine = width };
83 texture = { 0 << DM_TEXTURE_ORDER_SHIFT };
84 texture.build(image, 0, 0.0, 0 );
87 textureHeight = height;
91 int resizeTexture( int width, int height )
95 // Reuse create to resize too.
99 imageList = new DMImage[imageAlloc];
101 retval = createTexture( width, height );
105 void updateTexture( int *rect, const byte* data )
109 int glformat = GL_RED;
110 int w = rect[2] - rect[0];
111 int h = rect[3] - rect[1];
113 if( channelcount == 1 )
115 else if( channelcount == 2 )
117 else if( channelcount == 3 )
119 else if( channelcount == 4 )
122 // FIXME: no glPushAttrib() in core profile
124 glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
125 glPushAttrib( GL_TEXTURE_BIT );
127 glBindTexture( GL_TEXTURE_2D, texture.glTex );
128 glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
129 glPixelStorei( GL_UNPACK_ROW_LENGTH, textureWidth );
130 glPixelStorei( GL_UNPACK_SKIP_PIXELS, rect[0] );
131 glPixelStorei( GL_UNPACK_SKIP_ROWS, rect[1] );
132 glTexSubImage2D( GL_TEXTURE_2D, 0, rect[0], rect[1], w, h, glformat, GL_UNSIGNED_BYTE, data );
140 image.format.width = textureWidth;
141 image.format.height = textureHeight;
142 image.format.type = channelcount == 4 ? IMG_FORMAT_TYPE_RGBA32 : IMG_FORMAT_TYPE_GRAYSCALE;
143 image.format.bytesPerPixel = channelcount;
144 image.format.bytesPerLine = image.format.width * image.format.bytesPerPixel;
146 imgWritePngFile( "zzz2.png", &image, 1.0 );
156 int registerImage( int offsetx, int offsety, int width, int height )
158 int imageindex = imagecount;
161 if( imagecount >= imageAlloc )
164 imageList = renew imageList DMImage[imageAlloc];
168 image = &imageList[ imageindex ];
170 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR, stateLayer );
172 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY, stateLayer );
174 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND, stateLayer );
176 image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_NORMAL, stateLayer );
182 void drawImage( int targetx, int targety, int imageindex )
184 DMImage *image = &imageList[ imageindex ];
185 #if DM_ENABLE_EXT_COLOR
186 dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateColor, stateExtColor );
188 dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateColor );
192 void drawImageCursor( int targetx, int targety, int imageindex )
194 DMImage *image = &imageList[ imageindex ];
195 #if DM_ENABLE_EXT_COLOR
196 dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateCursorColor, stateExtColor );
198 dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateCursorColor );
202 void drawImageAlt( byte *texdata, int targetx, int targety, int offsetx, int offsety, int width, int height )
207 void drawImageFloat( float targetx, float targety, float angsin, float angcos, int imageindex )
209 DMImage *image = &imageList[ imageindex ];
211 /* 0.2588190451, 0.965925826289 */
213 #if DM_ENABLE_EXT_COLOR
214 dm.drawImageFloatExtColor( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor, stateExtColor );
216 dm.drawImageFloat( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor );
225 void setColor( ColorAlpha color )
227 stateColor = { color.a, { color.color.b, color.color.g, color.color.r } };
230 void setExtColor( ColorAlpha color )
232 stateExtColor = { color.a, { color.color.b, color.color.g, color.color.r } };
235 void setCursorColor( ColorAlpha color )
237 stateCursorColor = { color.a, { color.color.b, color.color.g, color.color.r } };
240 void setLayer( uint32 layerIndex )
242 stateLayer = layerIndex;