libdrawtext

view src/drawgl.c @ 70:80a2e0556fc1

added visual studio project and fixed some windows build problems
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 10 Mar 2013 18:29:21 +0200
parents 462bcb69de15
children 00f98842608f
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 va_start(ap, fmt);
180 buf_size = vsnprintf(&tmp, 0, fmt, ap);
181 va_end(ap);
183 if(buf_size == -1) {
184 buf_size = 512;
185 }
187 buf = alloca(buf_size + 1);
188 va_start(ap, fmt);
189 vsnprintf(buf, buf_size + 1, fmt, ap);
190 va_end(ap);
192 dtx_string(buf);
193 }
195 static void qvertex(struct vertex *v, float x, float y, float s, float t)
196 {
197 v->x = x;
198 v->y = y;
199 v->s = s;
200 v->t = t;
201 }
203 static void add_glyph(struct glyph *g, float x, float y)
204 {
205 struct quad *qptr = qbuf + num_quads;
207 x -= g->orig_x;
208 y -= g->orig_y;
210 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
211 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
212 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
214 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
215 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
216 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
218 if(++num_quads >= QBUF_SZ) {
219 dtx_flush();
220 }
221 }
223 void dtx_flush(void)
224 {
225 int vbo;
227 if(!num_quads) {
228 return;
229 }
231 if(glBindBuffer) {
232 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
233 glBindBuffer(GL_ARRAY_BUFFER, 0);
234 }
236 #ifndef GL_ES
237 glPushAttrib(GL_ENABLE_BIT);
238 glEnable(GL_TEXTURE_2D);
239 #endif
240 glBindTexture(GL_TEXTURE_2D, font_tex);
242 if(vattr != -1 && glEnableVertexAttribArray) {
243 glEnableVertexAttribArray(vattr);
244 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
245 #ifndef GL_ES
246 } else {
247 glEnableClientState(GL_VERTEX_ARRAY);
248 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
249 #endif
250 }
251 if(tattr != -1 && glEnableVertexAttribArray) {
252 glEnableVertexAttribArray(tattr);
253 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
254 #ifndef GL_ES
255 } else {
256 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
257 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
258 #endif
259 }
261 glEnable(GL_BLEND);
262 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
264 glDepthMask(0);
266 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
268 glDepthMask(1);
270 if(vattr != -1 && glDisableVertexAttribArray) {
271 glDisableVertexAttribArray(vattr);
272 #ifndef GL_ES
273 } else {
274 glDisableClientState(GL_VERTEX_ARRAY);
275 #endif
276 }
277 if(tattr != -1 && glDisableVertexAttribArray) {
278 glDisableVertexAttribArray(tattr);
279 #ifndef GL_ES
280 } else {
281 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
282 #endif
283 }
285 #ifndef GL_ES
286 glPopAttrib();
287 #else
288 glDisable(GL_BLEND);
289 #endif
291 if(glBindBuffer && vbo) {
292 glBindBuffer(GL_ARRAY_BUFFER, vbo);
293 }
295 num_quads = 0;
296 }
298 #endif /* !def NO_OPENGL */