libdrawtext

view src/drawgl.c @ 47:00f98842608f

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