libdrawtext

view src/drawgl.c @ 61:b03c0dcd0405

fixed for macosx
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 20 Sep 2011 15:16:35 +0300
parents 7e0c702f1223
children 55c034e70809
line source
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011 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 if(!num_quads) {
187 return;
188 }
190 #ifndef GL_ES
191 glPushAttrib(GL_ENABLE_BIT);
192 glEnable(GL_TEXTURE_2D);
193 #endif
194 glBindTexture(GL_TEXTURE_2D, font_tex);
196 if(vattr != -1 && glEnableVertexAttribArray) {
197 glEnableVertexAttribArray(vattr);
198 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
199 #ifndef GL_ES
200 } else {
201 glEnableClientState(GL_VERTEX_ARRAY);
202 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
203 #endif
204 }
205 if(tattr != -1 && glEnableVertexAttribArray) {
206 glEnableVertexAttribArray(tattr);
207 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
208 #ifndef GL_ES
209 } else {
210 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
211 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
212 #endif
213 }
215 glEnable(GL_BLEND);
216 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
218 glDepthMask(0);
220 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
222 glDepthMask(1);
224 if(vattr != -1 && glDisableVertexAttribArray) {
225 glDisableVertexAttribArray(vattr);
226 #ifndef GL_ES
227 } else {
228 glDisableClientState(GL_VERTEX_ARRAY);
229 #endif
230 }
231 if(tattr != -1 && glDisableVertexAttribArray) {
232 glDisableVertexAttribArray(tattr);
233 #ifndef GL_ES
234 } else {
235 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
236 #endif
237 }
239 #ifndef GL_ES
240 glPopAttrib();
241 #else
242 glDisable(GL_BLEND);
243 #endif
245 num_quads = 0;
246 }
248 #endif /* !def NO_OPENGL */