ecere/gfx/newFonts: Renaming 'fontManager' to solve conflict with JDK
[sdk] / ecere / src / gfx / newFonts / fontRenderer.ec
1 /* *****************************************************************************
2  * Original Version Copyright (c) 2007-2014 Alexis Naveros.
3  *
4  * Ecere Corporation has unlimited/unrestricted rights.
5  * *****************************************************************************/
6 import "Color"
7
8 #include <math.h>
9 #include <stdlib.h>
10
11 #if defined(_GLES)
12    #define ES1_1
13 #else
14    #define SHADERS
15 #endif
16
17 #if !defined(__ANDROID__) && !defined(__EMSCRIPTEN__) && !defined(__ODROID__)
18 #  if defined(SHADERS)
19 //#     include "gl_core_3_3.h"
20 #     include "gl_compat_4_4.h"     // FIXME: no glPushAttrib() in core profile
21 #  else
22 #     include "gl_compat_4_4.h"
23 #  endif
24 #endif
25
26 #include "cc.h"
27 #include "mm.h"
28
29 import "fmFontManager"
30 import "textureManager"
31 import "drawManager"
32
33 #define DM_ENABLE_EXT_COLOR (1)
34
35 public class FontRenderer : FontManagerRenderer
36 {
37    DrawManager dm;
38    Texture texture;
39    int textureWidth, textureHeight;
40    int channelcount;
41
42    int imagecount;
43    int imageAlloc;
44    DMImage *imageList;
45
46    ColorAlpha stateColor;
47    ColorAlpha stateExtColor;
48    ColorAlpha stateCursorColor;
49    uint32 stateLayer;
50
51    imageAlloc = 512;
52    imageList = new DMImage[imageAlloc];
53
54    stateColor = white;
55    stateCursorColor = white;
56    stateLayer = DM_LAYER_ABOVE;
57
58 public:
59
60    property DrawManager drawManager { set { dm = value; } }
61
62    bool init(int channelCount)
63    {
64       this.channelcount = channelCount;
65       return true;
66    }
67
68    ~FontRenderer()
69    {
70       delete texture;
71       delete imageList;
72    }
73
74    bool createTexture( int width, int height )
75    {
76       IMGImage image
77       {
78          format = { width = width, height = height, type = grayScale, bytesPerPixel = channelcount, bytesPerLine = width };
79       };
80
81       delete texture;
82
83       texture = { 0 << DM_TEXTURE_ORDER_SHIFT };
84       texture.build(image, 0, 0.0, 0 );
85
86       textureWidth = width;
87       textureHeight = height;
88       return true;
89    }
90
91    int resizeTexture( int width, int height )
92    {
93      int retval;
94
95      // Reuse create to resize too.
96      delete imageList;
97      imagecount = 0;
98      imageAlloc = 512;
99      imageList = new DMImage[imageAlloc];
100
101      retval = createTexture( width, height );
102      return retval;
103    }
104
105    void updateTexture( int *rect, const byte* data )
106    {
107      if(texture)
108      {
109         int glformat = GL_RED;
110         int w = rect[2] - rect[0];
111         int h = rect[3] - rect[1];
112
113         if( channelcount == 1 )
114           glformat = GL_RED;
115         else if( channelcount == 2 )
116           glformat = GL_RG;
117         else if( channelcount == 3 )
118           glformat = GL_RGB;
119         else if( channelcount == 4 )
120           glformat = GL_RGBA;
121
122         // FIXME: no glPushAttrib() in core profile
123 //#ifndef SHADERS
124         glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT );
125         glPushAttrib( GL_TEXTURE_BIT );
126 //#endif
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 );
133 //#ifndef SHADERS
134         glPopAttrib();
135         glPopClientAttrib();
136 //#endif
137
138       #if 0
139         IMGImage image;
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;
145         image.data = data;
146         imgWritePngFile( "zzz2.png", &image, 1.0 );
147       #endif
148      }
149    }
150
151    void flush( )
152    {
153       dm.flushImages( );
154    }
155
156    int registerImage( int offsetx, int offsety, int width, int height )
157    {
158       int imageindex = imagecount;
159       DMImage *image;
160
161       if( imagecount >= imageAlloc )
162       {
163          imageAlloc <<= 1;
164          imageList = renew imageList DMImage[imageAlloc];
165       }
166       imagecount++;
167
168       image = &imageList[ imageindex ];
169    #if 1
170       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY_EXTCOLOR, stateLayer );
171    #elif 1
172       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND_INTENSITY, stateLayer );
173    #elif 1
174       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_ALPHABLEND, stateLayer );
175    #else
176       image->defineImage( texture, offsetx, offsety, width, height, 1, DM_PROGRAM_NORMAL, stateLayer );
177    #endif
178
179       return imageindex;
180    }
181
182    void drawImage( int targetx, int targety, int imageindex )
183    {
184       DMImage *image = &imageList[ imageindex ];
185    #if DM_ENABLE_EXT_COLOR
186       dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateColor, stateExtColor );
187    #else
188       dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateColor );
189    #endif
190    }
191
192    void drawImageCursor( int targetx, int targety, int imageindex )
193    {
194       DMImage *image = &imageList[ imageindex ];
195    #if DM_ENABLE_EXT_COLOR
196       dm.drawImageExtColor( image, targetx, targety, image->sizex, image->sizey, stateCursorColor, stateExtColor );
197    #else
198       dm.drawImage( image, targetx, targety, image->sizex, image->sizey, stateCursorColor );
199    #endif
200    }
201
202    void drawImageAlt( byte *texdata, int targetx, int targety, int offsetx, int offsety, int width, int height )
203    {
204
205    }
206
207    void drawImageFloat( float targetx, float targety, float angsin, float angcos, int imageindex )
208    {
209       DMImage *image = &imageList[ imageindex ];
210
211       /* 0.2588190451, 0.965925826289 */
212
213    #if DM_ENABLE_EXT_COLOR
214       dm.drawImageFloatExtColor( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor, stateExtColor );
215    #else
216       dm.drawImageFloat( image, targetx, targety, (float)image->sizex, (float)image->sizey, angsin, angcos, stateColor );
217    #endif
218    }
219
220    void resetImages( )
221    {
222       imagecount = 0;
223    }
224
225    void setColor( ColorAlpha color )
226    {
227       stateColor = { color.a, { color.color.b, color.color.g, color.color.r } };
228    }
229
230    void setExtColor( ColorAlpha color )
231    {
232       stateExtColor = { color.a, { color.color.b, color.color.g, color.color.r } };
233    }
234
235    void setCursorColor( ColorAlpha color )
236    {
237       stateCursorColor = { color.a, { color.color.b, color.color.g, color.color.r } };
238    }
239
240    void setLayer( uint32 layerIndex )
241    {
242       stateLayer = layerIndex;
243    }
244 }