libdrawtext

view src/drawgl.c @ 15:462bcb69de15

added dtx_printf
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 28 Feb 2013 18:51:33 +0200
parents 55c034e70809
children baec6ad7b2dd
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 #ifdef TARGET_IPHONE
26 #include <OpenGLES/ES2/gl.h>
27 #else
28 #include <GL/glew.h>
29 #endif
31 #include "drawtext.h"
32 #include "drawtext_impl.h"
34 struct vertex {
35 float x, y;
36 float s, t;
37 };
39 struct quad {
40 struct vertex v[6];
41 };
43 static void cleanup(void);
44 static void add_glyph(struct glyph *g, float x, float y);
46 #define QBUF_SZ 512
47 static struct quad *qbuf;
48 static int num_quads;
49 static int vattr = -1;
50 static int tattr = -1;
51 static unsigned int font_tex;
52 static int buf_mode = DTX_NBF;
55 int dtx_gl_init(void)
56 {
57 if(qbuf) {
58 return 0; /* already initialized */
59 }
61 glewInit();
63 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
64 return -1;
65 }
66 num_quads = 0;
68 atexit(cleanup);
69 return 0;
70 }
72 static void cleanup(void)
73 {
74 free(qbuf);
75 }
78 void dtx_vertex_attribs(int vert_attr, int tex_attr)
79 {
80 vattr = vert_attr;
81 tattr = tex_attr;
82 }
84 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
85 {
86 if(!gmap->tex) {
87 glGenTextures(1, &gmap->tex);
88 glBindTexture(GL_TEXTURE_2D, gmap->tex);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
92 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
94 #ifdef GL_ES
95 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
96 glGenerateMipmap(GL_TEXTURE_2D);
97 #else
98 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
99 #endif
100 }
102 if(font_tex != gmap->tex) {
103 dtx_flush();
104 }
105 font_tex = gmap->tex;
106 }
108 void dtx_glyph(int code)
109 {
110 struct dtx_glyphmap *gmap;
112 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
113 return;
114 }
115 set_glyphmap_texture(gmap);
117 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
118 dtx_flush();
119 }
121 static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
122 {
123 struct dtx_glyphmap *gmap;
124 float px, py;
125 int code = dtx_utf8_char_code(str);
126 str = dtx_utf8_next_char((char*)str);
128 if(buf_mode == DTX_LBF && code == '\n') {
129 *should_flush = 1;
130 }
132 px = *pos_x;
133 py = *pos_y;
135 if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
136 int idx = code - gmap->cstart;
138 set_glyphmap_texture(gmap);
139 add_glyph(gmap->glyphs + idx, px, py);
140 }
141 return str;
142 }
144 void dtx_string(const char *str)
145 {
146 int should_flush = buf_mode == DTX_NBF;
147 float pos_x = 0.0f;
148 float pos_y = 0.0f;
150 if(!dtx_font) {
151 return;
152 }
154 while(*str) {
155 str = put_char(str, &pos_x, &pos_y, &should_flush);
156 }
158 if(should_flush) {
159 dtx_flush();
160 }
161 }
163 void dtx_printf(const char *fmt, ...)
164 {
165 va_list ap;
166 int buf_size;
167 char *buf, tmp;
169 if(!dtx_font) {
170 return;
171 }
173 va_start(ap, fmt);
174 buf_size = vsnprintf(&tmp, 0, fmt, ap);
175 va_end(ap);
177 if(buf_size == -1) {
178 buf_size = 512;
179 }
181 buf = alloca(buf_size + 1);
182 va_start(ap, fmt);
183 vsnprintf(buf, buf_size + 1, fmt, ap);
184 va_end(ap);
186 dtx_string(buf);
187 }
189 static void qvertex(struct vertex *v, float x, float y, float s, float t)
190 {
191 v->x = x;
192 v->y = y;
193 v->s = s;
194 v->t = t;
195 }
197 static void add_glyph(struct glyph *g, float x, float y)
198 {
199 struct quad *qptr = qbuf + num_quads;
201 x -= g->orig_x;
202 y -= g->orig_y;
204 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
205 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
206 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
208 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
209 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
210 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
212 if(++num_quads >= QBUF_SZ) {
213 dtx_flush();
214 }
215 }
217 void dtx_flush(void)
218 {
219 int vbo;
221 if(!num_quads) {
222 return;
223 }
225 if(glBindBuffer) {
226 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
227 glBindBuffer(GL_ARRAY_BUFFER, 0);
228 }
230 #ifndef GL_ES
231 glPushAttrib(GL_ENABLE_BIT);
232 glEnable(GL_TEXTURE_2D);
233 #endif
234 glBindTexture(GL_TEXTURE_2D, font_tex);
236 if(vattr != -1 && glEnableVertexAttribArray) {
237 glEnableVertexAttribArray(vattr);
238 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
239 #ifndef GL_ES
240 } else {
241 glEnableClientState(GL_VERTEX_ARRAY);
242 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
243 #endif
244 }
245 if(tattr != -1 && glEnableVertexAttribArray) {
246 glEnableVertexAttribArray(tattr);
247 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
248 #ifndef GL_ES
249 } else {
250 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
251 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
252 #endif
253 }
255 glEnable(GL_BLEND);
256 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
258 glDepthMask(0);
260 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
262 glDepthMask(1);
264 if(vattr != -1 && glDisableVertexAttribArray) {
265 glDisableVertexAttribArray(vattr);
266 #ifndef GL_ES
267 } else {
268 glDisableClientState(GL_VERTEX_ARRAY);
269 #endif
270 }
271 if(tattr != -1 && glDisableVertexAttribArray) {
272 glDisableVertexAttribArray(tattr);
273 #ifndef GL_ES
274 } else {
275 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
276 #endif
277 }
279 #ifndef GL_ES
280 glPopAttrib();
281 #else
282 glDisable(GL_BLEND);
283 #endif
285 if(glBindBuffer && vbo) {
286 glBindBuffer(GL_ARRAY_BUFFER, vbo);
287 }
289 num_quads = 0;
290 }
292 #endif /* !def NO_OPENGL */