3dphotoshoot

annotate src/text.c @ 25:ac80210d5fbe

preparing a pc version for easier development of non-android-specifics
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 18 Jun 2015 03:12:30 +0300
parents
children
rev   line source
nuclear@22 1 #include <stdio.h>
nuclear@22 2 #include <stdarg.h>
nuclear@22 3 #include <alloca.h>
nuclear@22 4 #include "game.h"
nuclear@22 5 #include "text.h"
nuclear@22 6 #include "sdr.h"
nuclear@22 7 #include "opengl.h"
nuclear@22 8 #include "sanegl.h"
nuclear@22 9 #include "drawtext/drawtext.h"
nuclear@22 10
nuclear@22 11 #define FONT_NAME "consolas_s24.glyphmap"
nuclear@22 12 #define FONT_SIZE 24
nuclear@22 13
nuclear@22 14 static struct dtx_font *font;
nuclear@22 15 static unsigned int prog;
nuclear@22 16 static int uloc_color = -1;
nuclear@22 17 static int aloc_vertex, aloc_texcoords;
nuclear@22 18 static float color[4];
nuclear@22 19 static int cpos[2];
nuclear@22 20 static int line_height;
nuclear@22 21
nuclear@22 22 static int init(void)
nuclear@22 23 {
nuclear@22 24 if(font) return 0; /* already initialized */
nuclear@22 25
nuclear@22 26 if(!(font = dtx_open_font_glyphmap("data/" FONT_NAME))) {
nuclear@22 27 fprintf(stderr, "failed to open font\n");
nuclear@22 28 return -1;
nuclear@22 29 }
nuclear@22 30 line_height = dtx_line_height();
nuclear@22 31
nuclear@22 32 if(!(prog = create_program_load("sdr/vertex.glsl", "sdr/font.p.glsl"))) {
nuclear@22 33 dtx_close_font(font);
nuclear@22 34 font = 0;
nuclear@22 35 return -1;
nuclear@22 36 }
nuclear@22 37 aloc_vertex = glGetAttribLocation(prog, "attr_vertex");
nuclear@22 38 aloc_texcoords = glGetAttribLocation(prog, "attr_texcoord");
nuclear@22 39 uloc_color = glGetUniformLocation(prog, "color");
nuclear@22 40
nuclear@22 41 return 0;
nuclear@22 42 }
nuclear@22 43
nuclear@22 44 void text_color(float r, float g, float b, float a)
nuclear@22 45 {
nuclear@22 46 color[0] = r;
nuclear@22 47 color[1] = g;
nuclear@22 48 color[2] = b;
nuclear@22 49 color[3] = a;
nuclear@22 50 }
nuclear@22 51
nuclear@22 52 void text_position(int x, int y)
nuclear@22 53 {
nuclear@22 54 cpos[0] = x;
nuclear@22 55 cpos[1] = y;
nuclear@22 56 }
nuclear@22 57
nuclear@22 58 static int pre_print(void)
nuclear@22 59 {
nuclear@22 60 int top = win_height - line_height;
nuclear@22 61
nuclear@22 62 if(init() == -1) {
nuclear@22 63 return -1;
nuclear@22 64 }
nuclear@22 65
nuclear@22 66 gl_matrix_mode(GL_TEXTURE);
nuclear@22 67 gl_push_matrix();
nuclear@22 68 gl_load_identity();
nuclear@22 69 gl_matrix_mode(GL_PROJECTION);
nuclear@22 70 gl_push_matrix();
nuclear@22 71 gl_load_identity();
nuclear@22 72 gl_ortho(0, win_width, 0, win_height, -1, 1);
nuclear@22 73 gl_matrix_mode(GL_MODELVIEW);
nuclear@22 74 gl_push_matrix();
nuclear@22 75 gl_load_identity();
nuclear@22 76 gl_translatef(cpos[0] * dtx_glyph_width(' '), top - cpos[1] * line_height, 0);
nuclear@22 77
nuclear@22 78 glUseProgram(prog);
nuclear@22 79 glUniform4fv(uloc_color, 1, color);
nuclear@22 80
nuclear@22 81 gl_apply_xform(prog);
nuclear@22 82
nuclear@22 83 dtx_use_font(font, FONT_SIZE);
nuclear@22 84 dtx_vertex_attribs(aloc_vertex, aloc_texcoords);
nuclear@22 85 return 0;
nuclear@22 86 }
nuclear@22 87
nuclear@22 88 static void post_print(void)
nuclear@22 89 {
nuclear@22 90 gl_matrix_mode(GL_TEXTURE);
nuclear@22 91 gl_pop_matrix();
nuclear@22 92 gl_matrix_mode(GL_PROJECTION);
nuclear@22 93 gl_pop_matrix();
nuclear@22 94 gl_matrix_mode(GL_MODELVIEW);
nuclear@22 95 gl_pop_matrix();
nuclear@22 96 }
nuclear@22 97
nuclear@22 98 void text_print(const char *s)
nuclear@22 99 {
nuclear@22 100 if(pre_print() == -1) {
nuclear@22 101 return;
nuclear@22 102 }
nuclear@22 103
nuclear@22 104 dtx_string(s);
nuclear@22 105
nuclear@22 106 post_print();
nuclear@22 107 }
nuclear@22 108
nuclear@22 109 void text_printf(const char *fmt, ...)
nuclear@22 110 {
nuclear@22 111 va_list ap;
nuclear@22 112 int buf_size;
nuclear@22 113 char *buf, tmp;
nuclear@22 114
nuclear@22 115 if(pre_print() == -1) {
nuclear@22 116 return;
nuclear@22 117 }
nuclear@22 118
nuclear@22 119 va_start(ap, fmt);
nuclear@22 120 buf_size = vsnprintf(&tmp, 0, fmt, ap);
nuclear@22 121 va_end(ap);
nuclear@22 122
nuclear@22 123 if(buf_size == -1) {
nuclear@22 124 buf_size = 512;
nuclear@22 125 }
nuclear@22 126
nuclear@22 127 buf = alloca(buf_size + 1);
nuclear@22 128 va_start(ap, fmt);
nuclear@22 129 vsnprintf(buf, buf_size + 1, fmt, ap);
nuclear@22 130 va_end(ap);
nuclear@22 131
nuclear@22 132 dtx_string(buf);
nuclear@22 133
nuclear@22 134 post_print();
nuclear@22 135 }