3dphotoshoot

view libs/drawtext/drawgl.c @ 27:3d082c566b53

fixed all the bugs, pc version works
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 04:32: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 #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 #define GL_ES
34 #elif defined(ANDROID)
35 #include <GLES2/gl2.h>
36 #define GL_ES
37 #else
38 #include <GL/glew.h>
39 #endif
41 #include "drawtext.h"
42 #include "drawtext_impl.h"
44 struct vertex {
45 float x, y;
46 float s, t;
47 };
49 struct quad {
50 struct vertex v[6];
51 };
53 static void cleanup(void);
54 static void add_glyph(struct glyph *g, float x, float y);
56 #define QBUF_SZ 512
57 static struct quad *qbuf;
58 static int num_quads;
59 static int vattr = -1;
60 static int tattr = -1;
61 static unsigned int font_tex;
62 static int buf_mode = DTX_NBF;
65 int dtx_gl_init(void)
66 {
67 if(qbuf) {
68 return 0; /* already initialized */
69 }
71 #ifdef __glew_h__
72 glewInit();
73 #endif
75 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
76 return -1;
77 }
78 num_quads = 0;
80 atexit(cleanup);
81 return 0;
82 }
84 static void cleanup(void)
85 {
86 free(qbuf);
87 }
90 void dtx_vertex_attribs(int vert_attr, int tex_attr)
91 {
92 vattr = vert_attr;
93 tattr = tex_attr;
94 }
96 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
97 {
98 if(!gmap->tex) {
99 glGenTextures(1, &gmap->tex);
100 glBindTexture(GL_TEXTURE_2D, gmap->tex);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
106 #ifdef GL_ES
107 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
108 glGenerateMipmap(GL_TEXTURE_2D);
109 #else
110 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
111 #endif
112 }
114 if(font_tex != gmap->tex) {
115 dtx_flush();
116 }
117 font_tex = gmap->tex;
118 }
120 void dtx_glyph(int code)
121 {
122 struct dtx_glyphmap *gmap;
124 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
125 return;
126 }
127 set_glyphmap_texture(gmap);
129 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
130 dtx_flush();
131 }
133 static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
134 {
135 struct dtx_glyphmap *gmap;
136 float px, py;
137 int code = dtx_utf8_char_code(str);
138 str = dtx_utf8_next_char((char*)str);
140 if(buf_mode == DTX_LBF && code == '\n') {
141 *should_flush = 1;
142 }
144 px = *pos_x;
145 py = *pos_y;
147 if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
148 int idx = code - gmap->cstart;
150 set_glyphmap_texture(gmap);
151 add_glyph(gmap->glyphs + idx, px, py);
152 }
153 return str;
154 }
156 void dtx_string(const char *str)
157 {
158 int should_flush = buf_mode == DTX_NBF;
159 float pos_x = 0.0f;
160 float pos_y = 0.0f;
162 if(!dtx_font) {
163 return;
164 }
166 while(*str) {
167 str = put_char(str, &pos_x, &pos_y, &should_flush);
168 }
170 if(should_flush) {
171 dtx_flush();
172 }
173 }
175 void dtx_printf(const char *fmt, ...)
176 {
177 va_list ap;
178 int buf_size;
179 char *buf, tmp;
181 if(!dtx_font) {
182 return;
183 }
185 va_start(ap, fmt);
186 buf_size = vsnprintf(&tmp, 0, fmt, ap);
187 va_end(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 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
238 glBindBuffer(GL_ARRAY_BUFFER, 0);
240 #ifndef GL_ES
241 glPushAttrib(GL_ENABLE_BIT);
242 glEnable(GL_TEXTURE_2D);
243 #endif
244 glBindTexture(GL_TEXTURE_2D, font_tex);
246 if(vattr != -1) {
247 glEnableVertexAttribArray(vattr);
248 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
249 #ifndef GL_ES
250 } else {
251 glEnableClientState(GL_VERTEX_ARRAY);
252 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
253 #endif
254 }
255 if(tattr != -1) {
256 glEnableVertexAttribArray(tattr);
257 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
258 #ifndef GL_ES
259 } else {
260 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
261 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
262 #endif
263 }
265 glEnable(GL_BLEND);
266 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
268 glDepthMask(0);
270 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
272 glDepthMask(1);
274 if(vattr != -1) {
275 glDisableVertexAttribArray(vattr);
276 #ifndef GL_ES
277 } else {
278 glDisableClientState(GL_VERTEX_ARRAY);
279 #endif
280 }
281 if(tattr != -1) {
282 glDisableVertexAttribArray(tattr);
283 #ifndef GL_ES
284 } else {
285 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
286 #endif
287 }
289 #ifndef GL_ES
290 glPopAttrib();
291 #else
292 glDisable(GL_BLEND);
293 #endif
295 if(vbo) {
296 glBindBuffer(GL_ARRAY_BUFFER, vbo);
297 }
299 num_quads = 0;
300 }
302 #endif /* !def NO_OPENGL */