rev |
line source |
nuclear@5
|
1 /*
|
nuclear@5
|
2 libdrawtext - a simple library for fast text rendering in OpenGL
|
nuclear@12
|
3 Copyright (C) 2011-2012 John Tsiombikas <nuclear@member.fsf.org>
|
nuclear@5
|
4
|
nuclear@5
|
5 This program is free software: you can redistribute it and/or modify
|
nuclear@5
|
6 it under the terms of the GNU Lesser General Public License as published by
|
nuclear@5
|
7 the Free Software Foundation, either version 3 of the License, or
|
nuclear@5
|
8 (at your option) any later version.
|
nuclear@5
|
9
|
nuclear@5
|
10 This program is distributed in the hope that it will be useful,
|
nuclear@5
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
nuclear@5
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
nuclear@5
|
13 GNU Lesser General Public License for more details.
|
nuclear@5
|
14
|
nuclear@5
|
15 You should have received a copy of the GNU Lesser General Public License
|
nuclear@5
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
nuclear@5
|
17 */
|
nuclear@0
|
18 #ifndef NO_OPENGL
|
nuclear@15
|
19 #include <stdarg.h>
|
nuclear@0
|
20 #include <math.h>
|
nuclear@0
|
21 #include <ctype.h>
|
nuclear@0
|
22
|
nuclear@0
|
23 #include <stdlib.h>
|
nuclear@0
|
24
|
nuclear@16
|
25 #ifndef _MSC_VER
|
nuclear@16
|
26 #include <alloca.h>
|
nuclear@16
|
27 #else
|
nuclear@16
|
28 #include <malloc.h>
|
nuclear@16
|
29 #endif
|
nuclear@16
|
30
|
nuclear@9
|
31 #ifdef TARGET_IPHONE
|
nuclear@0
|
32 #include <OpenGLES/ES2/gl.h>
|
nuclear@0
|
33 #else
|
nuclear@0
|
34 #include <GL/glew.h>
|
nuclear@0
|
35 #endif
|
nuclear@0
|
36
|
nuclear@0
|
37 #include "drawtext.h"
|
nuclear@0
|
38 #include "drawtext_impl.h"
|
nuclear@0
|
39
|
nuclear@0
|
40 struct vertex {
|
nuclear@0
|
41 float x, y;
|
nuclear@0
|
42 float s, t;
|
nuclear@0
|
43 };
|
nuclear@0
|
44
|
nuclear@0
|
45 struct quad {
|
nuclear@0
|
46 struct vertex v[6];
|
nuclear@0
|
47 };
|
nuclear@0
|
48
|
nuclear@0
|
49 static void cleanup(void);
|
nuclear@0
|
50 static void add_glyph(struct glyph *g, float x, float y);
|
nuclear@0
|
51
|
nuclear@0
|
52 #define QBUF_SZ 512
|
nuclear@0
|
53 static struct quad *qbuf;
|
nuclear@0
|
54 static int num_quads;
|
nuclear@0
|
55 static int vattr = -1;
|
nuclear@0
|
56 static int tattr = -1;
|
nuclear@0
|
57 static unsigned int font_tex;
|
nuclear@3
|
58 static int buf_mode = DTX_NBF;
|
nuclear@0
|
59
|
nuclear@0
|
60
|
nuclear@6
|
61 int dtx_gl_init(void)
|
nuclear@0
|
62 {
|
nuclear@0
|
63 if(qbuf) {
|
nuclear@0
|
64 return 0; /* already initialized */
|
nuclear@0
|
65 }
|
nuclear@0
|
66
|
nuclear@0
|
67 glewInit();
|
nuclear@0
|
68
|
nuclear@0
|
69 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
|
nuclear@0
|
70 return -1;
|
nuclear@0
|
71 }
|
nuclear@0
|
72 num_quads = 0;
|
nuclear@0
|
73
|
nuclear@0
|
74 atexit(cleanup);
|
nuclear@0
|
75 return 0;
|
nuclear@0
|
76 }
|
nuclear@0
|
77
|
nuclear@0
|
78 static void cleanup(void)
|
nuclear@0
|
79 {
|
nuclear@0
|
80 free(qbuf);
|
nuclear@0
|
81 }
|
nuclear@0
|
82
|
nuclear@0
|
83
|
nuclear@0
|
84 void dtx_vertex_attribs(int vert_attr, int tex_attr)
|
nuclear@0
|
85 {
|
nuclear@0
|
86 vattr = vert_attr;
|
nuclear@0
|
87 tattr = tex_attr;
|
nuclear@0
|
88 }
|
nuclear@0
|
89
|
nuclear@0
|
90 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
|
nuclear@0
|
91 {
|
nuclear@0
|
92 if(!gmap->tex) {
|
nuclear@0
|
93 glGenTextures(1, &gmap->tex);
|
nuclear@0
|
94 glBindTexture(GL_TEXTURE_2D, gmap->tex);
|
nuclear@0
|
95 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
|
nuclear@0
|
96 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@0
|
97 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
|
nuclear@0
|
98 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
|
nuclear@0
|
99
|
nuclear@0
|
100 #ifdef GL_ES
|
nuclear@0
|
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
|
nuclear@0
|
102 glGenerateMipmap(GL_TEXTURE_2D);
|
nuclear@0
|
103 #else
|
nuclear@0
|
104 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
|
nuclear@0
|
105 #endif
|
nuclear@0
|
106 }
|
nuclear@0
|
107
|
nuclear@0
|
108 if(font_tex != gmap->tex) {
|
nuclear@0
|
109 dtx_flush();
|
nuclear@0
|
110 }
|
nuclear@0
|
111 font_tex = gmap->tex;
|
nuclear@0
|
112 }
|
nuclear@0
|
113
|
nuclear@0
|
114 void dtx_glyph(int code)
|
nuclear@0
|
115 {
|
nuclear@0
|
116 struct dtx_glyphmap *gmap;
|
nuclear@0
|
117
|
nuclear@6
|
118 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
|
nuclear@0
|
119 return;
|
nuclear@0
|
120 }
|
nuclear@0
|
121 set_glyphmap_texture(gmap);
|
nuclear@0
|
122
|
nuclear@0
|
123 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
|
nuclear@0
|
124 dtx_flush();
|
nuclear@0
|
125 }
|
nuclear@0
|
126
|
nuclear@15
|
127 static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
|
nuclear@15
|
128 {
|
nuclear@15
|
129 struct dtx_glyphmap *gmap;
|
nuclear@15
|
130 float px, py;
|
nuclear@15
|
131 int code = dtx_utf8_char_code(str);
|
nuclear@15
|
132 str = dtx_utf8_next_char((char*)str);
|
nuclear@15
|
133
|
nuclear@15
|
134 if(buf_mode == DTX_LBF && code == '\n') {
|
nuclear@15
|
135 *should_flush = 1;
|
nuclear@15
|
136 }
|
nuclear@15
|
137
|
nuclear@15
|
138 px = *pos_x;
|
nuclear@15
|
139 py = *pos_y;
|
nuclear@15
|
140
|
nuclear@15
|
141 if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
|
nuclear@15
|
142 int idx = code - gmap->cstart;
|
nuclear@15
|
143
|
nuclear@15
|
144 set_glyphmap_texture(gmap);
|
nuclear@15
|
145 add_glyph(gmap->glyphs + idx, px, py);
|
nuclear@15
|
146 }
|
nuclear@15
|
147 return str;
|
nuclear@15
|
148 }
|
nuclear@15
|
149
|
nuclear@0
|
150 void dtx_string(const char *str)
|
nuclear@0
|
151 {
|
nuclear@0
|
152 int should_flush = buf_mode == DTX_NBF;
|
nuclear@0
|
153 float pos_x = 0.0f;
|
nuclear@0
|
154 float pos_y = 0.0f;
|
nuclear@0
|
155
|
nuclear@6
|
156 if(!dtx_font) {
|
nuclear@0
|
157 return;
|
nuclear@0
|
158 }
|
nuclear@0
|
159
|
nuclear@0
|
160 while(*str) {
|
nuclear@15
|
161 str = put_char(str, &pos_x, &pos_y, &should_flush);
|
nuclear@0
|
162 }
|
nuclear@0
|
163
|
nuclear@0
|
164 if(should_flush) {
|
nuclear@0
|
165 dtx_flush();
|
nuclear@0
|
166 }
|
nuclear@0
|
167 }
|
nuclear@0
|
168
|
nuclear@15
|
169 void dtx_printf(const char *fmt, ...)
|
nuclear@15
|
170 {
|
nuclear@15
|
171 va_list ap;
|
nuclear@47
|
172
|
nuclear@47
|
173 va_start(ap, fmt);
|
nuclear@47
|
174 dtx_vprintf(fmt, ap);
|
nuclear@47
|
175 va_end(ap);
|
nuclear@47
|
176 }
|
nuclear@47
|
177
|
nuclear@47
|
178 void dtx_vprintf(const char *fmt, va_list ap)
|
nuclear@47
|
179 {
|
nuclear@15
|
180 int buf_size;
|
nuclear@15
|
181 char *buf, tmp;
|
nuclear@15
|
182
|
nuclear@15
|
183 if(!dtx_font) {
|
nuclear@15
|
184 return;
|
nuclear@15
|
185 }
|
nuclear@15
|
186
|
nuclear@15
|
187 buf_size = vsnprintf(&tmp, 0, fmt, ap);
|
nuclear@15
|
188
|
nuclear@15
|
189 if(buf_size == -1) {
|
nuclear@15
|
190 buf_size = 512;
|
nuclear@15
|
191 }
|
nuclear@15
|
192
|
nuclear@15
|
193 buf = alloca(buf_size + 1);
|
nuclear@15
|
194 va_start(ap, fmt);
|
nuclear@15
|
195 vsnprintf(buf, buf_size + 1, fmt, ap);
|
nuclear@15
|
196 va_end(ap);
|
nuclear@15
|
197
|
nuclear@15
|
198 dtx_string(buf);
|
nuclear@15
|
199 }
|
nuclear@15
|
200
|
nuclear@0
|
201 static void qvertex(struct vertex *v, float x, float y, float s, float t)
|
nuclear@0
|
202 {
|
nuclear@0
|
203 v->x = x;
|
nuclear@0
|
204 v->y = y;
|
nuclear@0
|
205 v->s = s;
|
nuclear@0
|
206 v->t = t;
|
nuclear@0
|
207 }
|
nuclear@0
|
208
|
nuclear@0
|
209 static void add_glyph(struct glyph *g, float x, float y)
|
nuclear@0
|
210 {
|
nuclear@0
|
211 struct quad *qptr = qbuf + num_quads;
|
nuclear@0
|
212
|
nuclear@0
|
213 x -= g->orig_x;
|
nuclear@0
|
214 y -= g->orig_y;
|
nuclear@0
|
215
|
nuclear@0
|
216 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
|
nuclear@0
|
217 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
|
nuclear@0
|
218 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
|
nuclear@0
|
219
|
nuclear@0
|
220 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
|
nuclear@0
|
221 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
|
nuclear@0
|
222 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
|
nuclear@0
|
223
|
nuclear@0
|
224 if(++num_quads >= QBUF_SZ) {
|
nuclear@0
|
225 dtx_flush();
|
nuclear@0
|
226 }
|
nuclear@0
|
227 }
|
nuclear@0
|
228
|
nuclear@0
|
229 void dtx_flush(void)
|
nuclear@0
|
230 {
|
nuclear@12
|
231 int vbo;
|
nuclear@12
|
232
|
nuclear@0
|
233 if(!num_quads) {
|
nuclear@0
|
234 return;
|
nuclear@0
|
235 }
|
nuclear@0
|
236
|
nuclear@12
|
237 if(glBindBuffer) {
|
nuclear@12
|
238 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
|
nuclear@12
|
239 glBindBuffer(GL_ARRAY_BUFFER, 0);
|
nuclear@12
|
240 }
|
nuclear@12
|
241
|
nuclear@0
|
242 #ifndef GL_ES
|
nuclear@0
|
243 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@0
|
244 glEnable(GL_TEXTURE_2D);
|
nuclear@0
|
245 #endif
|
nuclear@0
|
246 glBindTexture(GL_TEXTURE_2D, font_tex);
|
nuclear@0
|
247
|
nuclear@0
|
248 if(vattr != -1 && glEnableVertexAttribArray) {
|
nuclear@0
|
249 glEnableVertexAttribArray(vattr);
|
nuclear@0
|
250 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
|
nuclear@0
|
251 #ifndef GL_ES
|
nuclear@0
|
252 } else {
|
nuclear@0
|
253 glEnableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
254 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
|
nuclear@0
|
255 #endif
|
nuclear@0
|
256 }
|
nuclear@0
|
257 if(tattr != -1 && glEnableVertexAttribArray) {
|
nuclear@0
|
258 glEnableVertexAttribArray(tattr);
|
nuclear@0
|
259 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
|
nuclear@0
|
260 #ifndef GL_ES
|
nuclear@0
|
261 } else {
|
nuclear@0
|
262 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
263 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
|
nuclear@0
|
264 #endif
|
nuclear@0
|
265 }
|
nuclear@0
|
266
|
nuclear@0
|
267 glEnable(GL_BLEND);
|
nuclear@0
|
268 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
nuclear@0
|
269
|
nuclear@0
|
270 glDepthMask(0);
|
nuclear@0
|
271
|
nuclear@0
|
272 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
|
nuclear@0
|
273
|
nuclear@0
|
274 glDepthMask(1);
|
nuclear@0
|
275
|
nuclear@0
|
276 if(vattr != -1 && glDisableVertexAttribArray) {
|
nuclear@0
|
277 glDisableVertexAttribArray(vattr);
|
nuclear@0
|
278 #ifndef GL_ES
|
nuclear@0
|
279 } else {
|
nuclear@0
|
280 glDisableClientState(GL_VERTEX_ARRAY);
|
nuclear@0
|
281 #endif
|
nuclear@0
|
282 }
|
nuclear@0
|
283 if(tattr != -1 && glDisableVertexAttribArray) {
|
nuclear@0
|
284 glDisableVertexAttribArray(tattr);
|
nuclear@0
|
285 #ifndef GL_ES
|
nuclear@0
|
286 } else {
|
nuclear@0
|
287 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
nuclear@0
|
288 #endif
|
nuclear@0
|
289 }
|
nuclear@0
|
290
|
nuclear@0
|
291 #ifndef GL_ES
|
nuclear@0
|
292 glPopAttrib();
|
nuclear@0
|
293 #else
|
nuclear@0
|
294 glDisable(GL_BLEND);
|
nuclear@0
|
295 #endif
|
nuclear@0
|
296
|
nuclear@12
|
297 if(glBindBuffer && vbo) {
|
nuclear@12
|
298 glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
nuclear@12
|
299 }
|
nuclear@12
|
300
|
nuclear@0
|
301 num_quads = 0;
|
nuclear@0
|
302 }
|
nuclear@0
|
303
|
nuclear@0
|
304 #endif /* !def NO_OPENGL */
|