3dphotoshoot

diff src/text.c @ 22:d7fe157c402d

fonts
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 13 Jun 2015 05:32:07 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/text.c	Sat Jun 13 05:32:07 2015 +0300
     1.3 @@ -0,0 +1,135 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdarg.h>
     1.6 +#include <alloca.h>
     1.7 +#include "game.h"
     1.8 +#include "text.h"
     1.9 +#include "sdr.h"
    1.10 +#include "opengl.h"
    1.11 +#include "sanegl.h"
    1.12 +#include "drawtext/drawtext.h"
    1.13 +
    1.14 +#define FONT_NAME	"consolas_s24.glyphmap"
    1.15 +#define FONT_SIZE	24
    1.16 +
    1.17 +static struct dtx_font *font;
    1.18 +static unsigned int prog;
    1.19 +static int uloc_color = -1;
    1.20 +static int aloc_vertex, aloc_texcoords;
    1.21 +static float color[4];
    1.22 +static int cpos[2];
    1.23 +static int line_height;
    1.24 +
    1.25 +static int init(void)
    1.26 +{
    1.27 +	if(font) return 0;	/* already initialized */
    1.28 +
    1.29 +	if(!(font = dtx_open_font_glyphmap("data/" FONT_NAME))) {
    1.30 +		fprintf(stderr, "failed to open font\n");
    1.31 +		return -1;
    1.32 +	}
    1.33 +	line_height = dtx_line_height();
    1.34 +
    1.35 +	if(!(prog = create_program_load("sdr/vertex.glsl", "sdr/font.p.glsl"))) {
    1.36 +		dtx_close_font(font);
    1.37 +		font = 0;
    1.38 +		return -1;
    1.39 +	}
    1.40 +	aloc_vertex = glGetAttribLocation(prog, "attr_vertex");
    1.41 +	aloc_texcoords = glGetAttribLocation(prog, "attr_texcoord");
    1.42 +	uloc_color = glGetUniformLocation(prog, "color");
    1.43 +
    1.44 +	return 0;
    1.45 +}
    1.46 +
    1.47 +void text_color(float r, float g, float b, float a)
    1.48 +{
    1.49 +	color[0] = r;
    1.50 +	color[1] = g;
    1.51 +	color[2] = b;
    1.52 +	color[3] = a;
    1.53 +}
    1.54 +
    1.55 +void text_position(int x, int y)
    1.56 +{
    1.57 +	cpos[0] = x;
    1.58 +	cpos[1] = y;
    1.59 +}
    1.60 +
    1.61 +static int pre_print(void)
    1.62 +{
    1.63 +	int top = win_height - line_height;
    1.64 +
    1.65 +	if(init() == -1) {
    1.66 +		return -1;
    1.67 +	}
    1.68 +
    1.69 +	gl_matrix_mode(GL_TEXTURE);
    1.70 +	gl_push_matrix();
    1.71 +	gl_load_identity();
    1.72 +	gl_matrix_mode(GL_PROJECTION);
    1.73 +	gl_push_matrix();
    1.74 +	gl_load_identity();
    1.75 +	gl_ortho(0, win_width, 0, win_height, -1, 1);
    1.76 +	gl_matrix_mode(GL_MODELVIEW);
    1.77 +	gl_push_matrix();
    1.78 +	gl_load_identity();
    1.79 +	gl_translatef(cpos[0] * dtx_glyph_width(' '), top - cpos[1] * line_height, 0);
    1.80 +
    1.81 +	glUseProgram(prog);
    1.82 +	glUniform4fv(uloc_color, 1, color);
    1.83 +
    1.84 +	gl_apply_xform(prog);
    1.85 +
    1.86 +	dtx_use_font(font, FONT_SIZE);
    1.87 +	dtx_vertex_attribs(aloc_vertex, aloc_texcoords);
    1.88 +	return 0;
    1.89 +}
    1.90 +
    1.91 +static void post_print(void)
    1.92 +{
    1.93 +	gl_matrix_mode(GL_TEXTURE);
    1.94 +	gl_pop_matrix();
    1.95 +	gl_matrix_mode(GL_PROJECTION);
    1.96 +	gl_pop_matrix();
    1.97 +	gl_matrix_mode(GL_MODELVIEW);
    1.98 +	gl_pop_matrix();
    1.99 +}
   1.100 +
   1.101 +void text_print(const char *s)
   1.102 +{
   1.103 +	if(pre_print() == -1) {
   1.104 +		return;
   1.105 +	}
   1.106 +
   1.107 +	dtx_string(s);
   1.108 +
   1.109 +	post_print();
   1.110 +}
   1.111 +
   1.112 +void text_printf(const char *fmt, ...)
   1.113 +{
   1.114 +	va_list ap;
   1.115 +	int buf_size;
   1.116 +	char *buf, tmp;
   1.117 +
   1.118 +	if(pre_print() == -1) {
   1.119 +		return;
   1.120 +	}
   1.121 +
   1.122 +	va_start(ap, fmt);
   1.123 +	buf_size = vsnprintf(&tmp, 0, fmt, ap);
   1.124 +	va_end(ap);
   1.125 +
   1.126 +	if(buf_size == -1) {
   1.127 +		buf_size = 512;
   1.128 +	}
   1.129 +
   1.130 +	buf = alloca(buf_size + 1);
   1.131 +	va_start(ap, fmt);
   1.132 +	vsnprintf(buf, buf_size + 1, fmt, ap);
   1.133 +	va_end(ap);
   1.134 +
   1.135 +	dtx_string(buf);
   1.136 +
   1.137 +	post_print();
   1.138 +}