istereo2
diff libs/drawtext/drawgl.c @ 7:a3c4fcc9f8f3
- started a goatkit UI theme
- font rendering with drawtext and shaders
- asset manager (only used by drawtext for now, will replace respath eventually)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 24 Sep 2015 06:49:25 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/drawtext/drawgl.c Thu Sep 24 06:49:25 2015 +0300 1.3 @@ -0,0 +1,306 @@ 1.4 +/* 1.5 +libdrawtext - a simple library for fast text rendering in OpenGL 1.6 +Copyright (C) 2011-2012 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#ifdef HAVE_CONFIG_H 1.22 +#include "config.h" 1.23 +#endif 1.24 + 1.25 +#ifndef NO_OPENGL 1.26 +#include <stdarg.h> 1.27 +#include <math.h> 1.28 +#include <ctype.h> 1.29 + 1.30 +#include <stdlib.h> 1.31 + 1.32 +#ifndef _MSC_VER 1.33 +#include <alloca.h> 1.34 +#else 1.35 +#include <malloc.h> 1.36 +#endif 1.37 + 1.38 +#ifdef TARGET_IPHONE 1.39 +#include <OpenGLES/ES2/gl.h> 1.40 +#define GL_ES 1.41 +#elif defined(ANDROID) 1.42 +#include <GLES2/gl2.h> 1.43 +#define GL_ES 1.44 +#else 1.45 +#include <GL/glew.h> 1.46 +#endif 1.47 + 1.48 +#include "drawtext.h" 1.49 +#include "drawtext_impl.h" 1.50 + 1.51 +struct vertex { 1.52 + float x, y; 1.53 + float s, t; 1.54 +}; 1.55 + 1.56 +struct quad { 1.57 + struct vertex v[6]; 1.58 +}; 1.59 + 1.60 +static void cleanup(void); 1.61 +static void add_glyph(struct glyph *g, float x, float y); 1.62 + 1.63 +#define QBUF_SZ 512 1.64 +static struct quad *qbuf; 1.65 +static int num_quads; 1.66 +static int vattr = -1; 1.67 +static int tattr = -1; 1.68 +static unsigned int font_tex; 1.69 +static int buf_mode = DTX_NBF; 1.70 + 1.71 + 1.72 +int dtx_gl_init(void) 1.73 +{ 1.74 + if(qbuf) { 1.75 + return 0; /* already initialized */ 1.76 + } 1.77 + 1.78 +#ifdef __glew_h__ 1.79 + glewInit(); 1.80 +#endif 1.81 + 1.82 + if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) { 1.83 + return -1; 1.84 + } 1.85 + num_quads = 0; 1.86 + 1.87 + atexit(cleanup); 1.88 + return 0; 1.89 +} 1.90 + 1.91 +static void cleanup(void) 1.92 +{ 1.93 + free(qbuf); 1.94 +} 1.95 + 1.96 + 1.97 +void dtx_vertex_attribs(int vert_attr, int tex_attr) 1.98 +{ 1.99 + vattr = vert_attr; 1.100 + tattr = tex_attr; 1.101 +} 1.102 + 1.103 +static void set_glyphmap_texture(struct dtx_glyphmap *gmap) 1.104 +{ 1.105 + if(!gmap->tex) { 1.106 + glGenTextures(1, &gmap->tex); 1.107 + glBindTexture(GL_TEXTURE_2D, gmap->tex); 1.108 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 1.109 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.110 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 1.111 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 1.112 + 1.113 +#ifdef GL_ES 1.114 + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.115 + glGenerateMipmap(GL_TEXTURE_2D); 1.116 +#else 1.117 + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.118 +#endif 1.119 + } 1.120 + 1.121 + if(font_tex != gmap->tex) { 1.122 + dtx_flush(); 1.123 + } 1.124 + font_tex = gmap->tex; 1.125 +} 1.126 + 1.127 +void dtx_glyph(int code) 1.128 +{ 1.129 + struct dtx_glyphmap *gmap; 1.130 + 1.131 + if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) { 1.132 + return; 1.133 + } 1.134 + set_glyphmap_texture(gmap); 1.135 + 1.136 + add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0); 1.137 + dtx_flush(); 1.138 +} 1.139 + 1.140 +static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush) 1.141 +{ 1.142 + struct dtx_glyphmap *gmap; 1.143 + float px, py; 1.144 + int code = dtx_utf8_char_code(str); 1.145 + str = dtx_utf8_next_char((char*)str); 1.146 + 1.147 + if(buf_mode == DTX_LBF && code == '\n') { 1.148 + *should_flush = 1; 1.149 + } 1.150 + 1.151 + px = *pos_x; 1.152 + py = *pos_y; 1.153 + 1.154 + if((gmap = dtx_proc_char(code, pos_x, pos_y))) { 1.155 + int idx = code - gmap->cstart; 1.156 + 1.157 + set_glyphmap_texture(gmap); 1.158 + add_glyph(gmap->glyphs + idx, px, py); 1.159 + } 1.160 + return str; 1.161 +} 1.162 + 1.163 +void dtx_string(const char *str) 1.164 +{ 1.165 + int should_flush = buf_mode == DTX_NBF; 1.166 + float pos_x = 0.0f; 1.167 + float pos_y = 0.0f; 1.168 + 1.169 + if(!dtx_font) { 1.170 + return; 1.171 + } 1.172 + 1.173 + while(*str) { 1.174 + str = put_char(str, &pos_x, &pos_y, &should_flush); 1.175 + } 1.176 + 1.177 + if(should_flush) { 1.178 + dtx_flush(); 1.179 + } 1.180 +} 1.181 + 1.182 +void dtx_printf(const char *fmt, ...) 1.183 +{ 1.184 + va_list ap; 1.185 + int buf_size; 1.186 + char *buf, tmp; 1.187 + 1.188 + if(!dtx_font) { 1.189 + return; 1.190 + } 1.191 + 1.192 + va_start(ap, fmt); 1.193 + buf_size = vsnprintf(&tmp, 0, fmt, ap); 1.194 + va_end(ap); 1.195 + 1.196 + if(buf_size == -1) { 1.197 + buf_size = 512; 1.198 + } 1.199 + 1.200 + buf = alloca(buf_size + 1); 1.201 + va_start(ap, fmt); 1.202 + vsnprintf(buf, buf_size + 1, fmt, ap); 1.203 + va_end(ap); 1.204 + 1.205 + dtx_string(buf); 1.206 +} 1.207 + 1.208 +static void qvertex(struct vertex *v, float x, float y, float s, float t) 1.209 +{ 1.210 + v->x = x; 1.211 + v->y = y; 1.212 + v->s = s; 1.213 + v->t = t; 1.214 +} 1.215 + 1.216 +static void add_glyph(struct glyph *g, float x, float y) 1.217 +{ 1.218 + struct quad *qptr = qbuf + num_quads; 1.219 + 1.220 + x -= g->orig_x; 1.221 + y -= g->orig_y; 1.222 + 1.223 + qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight); 1.224 + qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight); 1.225 + qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.226 + 1.227 + qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight); 1.228 + qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.229 + qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny); 1.230 + 1.231 + if(++num_quads >= QBUF_SZ) { 1.232 + dtx_flush(); 1.233 + } 1.234 +} 1.235 + 1.236 +void dtx_flush(void) 1.237 +{ 1.238 + int vbo; 1.239 + 1.240 + if(!num_quads) { 1.241 + return; 1.242 + } 1.243 + 1.244 + glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo); 1.245 + glBindBuffer(GL_ARRAY_BUFFER, 0); 1.246 + 1.247 +#ifndef GL_ES 1.248 + glPushAttrib(GL_ENABLE_BIT); 1.249 + glEnable(GL_TEXTURE_2D); 1.250 +#endif 1.251 + glBindTexture(GL_TEXTURE_2D, font_tex); 1.252 + 1.253 + if(vattr != -1) { 1.254 + glEnableVertexAttribArray(vattr); 1.255 + glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf); 1.256 +#ifndef GL_ES 1.257 + } else { 1.258 + glEnableClientState(GL_VERTEX_ARRAY); 1.259 + glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf); 1.260 +#endif 1.261 + } 1.262 + if(tattr != -1) { 1.263 + glEnableVertexAttribArray(tattr); 1.264 + glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s); 1.265 +#ifndef GL_ES 1.266 + } else { 1.267 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.268 + glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s); 1.269 +#endif 1.270 + } 1.271 + 1.272 + glEnable(GL_BLEND); 1.273 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.274 + 1.275 + glDepthMask(0); 1.276 + 1.277 + glDrawArrays(GL_TRIANGLES, 0, num_quads * 6); 1.278 + 1.279 + glDepthMask(1); 1.280 + 1.281 + if(vattr != -1) { 1.282 + glDisableVertexAttribArray(vattr); 1.283 +#ifndef GL_ES 1.284 + } else { 1.285 + glDisableClientState(GL_VERTEX_ARRAY); 1.286 +#endif 1.287 + } 1.288 + if(tattr != -1) { 1.289 + glDisableVertexAttribArray(tattr); 1.290 +#ifndef GL_ES 1.291 + } else { 1.292 + glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.293 +#endif 1.294 + } 1.295 + 1.296 +#ifndef GL_ES 1.297 + glPopAttrib(); 1.298 +#else 1.299 + glDisable(GL_BLEND); 1.300 +#endif 1.301 + 1.302 + if(vbo) { 1.303 + glBindBuffer(GL_ARRAY_BUFFER, vbo); 1.304 + } 1.305 + 1.306 + num_quads = 0; 1.307 +} 1.308 + 1.309 +#endif /* !def NO_OPENGL */