dungeon_crawler
diff prototype/drawtext/drawgl.c @ 24:e122ba214ee1
implementing the command console
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Aug 2012 18:03:11 +0300 |
parents | |
children | 2fc004802739 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/drawtext/drawgl.c Thu Aug 23 18:03:11 2012 +0300 1.3 @@ -0,0 +1,248 @@ 1.4 +/* 1.5 +libdrawtext - a simple library for fast text rendering in OpenGL 1.6 +Copyright (C) 2011 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 +#ifndef NO_OPENGL 1.22 +#include <math.h> 1.23 +#include <ctype.h> 1.24 + 1.25 +#include <stdlib.h> 1.26 + 1.27 +#ifdef TARGET_IPHONE 1.28 +#include <OpenGLES/ES2/gl.h> 1.29 +#else 1.30 +#include <GL/glew.h> 1.31 +#endif 1.32 + 1.33 +#include "drawtext.h" 1.34 +#include "drawtext_impl.h" 1.35 + 1.36 +struct vertex { 1.37 + float x, y; 1.38 + float s, t; 1.39 +}; 1.40 + 1.41 +struct quad { 1.42 + struct vertex v[6]; 1.43 +}; 1.44 + 1.45 +static void cleanup(void); 1.46 +static void add_glyph(struct glyph *g, float x, float y); 1.47 + 1.48 +#define QBUF_SZ 512 1.49 +static struct quad *qbuf; 1.50 +static int num_quads; 1.51 +static int vattr = -1; 1.52 +static int tattr = -1; 1.53 +static unsigned int font_tex; 1.54 +static int buf_mode = DTX_NBF; 1.55 + 1.56 + 1.57 +int dtx_gl_init(void) 1.58 +{ 1.59 + if(qbuf) { 1.60 + return 0; /* already initialized */ 1.61 + } 1.62 + 1.63 + glewInit(); 1.64 + 1.65 + if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) { 1.66 + return -1; 1.67 + } 1.68 + num_quads = 0; 1.69 + 1.70 + atexit(cleanup); 1.71 + return 0; 1.72 +} 1.73 + 1.74 +static void cleanup(void) 1.75 +{ 1.76 + free(qbuf); 1.77 +} 1.78 + 1.79 + 1.80 +void dtx_vertex_attribs(int vert_attr, int tex_attr) 1.81 +{ 1.82 + vattr = vert_attr; 1.83 + tattr = tex_attr; 1.84 +} 1.85 + 1.86 +static void set_glyphmap_texture(struct dtx_glyphmap *gmap) 1.87 +{ 1.88 + if(!gmap->tex) { 1.89 + glGenTextures(1, &gmap->tex); 1.90 + glBindTexture(GL_TEXTURE_2D, gmap->tex); 1.91 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 1.92 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 1.93 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); 1.94 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); 1.95 + 1.96 +#ifdef GL_ES 1.97 + glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.98 + glGenerateMipmap(GL_TEXTURE_2D); 1.99 +#else 1.100 + gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels); 1.101 +#endif 1.102 + } 1.103 + 1.104 + if(font_tex != gmap->tex) { 1.105 + dtx_flush(); 1.106 + } 1.107 + font_tex = gmap->tex; 1.108 +} 1.109 + 1.110 +void dtx_glyph(int code) 1.111 +{ 1.112 + struct dtx_glyphmap *gmap; 1.113 + 1.114 + if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) { 1.115 + return; 1.116 + } 1.117 + set_glyphmap_texture(gmap); 1.118 + 1.119 + add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0); 1.120 + dtx_flush(); 1.121 +} 1.122 + 1.123 +void dtx_string(const char *str) 1.124 +{ 1.125 + struct dtx_glyphmap *gmap; 1.126 + int should_flush = buf_mode == DTX_NBF; 1.127 + float pos_x = 0.0f; 1.128 + float pos_y = 0.0f; 1.129 + 1.130 + if(!dtx_font) { 1.131 + return; 1.132 + } 1.133 + 1.134 + while(*str) { 1.135 + float px, py; 1.136 + int code = dtx_utf8_char_code(str); 1.137 + str = dtx_utf8_next_char((char*)str); 1.138 + 1.139 + if(buf_mode == DTX_LBF && code == '\n') { 1.140 + should_flush = 1; 1.141 + } 1.142 + 1.143 + px = pos_x; 1.144 + py = pos_y; 1.145 + 1.146 + if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) { 1.147 + int idx = code - gmap->cstart; 1.148 + 1.149 + set_glyphmap_texture(gmap); 1.150 + add_glyph(gmap->glyphs + idx, px, py); 1.151 + } 1.152 + } 1.153 + 1.154 + if(should_flush) { 1.155 + dtx_flush(); 1.156 + } 1.157 +} 1.158 + 1.159 +static void qvertex(struct vertex *v, float x, float y, float s, float t) 1.160 +{ 1.161 + v->x = x; 1.162 + v->y = y; 1.163 + v->s = s; 1.164 + v->t = t; 1.165 +} 1.166 + 1.167 +static void add_glyph(struct glyph *g, float x, float y) 1.168 +{ 1.169 + struct quad *qptr = qbuf + num_quads; 1.170 + 1.171 + x -= g->orig_x; 1.172 + y -= g->orig_y; 1.173 + 1.174 + qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight); 1.175 + qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight); 1.176 + qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.177 + 1.178 + qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight); 1.179 + qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny); 1.180 + qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny); 1.181 + 1.182 + if(++num_quads >= QBUF_SZ) { 1.183 + dtx_flush(); 1.184 + } 1.185 +} 1.186 + 1.187 +void dtx_flush(void) 1.188 +{ 1.189 + if(!num_quads) { 1.190 + return; 1.191 + } 1.192 + 1.193 +#ifndef GL_ES 1.194 + glPushAttrib(GL_ENABLE_BIT); 1.195 + glEnable(GL_TEXTURE_2D); 1.196 +#endif 1.197 + glBindTexture(GL_TEXTURE_2D, font_tex); 1.198 + 1.199 + if(vattr != -1 && glEnableVertexAttribArray) { 1.200 + glEnableVertexAttribArray(vattr); 1.201 + glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf); 1.202 +#ifndef GL_ES 1.203 + } else { 1.204 + glEnableClientState(GL_VERTEX_ARRAY); 1.205 + glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf); 1.206 +#endif 1.207 + } 1.208 + if(tattr != -1 && glEnableVertexAttribArray) { 1.209 + glEnableVertexAttribArray(tattr); 1.210 + glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s); 1.211 +#ifndef GL_ES 1.212 + } else { 1.213 + glEnableClientState(GL_TEXTURE_COORD_ARRAY); 1.214 + glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s); 1.215 +#endif 1.216 + } 1.217 + 1.218 + glEnable(GL_BLEND); 1.219 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.220 + 1.221 + glDepthMask(0); 1.222 + 1.223 + glDrawArrays(GL_TRIANGLES, 0, num_quads * 6); 1.224 + 1.225 + glDepthMask(1); 1.226 + 1.227 + if(vattr != -1 && glDisableVertexAttribArray) { 1.228 + glDisableVertexAttribArray(vattr); 1.229 +#ifndef GL_ES 1.230 + } else { 1.231 + glDisableClientState(GL_VERTEX_ARRAY); 1.232 +#endif 1.233 + } 1.234 + if(tattr != -1 && glDisableVertexAttribArray) { 1.235 + glDisableVertexAttribArray(tattr); 1.236 +#ifndef GL_ES 1.237 + } else { 1.238 + glDisableClientState(GL_TEXTURE_COORD_ARRAY); 1.239 +#endif 1.240 + } 1.241 + 1.242 +#ifndef GL_ES 1.243 + glPopAttrib(); 1.244 +#else 1.245 + glDisable(GL_BLEND); 1.246 +#endif 1.247 + 1.248 + num_quads = 0; 1.249 +} 1.250 + 1.251 +#endif /* !def NO_OPENGL */