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