libdrawtext

view src/drawgl.c @ 51:670d0affc08f

fixed bad commit
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 Oct 2015 06:30:17 +0200
parents 00f98842608f
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 #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;
172 int buf_size;
173 char *buf, tmp;
175 if(!dtx_font) {
176 return;
177 }
179 buf_size = vsnprintf(&tmp, 0, fmt, ap);
181 if(buf_size == -1) {
182 buf_size = 512;
183 }
185 buf = alloca(buf_size + 1);
186 va_start(ap, fmt);
187 vsnprintf(buf, buf_size + 1, fmt, ap);
188 va_end(ap);
190 dtx_string(buf);
191 }
193 static void qvertex(struct vertex *v, float x, float y, float s, float t)
194 {
195 v->x = x;
196 v->y = y;
197 v->s = s;
198 v->t = t;
199 }
201 static void add_glyph(struct glyph *g, float x, float y)
202 {
203 struct quad *qptr = qbuf + num_quads;
205 x -= g->orig_x;
206 y -= g->orig_y;
208 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
209 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
210 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
212 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
213 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
214 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
216 if(++num_quads >= QBUF_SZ) {
217 dtx_flush();
218 }
219 }
221 void dtx_flush(void)
222 {
223 int vbo;
225 if(!num_quads) {
226 return;
227 }
229 if(glBindBuffer) {
230 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
231 glBindBuffer(GL_ARRAY_BUFFER, 0);
232 }
234 #ifndef GL_ES
235 glPushAttrib(GL_ENABLE_BIT);
236 glEnable(GL_TEXTURE_2D);
237 #endif
238 glBindTexture(GL_TEXTURE_2D, font_tex);
240 if(vattr != -1 && glEnableVertexAttribArray) {
241 glEnableVertexAttribArray(vattr);
242 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
243 #ifndef GL_ES
244 } else {
245 glEnableClientState(GL_VERTEX_ARRAY);
246 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
247 #endif
248 }
249 if(tattr != -1 && glEnableVertexAttribArray) {
250 glEnableVertexAttribArray(tattr);
251 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
252 #ifndef GL_ES
253 } else {
254 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
255 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
256 #endif
257 }
259 glEnable(GL_BLEND);
260 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
262 glDepthMask(0);
264 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
266 glDepthMask(1);
268 if(vattr != -1 && glDisableVertexAttribArray) {
269 glDisableVertexAttribArray(vattr);
270 #ifndef GL_ES
271 } else {
272 glDisableClientState(GL_VERTEX_ARRAY);
273 #endif
274 }
275 if(tattr != -1 && glDisableVertexAttribArray) {
276 glDisableVertexAttribArray(tattr);
277 #ifndef GL_ES
278 } else {
279 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
280 #endif
281 }
283 #ifndef GL_ES
284 glPopAttrib();
285 #else
286 glDisable(GL_BLEND);
287 #endif
289 if(glBindBuffer && vbo) {
290 glBindBuffer(GL_ARRAY_BUFFER, vbo);
291 }
293 num_quads = 0;
294 }
296 #endif /* !def NO_OPENGL */