libdrawtext

view src/drawgl.c @ 64:d97cfb44221b

make sure to unbind any vbo's before trying to render
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 17:50:17 +0300
parents 9d44f6b0591f
children 462bcb69de15
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 <math.h>
20 #include <ctype.h>
22 #include <stdlib.h>
24 #ifdef TARGET_IPHONE
25 #include <OpenGLES/ES2/gl.h>
26 #else
27 #include <GL/glew.h>
28 #endif
30 #include "drawtext.h"
31 #include "drawtext_impl.h"
33 struct vertex {
34 float x, y;
35 float s, t;
36 };
38 struct quad {
39 struct vertex v[6];
40 };
42 static void cleanup(void);
43 static void add_glyph(struct glyph *g, float x, float y);
45 #define QBUF_SZ 512
46 static struct quad *qbuf;
47 static int num_quads;
48 static int vattr = -1;
49 static int tattr = -1;
50 static unsigned int font_tex;
51 static int buf_mode = DTX_NBF;
54 int dtx_gl_init(void)
55 {
56 if(qbuf) {
57 return 0; /* already initialized */
58 }
60 glewInit();
62 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
63 return -1;
64 }
65 num_quads = 0;
67 atexit(cleanup);
68 return 0;
69 }
71 static void cleanup(void)
72 {
73 free(qbuf);
74 }
77 void dtx_vertex_attribs(int vert_attr, int tex_attr)
78 {
79 vattr = vert_attr;
80 tattr = tex_attr;
81 }
83 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
84 {
85 if(!gmap->tex) {
86 glGenTextures(1, &gmap->tex);
87 glBindTexture(GL_TEXTURE_2D, gmap->tex);
88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
93 #ifdef GL_ES
94 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
95 glGenerateMipmap(GL_TEXTURE_2D);
96 #else
97 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
98 #endif
99 }
101 if(font_tex != gmap->tex) {
102 dtx_flush();
103 }
104 font_tex = gmap->tex;
105 }
107 void dtx_glyph(int code)
108 {
109 struct dtx_glyphmap *gmap;
111 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
112 return;
113 }
114 set_glyphmap_texture(gmap);
116 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
117 dtx_flush();
118 }
120 void dtx_string(const char *str)
121 {
122 struct dtx_glyphmap *gmap;
123 int should_flush = buf_mode == DTX_NBF;
124 float pos_x = 0.0f;
125 float pos_y = 0.0f;
127 if(!dtx_font) {
128 return;
129 }
131 while(*str) {
132 float px, py;
133 int code = dtx_utf8_char_code(str);
134 str = dtx_utf8_next_char((char*)str);
136 if(buf_mode == DTX_LBF && code == '\n') {
137 should_flush = 1;
138 }
140 px = pos_x;
141 py = pos_y;
143 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
144 int idx = code - gmap->cstart;
146 set_glyphmap_texture(gmap);
147 add_glyph(gmap->glyphs + idx, px, py);
148 }
149 }
151 if(should_flush) {
152 dtx_flush();
153 }
154 }
156 static void qvertex(struct vertex *v, float x, float y, float s, float t)
157 {
158 v->x = x;
159 v->y = y;
160 v->s = s;
161 v->t = t;
162 }
164 static void add_glyph(struct glyph *g, float x, float y)
165 {
166 struct quad *qptr = qbuf + num_quads;
168 x -= g->orig_x;
169 y -= g->orig_y;
171 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
172 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
173 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
175 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
176 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
177 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
179 if(++num_quads >= QBUF_SZ) {
180 dtx_flush();
181 }
182 }
184 void dtx_flush(void)
185 {
186 int vbo;
188 if(!num_quads) {
189 return;
190 }
192 if(glBindBuffer) {
193 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
194 glBindBuffer(GL_ARRAY_BUFFER, 0);
195 }
197 #ifndef GL_ES
198 glPushAttrib(GL_ENABLE_BIT);
199 glEnable(GL_TEXTURE_2D);
200 #endif
201 glBindTexture(GL_TEXTURE_2D, font_tex);
203 if(vattr != -1 && glEnableVertexAttribArray) {
204 glEnableVertexAttribArray(vattr);
205 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
206 #ifndef GL_ES
207 } else {
208 glEnableClientState(GL_VERTEX_ARRAY);
209 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
210 #endif
211 }
212 if(tattr != -1 && glEnableVertexAttribArray) {
213 glEnableVertexAttribArray(tattr);
214 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
215 #ifndef GL_ES
216 } else {
217 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
218 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
219 #endif
220 }
222 glEnable(GL_BLEND);
223 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
225 glDepthMask(0);
227 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
229 glDepthMask(1);
231 if(vattr != -1 && glDisableVertexAttribArray) {
232 glDisableVertexAttribArray(vattr);
233 #ifndef GL_ES
234 } else {
235 glDisableClientState(GL_VERTEX_ARRAY);
236 #endif
237 }
238 if(tattr != -1 && glDisableVertexAttribArray) {
239 glDisableVertexAttribArray(tattr);
240 #ifndef GL_ES
241 } else {
242 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
243 #endif
244 }
246 #ifndef GL_ES
247 glPopAttrib();
248 #else
249 glDisable(GL_BLEND);
250 #endif
252 if(glBindBuffer && vbo) {
253 glBindBuffer(GL_ARRAY_BUFFER, vbo);
254 }
256 num_quads = 0;
257 }
259 #endif /* !def NO_OPENGL */