dungeon_crawler

annotate prototype/drawtext/drawgl.c @ 67:2560a7ab0243

internalized libanim, libimago2, and libpsys
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Oct 2012 02:04:00 +0300
parents e122ba214ee1
children
rev   line source
nuclear@24 1 /*
nuclear@24 2 libdrawtext - a simple library for fast text rendering in OpenGL
nuclear@29 3 Copyright (C) 2011-2012 John Tsiombikas <nuclear@member.fsf.org>
nuclear@24 4
nuclear@24 5 This program is free software: you can redistribute it and/or modify
nuclear@24 6 it under the terms of the GNU Lesser General Public License as published by
nuclear@24 7 the Free Software Foundation, either version 3 of the License, or
nuclear@24 8 (at your option) any later version.
nuclear@24 9
nuclear@24 10 This program is distributed in the hope that it will be useful,
nuclear@24 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@24 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@24 13 GNU Lesser General Public License for more details.
nuclear@24 14
nuclear@24 15 You should have received a copy of the GNU Lesser General Public License
nuclear@24 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@24 17 */
nuclear@24 18 #ifndef NO_OPENGL
nuclear@24 19 #include <math.h>
nuclear@24 20 #include <ctype.h>
nuclear@24 21
nuclear@24 22 #include <stdlib.h>
nuclear@24 23
nuclear@24 24 #ifdef TARGET_IPHONE
nuclear@24 25 #include <OpenGLES/ES2/gl.h>
nuclear@24 26 #else
nuclear@24 27 #include <GL/glew.h>
nuclear@24 28 #endif
nuclear@24 29
nuclear@24 30 #include "drawtext.h"
nuclear@24 31 #include "drawtext_impl.h"
nuclear@24 32
nuclear@24 33 struct vertex {
nuclear@24 34 float x, y;
nuclear@24 35 float s, t;
nuclear@24 36 };
nuclear@24 37
nuclear@24 38 struct quad {
nuclear@24 39 struct vertex v[6];
nuclear@24 40 };
nuclear@24 41
nuclear@24 42 static void cleanup(void);
nuclear@24 43 static void add_glyph(struct glyph *g, float x, float y);
nuclear@24 44
nuclear@24 45 #define QBUF_SZ 512
nuclear@24 46 static struct quad *qbuf;
nuclear@24 47 static int num_quads;
nuclear@24 48 static int vattr = -1;
nuclear@24 49 static int tattr = -1;
nuclear@24 50 static unsigned int font_tex;
nuclear@24 51 static int buf_mode = DTX_NBF;
nuclear@24 52
nuclear@24 53
nuclear@24 54 int dtx_gl_init(void)
nuclear@24 55 {
nuclear@24 56 if(qbuf) {
nuclear@24 57 return 0; /* already initialized */
nuclear@24 58 }
nuclear@24 59
nuclear@24 60 glewInit();
nuclear@24 61
nuclear@24 62 if(!(qbuf = malloc(QBUF_SZ * sizeof *qbuf))) {
nuclear@24 63 return -1;
nuclear@24 64 }
nuclear@24 65 num_quads = 0;
nuclear@24 66
nuclear@24 67 atexit(cleanup);
nuclear@24 68 return 0;
nuclear@24 69 }
nuclear@24 70
nuclear@24 71 static void cleanup(void)
nuclear@24 72 {
nuclear@24 73 free(qbuf);
nuclear@24 74 }
nuclear@24 75
nuclear@24 76
nuclear@24 77 void dtx_vertex_attribs(int vert_attr, int tex_attr)
nuclear@24 78 {
nuclear@24 79 vattr = vert_attr;
nuclear@24 80 tattr = tex_attr;
nuclear@24 81 }
nuclear@24 82
nuclear@24 83 static void set_glyphmap_texture(struct dtx_glyphmap *gmap)
nuclear@24 84 {
nuclear@24 85 if(!gmap->tex) {
nuclear@24 86 glGenTextures(1, &gmap->tex);
nuclear@24 87 glBindTexture(GL_TEXTURE_2D, gmap->tex);
nuclear@24 88 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
nuclear@24 89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@24 90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
nuclear@24 91 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
nuclear@24 92
nuclear@24 93 #ifdef GL_ES
nuclear@24 94 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gmap->xsz, gmap->ysz, 0, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
nuclear@24 95 glGenerateMipmap(GL_TEXTURE_2D);
nuclear@24 96 #else
nuclear@24 97 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_ALPHA, gmap->xsz, gmap->ysz, GL_ALPHA, GL_UNSIGNED_BYTE, gmap->pixels);
nuclear@24 98 #endif
nuclear@24 99 }
nuclear@24 100
nuclear@24 101 if(font_tex != gmap->tex) {
nuclear@24 102 dtx_flush();
nuclear@24 103 }
nuclear@24 104 font_tex = gmap->tex;
nuclear@24 105 }
nuclear@24 106
nuclear@24 107 void dtx_glyph(int code)
nuclear@24 108 {
nuclear@24 109 struct dtx_glyphmap *gmap;
nuclear@24 110
nuclear@24 111 if(!dtx_font || !(gmap = dtx_get_font_glyphmap(dtx_font, dtx_font_sz, code))) {
nuclear@24 112 return;
nuclear@24 113 }
nuclear@24 114 set_glyphmap_texture(gmap);
nuclear@24 115
nuclear@24 116 add_glyph(gmap->glyphs + code - gmap->cstart, 0, 0);
nuclear@24 117 dtx_flush();
nuclear@24 118 }
nuclear@24 119
nuclear@24 120 void dtx_string(const char *str)
nuclear@24 121 {
nuclear@24 122 struct dtx_glyphmap *gmap;
nuclear@24 123 int should_flush = buf_mode == DTX_NBF;
nuclear@24 124 float pos_x = 0.0f;
nuclear@24 125 float pos_y = 0.0f;
nuclear@24 126
nuclear@24 127 if(!dtx_font) {
nuclear@24 128 return;
nuclear@24 129 }
nuclear@24 130
nuclear@24 131 while(*str) {
nuclear@24 132 float px, py;
nuclear@24 133 int code = dtx_utf8_char_code(str);
nuclear@24 134 str = dtx_utf8_next_char((char*)str);
nuclear@24 135
nuclear@24 136 if(buf_mode == DTX_LBF && code == '\n') {
nuclear@24 137 should_flush = 1;
nuclear@24 138 }
nuclear@24 139
nuclear@24 140 px = pos_x;
nuclear@24 141 py = pos_y;
nuclear@24 142
nuclear@24 143 if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
nuclear@24 144 int idx = code - gmap->cstart;
nuclear@24 145
nuclear@24 146 set_glyphmap_texture(gmap);
nuclear@24 147 add_glyph(gmap->glyphs + idx, px, py);
nuclear@24 148 }
nuclear@24 149 }
nuclear@24 150
nuclear@24 151 if(should_flush) {
nuclear@24 152 dtx_flush();
nuclear@24 153 }
nuclear@24 154 }
nuclear@24 155
nuclear@24 156 static void qvertex(struct vertex *v, float x, float y, float s, float t)
nuclear@24 157 {
nuclear@24 158 v->x = x;
nuclear@24 159 v->y = y;
nuclear@24 160 v->s = s;
nuclear@24 161 v->t = t;
nuclear@24 162 }
nuclear@24 163
nuclear@24 164 static void add_glyph(struct glyph *g, float x, float y)
nuclear@24 165 {
nuclear@24 166 struct quad *qptr = qbuf + num_quads;
nuclear@24 167
nuclear@24 168 x -= g->orig_x;
nuclear@24 169 y -= g->orig_y;
nuclear@24 170
nuclear@24 171 qvertex(qptr->v + 0, x, y, g->nx, g->ny + g->nheight);
nuclear@24 172 qvertex(qptr->v + 1, x + g->width, y, g->nx + g->nwidth, g->ny + g->nheight);
nuclear@24 173 qvertex(qptr->v + 2, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
nuclear@24 174
nuclear@24 175 qvertex(qptr->v + 3, x, y, g->nx, g->ny + g->nheight);
nuclear@24 176 qvertex(qptr->v + 4, x + g->width, y + g->height, g->nx + g->nwidth, g->ny);
nuclear@24 177 qvertex(qptr->v + 5, x, y + g->height, g->nx, g->ny);
nuclear@24 178
nuclear@24 179 if(++num_quads >= QBUF_SZ) {
nuclear@24 180 dtx_flush();
nuclear@24 181 }
nuclear@24 182 }
nuclear@24 183
nuclear@24 184 void dtx_flush(void)
nuclear@24 185 {
nuclear@29 186 int vbo;
nuclear@29 187
nuclear@24 188 if(!num_quads) {
nuclear@24 189 return;
nuclear@24 190 }
nuclear@24 191
nuclear@29 192 if(glBindBuffer) {
nuclear@29 193 glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &vbo);
nuclear@29 194 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@29 195 }
nuclear@29 196
nuclear@24 197 #ifndef GL_ES
nuclear@24 198 glPushAttrib(GL_ENABLE_BIT);
nuclear@24 199 glEnable(GL_TEXTURE_2D);
nuclear@24 200 #endif
nuclear@24 201 glBindTexture(GL_TEXTURE_2D, font_tex);
nuclear@24 202
nuclear@24 203 if(vattr != -1 && glEnableVertexAttribArray) {
nuclear@24 204 glEnableVertexAttribArray(vattr);
nuclear@24 205 glVertexAttribPointer(vattr, 2, GL_FLOAT, 0, sizeof(struct vertex), qbuf);
nuclear@24 206 #ifndef GL_ES
nuclear@24 207 } else {
nuclear@24 208 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@24 209 glVertexPointer(2, GL_FLOAT, sizeof(struct vertex), qbuf);
nuclear@24 210 #endif
nuclear@24 211 }
nuclear@24 212 if(tattr != -1 && glEnableVertexAttribArray) {
nuclear@24 213 glEnableVertexAttribArray(tattr);
nuclear@24 214 glVertexAttribPointer(tattr, 2, GL_FLOAT, 0, sizeof(struct vertex), &qbuf->v[0].s);
nuclear@24 215 #ifndef GL_ES
nuclear@24 216 } else {
nuclear@24 217 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@24 218 glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertex), &qbuf->v[0].s);
nuclear@24 219 #endif
nuclear@24 220 }
nuclear@24 221
nuclear@24 222 glEnable(GL_BLEND);
nuclear@24 223 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
nuclear@24 224
nuclear@24 225 glDepthMask(0);
nuclear@24 226
nuclear@24 227 glDrawArrays(GL_TRIANGLES, 0, num_quads * 6);
nuclear@24 228
nuclear@24 229 glDepthMask(1);
nuclear@24 230
nuclear@24 231 if(vattr != -1 && glDisableVertexAttribArray) {
nuclear@24 232 glDisableVertexAttribArray(vattr);
nuclear@24 233 #ifndef GL_ES
nuclear@24 234 } else {
nuclear@24 235 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@24 236 #endif
nuclear@24 237 }
nuclear@24 238 if(tattr != -1 && glDisableVertexAttribArray) {
nuclear@24 239 glDisableVertexAttribArray(tattr);
nuclear@24 240 #ifndef GL_ES
nuclear@24 241 } else {
nuclear@24 242 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
nuclear@24 243 #endif
nuclear@24 244 }
nuclear@24 245
nuclear@24 246 #ifndef GL_ES
nuclear@24 247 glPopAttrib();
nuclear@24 248 #else
nuclear@24 249 glDisable(GL_BLEND);
nuclear@24 250 #endif
nuclear@24 251
nuclear@29 252 if(glBindBuffer && vbo) {
nuclear@29 253 glBindBuffer(GL_ARRAY_BUFFER, vbo);
nuclear@29 254 }
nuclear@29 255
nuclear@24 256 num_quads = 0;
nuclear@24 257 }
nuclear@24 258
nuclear@24 259 #endif /* !def NO_OPENGL */