libdrawtext

view src/drawgl.c @ 55:59e5858de836

fixed a bug in utf-8 decoding
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 15 Sep 2011 23:32:39 +0300
parents bfe431dd1d80
children 095ff7ca4e74
line source
1 #ifndef NO_OPENGL
2 #include <math.h>
3 #include <ctype.h>
5 #include <stdlib.h>
7 #if defined(__IPHONE_3_0) || defined(__IPHONE_3_2) || defined(__IPHONE_4_0)
8 #include <OpenGLES/ES2/gl.h>
9 #else
10 #include <GL/glew.h>
11 #endif
13 #include "drawtext.h"
14 #include "drawtext_impl.h"
16 struct vertex {
17 float x, y;
18 float s, t;
19 };
21 struct quad {
22 struct vertex v[6];
23 };
25 static int init(void);
26 static void cleanup(void);
27 static void add_glyph(struct glyph *g, float x, float y);
29 #define QBUF_SZ 512
30 static struct quad *qbuf;
31 static int num_quads;
32 static int vattr = -1;
33 static int tattr = -1;
34 static unsigned int font_tex;
35 static int buf_mode = DTX_NBF;
37 static struct dtx_font *font;
38 static int font_sz;
41 static int init(void)
42 {
43 if(qbuf) {
44 return 0; /* already initialized */
45 }
47 glewInit();
49 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
50 return -1;
51 }
52 num_quads = 0;
54 atexit(cleanup);
55 return 0;
56 }
58 static void cleanup(void)
59 {
60 free(qbuf);
61 }
64 void dtx_use_font(struct dtx_font *fnt, int sz)
65 {
66 init();
68 font = fnt;
69 font_sz = sz;
70 }
72 void dtx_vertex_attribs(int vert_attr, int tex_attr)
73 {
74 vattr = vert_attr;
75 tattr = tex_attr;
76 }
78 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
79 {
80 if(!gmap->tex) {
81 glGenTextures(1, &gmap->tex);
82 glBindTexture(GL_TEXTURE_2D, gmap->tex);
83 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
84 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
85 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
86 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
88 #ifdef GL_ES
89 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
90 glGenerateMipmap(GL_TEXTURE_2D);
91 #else
92 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
93 #endif
94 }
96 if(font_tex != gmap->tex) {
97 dtx_flush();
98 }
99 font_tex = gmap->tex;
100 }
102 void dtx_glyph(int code)
103 {
104 struct dtx_glyphmap *gmap;
106 if(!font || !(gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
107 return;
108 }
109 set_glyphmap_texture(gmap);
111 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
112 dtx_flush();
113 }
115 void dtx_string(const char *str)
116 {
117 struct dtx_glyphmap *gmap;
118 int should_flush = buf_mode == DTX_NBF;
119 float pos_x = 0.0f;
120 float pos_y = 0.0f;
122 if(!font) {
123 return;
124 }
126 while(*str) {
127 int code = dtx_utf8_char_code(str);
128 str = dtx_utf8_next_char((char*)str);
130 switch(code) {
131 case '\n':
132 if(buf_mode == DTX_LBF) {
133 should_flush = 1;
134 }
135 pos_x = 0.0;
136 pos_y -= gmap->line_advance;
137 break;
139 case '\t':
140 pos_x = fmod(pos_x, 4.0) + 4.0;
141 break;
143 case '\r':
144 pos_x = 0.0;
145 break;
147 default:
148 if((gmap = dtx_get_font_glyphmap(font, font_sz, code))) {
149 int idx = code - gmap->cstart;
151 set_glyphmap_texture(gmap);
152 add_glyph(gmap->glyphs + idx, pos_x, pos_y);
153 pos_x += gmap->glyphs[idx].advance;
154 }
155 }
156 }
158 if(should_flush) {
159 dtx_flush();
160 }
161 }
163 static void qvertex(struct vertex *v, float x, float y, float s, float t)
164 {
165 v->x = x;
166 v->y = y;
167 v->s = s;
168 v->t = t;
169 }
171 static void add_glyph(struct glyph *g, float x, float y)
172 {
173 struct quad *qptr = qbuf + num_quads;
175 x -= g->orig_x;
176 y -= g->orig_y;
178 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
179 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
180 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
182 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
183 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
184 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
186 if(++num_quads >= QBUF_SZ) {
187 dtx_flush();
188 }
189 }
191 void dtx_flush(void)
192 {
193 if(!num_quads) {
194 return;
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 num_quads = 0;
253 }
255 #endif /* !def NO_OPENGL */