rev |
line source |
nuclear@0
|
1 #ifndef NO_OPENGL
|
nuclear@0
|
2 #include <math.h>
|
nuclear@0
|
3 #include <ctype.h>
|
nuclear@0
|
4
|
nuclear@0
|
5 #include <stdlib.h>
|
nuclear@0
|
6
|
nuclear@0
|
7 #if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0)
|
nuclear@0
|
8 #include <OpenGLES/ES2/gl.h>
|
nuclear@0
|
9 #else
|
nuclear@0
|
10 #include <GL/glew.h>
|
nuclear@0
|
11 #endif
|
nuclear@0
|
12
|
nuclear@0
|
13 #include "drawtext.h"
|
nuclear@0
|
14 #include "drawtext_impl.h"
|
nuclear@0
|
15
|
nuclear@0
|
16 struct vertex {
|
nuclear@0
|
17 float x, y;
|
nuclear@0
|
18 float s, t;
|
nuclear@0
|
19 };
|
nuclear@0
|
20
|
nuclear@0
|
21 struct quad {
|
nuclear@0
|
22 struct vertex v[6];
|
nuclear@0
|
23 };
|
nuclear@0
|
24
|
nuclear@0
|
25 static int init(void);
|
nuclear@0
|
26 static void cleanup(void);
|
nuclear@0
|
27 static void add_glyph(struct glyph *g, float x, float y);
|
nuclear@0
|
28
|
nuclear@0
|
29 #define QBUF_SZ 512
|
nuclear@0
|
30 static struct quad *qbuf;
|
nuclear@0
|
31 static int num_quads;
|
nuclear@0
|
32 static int vattr = -1;
|
nuclear@0
|
33 static int tattr = -1;
|
nuclear@0
|
34 static unsigned int font_tex;
|
nuclear@3
|
35 static int buf_mode = DTX_NBF;
|
nuclear@0
|
36
|
nuclear@0
|
37 static struct dtx_font *font;
|
nuclear@0
|
38 static int font_sz;
|
nuclear@0
|
39
|
nuclear@0
|
40
|
nuclear@0
|
41 static int init(void)
|
nuclear@0
|
42 {
|
nuclear@0
|
43 if(qbuf) {
|
nuclear@0
|
44 return 0; /* already initialized */
|
nuclear@0
|
45 }
|
nuclear@0
|
46
|
nuclear@0
|
47 glewInit();
|
nuclear@0
|
48
|
nuclear@0
|
49 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
|
nuclear@0
|
50 return -1;
|
nuclear@0
|
51 }
|
nuclear@0
|
52 num_quads = 0;
|
nuclear@0
|
53
|
nuclear@0
|
54 atexit(cleanup);
|
nuclear@0
|
55 return 0;
|
nuclear@0
|
56 }
|
nuclear@0
|
57
|
nuclear@0
|
58 static void cleanup(void)
|
nuclear@0
|
59 {
|
nuclear@0
|
60 free(qbuf);
|
nuclear@0
|
61 }
|
nuclear@0
|
62
|
nuclear@0
|
63
|
nuclear@0
|
64 void dtx_use_font(struct dtx_font *fnt, int sz)
|
nuclear@0
|
65 {
|
nuclear@0
|
66 init();
|
nuclear@0
|
67
|
nuclear@0
|
68 font = fnt;
|
nuclear@0
|
69 font_sz = sz;
|
nuclear@0
|
70 }
|
nuclear@0
|
71
|
nuclear@0
|
72 void dtx_vertex_attribs(int vert_attr, int tex_attr)
|
nuclear@0
|
73 {
|
nuclear@0
|
74 vattr = vert_attr;
|
nuclear@0
|
75 tattr = tex_attr;
|
nuclear@0
|
76 }
|
nuclear@0
|
77
|
nuclear@0
|
78 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
|
nuclear@0
|
79 {
|
nuclear@0
|
80 if(!gmap->tex) {
|
nuclear@0
|
81 glGenTextures(1, &gmap->tex);
|
nuclear@0
|
82 glBindTexture(GL_TEXTURE_2D, gmap->tex);
|
nuclear@0
|
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
nuclear@0
|
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@0
|
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
nuclear@0
|
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
nuclear@0
|
87
|
nuclear@0
|
88 #ifdef GL_ES
|
nuclear@0
|
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
|
nuclear@0
|
90 glGenerateMipmap(GL_TEXTURE_2D);
|
nuclear@0
|
91 #else
|
nuclear@0
|
92 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
|
nuclear@0
|
93 #endif
|
nuclear@0
|
94 }
|
nuclear@0
|
95
|
nuclear@0
|
96 if(font_tex != gmap->tex) {
|
nuclear@0
|
97 dtx_flush();
|
nuclear@0
|
98 }
|
nuclear@0
|
99 font_tex = gmap->tex;
|
nuclear@0
|
100 }
|
nuclear@0
|
101
|
nuclear@0
|
102 void dtx_glyph(int code)
|
nuclear@0
|
103 {
|
nuclear@0
|
104 struct dtx_glyphmap *gmap;
|
nuclear@0
|
105
|
nuclear@0
|
106 if(!font || !(gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
|
nuclear@0
|
107 return;
|
nuclear@0
|
108 }
|
nuclear@0
|
109 set_glyphmap_texture(gmap);
|
nuclear@0
|
110
|
nuclear@0
|
111 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
|
nuclear@0
|
112 dtx_flush();
|
nuclear@0
|
113 }
|
nuclear@0
|
114
|
nuclear@0
|
115 void dtx_string(const char *str)
|
nuclear@0
|
116 {
|
nuclear@0
|
117 struct dtx_glyphmap *gmap;
|
nuclear@0
|
118 int should_flush = buf_mode == DTX_NBF;
|
nuclear@0
|
119 float pos_x = 0.0f;
|
nuclear@0
|
120 float pos_y = 0.0f;
|
nuclear@0
|
121
|
nuclear@0
|
122 if(!font) {
|
nuclear@0
|
123 return;
|
nuclear@0
|
124 }
|
nuclear@0
|
125
|
nuclear@0
|
126 while(*str) {
|
nuclear@0
|
127 int code = dtx_utf8_char_code(str);
|
nuclear@0
|
128 str = dtx_utf8_next_char((char*)str);
|
nuclear@0
|
129
|
nuclear@0
|
130 switch(code) {
|
nuclear@0
|
131 case '\n':
|
nuclear@0
|
132 if(buf_mode == DTX_LBF) {
|
nuclear@0
|
133 should_flush = 1;
|
nuclear@0
|
134 }
|
nuclear@0
|
135 pos_x = 0.0;
|
nuclear@0
|
136 pos_y -= gmap->line_advance;
|
nuclear@0
|
137 break;
|
nuclear@0
|
138
|
nuclear@0
|
139 case '\t':
|
nuclear@0
|
140 pos_x = fmod(pos_x, 4.0) + 4.0;
|
nuclear@0
|
141 break;
|
nuclear@0
|
142
|
nuclear@0
|
143 case '\r':
|
nuclear@0
|
144 pos_x = 0.0;
|
nuclear@0
|
145 break;
|
nuclear@0
|
146
|
nuclear@0
|
147 default:
|
nuclear@0
|
148 if((gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
|
nuclear@0
|
149 int idx = code - gmap->cstart;
|
nuclear@0
|
150
|
nuclear@0
|
151 set_glyphmap_texture(gmap);
|
nuclear@0
|
152 add_glyph(gmap->glyphs + idx, pos_x, pos_y);
|
nuclear@0
|
153 pos_x += gmap->glyphs[idx].advance;
|
nuclear@0
|
154 }
|
nuclear@0
|
155 }
|
nuclear@0
|
156 }
|
nuclear@0
|
157
|
nuclear@0
|
158 if(should_flush) {
|
nuclear@0
|
159 dtx_flush();
|
nuclear@0
|
160 }
|
nuclear@0
|
161 }
|
nuclear@0
|
162
|
nuclear@0
|
163 static void qvertex(struct vertex *v, float x, float y, float s, float t)
|
nuclear@0
|
164 {
|
nuclear@0
|
165 v->x = x;
|
nuclear@0
|
166 v->y = y;
|
nuclear@0
|
167 v->s = s;
|
nuclear@0
|
168 v->t = t;
|
nuclear@0
|
169 }
|
nuclear@0
|
170
|
nuclear@0
|
171 static void add_glyph(struct glyph *g, float x, float y)
|
nuclear@0
|
172 {
|
nuclear@0
|
173 struct quad *qptr = qbuf + num_quads;
|
nuclear@0
|
174
|
nuclear@0
|
175 x -= g->orig_x;
|
nuclear@0
|
176 y -= g->orig_y;
|
nuclear@0
|
177
|
nuclear@0
|
178 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
|
nuclear@0
|
179 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
|
nuclear@0
|
180 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
|
nuclear@0
|
181
|
nuclear@0
|
182 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
|
nuclear@0
|
183 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
|
nuclear@0
|
184 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
|
nuclear@0
|
185
|
nuclear@0
|
186 if(++num_quads >= QBUF_SZ) {
|
nuclear@0
|
187 dtx_flush();
|
nuclear@0
|
188 }
|
nuclear@0
|
189 }
|
nuclear@0
|
190
|
nuclear@0
|
191 void dtx_flush(void)
|
nuclear@0
|
192 {
|
nuclear@0
|
193 if(!num_quads) {
|
nuclear@0
|
194 return;
|
nuclear@0
|
195 }
|
nuclear@0
|
196
|
nuclear@0
|
197 #ifndef GL_ES
|
nuclear@0
|
198 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@0
|
199 glEnable(GL_TEXTURE_2D);
|
nuclear@0
|
200 #endif
|
nuclear@0
|
201 glBindTexture(GL_TEXTURE_2D, font_tex);
|
nuclear@0
|
202
|
nuclear@0
|
203 if(vattr != -1 && glEnableVertexAttribArray) {
|
nuclear@0
|
204 glEnableVertexAttribArray(vattr);
|
nuclear@0
|
205 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
|
nuclear@0
|
206 #ifndef GL_ES
|
nuclear@0
|
207 } else {
|
nuclear@0
|
208 glEnableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
209 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
|
nuclear@0
|
210 #endif
|
nuclear@0
|
211 }
|
nuclear@0
|
212 if(tattr != -1 && glEnableVertexAttribArray) {
|
nuclear@0
|
213 glEnableVertexAttribArray(tattr);
|
nuclear@0
|
214 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
|
nuclear@0
|
215 #ifndef GL_ES
|
nuclear@0
|
216 } else {
|
nuclear@0
|
217 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
218 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
|
nuclear@0
|
219 #endif
|
nuclear@0
|
220 }
|
nuclear@0
|
221
|
nuclear@0
|
222 glEnable(GL_BLEND);
|
nuclear@0
|
223 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
nuclear@0
|
224
|
nuclear@0
|
225 glDepthMask(0);
|
nuclear@0
|
226
|
nuclear@0
|
227 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
|
nuclear@0
|
228
|
nuclear@0
|
229 glDepthMask(1);
|
nuclear@0
|
230
|
nuclear@0
|
231 if(vattr != -1 && glDisableVertexAttribArray) {
|
nuclear@0
|
232 glDisableVertexAttribArray(vattr);
|
nuclear@0
|
233 #ifndef GL_ES
|
nuclear@0
|
234 } else {
|
nuclear@0
|
235 glDisableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
236 #endif
|
nuclear@0
|
237 }
|
nuclear@0
|
238 if(tattr != -1 && glDisableVertexAttribArray) {
|
nuclear@0
|
239 glDisableVertexAttribArray(tattr);
|
nuclear@0
|
240 #ifndef GL_ES
|
nuclear@0
|
241 } else {
|
nuclear@0
|
242 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
243 #endif
|
nuclear@0
|
244 }
|
nuclear@0
|
245
|
nuclear@0
|
246 #ifndef GL_ES
|
nuclear@0
|
247 glPopAttrib();
|
nuclear@0
|
248 #else
|
nuclear@0
|
249 glDisable(GL_BLEND);
|
nuclear@0
|
250 #endif
|
nuclear@0
|
251
|
nuclear@0
|
252 num_quads = 0;
|
nuclear@0
|
253 }
|
nuclear@0
|
254
|
nuclear@0
|
255 #endif /* !def NO_OPENGL */
|