istereo2

view libs/drawtext/drawgl.c @ 7:a3c4fcc9f8f3

- started a goatkit UI theme - font rendering with drawtext and shaders - asset manager (only used by drawtext for now, will replace respath eventually)
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 24 Sep 2015 06:49:25 +0300
parents
children
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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
22 #ifndef NO_OPENGL
23 #include <stdarg.h>
24 #include <math.h>
25 #include <ctype.h>
27 #include <stdlib.h>
29 #ifndef _MSC_VER
30 #include <alloca.h>
31 #else
32 #include <malloc.h>
33 #endif
35 #ifdef TARGET_IPHONE
36 #include <OpenGLES/ES2/gl.h>
37 #define GL_ES
38 #elif defined(ANDROID)
39 #include <GLES2/gl2.h>
40 #define GL_ES
41 #else
42 #include <GL/glew.h>
43 #endif
45 #include "drawtext.h"
46 #include "drawtext_impl.h"
48 struct vertex {
49 float x, y;
50 float s, t;
51 };
53 struct quad {
54 struct vertex v[6];
55 };
57 static void cleanup(void);
58 static void add_glyph(struct glyph *g, float x, float y);
60 #define QBUF_SZ 512
61 static struct quad *qbuf;
62 static int num_quads;
63 static int vattr = -1;
64 static int tattr = -1;
65 static unsigned int font_tex;
66 static int buf_mode = DTX_NBF;
69 int dtx_gl_init(void)
70 {
71 if(qbuf) {
72 return 0; /* already initialized */
73 }
75 #ifdef __glew_h__
76 glewInit();
77 #endif
79 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
80 return -1;
81 }
82 num_quads = 0;
84 atexit(cleanup);
85 return 0;
86 }
88 static void cleanup(void)
89 {
90 free(qbuf);
91 }
94 void dtx_vertex_attribs(int vert_attr, int tex_attr)
95 {
96 vattr = vert_attr;
97 tattr = tex_attr;
98 }
100 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
101 {
102 if(!gmap->tex) {
103 glGenTextures(1, &gmap->tex);
104 glBindTexture(GL_TEXTURE_2D, gmap->tex);
105 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
110 #ifdef GL_ES
111 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
112 glGenerateMipmap(GL_TEXTURE_2D);
113 #else
114 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
115 #endif
116 }
118 if(font_tex != gmap->tex) {
119 dtx_flush();
120 }
121 font_tex = gmap->tex;
122 }
124 void dtx_glyph(int code)
125 {
126 struct dtx_glyphmap *gmap;
128 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
129 return;
130 }
131 set_glyphmap_texture(gmap);
133 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
134 dtx_flush();
135 }
137 static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
138 {
139 struct dtx_glyphmap *gmap;
140 float px, py;
141 int code = dtx_utf8_char_code(str);
142 str = dtx_utf8_next_char((char*)str);
144 if(buf_mode == DTX_LBF && code == '\n') {
145 *should_flush = 1;
146 }
148 px = *pos_x;
149 py = *pos_y;
151 if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
152 int idx = code - gmap->cstart;
154 set_glyphmap_texture(gmap);
155 add_glyph(gmap->glyphs + idx, px, py);
156 }
157 return str;
158 }
160 void dtx_string(const char *str)
161 {
162 int should_flush = buf_mode == DTX_NBF;
163 float pos_x = 0.0f;
164 float pos_y = 0.0f;
166 if(!dtx_font) {
167 return;
168 }
170 while(*str) {
171 str = put_char(str, &pos_x, &pos_y, &should_flush);
172 }
174 if(should_flush) {
175 dtx_flush();
176 }
177 }
179 void dtx_printf(const char *fmt, ...)
180 {
181 va_list ap;
182 int buf_size;
183 char *buf, tmp;
185 if(!dtx_font) {
186 return;
187 }
189 va_start(ap, fmt);
190 buf_size = vsnprintf(&tmp, 0, fmt, ap);
191 va_end(ap);
193 if(buf_size == -1) {
194 buf_size = 512;
195 }
197 buf = alloca(buf_size + 1);
198 va_start(ap, fmt);
199 vsnprintf(buf, buf_size + 1, fmt, ap);
200 va_end(ap);
202 dtx_string(buf);
203 }
205 static void qvertex(struct vertex *v, float x, float y, float s, float t)
206 {
207 v->x = x;
208 v->y = y;
209 v->s = s;
210 v->t = t;
211 }
213 static void add_glyph(struct glyph *g, float x, float y)
214 {
215 struct quad *qptr = qbuf + num_quads;
217 x -= g->orig_x;
218 y -= g->orig_y;
220 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
221 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
222 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
224 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
225 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
226 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
228 if(++num_quads >= QBUF_SZ) {
229 dtx_flush();
230 }
231 }
233 void dtx_flush(void)
234 {
235 int vbo;
237 if(!num_quads) {
238 return;
239 }
241 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
242 glBindBuffer(GL_ARRAY_BUFFER, 0);
244 #ifndef GL_ES
245 glPushAttrib(GL_ENABLE_BIT);
246 glEnable(GL_TEXTURE_2D);
247 #endif
248 glBindTexture(GL_TEXTURE_2D, font_tex);
250 if(vattr != -1) {
251 glEnableVertexAttribArray(vattr);
252 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
253 #ifndef GL_ES
254 } else {
255 glEnableClientState(GL_VERTEX_ARRAY);
256 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
257 #endif
258 }
259 if(tattr != -1) {
260 glEnableVertexAttribArray(tattr);
261 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
262 #ifndef GL_ES
263 } else {
264 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
265 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
266 #endif
267 }
269 glEnable(GL_BLEND);
270 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
272 glDepthMask(0);
274 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
276 glDepthMask(1);
278 if(vattr != -1) {
279 glDisableVertexAttribArray(vattr);
280 #ifndef GL_ES
281 } else {
282 glDisableClientState(GL_VERTEX_ARRAY);
283 #endif
284 }
285 if(tattr != -1) {
286 glDisableVertexAttribArray(tattr);
287 #ifndef GL_ES
288 } else {
289 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
290 #endif
291 }
293 #ifndef GL_ES
294 glPopAttrib();
295 #else
296 glDisable(GL_BLEND);
297 #endif
299 if(vbo) {
300 glBindBuffer(GL_ARRAY_BUFFER, vbo);
301 }
303 num_quads = 0;
304 }
306 #endif /* !def NO_OPENGL */